Exemplo n.º 1
0
            public static string Update(HttpListenerContext context)
            {
                string response = null;

                try
                {
                    lock (_lock)
                    {
                        using (var reader = new StreamReader(context.Request.InputStream))
                        {
                            var body = reader.ReadToEnd();

                            string json = HTTP.GetPostValue(body, "devices");
                            if (!string.IsNullOrEmpty(json))
                            {
                                var devices = JSON.ToType <List <API.Data.DeviceInfo> >(json);
                                if (devices != null && devices.Count > 0)
                                {
                                    // Load backup data on first pass
                                    if (first)
                                    {
                                        var backupInfos = Backup.Load(devices.ToArray());
                                        if (backupInfos != null && backupInfos.Count > 0)
                                        {
                                            DeviceInfos.AddRange(backupInfos);
                                        }
                                    }

                                    foreach (var device in devices)
                                    {
                                        int i = DeviceInfos.FindIndex(o => o.UniqueId == device.UniqueId);
                                        if (i < 0)
                                        {
                                            DeviceInfos.Add(device);
                                            i = DeviceInfos.FindIndex(o => o.UniqueId == device.UniqueId);
                                        }

                                        var info = DeviceInfos[i];

                                        object obj = null;

                                        obj = device.GetClass("status");
                                        if (obj != null)
                                        {
                                            info.AddClass("status", obj);
                                        }

                                        obj = device.GetClass("controller");
                                        if (obj != null)
                                        {
                                            info.AddClass("controller", obj);
                                        }

                                        // Get HourInfos for current day
                                        List <API.Data.HourInfo> hours = null;
                                        obj = info.GetClass("hours");
                                        if (obj != null)
                                        {
                                            info.RemoveClass("hours");
                                            hours = (List <API.Data.HourInfo>)obj;
                                        }

                                        // Add new HourInfo objects and then combine them into the current list
                                        obj = device.GetClass("hours");
                                        if (obj != null)
                                        {
                                            if (hours == null)
                                            {
                                                hours = new List <API.Data.HourInfo>();
                                            }

                                            hours.AddRange((List <API.Data.HourInfo>)obj);
                                        }

                                        if (hours != null)
                                        {
                                            DateTime d    = DateTime.Now;
                                            DateTime from = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0, DateTimeKind.Local);
                                            DateTime to   = from.AddDays(1);

                                            hours = hours.FindAll(o => TestHourDate(o, from, to));
                                            hours = API.Data.HourInfo.CombineHours(hours);

                                            // Add Hours
                                            info.AddClass("hours", hours);

                                            // Add OEE
                                            var oee = API.Data.HourInfo.GetOeeInfo(hours);
                                            if (oee != null)
                                            {
                                                info.AddClass("oee", oee);
                                            }

                                            // Add Timers
                                            var timers = new API.Data.TimersInfo();
                                            timers.Total = hours.Select(o => o.PlannedProductionTime).Sum();

                                            timers.Active = hours.Select(o => o.Active).Sum();
                                            timers.Idle   = hours.Select(o => o.Idle).Sum();
                                            timers.Alert  = hours.Select(o => o.Alert).Sum();

                                            timers.Production         = hours.Select(o => o.Production).Sum();
                                            timers.Setup              = hours.Select(o => o.Setup).Sum();
                                            timers.Teardown           = hours.Select(o => o.Teardown).Sum();
                                            timers.Maintenance        = hours.Select(o => o.Maintenance).Sum();
                                            timers.ProcessDevelopment = hours.Select(o => o.ProcessDevelopment).Sum();

                                            info.AddClass("timers", timers);

                                            response = "Devices Updated Successfully";
                                        }
                                    }

                                    first = false;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex) { logger.Error(ex); }

                return(response);
            }