Advertisement
Hubert_M

Untitled

Jun 2nd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.36 KB | None | 0 0
  1. using System;
  2. using System.Timers;
  3. using System.ComponentModel.Design;
  4. using System.Net;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Net.Sockets;
  8. using System.Reflection.Metadata;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12.  
  13. namespace Obiekt1
  14. {
  15.     class Tank
  16.     {
  17.         public double WYP = 0.63;
  18.         public static double PI = 3.14;
  19.         private static double G = 9.81;
  20.         private static double TM = 6;
  21.         private static double QM = 11;
  22.         private static double X1 = 0.6;
  23.         private static double X2 = 1.3;
  24.         private static double X3 = 1.9;
  25.         private static double XT = 90;
  26.  
  27.         public int x1 = 0, x2 = 0, x3 = 0, xT = 0;
  28.         public static double hT = 2; // wysokość zbiornika w m2
  29.         static double r = 0.5; // promień zbiornika w m
  30.         public static double sT = PI * r * r; // powierzchnia podstawy zbiornika w m2
  31.         public double vT = sT * hT; // objętość zbiornika w m3
  32.         public double hW = 0.0; // poziom wody w zbiorniku w m
  33.         public double tWp = 20;
  34.         public double tWn = 20;
  35.         public int m = 0;
  36.         public double vW = 0; // objętość wody w zbiorniku w m3 50litra max objętość
  37.         public int overflowed = 0; // czujnik przelania
  38.  
  39.         public void check()
  40.         {
  41.             if (hW > hT)
  42.             {
  43.                 hW = 2;
  44.                 overflowed = 1;
  45.             }
  46.             else overflowed = 0;
  47.  
  48.             if (hW >= X1)
  49.                 x1 = 1;
  50.             else
  51.                 x1 = 0;
  52.  
  53.             if (hW >= X2)
  54.                 x2 = 1;
  55.             else
  56.                 x2 = 0;
  57.  
  58.             if (hW >= X3)
  59.                 x3 = 1;
  60.             else
  61.                 x3 = 0;
  62.  
  63.             if (tWp >= XT)
  64.                 xT = 1;
  65.             else
  66.                 xT = 0;
  67.  
  68.             vW = sT * hW;
  69.         }
  70.  
  71.     }
  72.  
  73.     class Valve
  74.     {
  75.         private static double PI = 3.14;
  76.         private static double QM = 11;
  77.  
  78.         public int status = 0; // 1 nalewa/ 0 nic nie robi
  79.         static double r = 0.03; // promień przekorju zaworu w m
  80.         static double sV = PI * r * r; // przekrój otworu w m2
  81.         public double rSV = sV; // Obecny przekrój
  82.  
  83.         public void qV(double x)
  84.         {
  85.             if (x != QM)
  86.             {
  87.                 rSV = sV / x;
  88.             }
  89.             else
  90.             {
  91.                 rSV = 0;
  92.             }
  93.         }
  94.     }
  95.  
  96.     class Heater
  97.     {
  98.         public int status = 0; // 1 grzeje/ o nie grzeje
  99.         public int overheat = 0;
  100.         public int p = 100;
  101.         public int oh = 0;
  102.         public double alfa1 = 2.8;
  103.         public double alfa2 = -0.8;
  104.         public double alfa3 = 0.04;
  105.         public double tHp = 20;
  106.         public double tHn = 20;
  107.  
  108.         public void dH(double x)
  109.         {
  110.             alfa2 = -x;
  111.         }
  112.         public void check()
  113.         {
  114.             oh++;
  115.             if (oh > 50)
  116.             {
  117.                 overheat = 1;
  118.                 oh = 0;
  119.             }
  120.         }
  121.     };
  122.  
  123.  
  124.     public class handleClinet
  125.     {
  126.         TcpClient clientSocket;
  127.         string clNo;
  128.         public void startClient(TcpClient inClientSocket, string clineNo)
  129.         {
  130.             this.clientSocket = inClientSocket;
  131.             this.clNo = clineNo;
  132.             Thread ctThread = new Thread(doChat);
  133.             ctThread.Start();
  134.         }
  135.         private void doChat()
  136.         {
  137.             int requestCount = 0;
  138.             byte[] bytesFrom = new byte[10025];
  139.             string dataFromClient = null;
  140.             Byte[] sendBytes = null;
  141.             string serverResponse = null;
  142.             string rCount = null;
  143.             requestCount = 0;
  144.  
  145.             while ((true))
  146.             {
  147.                 try
  148.                 {
  149.                     requestCount = requestCount + 1;
  150.                     NetworkStream networkStream = clientSocket.GetStream();
  151.                     networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  152.                     dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  153.                     dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  154.                     Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);
  155.  
  156.                     rCount = Convert.ToString(requestCount);
  157.                     serverResponse = "Server to clinet(" + clNo + ") " + rCount;
  158.                     sendBytes = Encoding.ASCII.GetBytes(serverResponse);
  159.                     networkStream.Write(sendBytes, 0, sendBytes.Length);
  160.                     networkStream.Flush();
  161.                     Console.WriteLine(" >> " + serverResponse);
  162.                 }
  163.                 catch (Exception ex)
  164.                 {
  165.                     Console.WriteLine(" >> " + ex.ToString());
  166.                 }
  167.             }
  168.         }
  169.     }
  170.  
  171.     class Program
  172.     {
  173.  
  174.         private static System.Timers.Timer aTimer;
  175.         private static System.Timers.Timer bTimer;
  176.  
  177.         public static double TM = 6;
  178.         public static double G = 9.81;
  179.         public static double WYP = 0.63;
  180.         static double PI = 3.14;
  181.         static double hT = 2; // wysokość zbiornika w m2
  182.         static double r = 0.5; // promień zbiornika w m
  183.         static double sT = PI * r * r; // powierzchnia podstawy zbiornika w m2
  184.         static double vT = sT * hT; // objętość zbiornika w m3
  185.         static double hW = 0.0; // poziom wody w zbiorniku w m
  186.         static double vW = 0; // objętość wody w zbiorniku w m3 50litra max objętość
  187.         static double sV = PI * r * r; // przekrój otworu w m2
  188.         static double rSV = sV; // Obecny przekrój
  189.  
  190.  
  191.  
  192.         static async void Connect(Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5,
  193.             Valve v6, Heater h1, Heater h2, Heater h3, Heater h4)
  194.         {
  195.             TcpListener server = null;
  196.             try
  197.             {
  198.                 Int32 port = 13131;
  199.                 IPAddress localAddr = IPAddress.Parse("127.0.0.1");
  200.  
  201.                 server = new TcpListener(localAddr, port);
  202.  
  203.                 server.Start();
  204.                 // Buffer for reading data
  205.                 Byte[] bytes = new Byte[512];
  206.                 String data = null;
  207.  
  208.                 //Uruchomienie petli nasluchujacej połączenia
  209.                 while (true)
  210.                 {
  211.                     TcpClient client = server.AcceptTcpClient();
  212.                     handleClinet client = new handleClinet();
  213.                     data = null;
  214.                     NetworkStream stream = client.GetStream();
  215.  
  216.  
  217.                     int i;
  218.  
  219.                     //Zapis otrzymywanych danych oraz odpowiedz do klienta
  220.                     while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  221.                     {
  222.                         data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  223.                         if (data[0] == '0')
  224.                         {
  225.                             v1.status = Convert.ToInt32(data.Split(':')[1]);
  226.                             v2.status = Convert.ToInt32(data.Split(':')[2]);
  227.                             v3.status = Convert.ToInt32(data.Split(':')[3]);
  228.                             v4.status = Convert.ToInt32(data.Split(':')[4]);
  229.                             v5.status = Convert.ToInt32(data.Split(':')[5]);
  230.                             v6.status = Convert.ToInt32(data.Split(':')[6]);
  231.                             h1.status = Convert.ToInt32(data.Split(':')[7]);
  232.                             h2.status = Convert.ToInt32(data.Split(':')[8]);
  233.                             h3.status = Convert.ToInt32(data.Split(':')[9]);
  234.                             h4.status = Convert.ToInt32(data.Split(':')[10]);
  235.                             t1.m = Convert.ToInt32(data.Split(':')[11]);
  236.                             t2.m = Convert.ToInt32(data.Split(':')[12]);
  237.                             t3.m = Convert.ToInt32(data.Split(':')[13]);
  238.                             t4.m = Convert.ToInt32(data.Split(':')[14]);
  239.                         }
  240.                         else if (data[0] == '1')
  241.                         {
  242.                             //Console.WriteLine(data);
  243.                             v1.qV(Convert.ToInt32(data.Split(":")[1]));
  244.                             v2.qV(Convert.ToInt32(data.Split(":")[2]));
  245.                             v3.qV(Convert.ToInt32(data.Split(":")[3]));
  246.                             v4.qV(Convert.ToInt32(data.Split(":")[4]));
  247.                             v5.qV(Convert.ToInt32(data.Split(":")[5]));
  248.                             v6.qV(Convert.ToInt32(data.Split(":")[6]));
  249.  
  250.                             h1.dH(Convert.ToInt32(data.Split(":")[7]));
  251.                             h2.dH(Convert.ToInt32(data.Split(":")[8]));
  252.                             h3.dH(Convert.ToInt32(data.Split(":")[9]));
  253.                             h4.dH(Convert.ToInt32(data.Split(":")[10]));
  254.                         }
  255.  
  256.                         string message = t1.x1.ToString() + ":" + t1.x2.ToString() + ":" + t1.x3.ToString() + ":" +
  257.                         t2.x1.ToString() + ":" + t2.x2.ToString() + ":" + t2.x3.ToString() + ":" +
  258.                         t3.x1.ToString() + ":" + t3.x2.ToString() + ":" + t3.x3.ToString() + ":" +
  259.                         t4.x1.ToString() + ":" + t4.x2.ToString() + ":" + t4.x3.ToString() + ":" +
  260.                         t1.xT.ToString() + ":" + t2.xT.ToString() + ":" + t3.xT.ToString() + ":" +
  261.                         t4.xT.ToString() + "."
  262.                         + v1.status.ToString() + ":" + v2.status.ToString() + ":" +
  263.                         v3.status.ToString() + ":" + v4.status.ToString() + ":" +
  264.                         v5.status.ToString() + ":" + v6.status.ToString() + ":" + t1.m.ToString() +
  265.                         ":" + t2.m.ToString() + ":" + t3.m.ToString() + ":" + t4.m.ToString() + ":" +
  266.                         t1.hW.ToString() + ":" + t1.tWp.ToString() + ":" + t2.hW.ToString() + ":" +
  267.                         t2.tWp.ToString() + ":" + t3.hW.ToString() + ":" + t3.tWp.ToString() + ":" +
  268.                         t4.hW.ToString() + ":" + t4.tWp.ToString();
  269.                                          
  270.                                            
  271.                         data = message.ToUpper();
  272.                         byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
  273.                         stream.Write(msg, 0, msg.Length);
  274.                     }
  275.                     Console.WriteLine(data);
  276.                 }
  277.             }
  278.             catch (ArgumentNullException e)
  279.             {
  280.                 Console.WriteLine("ArgumentNullException: {0}", e);
  281.             }
  282.             catch (SocketException e)
  283.             {
  284.                 Console.WriteLine("SocketException: {0}", e);
  285.             }
  286.         }
  287.  
  288.  
  289.  
  290.         static void Main(string[] args)
  291.         {
  292.  
  293.             Tank t1 = new Tank();
  294.             Tank t2 = new Tank();
  295.             Tank t3 = new Tank();
  296.             Tank t4 = new Tank();
  297.             Valve v1 = new Valve();
  298.             Valve v2 = new Valve();
  299.             Valve v3 = new Valve();
  300.             Valve v4 = new Valve();
  301.             Valve v5 = new Valve();
  302.             Valve v6 = new Valve();
  303.             Heater h1 = new Heater();
  304.             Heater h2 = new Heater();
  305.             Heater h3 = new Heater();
  306.             Heater h4 = new Heater();
  307.  
  308.             //Connect(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  309.             SetTimer(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6, h1, h2, h3, h4);
  310.             Connect(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6, h1, h2, h3, h4);
  311.             //SetTimer2(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  312.             //SetTimer(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  313.             //SetTimer2(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6);
  314.  
  315.             while (true) { }
  316.  
  317.  
  318.         }
  319.  
  320.         private static void SetTimer(Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6, Heater h1, Heater h2, Heater h3, Heater h4)
  321.         {
  322.             // Create a timer with a two second interval.
  323.             aTimer = new System.Timers.Timer(100);
  324.             // Hook up the Elapsed event for the timer.
  325.             aTimer.Elapsed += (source, e) => OnTimedEvent(source, e, t1, t2, t3, t4, v1, v2, v3, v4, v5, v6, h1, h2, h3, h4);
  326.             aTimer.AutoReset = true;
  327.             aTimer.Enabled = true;
  328.         }
  329.  
  330.         private static async void OnTimedEvent(Object source, ElapsedEventArgs e, Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6, Heater h1, Heater h2, Heater h3, Heater h4)
  331.         {
  332.             var taska = Task.Run(() => loop(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6, h1, h2, h3, h4));
  333.             //var taskb = Task.Run(() => Connect(t1, t2, t3, t4, v1, v2, v3, v4, v5, v6));
  334.         }
  335.         long currentTime = 0;
  336.         long previousTime = 0;
  337.         long deltaTime = 0;
  338.         double[] data;
  339.  
  340.  
  341.         static double d1 = 0; // różnica w poziomie wodym w całym cyklu
  342.         static double d2 = 0; // różnica w poziomie wodym w całym cyklu
  343.         static double d3 = 0; // różnica w poziomie wodym w całym cyklu
  344.         static double d4 = 0; // różnica w poziomie wodym w całym cyklu
  345.  
  346.         static void heat(Tank t, Heater h) // zmiany tu
  347.         {
  348.             h.tHn = h.tHp + 0.1 * (h.p - h.alfa1 * (h.tHp - t.tWp));
  349.             if (t.vW > 0)
  350.                 t.tWn = t.tWp + 0.1 * (h.alfa2 * 1 / t.vW * (t.tWp - h.tHp) + h.alfa3 * (t.tWp - 20));
  351.             else
  352.             {
  353.                 h.check();
  354.                 t.tWn = 20;
  355.             }
  356.             if (t.tWn > 100)
  357.             {
  358.                 t.tWn = 100;
  359.             }
  360.             t.tWp = t.tWn;
  361.             h.tHp = h.tHn;
  362.  
  363.         }
  364.  
  365.         static void cool(Tank t, Heater h) // zmiany tu
  366.         {
  367.             h.oh = 0;
  368.             t.tWn = 20 + (t.tWp - 20) * Math.Exp(-0.017 * 1 / t.vW);
  369.             t.tWp = t.tWn;
  370.             h.tHp = t.tWn;
  371.  
  372.         }
  373.  
  374.         static void mixTemp(Tank tP, Tank tS, double d) // zmiany tu
  375.         {
  376.             if (d > 0)
  377.                 tP.tWp = (tP.vW * 997 * tP.tWp + d * Tank.sT * 997 * tS.tWp) / (tP.vW * 997 + d * Tank.sT * 997);
  378.         }
  379.  
  380.  
  381.         public static double bind(Tank t, Valve v, int s, double h = 1000)
  382.         {
  383.             double d, q;
  384.             if (v.rSV == 0)
  385.                 return 0;
  386.             if (h == 1000)
  387.             {
  388.                 q = WYP * v.rSV * Math.Sqrt(2 * G * (h / 1000));
  389.             }
  390.             else
  391.             {
  392.                 q = WYP * v.rSV * Math.Sqrt(2 * G * h);
  393.             } // natężenie wypływu w m3/s
  394.  
  395.             d = TM * q / Tank.sT;
  396.             if (h < 1000)
  397.                 if (d >= h)
  398.                     d = h;
  399.             if (d >= h) d = h;
  400.             if (s == -1)
  401.             {
  402.                 return -d;
  403.             }
  404.  
  405.             return d;
  406.         }
  407.  
  408.  
  409.  
  410.         static void loop(Tank t1, Tank t2, Tank t3, Tank t4, Valve v1, Valve v2, Valve v3, Valve v4, Valve v5, Valve v6, Heater h1, Heater h2, Heater h3, Heater h4)
  411.         {
  412.             v1.qV(1);
  413.             v2.qV(1);
  414.             v3.qV(1);
  415.             v4.qV(1);
  416.             v5.qV(1);
  417.             v6.qV(1);
  418.  
  419.             h1.dH(0.8);
  420.             h2.dH(0.8);
  421.             h3.dH(0.8);
  422.             h4.dH(0.8);
  423.             //v1.status = 1;
  424.  
  425.             if (v1.status == 1)
  426.             {
  427.                 d1 += bind(t1, v1, 1);
  428.             }
  429.  
  430.             if (v2.status == 1)
  431.             {
  432.                 d1 += bind(t1, v2, -1, t1.hW);
  433.             }
  434.  
  435.             if (v2.status == 1)
  436.             {
  437.                 d2 += bind(t2, v2, 1, t1.hW);
  438.  
  439.             }
  440.  
  441.  
  442.             mixTemp(t2, t1, d2);
  443.  
  444.  
  445.             t1.hW += d1;
  446.             d1 = 0;
  447.  
  448.             if (v3.status == 1)
  449.             {
  450.                 d1 += bind(t1, v3, -1, t1.hW);
  451.             }
  452.  
  453.  
  454.             d2 += bind(t2, v4, -1, t2.hW);
  455.  
  456.  
  457.             if (v3.status == 1)
  458.             {
  459.                 d3 += bind(t3, v3, 1, t1.hW);
  460.             }
  461.  
  462.  
  463.             mixTemp(t3, t1, d3);
  464.  
  465.  
  466.             if (v5.status == 1)
  467.             {
  468.                 d3 += bind(t3, v5, -1, t3.hW);
  469.             }
  470.  
  471.  
  472.             if (v4.status == 1)
  473.             {
  474.                 d4 += bind(t4, v4, 1, t2.hW);
  475.             }
  476.  
  477.  
  478.             mixTemp(t4, t2, d4);
  479.  
  480.  
  481.             t4.hW += d4;
  482.             d4 = 0;
  483.  
  484.             if (v5.status == 1)
  485.             {
  486.                 d4 += bind(t4, v5, 1, t3.hW);
  487.             }
  488.  
  489.  
  490.             mixTemp(t4, t3, d4);
  491.  
  492.  
  493.             if (v6.status == 1)
  494.             {
  495.                 d4 += bind(t4, v6, -1, t4.hW);
  496.             }
  497.  
  498.  
  499.             t1.hW += d1;
  500.             t2.hW += d2;
  501.             t3.hW += d3;
  502.             t4.hW += d4;
  503.  
  504.             t1.check();
  505.             t2.check();
  506.             t3.check();
  507.             t4.check();
  508.  
  509.             if (h1.status == 1) heat(t1, h1);
  510.             else cool(t1, h1);
  511.             if (h2.status == 1) heat(t2, h2);
  512.             else cool(t2, h2);
  513.             if (h3.status == 1) heat(t3, h3);
  514.             else cool(t3, h3);
  515.             if (h4.status == 1) heat(t4, h4);
  516.             else cool(t4, h4);
  517.  
  518.             d1 = 0;
  519.             d2 = 0;
  520.             d3 = 0;
  521.             d4 = 0;
  522.  
  523.  
  524.             //Console.WriteLine("Poziom : " + t1.hW);
  525.             //Console.Write("Z1 : " + v1.status);
  526.             //Console.WriteLine(" |Z2 : " + v2.status);
  527.  
  528.         }
  529.  
  530.     }
  531. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement