public DeviceStatusHistory GetLastDeviceStatusHistory(string ip)
        {
            var lastRecordQuery = from data in Entities.DeviceStatusHistories
                                  where data.IPAddress == ip
                                  orderby data.Date descending
                                  select data;

            DeviceStatusHistory lastRecordTemp = lastRecordQuery.FirstOrDefault();

            return(lastRecordTemp);
        }
//        public DeviceStatusCache GetDeviceStatus(string IPAddress)
//        {
//            string address = string.Format("http://{0}/remote_diagnostics.htm", IPAddress);

//            WebClient client = new WebClient();
//            HtmlDocument htmlDocument = new HtmlDocument();

//            try
//            {
//                htmlDocument.LoadHtml(client.DownloadString(address));
//            }
//            catch (System.Net.WebException ex)
//            {
//                logger.LogException(LogLevel.Info, string.Format("MonitoringService.GetDeviceStatus"), ex);

//                return null;
//            }

//            //HtmlDocument htmlDocument = new HtmlWeb().Load(address);

//            if (htmlDocument.DocumentNode == null)
//            {
//                return null;
//            }

//            HtmlNode StatusNode = null;

//#if DEBUG
//            StatusNode =
//             htmlDocument.DocumentNode.SelectSingleNode(
//                 "/html/body/table/tbody/tr/td[2]/table[2]/tbody/tr[2]/td[2]/table/tbody/tr/td/div/font/b/font");
//#endif

//#if RELEASE
//                StatusNode = htmlDocument.DocumentNode.SelectSingleNode(
//                    "/html/body/table/tr/td[2]/table[2]/tr[2]/td[2]/table/tr/td/div/font/b/font");
//#endif
//            string StatusString = StatusNode.InnerText.Replace(" ", "").Replace(" ", "").Trim();

//            var deviceStatusQuery = Entities.DeviceStatus;
//            DeviceStatu match = deviceStatusQuery.FirstOrDefault(x => x.Status == StatusString);
//            DeviceStatusCache result = new DeviceStatusCache(match.StatusId, match.Status, match.Description, (StatusColor)match.StatusColor, IPAddress);

//            return result;
//        }

        public bool AddNewDeviceStatusHistory(int id, string ip)
        {
            DeviceStatusHistory deviceStatusHistory = new DeviceStatusHistory();

            deviceStatusHistory.IPAddress = ip;
            deviceStatusHistory.StatusId  = id;
            deviceStatusHistory.Date      = DateTime.Now;
            Entities.DeviceStatusHistories.AddObject(deviceStatusHistory);

            if (Entities.SaveChanges() > 0)
            {
                return(true);
            }

            return(false);
        }
Exemple #3
0
 //http://localhost:5001/IPDevice/deviceStatus?deviceInfoId=7d57976c-a472-8a47-81d4-0005d4dff4ec&StatusId=A0002016-E009-B019-E001-ABCD13800002
 public IActionResult UpdateDeviceStatus(Guid deviceInfoId, Guid statusId)
 {
     using (var db = new AllInOneContext.AllInOneContext())
     {
         try
         {
             //SystemOption statusOptions = db.SystemOption.FirstOrDefault(t => t.SystemOptionCode.Equals(statusId));
             //if (statusOptions == null)
             //    return BadRequest(new ApplicationException() { ErrorCode = "Unknow", ErrorMessage = "未定义设备状态" });
             var device = db.IPDeviceInfo.FirstOrDefault(t => t.IPDeviceInfoId.Equals(deviceInfoId));
             if (device != null && !device.StatusId.Equals(statusId))
             {
                 device.StatusId = statusId;
                 db.IPDeviceInfo.Update(device);
                 //status history
                 DeviceStatusHistory statusHis = new DeviceStatusHistory()
                 {
                     DeviceStatusHistoryId = Guid.NewGuid(),
                     DeviceInfoId          = deviceInfoId,
                     StatusId   = statusId,
                     CreateTime = DateTime.Now
                 };
                 db.DeviceStatusHistory.Add(statusHis);
                 db.SaveChanges();
                 MQPulish.PublishMessage("DeviceStatus", statusHis);
             }
             return(Ok());
         }
         catch (Exception ex)
         {
             _logger.LogError("更新设备状态异常:Message:{0}\r\nStackTrace:{1}", ex.Message, ex.StackTrace);
             return(BadRequest(new ApplicationException()
             {
                 ErrorCode = "Unknow", ErrorMessage = ex.Message
             }));
         }
     }
 }
        public void CheckDeviceStatus()
        {
            SharedLibraryMonitoringDevice.DeviceStatusCache result = null;

            try
            {
                //result = channel.GetDeviceStatus(IPAddress);

                if (Appl18 != null)
                {
                    result = Appl18.GetDeviceStatus();
                }
                else if (Appl19 != null)
                {
                    result = Appl19.GetDeviceStatus();
                }
            }
            catch (Exception ex)
            {
                logger.LogException(LogLevel.Info, string.Format("SaveDeviceStatus.CheckDeviceStatus.{0}", this.IPAddress), ex);
                return;
            }

            if (result == null)
            {
                return;
            }

            DeviceStatusHistory lastRecord = null;

            try
            {
                lastRecord = channel.GetLastDeviceStatusHistory(IPAddress);
            }
            catch (Exception ex)
            {
                logger.LogException(LogLevel.Info, string.Format("SaveDeviceStatus.CheckDeviceStatus.{0}", this.IPAddress), ex);
                //return;
            }

            if (lastRecord == null)
            {
                try
                {
                    channel.AddNewDeviceStatusHistory(result.Id, IPAddress);
                }
                catch (Exception ex)
                {
                    logger.LogException(LogLevel.Info, string.Format("SaveDeviceStatus.CheckDeviceStatus.{0}", this.IPAddress), ex);
                }
                SaveValueInDeviceStatusLastRecord(result.Id);
                return;
            }

            //TimeSpan timeSpanDiff = DateTime.Now - lastRecord.Date;

            //if (timeSpanDiff > MaxTimeInterval)
            //{
            //    try
            //    {
            //        channel.AddNewDeviceStatusHistory(result.Id, IPAddress);
            //    }
            //    catch (System.Exception ex)
            //    {
            //        //TODO Exception
            //    }
            //    SaveValueInDeviceStatusLastRecord(result.Id);
            //    return;
            //}

            if (result.Id != lastRecord.StatusId)
            {
                try
                {
                    channel.AddNewDeviceStatusHistory(result.Id, IPAddress);
                }
                catch (Exception ex)
                {
                    logger.LogException(LogLevel.Info, string.Format("SaveDeviceStatus.CheckDeviceStatus.{0}", this.IPAddress), ex);
                }

                SaveValueInDeviceStatusLastRecord(result.Id);
                return;
            }

            SaveValueInDeviceStatusLastRecord(result.Id);
        }