Esempio n. 1
0
        public void Post()
        {
            byte[] m_Bytes = BinaryData.ReadToEnd(Request.Body);
            var    invalue = System.Text.Encoding.UTF8.GetString(m_Bytes);

            Console.WriteLine("Value in : [" + invalue + "]");

            try
            {
                WISE4012ViewModel objWISE  = JsonConvert.DeserializeObject <WISE4012ViewModel>(invalue);
                string            strMAC12 = StringConverts.MAC17ToMAC12Digit(objWISE.MAC);
                // Get Device/Port/Machine by MAC Address

                var objMN = _context.TbIoTdevice.First(x => x.IoTdeviceMacAddress.Equals(strMAC12));

                var objMachine = _thingService.GetCurrentActivityByMachine(objMN.MachineId);

                if (objMachine == null)
                {
                    return;                     // Machine Not Found -- not keep log data to system
                }
                var objMongo  = new IotLoggerMongo();
                var objMCLog  = new MachineRunningStatus();
                var objMCStat = new MachineStatusLog();

                //if (objMachine != null)
                //{
                //    //objMachine.LastUpdateDateTime = DateTime.Now;

                //    objMCStat.MachineID = objMachine.Id;
                //    objMCStat.CDateTime = objWISE.TIM;
                //    objMCStat.SDateTime = DateTime.Now;
                //    objMCStat.MachineRunningStatus = "0"; //Set values to Default 0 Machine Turn off
                //}

                bool changeStatus = false;

                foreach (PortStatus pd in objMachine.ThingPortsStatus)
                {
                    // Check match port and value
                    for (int i = 0; i <= objWISE.Record.GetUpperBound(0); i++)
                    {
                        if (objWISE.Record[i, 0] == 0 && objWISE.Record[i, 1] == pd.IoTdevicePort &&
                            new[] { "1", "2", "3", "7", "10", "12", "19", "20" }.Contains(objWISE.Record[i, 2].ToString()))
                        {
                            var objMGLogger = new MGLogger();
                            objMGLogger.MachineID   = objMachine.Id;
                            objMGLogger.CDateTime   = objWISE.TIM;
                            objMGLogger.Port        = pd.IoTdevicePort.ToString();
                            objMGLogger.SensorValue = objWISE.Record[i, 3].ToString();
                            objMGLogger.MAC         = strMAC12;
                            objMGLogger.UID         = objWISE.UID;
                            objMGLogger.SDateTime   = DateTime.Now; //objMCStat.SDateTime;
                                                                    //var atvlog = new TbIoTactivityLog();
                                                                    //atvlog.MachineId = objMachine.Id;
                                                                    //atvlog.ClientDateTime = objWISE.TIM;
                                                                    //atvlog.SensorId = pd.IoTdevicePort;
                                                                    //atvlog.SensorValue = objWISE.Record[i, 3];
                                                                    //atvlog.ServerDateTime = DateTime.Now;

                            //_context.TbIoTactivityLog.Add(atvlog);
                            //if (objMGLogger.SensorValue == "1")
                            //{
                            // var statusVal = i + 1;
                            // objMCStat.MachineRunningStatus = statusVal.ToString(); // Set Port Number to Machine Status
                            // }

                            pd.LastUpdateDateTime = objWISE.TIM;

                            // i < 3 mean check change status only port 0 to 2
                            if (i < 3 && (pd.Value != objWISE.Record[i, 3]))
                            {
                                pd.Value = objWISE.Record[i, 3];

                                changeStatus = true;
                            }

                            objMongo.Create(objMGLogger);



                            // ------ Comment for run production
                            //if (objMachine != null)
                            //{
                            //    this.UpdateMonitorSensor(objMachine.Id.Trim() + pd.IoTdevicePortID.Trim(), pd.Value.ToString());
                            //}



                            //Thread.Sleep(5000);

                            /*
                             * var tmpLog = new DeviceActivityLogModel();
                             * tmpLog.MachineID = pd.MachineId;
                             * tmpLog.ClientDateTime = objWISE.TIM;
                             * tmpLog.DevicePort = pd.IoTdevicePort;
                             * tmpLog.SensorValue = objWISE.Record[i, 3];
                             * _thingService.SetCurrentActivity(tmpLog);
                             */
                        }
                    }
                }

                if (changeStatus && objMachine != null)
                {
                    if (objMachine.ThingPortsStatus[0].Value == 1)
                    {
                        objMachine.OnlineStatus = "1"; // Green
                    }
                    else
                    if (objMachine.ThingPortsStatus[1].Value == 1)
                    {
                        objMachine.OnlineStatus = "2"; // Red
                    }
                    else
                    if (objMachine.ThingPortsStatus[2].Value == 1)
                    {
                        objMachine.OnlineStatus = "3"; // Yellow
                    }
                    else
                    {
                        objMachine.OnlineStatus = "0"; // Black
                    }
                    objMachine.LastUpdateDateTime = DateTime.Now;

                    objMCStat.MachineID            = objMachine.Id;
                    objMCStat.MachineName          = objMachine.Name;
                    objMCStat.CDateTime            = objWISE.TIM;
                    objMCStat.SDateTime            = DateTime.Now;
                    objMCStat.MachineRunningStatus = objMachine.OnlineStatus;

                    objMCLog.Create(objMCStat);

                    this.UpdateMachineChangeStatus(objMachine);

                    //objMachine.OnlineStatus = objMCStat.MachineRunningStatus;
                }

                //Console.WriteLine(strMAC12);
            }
            catch (Exception ex)
            {
                var errdata = new TbUdlog();
                errdata.Indata         = "Error Value : " + ex.Message + " : " + ex.StackTrace + " || " + invalue;
                errdata.ServerDateTime = DateTime.Now;
                errdata.Id             = Guid.NewGuid().ToString();
                _context.TbUdlog.Add(errdata);
                _context.SaveChanges();
            }

            //Console.WriteLine(" ------------- Application Content-type : [" + Request.Headers["Content-Type"] + "]");
            //_context.SaveChanges();
            //Console.WriteLine(value.MAC + " : " + value.PE);
        }
Esempio n. 2
0
 public MachineStatusLog Create(MachineStatusLog p)
 {
     p.Id = Guid.NewGuid().ToString();
     _db.GetCollection <MachineStatusLog>(_mgCollName).Save(p);
     return(p);
 }