// The method is called when a message from the client is received.
        private void MessageReceived(object sender, StringRequestReceivedEventArgs e)
        {
            Logger.Info("Received : " + e.RequestMessage);

            // Analyze message
            // split strings
            // receiving responsereceiverid from client
            // Calculate mtu
            // Warm up stopwatch
            // Send message
            CommandReceiver.SendResponseMessage(e.ResponseReceiverId, e.RequestMessage);
        }
Пример #2
0
        private static void OnMessageReceived(object sender, StringRequestReceivedEventArgs e)
        {
            // Insert received message at the beginning of the listbox.
            // Note: we can directly access the listbox because we set threading mode of
            //       InputChannelThreading to the main UI thread.
            using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
            {
                var query = (from pucs in db.KPI_PU_Current_Summary
                             join KPI in db.KPIs on pucs.KPI_Id equals KPI.Id
                             where pucs.KPI_Id == 1 && pucs.KPICP_Id == 1 && pucs.KPICT_Id == 1
                             select new { Value = pucs.Value }).ToList();


                float avg = query.Average(x => x.Value).Value;

                myStringReceiver.SendResponseMessage(e.ResponseReceiverId, avg.ToString());
            }
        }
    public void SendResponseMessage(string responseReceiverId, TResponse responseMessage)
    {
        string stringMessage = (string)serializers_by_receiver_id_[responseReceiverId].Serialize(responseMessage);

        string_receiver_.SendResponseMessage(responseReceiverId, stringMessage);
    }
        public void WrapUnwrapMessage()
        {
            // Wrapped/unwrapped channels
            string aChannel1Id = "Channel1Id";
            string aChannel2Id = "Channel2Id";

            IDuplexStringMessagesFactory aStringMessagesFactory = new DuplexStringMessagesFactory();

            IDuplexStringMessageReceiver aStringMessageReceiver1 = aStringMessagesFactory.CreateDuplexStringMessageReceiver();
            IDuplexStringMessageReceiver aStringMessageReceiver2 = aStringMessagesFactory.CreateDuplexStringMessageReceiver();

            IDuplexStringMessageSender aStringMessageSender1 = aStringMessagesFactory.CreateDuplexStringMessageSender();
            IDuplexStringMessageSender aStringMessageSender2 = aStringMessagesFactory.CreateDuplexStringMessageSender();

            // Attach input channels to string receivers.
            aStringMessageReceiver1.AttachDuplexInputChannel(myMessagingForUnwrapper.CreateDuplexInputChannel(aChannel1Id));
            aStringMessageReceiver2.AttachDuplexInputChannel(myMessagingForUnwrapper.CreateDuplexInputChannel(aChannel2Id));

            // Connect string senders with the channel wrapper.
            myDuplexChannelWrapper.AttachDuplexInputChannel(myMessagingForWrapper.CreateDuplexInputChannel(aChannel1Id));
            myDuplexChannelWrapper.AttachDuplexInputChannel(myMessagingForWrapper.CreateDuplexInputChannel(aChannel2Id));
            aStringMessageSender1.AttachDuplexOutputChannel(myMessagingForWrapper.CreateDuplexOutputChannel(aChannel1Id));
            aStringMessageSender2.AttachDuplexOutputChannel(myMessagingForWrapper.CreateDuplexOutputChannel(aChannel2Id));

            // Connect wrapper and unwrapper to global channels.
            myDuplexChannelUnwrapper.AttachDuplexInputChannel(myDuplexGlobalInputChannel);
            myDuplexChannelWrapper.AttachDuplexOutputChannel(myDuplexGlobalOutputChannel);


            StringRequestReceivedEventArgs aReceivedMessage1 = null;

            aStringMessageReceiver1.RequestReceived += (x, y) =>
            {
                aReceivedMessage1 = y;
                aStringMessageReceiver1.SendResponseMessage(y.ResponseReceiverId, "Response1");
            };

            StringRequestReceivedEventArgs aReceivedMessage2 = null;

            aStringMessageReceiver2.RequestReceived += (x, y) =>
            {
                aReceivedMessage2 = y;
                aStringMessageReceiver2.SendResponseMessage(y.ResponseReceiverId, "Response2");
            };

            StringResponseReceivedEventArgs aReceivedResponse1 = null;

            aStringMessageSender1.ResponseReceived += (x, y) =>
            {
                aReceivedResponse1 = y;
            };

            StringResponseReceivedEventArgs aReceivedResponse2 = null;

            aStringMessageSender2.ResponseReceived += (x, y) =>
            {
                aReceivedResponse2 = y;
            };

            aStringMessageSender1.SendMessage("Message1");

            Assert.AreEqual("Message1", aReceivedMessage1.RequestMessage, "Message receiver 1 received incorrect message.");
            Assert.AreEqual("Response1", aReceivedResponse1.ResponseMessage, "Response receiver 1 received incorrect message.");
            Assert.IsNull(aReceivedMessage2, "Message receiver 2 should not receive a message.");
            Assert.IsNull(aReceivedResponse2, "Response receiver 2 should not receive a message.");

            string anAssociatedResponseReceiverId = myDuplexChannelUnwrapper.GetAssociatedResponseReceiverId(aReceivedMessage1.ResponseReceiverId);

            Assert.AreEqual(myDuplexChannelWrapper.AttachedDuplexOutputChannel.ResponseReceiverId, anAssociatedResponseReceiverId);


            aReceivedMessage1  = null;
            aReceivedResponse1 = null;

            aStringMessageSender2.SendMessage("Message2");

            Assert.AreEqual("Message2", aReceivedMessage2.RequestMessage, "Message receiver 2 received incorrect message.");
            Assert.AreEqual("Response2", aReceivedResponse2.ResponseMessage, "Response receiver 2 received incorrect message.");
            Assert.IsNull(aReceivedMessage1, "Message receiver 1 should not receive a message.");
            Assert.IsNull(aReceivedResponse1, "Response receiver 1 should not receive a message.");
        }
Пример #5
0
        private static void OnRequestReceived(object sender, StringRequestReceivedEventArgs e)
        {
            // Handle received messages here.

            ISerializer  serializer = new XmlStringSerializer();
            TimedRequest timedRequest;

            try
            {
                timedRequest = serializer.Deserialize <TimedRequest>(e.RequestMessage);
                Console.WriteLine("Received message: " + timedRequest.Request);
            }
            catch (Exception ex) {
                Console.WriteLine("Cannot deserialize Request: " + ex.Message);
                return;
            }

            if (timedRequest.Request.Equals("Machines"))
            {
                using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
                {
                    var query = (from KPIPUCS in db.KPI_PU_Current_Summary
                                 join pu in db.Production_Unit on KPIPUCS.PU_Id equals pu.Id
                                 join KPIPUCS1 in db.KPI_PU_Current_Summary on new { KPIPUCS.PU_Id, KPIPUCS.PUKPICT_Id, KPIPUCS.KPICP_Id } equals new { KPIPUCS1.PU_Id, KPIPUCS1.PUKPICT_Id, KPIPUCS1.KPICP_Id }
                                 join KPIPUCS2 in db.KPI_PU_Current_Summary on new { KPIPUCS.PU_Id, KPIPUCS.PUKPICT_Id, KPIPUCS.KPICP_Id } equals new { KPIPUCS2.PU_Id, KPIPUCS2.PUKPICT_Id, KPIPUCS2.KPICP_Id }
                                 join a in db.Areas on pu.A_Id equals a.Id
                                 where KPIPUCS.KPICP_Id == 1 && KPIPUCS.KPICT_Id == 1 && KPIPUCS.KPI_Id == 1 && KPIPUCS.IsDeleted == false && KPIPUCS1.KPI_Id == 2 && KPIPUCS2.KPI_Id == 3
                                 select new { Availibility = KPIPUCS.Value, Performance = KPIPUCS1.Value, Quality = KPIPUCS2.Value, a.NamePL, pu.Name, }).ToList();
                    var machinesList = new List <MachinesOverview>();
                    foreach (var row in query)
                    {
                        MachinesOverview machinesOverview1 = new MachinesOverview();
                        machinesOverview1.Availibility = (row.Availibility == null) ? 0.0f : (float)row.Availibility;
                        machinesOverview1.Performance  = (row.Performance == null) ? 0.0f : (float)row.Performance;
                        machinesOverview1.Quality      = (row.Quality == null) ? 0.0f : (float)row.Quality;
                        machinesOverview1.MachineName  = (string)row.Name;
                        machinesOverview1.LineName     = (string)row.NamePL;
                        machinesOverview1.OEE          = machinesOverview1.Performance * machinesOverview1.Quality * machinesOverview1.Availibility / 10000;

                        string message = (string)serializer.Serialize <MachinesOverview>(machinesOverview1);
                        myReceiver.SendResponseMessage(e.ResponseReceiverId, message);
                    }
                    myReceiver.SendResponseMessage(e.ResponseReceiverId, "Finished");
                }
            }
            else if (timedRequest.Request.Equals("Areas"))
            {
                using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
                {
                    var query = (from pu in db.Production_Unit
                                 join area in db.Areas on pu.A_Id equals area.Id
                                 where pu.IsDeleted == false
                                 select new { Line = area.NamePL }).Distinct().ToList();
                    string csv = "";
                    for (int i = 0; i < query.Count; i++)
                    {
                        csv = csv + query[i].Line + ",";
                    }
                    csv = csv.Substring(0, csv.Length - 1);
                    //string csv = String.Join(",", query.Select(x => x.Line.ToString().ToArray()));
                    myReceiver.SendResponseMessage(e.ResponseReceiverId, csv);
                }
            }
            else if (timedRequest.Request.Equals("Downtimes"))
            {
                using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
                {
                    DateTime Start = DateTime.ParseExact(timedRequest.StartTime, "yyyy-M-dd H:m", System.Globalization.CultureInfo.InvariantCulture);
                    DateTime End   = DateTime.ParseExact(timedRequest.EndTime, "yyyy-M-dd H:m", System.Globalization.CultureInfo.InvariantCulture);
                    var      query = (from dh in db.Downtime_History
                                      join pu in db.Production_Unit on dh.PU_Id equals pu.Id
                                      join area in db.Areas on pu.A_Id equals area.Id
                                      join tc in db.Tree_Cause_Config on dh.TCC_Id equals tc.Id
                                      where pu.IsDeleted == false && (dh.StartTime > Start && dh.StartTime < End) || (dh.EndTime == null || (dh.EndTime > Start && dh.EndTime < End))
                                      select new { Line = area.Id, LinePL = area.NamePL, DHStartTime = dh.StartTime, DHEndTime = dh.EndTime, MachineName = pu.Name, Reason = tc.NamePL });


                    if (!timedRequest.LineName.Equals("Wszystkie linie"))
                    {
                        query = query.Where(x => x.LinePL.Equals(timedRequest.LineName));
                        //Where(x => x.Line.Equals(timedRequest.LineName));
                    }
                    var data = query.ToList();

                    foreach (var row in data)
                    {
                        DowntimeEvents downtime = new DowntimeEvents();
                        downtime.lineName    = row.Line;
                        downtime.StartDate   = row.DHStartTime.ToString("yyyy-M-dd H:m");
                        downtime.EndDate     = row.DHEndTime?.ToString("yyyy-M-dd H:m:s");
                        downtime.Reason      = row.Reason;
                        downtime.machineName = row.MachineName;

                        string message = (string)serializer.Serialize <DowntimeEvents>(downtime);
                        myReceiver.SendResponseMessage(e.ResponseReceiverId, message);
                    }
                    myReceiver.SendResponseMessage(e.ResponseReceiverId, "Finished");
                }
            }
            else if (timedRequest.Request.Equals("Wastes"))
            {
                using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
                {
                    DateTime Start = DateTime.ParseExact(timedRequest.StartTime, "yyyy-M-dd H:m", System.Globalization.CultureInfo.InvariantCulture);
                    DateTime End   = DateTime.ParseExact(timedRequest.EndTime, "yyyy-M-dd H:m", System.Globalization.CultureInfo.InvariantCulture);
                    var      query = (from wh in db.Waste_History
                                      join pu in db.Production_Unit on wh.PU_Id equals pu.Id
                                      join area in db.Areas on pu.A_Id equals area.Id
                                      where pu.IsDeleted == false && (wh.TimeStamp > Start && wh.TimeStamp < End)
                                      select new { Line = area.Id, LinePL = area.NamePL, WHTimestamp = wh.TimeStamp, MachineName = pu.Name, Reason = "Strata" });


                    if (!timedRequest.LineName.Equals("Wszystkie linie"))
                    {
                        query = query.Where(x => x.LinePL.Equals(timedRequest.LineName));
                        //Where(x => x.Line.Equals(timedRequest.LineName));
                    }
                    var data = query.ToList();

                    foreach (var row in data)
                    {
                        WasteEvent waste = new WasteEvent();
                        waste.lineNumber    = row.Line;
                        waste.date          = row.WHTimestamp.ToString("yyyy-M-dd H:m");
                        waste.wasteReason   = row.Reason;
                        waste.wasteValue    = row.MachineName.ToString();
                        waste.machineNumber = row.MachineName;
                        string message = (string)serializer.Serialize <WasteEvent>(waste);
                        myReceiver.SendResponseMessage(e.ResponseReceiverId, message);
                    }
                    myReceiver.SendResponseMessage(e.ResponseReceiverId, "Finished");
                }
            }
            else if (timedRequest.Request.Equals("ChartMachines"))
            {
                //StateEvent stateEvent = new StateEvent();
                //stateEvent.machines = new List<string>();
                //stateEvent.machines.Add("Maszyna 1");
                //stateEvent.machines.Add("Maszyna 2");
                //stateEvent.lineName = ("Linia 1");
                //string message1 = (string)serializer.Serialize<StateEvent>(stateEvent);
                //myReceiver.SendResponseMessage(e.ResponseReceiverId, message1);


                using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
                {
                    var query = (from pu in db.Production_Unit
                                 join a in db.Areas on pu.A_Id equals a.Id
                                 select new { Line = a.NamePL, LineNumber = a.Id, MachineName = pu.Name, MachineNumber = pu.Id }).ToList();
                    var machinesList = new List <MachinesOverview>();
                    var Lines        = query.Select(x => x.Line).Distinct();

                    foreach (var row in Lines)
                    {
                        StateEvent state = new StateEvent();
                        state.lineName = row;

                        var machines = (from q in query
                                        where q.Line.Equals(row)
                                        select new { MachineName = q.MachineName }).ToList();
                        string csv = "";
                        for (int i = 0; i < machines.Count; i++)
                        {
                            csv = csv + machines[i].MachineName + ",";
                        }
                        csv            = csv.Substring(0, csv.Length - 1);
                        state.machines = csv;
                        string message = (string)serializer.Serialize <StateEvent>(state);
                        myReceiver.SendResponseMessage(e.ResponseReceiverId, message);
                    }
                    myReceiver.SendResponseMessage(e.ResponseReceiverId, "Finished");
                }
            }
            else if (timedRequest.Request.Equals("ChartData"))
            {
                //StateEvent stateEvent = new StateEvent();
                //stateEvent.machines = new List<string>();
                //stateEvent.machines.Add("Maszyna 1");
                //stateEvent.machines.Add("Maszyna 2");
                //stateEvent.lineName = ("Linia 1");
                //string message1 = (string)serializer.Serialize<StateEvent>(stateEvent);
                //myReceiver.SendResponseMessage(e.ResponseReceiverId, message1);


                using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
                {
                    try
                    {
                        DateTime end   = DateTime.ParseExact(timedRequest.EndTime, "yyyy-M-dd H:m", System.Globalization.CultureInfo.InvariantCulture);
                        DateTime start = DateTime.ParseExact(timedRequest.StartTime, "yyyy-M-dd H:m", System.Globalization.CultureInfo.InvariantCulture);
                        var      query = (from kh in db.KPI_History
                                          join kh2 in db.KPI_History on new { kh.SH_Id, kh.PU_Id } equals new { kh2.SH_Id, kh2.PU_Id }
                                          join kh3 in db.KPI_History on new { kh.SH_Id, kh.PU_Id } equals new { kh3.SH_Id, kh3.PU_Id }
                                          join pukpi in db.Prod_Unit_KPI on kh.PUKPI_Id equals pukpi.Id
                                          join kpi in db.KPIs on pukpi.KPI_Id equals kpi.Id
                                          join pukpi2 in db.Prod_Unit_KPI on kh2.PUKPI_Id equals pukpi2.Id
                                          join kpi2 in db.KPIs on pukpi2.KPI_Id equals kpi2.Id
                                          join pukpi3 in db.Prod_Unit_KPI on kh3.PUKPI_Id equals pukpi3.Id
                                          join kpi3 in db.KPIs on pukpi3.KPI_Id equals kpi3.Id
                                          join sh in db.Shift_History on kh.SH_Id equals sh.Id
                                          join pu in db.Production_Unit on kh.PU_Id equals pu.Id
                                          join a in db.Areas on pu.A_Id equals a.Id
                                          where pu.Name.Equals(timedRequest.MachineName) && a.NamePL.Equals(timedRequest.LineName) && sh.StartTime >= start &&
                                          sh.StartTime < end &&
                                          kpi.Name.Equals("Availability") && kpi2.Name.Equals("Performance") && kpi3.Name.Equals("Quality")
                                          select new { LineNumber = a.Id, Timestamp = sh.StartTime, MachineNumber = pu.Id, Availibility = kh.Value, Performance = kh2.Value, Quality = kh3.Value }).ToList();
                        var kPIevent = new KPIevent();
                        kPIevent.timeStamp         = "";
                        kPIevent.performanceValue  = "";
                        kPIevent.qualityValue      = "";
                        kPIevent.oeeValue          = "";
                        kPIevent.availabilityValue = "";

                        if (query.Count == 0)
                        {
                            myReceiver.SendResponseMessage(e.ResponseReceiverId, "Finished");
                        }
                        foreach (var row in query)
                        {
                            kPIevent.lineNumber         = row.LineNumber;
                            kPIevent.machineNumber      = row.MachineNumber;
                            kPIevent.timeStamp         += row.Timestamp.ToString("yyyy-M-dd H:m") + ",";
                            kPIevent.performanceValue  += row.Performance.ToString() + ",";
                            kPIevent.qualityValue      += row.Quality.ToString() + ",";
                            kPIevent.availabilityValue += row.Availibility.ToString() + ",";
                            kPIevent.oeeValue          += (row.Performance * row.Availibility * row.Quality / 10000).ToString() + ",";
                        }

                        kPIevent.timeStamp         = kPIevent.timeStamp.Substring(0, kPIevent.timeStamp.Length - 1);
                        kPIevent.performanceValue  = kPIevent.performanceValue.Substring(0, kPIevent.performanceValue.Length - 1);
                        kPIevent.qualityValue      = kPIevent.qualityValue.Substring(0, kPIevent.qualityValue.Length - 1);
                        kPIevent.availabilityValue = kPIevent.availabilityValue.Substring(0, kPIevent.availabilityValue.Length - 1);
                        kPIevent.oeeValue          = kPIevent.oeeValue.Substring(0, kPIevent.oeeValue.Length - 1);
                        string message = (string)serializer.Serialize <KPIevent>(kPIevent);
                        myReceiver.SendResponseMessage(e.ResponseReceiverId, message);
                        //myReceiver.SendResponseMessage(e.ResponseReceiverId, null);
                        myReceiver.SendResponseMessage(e.ResponseReceiverId, "Finished");
                    }
                    catch (Exception exc) {
                        myReceiver.SendResponseMessage(e.ResponseReceiverId, "Finished");
                    }
                }
            }

            // Send back the response.
            //myReceiver.SendResponseMessage(e.ResponseReceiverId, "Hello client");
        }
Пример #6
0
        private void Command_Received(object sender, StringRequestReceivedEventArgs e)
        {
            Logger.Info("Received : " + e.RequestMessage + " from " + e.ResponseReceiverId);
            try
            {
                Command_MessageReceiver.SendResponseMessage(e.ResponseReceiverId, e.RequestMessage);
            }
            catch (Exception)
            {
            }

            // Analyze message
            // split strings
            string[] received = e.RequestMessage.Split('|');
            //Logger.Info("Split");
            foreach (string s in received)
            {
                //Logger.Info(s);
            }
            // version|command|target    |param1|param2
            // 1      |send   |responseid|123456|10
            // 1      |receive|responseid|123456|10
            // 1      |notify |responseid|open  |0
            // 1      |notify |responseid|close |0

            // receiving responsereceiverid from client
            // identify command
            // TODO:Try catch
            double version = double.Parse(received[0]);
            string command = received[1];
            string target  = received[2];
            string param1  = received[3];
            string param2  = received[4];

            Logger.Info("Split " + version + " " + command + " " + target + " " + param1 + " " + param2);
            //Logger.Info("Version : " + version);
            //Logger.Info("Command : " + command);
            //Logger.Info("Target : " + target);
            //Logger.Info("Param1 : " + param1);
            //Logger.Info("Param2 : " + param2);

            if (version == 1.0)
            {
                if (command.Equals("send"))
                {
                    // convert param to appropriate type
                    // TODO:tryparse
                    int length = int.Parse(param1);
                    int repeat = int.Parse(param2);

                    // Send a message.
                    byte[] data = new byte[length];                     // initialize 1MB data
                    //byte[] data = new byte[10]; // initialize 1MB data
                    Random random = new Random();
                    random.NextBytes(data);

                    int    i = 0;
                    string s = "a";
                    Logger.Info("Start sending " + length + " bytes of data " + repeat + " times to " + target);
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    Command_MessageReceiver.SendResponseMessage(e.ResponseReceiverId, s);
                    do
                    {
                        Worker4504_InputChannel.SendResponseMessage(target, data);

                        i++;
                    } while (i < repeat);
                    s = "z";
                    Command_MessageReceiver.SendResponseMessage(e.ResponseReceiverId, s);
                    stopwatch.Stop();
                    Logger.Info("Finished sending " + length + " bytes of data " + repeat + " times to " + target + " in " + stopwatch.ElapsedMilliseconds + " milliseconds");
                }
                else if (command.Equals("receive"))
                {
                }
                else if (command.Equals("notify"))
                {
                }
                else
                {
                    Logger.Error("Unknown command : " + command);
                }
            }
            else
            {
                Logger.Error("Unknown command : " + e.RequestMessage);
            }

            // Calculate mtu
            // Warm up stopwatch
            // Send message
        }