Exemple #1
0
        private void onNewState_refresh(KEL103State a)
        {
            Invoke((MethodInvoker)(() =>
            {
                button2.BackColor = a.InputState ? Color.Red : Color.Green;
                button2.Text = a.InputState ? "Load Active" : "Load Inactive";

                toolStripStatusLabel1.Text = a.ValueAquisitionTimespan.ToString();
            }));

            kel103_states.Enqueue(a);

            Parallel.For(0, 2, i =>
            {
                Chart c     = charts[i];
                TextBox[] t = text_boxes[i];
                Queue <DateTime> timestamp_queue = chart_values[i][0];
                Queue <double> value_queue       = chart_values[i][1];

                if (channel_value_type_invalid[i])
                {
                    value_queue.Clear();
                    timestamp_queue.Clear();

                    //Invoke((MethodInvoker)(() => c.Series[0].Points.Clear()));


                    var cvt = channel_value_type[i];
                    foreach (var kel103val in kel103_states)
                    {
                        var value = new Func <dynamic>(() => {
                            switch (cvt)
                            {
                            case 0: return(a.Voltage);

                            case 1: return(a.Current);

                            case 2: return(a.Power);

                            default: throw new Exception("invalid field type");
                            }
                        })();

                        value_queue.Enqueue(value);
                        timestamp_queue.Enqueue(kel103val.TimeStamp);
                    }

                    channel_value_type_invalid[i] = false;
                }



                while (kel103_states.Count() > max_chart_points)
                {
                    kel103_states.Dequeue();
                }

                timestamp_queue.Enqueue(a.TimeStamp);

                switch (channel_value_type[i])
                {
                case 0: value_queue.Enqueue(a.Voltage); break;

                case 1: value_queue.Enqueue(a.Current); break;

                case 2: value_queue.Enqueue(a.Power); break;
                }

                Invoke((MethodInvoker)(() =>
                {
                    t[0].Text = KEL103Tools.FormatString(value_queue.Max());     //max
                    t[1].Text = KEL103Tools.FormatString(value_queue.Min());     //min
                    t[2].Text = KEL103Tools.FormatString(value_queue.Average()); //avg

                    c.Series[0].Points.DataBindXY(timestamp_queue, value_queue);

                    var max = value_queue.Max();
                    var min = value_queue.Min();

                    if (max != min)
                    {
                        c.ChartAreas[0].AxisY.Maximum = max + max * 0.1;
                        c.ChartAreas[0].AxisY.Minimum = min - min * 0.1;
                    }
                    else
                    {
                        if (max != 0)
                        {
                            c.ChartAreas[0].AxisY.Maximum = max + max * 0.5;
                            c.ChartAreas[0].AxisY.Minimum = max - max * 0.5;
                        }
                        else
                        {
                            c.ChartAreas[0].AxisY.Maximum = 1;
                            c.ChartAreas[0].AxisY.Minimum = -1;
                        }
                    }

                    while (timestamp_queue.Count() > max_chart_points)
                    {
                        timestamp_queue.Dequeue();
                        value_queue.Dequeue();
                    }
                }));
            });

            Invoke((MethodInvoker)(() =>
            {
                Refresh();
            }));
        }
Exemple #2
0
        public static async void StartListening()
        {
            while (true)
            {
                Console.WriteLine("Searching for load...");
                IPAddress load_address = null;
                while (true)
                {
                    try
                    {
                        load_address = KEL103Tools.FindLoadAddress();
                        break;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error searching for load. Retry in 30 seconds.");
                        await Task.Delay(30 * 1000);
                    }
                }

                Console.WriteLine("Load found at " + load_address.ToString() + ".");

                // Data buffer for incoming data.
                byte[] bytes = new Byte[1024];

                IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress   ipAddress  = ipHostInfo.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork).ToArray()[0];
                Console.WriteLine("My IP is " + ipAddress.ToString());

                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 5025);

                // Bind the socket to the local endpoint and
                // listen for incoming connections.
                try
                {
                    // Create a TCP/IP socket.
                    using (Socket listener = new Socket(ipAddress.AddressFamily,
                                                        SocketType.Stream, ProtocolType.Tcp))
                    {
                        listener.Bind(localEndPoint);
                        listener.Listen(10);

                        using (Socket handler = listener.Accept())
                        {
                            // Start listening for connections.
                            while (true)
                            {
                                // Program is suspended while waiting for an incoming connection.

                                data = null;

                                // An incoming connection needs to be processed.
                                int bytesRec = -1;
                                while (bytesRec == -1)
                                {
                                    bytesRec = handler.Receive(bytes);
                                    data    += Encoding.ASCII.GetString(bytes, 0, bytesRec);

                                    await Task.Delay(1);
                                }

                                byte[] msg = Encoding.ASCII.GetBytes(data);

                                using (UdpClient client = new UdpClient(KEL103Persistance.Configuration.CommandPort))
                                {
                                    KEL103Tools.ConfigureClient(load_address, client);
                                    client.Send(bytes, bytesRec);

                                    if (data.Contains('?'))
                                    {
                                        var rx = (await client.ReceiveAsync()).Buffer;
                                        handler.Send(rx);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                Console.Clear();
            }
        }