Пример #1
0
        public String InspectAlarmInfo()
        {
            List <DeviceDataModel> deviceData = _deviceDataDao.GetNotInspected();

            foreach (DeviceDataModel dd in deviceData)
            {
                if (Convert.ToInt32(dd.IndexValue) > 100)
                {
                    AlarmInfoModel alarmInfo = new AlarmInfoModel();
                    alarmInfo.AlarmInfo      = dd.Id;
                    alarmInfo.DeviceId       = dd.DeviceId;
                    alarmInfo.IndexId        = dd.IndexId;
                    alarmInfo.IndexName      = dd.IndexName;
                    alarmInfo.IndexValue     = dd.IndexValue;
                    alarmInfo.ThresholdValue = "100";
                    alarmInfo.Timestamp      = DateTime.Now;

                    _alarmInfoDao.Create(alarmInfo);
                }
            }
            return("success");
        }
Пример #2
0
        public void Run()
        {
            List <DeviceDataModel> dataNotInspected = _deviceDataDao.GetNotInspected();
            Dictionary <String, List <DeviceDataModel> > sortedData = new Dictionary <string, List <DeviceDataModel> >();
            List <String> deviceIds = new List <string>();

            foreach (DeviceDataModel d in dataNotInspected)
            {
                if (!sortedData.ContainsKey(d.DeviceId))
                {
                    sortedData.Add(d.DeviceId, new List <DeviceDataModel>());
                    sortedData[d.DeviceId].Add(d);
                }
                else
                {
                    sortedData[d.DeviceId].Add(d);
                }
                deviceIds.Add(d.DeviceId);
            }
            deviceIds = deviceIds.Distinct().ToList();
            Dictionary <String, String> operatorName = new Dictionary <string, string>();

            operatorName.Add("equal", "=");
            operatorName.Add("greater", ">");
            operatorName.Add("less", "<");
            foreach (String did in deviceIds)
            {
                List <ThresholdModel> thresholdDic = _thresholdDao.GetByDeviceId(did);
                foreach (DeviceDataModel data in sortedData[did])
                {
                    var query = thresholdDic.AsQueryable()
                                .Where(t => t.IndexId == data.IndexId)
                                .ToList();
                    foreach (var th in query)
                    {
                        String op        = th.Operator;
                        double threshold = th.ThresholdValue;
                        String svty      = this._severityDao.GetById(int.Parse(th.Severity)).SeverityName;

                        Boolean abnormal = false;

                        if (op == "equal")
                        {
                            if (double.Parse(data.IndexValue) - threshold < 0.0001)
                            {
                                abnormal = true;
                            }
                        }
                        else if (op == "less")
                        {
                            if (double.Parse(data.IndexValue) <= threshold)
                            {
                                abnormal = true;
                            }
                        }
                        else if (op == "greater")
                        {
                            if (double.Parse(data.IndexValue) >= threshold)
                            {
                                abnormal = true;
                            }
                        }

                        if (abnormal == true)
                        {
                            AlarmInfoModel alarmInfo = new AlarmInfoModel();
                            alarmInfo.AlarmInfo      = th.Description;
                            alarmInfo.DeviceId       = data.DeviceId;
                            alarmInfo.IndexId        = data.IndexId;
                            alarmInfo.IndexName      = data.IndexName;
                            alarmInfo.IndexValue     = data.IndexValue;
                            alarmInfo.ThresholdValue = operatorName[op] + threshold.ToString();
                            alarmInfo.Timestamp      = DateTime.Now;
                            alarmInfo.Severity       = svty;

                            _alarmInfoDao.Create(alarmInfo);
                        }
                    }
                }
            }
        }
        public void Run()
        {
            /*
             * this._logger.LogInformation("AlarmInfoJob Run ...");
             * List<DeviceModel> devices = this._deviceDao.Get("all");
             * //将所有设备置为在线状态
             * foreach (DeviceModel device in devices)
             * {
             *  this._deviceDao.SetDeviceOnlineStatus(device.HardwareDeviceId, "yes");
             * }
             * //获取设备数据中"IsScam"="false"的所有数据,并置为"true"
             * List<DeviceDataModel> dataNotInspected = _deviceDataDao.GetNotInspected();
             * Dictionary<String, List<DeviceDataModel>> sortedData = new Dictionary<string, List<DeviceDataModel>>();
             * Dictionary<String, List<String>> fieldMap = new Dictionary<string, List<string>>();
             * List<String> deviceIds = new List<string>();
             * //将deviceId相同的数据整合成sortedData,key存放deviceId,value存放相应的数据列表
             * //将deviceId相同的属性Id整合成fieldMap,key存放deviceId,value存放属性Id的列表
             * foreach (DeviceDataModel d in dataNotInspected)
             * {
             *  if (!sortedData.ContainsKey(d.DeviceId))
             *  {
             *      sortedData.Add(d.DeviceId, new List<DeviceDataModel>());
             *      sortedData[d.DeviceId].Add(d);
             *  }
             *  else
             *  {
             *      sortedData[d.DeviceId].Add(d);
             *  }
             *
             *  if (!fieldMap.ContainsKey(d.DeviceId))
             *  {
             *      fieldMap.Add(d.DeviceId, new List<string>());
             *      fieldMap[d.DeviceId].Add(d.IndexId);
             *  }
             *  else
             *  {
             *      fieldMap[d.DeviceId].Add(d.IndexId);
             *  }
             *
             *  deviceIds.Add(d.DeviceId);
             * }
             *
             * //Automatically add fields from device data
             * foreach (String did in fieldMap.Keys)
             * {
             *  List<FieldSerializer> affiliateFields = _fieldBus.GetAffiliateFields(did);
             *  List<String> affiliateFieldsId = new List<string>();
             *  foreach (var field in affiliateFields)
             *  {
             *      affiliateFieldsId.Add(field.fieldId);
             *  }
             *
             *  foreach (String fid in fieldMap[did])
             *  {
             *      if (!affiliateFieldsId.Contains(fid))
             *      {
             *          FieldSerializer tmpField = new FieldSerializer();
             *          tmpField.fieldName = fid;
             *          tmpField.fieldId = fid;
             *          DeviceModel tmpDevice = this._deviceDao.GetByDeviceId(did);
             *          tmpField.device = tmpDevice.DeviceName;
             *          this._fieldBus.CreateNewField(tmpField);
             *      }
             *  }
             * }
             *
             * deviceIds = deviceIds.Distinct().ToList();
             *
             * foreach (String did in deviceIds)
             * {
             *  this._deviceDao.UpdateLastConnectionTimeByDeviceId(did);
             * }
             *
             * foreach (DeviceModel d in devices)
             * {
             *  DeviceTypeModel tmpDeviceType = this._stateTypeDao.GetDeviceTypeByName(d.DeviceType);
             *  Double tmpOfflineTime = tmpDeviceType.OfflineTime;
             *  TimeSpan passTime = DateTime.Now - d.LastConnectionTime.ToLocalTime();
             *  if (passTime.TotalMinutes > tmpOfflineTime)
             *  {
             *      this._deviceDao.SetDeviceOnlineStatus(d.HardwareDeviceId, "no");
             *  }
             * }
             *
             * Dictionary<String, String> operatorName = new Dictionary<string, string>();
             * operatorName.Add("equal", "=");
             * operatorName.Add("greater", ">");
             * operatorName.Add("less", "<");
             * foreach (String did in deviceIds)
             * {
             *  List<ThresholdModel> thresholdDic = _thresholdDao.GetByDeviceId(did);
             *  foreach (DeviceDataModel data in sortedData[did])
             *  {
             *      var query = thresholdDic.AsQueryable()
             *          .Where(t => t.IndexId == data.IndexId)
             *          .ToList();
             *      foreach (var th in query)
             *      {
             *          String op = th.Operator;
             *          double threshold = th.ThresholdValue;
             *
             *          Boolean abnormal = false;
             *
             *          if (op == "equal")
             *          {
             *              if (data.IndexValue - threshold < 0.0001)
             *              {
             *                  abnormal = true;
             *              }
             *          }
             *          else if (op == "less")
             *          {
             *              if (data.IndexValue <= threshold)
             *              {
             *                  abnormal = true;
             *              }
             *          }
             *          else if (op == "greater")
             *          {
             *              if (data.IndexValue >= threshold)
             *              {
             *                  abnormal = true;
             *              }
             *          }
             *
             *          if (abnormal == true)
             *          {
             *              AlarmInfoModel alarmInfo = new AlarmInfoModel();
             *              alarmInfo.AlarmInfo = th.Description;
             *              alarmInfo.DeviceId = data.DeviceId;
             *              alarmInfo.IndexId = data.IndexId;
             *              alarmInfo.IndexName = data.IndexName;
             *              alarmInfo.IndexValue = data.IndexValue;
             *              alarmInfo.ThresholdValue = threshold;
             *              alarmInfo.Timestamp = DateTime.Now;
             *              alarmInfo.Severity = th.Severity;
             *              alarmInfo.Processed = "No";
             *
             *              _alarmInfoDao.Create(alarmInfo);
             *          }
             *      }
             *  }
             * }*/
            /* zxin-告警信息处理和离线判断方法的理解:
             * 告警信息处理:告警判断频率 1min
             * 1、从threshold表中获取所有告警规则
             * 2、按deviceId、MonitoringId获取前1min的数据,判断是否超过阈值,超过阈值的则向alarmInfo集合中插入告警信息
             * 设备离线判断:频率 1min
             * 1、从MySQL获取所有注册设备信息以及设备类型数据
             * 2、根据deviceId从MongoDB中获取最新一条数据,如果与当前时间差值大于超时告警时间则为离线状态(设备状态更新)
             */
            _logger.LogInformation("AlarmInfoJob Run...");
            _logger.LogInformation("告警判断...");
            /*告警判断*/
            //获取所有告警规则,插入告警信息到MongoDB
            List <ThresholdModel> alarmRules = this._thresholdDao.Get("all");

            foreach (var rule in alarmRules)
            {
                //获取具有告警规则的最新60秒的设备数据
                List <DeviceDataModel> deviceDatas = this._deviceDataDao.ListNewData(rule.DeviceId, 60, rule.IndexId);
                //var isAlarmInfo=deviceDatas.AsQueryable()
                //    .Where(dd=>dd.IndexValue>rule.ThresholdValue)
                foreach (var dd in deviceDatas)
                {
                    AlarmInfoModel alarmInfo = AlarmInfoGenerator(dd, rule);
                    if (alarmInfo != null)
                    {
                        _alarmInfoDao.Create(alarmInfo);
                    }
                }
            }


            _logger.LogInformation("设备离线判断...");
            /*设备离线判断*/
            List <DeviceModel>     devices               = this._deviceDao.Get("all");
            List <DeviceTypeModel> deviceTypes           = this._stateTypeDao.ListAllDeviceType();
            Dictionary <String, List <String> > fieldMap = new Dictionary <string, List <string> >();

            foreach (var device in devices)
            {
                //获取超时告警时间(分钟)
                double   offlinetime = deviceTypes.AsQueryable().Where(dt => dt.DeviceTypeName == device.DeviceType).FirstOrDefault().OfflineTime;
                DateTime date        = DateTime.UtcNow - TimeSpan.FromMinutes(offlinetime);
                //获取最新一条设备数据
                DeviceDataModel deviceData = this._deviceDataDao.GetByDeviceName(device.DeviceName, 1).FirstOrDefault();
                if (deviceData != null)
                {
                    if (deviceData.Timestamp >= date)
                    {
                        this._deviceDao.SetDeviceOnlineStatus(device.HardwareDeviceId, "yes");//设备在线
                    }
                    else
                    {
                        this._deviceDao.SetDeviceOnlineStatus(device.HardwareDeviceId, "no");
                    }
                }
                else
                {
                    this._deviceDao.SetDeviceOnlineStatus(device.HardwareDeviceId, "no");
                }

                /* 更新设备属性:
                 * 1、列出MySQL中现有属性
                 * 2、获取MongoDB中最新数据中的属性
                 * 3、比对并创建新属性
                 */
                List <string>          existedFieldIds = this._fieldDao.ListFieldIdsByDeviceId(device.HardwareDeviceId);
                List <DeviceDataModel> deviceDatas     = this._deviceDataDao.ListNewData(device.HardwareDeviceId, 60);
                foreach (var dd in deviceDatas)
                {
                    if (!existedFieldIds.Contains(dd.IndexId))
                    {
                        FieldModel field = new FieldModel
                        {
                            FieldId   = dd.IndexId,
                            FieldName = dd.IndexName,
                            Device    = dd.DeviceName
                        };
                        this._fieldDao.Create(field);
                    }
                }
                /*更新设备的总告警次数*/
                //获取设备当前总的告警次数
                //int totalInfo = _alarmInfoDao.GetDeviceAffiliateAlarmInfoNumber(device.HardwareDeviceId);
                //int totalInfo = 0;
                //更新MySQL中的设备的告警总次数
                //_deviceBus.UpdateTotalAlarmInfo(device.HardwareDeviceId, totalInfo);
            }
        }
Пример #4
0
        public void Run()
        {
            List <DeviceDataModel> dataNotInspected = _deviceDataDao.GetNotInspected();
            Dictionary <String, List <DeviceDataModel> > sortedData = new Dictionary <string, List <DeviceDataModel> >();
            List <String> deviceIds = new List <string>();

            foreach (DeviceDataModel d in dataNotInspected)
            {
                if (!sortedData.ContainsKey(d.DeviceId))
                {
                    sortedData.Add(d.DeviceId, new List <DeviceDataModel>());
                    sortedData[d.DeviceId].Add(d);
                }
                else
                {
                    sortedData[d.DeviceId].Add(d);
                }
                deviceIds.Add(d.DeviceId);
            }
            //TODO: Uniquify
            deviceIds = deviceIds.Distinct().ToList();
            foreach (String did in deviceIds)
            {
                Dictionary <String, Tuple <String, int> > thresholdDic = _thresholdDao.GetByDeviceId(did);
                foreach (DeviceDataModel data in sortedData[did])
                {
                    String op        = thresholdDic[data.IndexId].Item1;
                    int    threshold = thresholdDic[data.IndexId].Item2;

                    Boolean abnormal = false;

                    if (op == "equal")
                    {
                        if (Int32.Parse(data.IndexValue) != threshold)
                        {
                            abnormal = true;
                        }
                    }
                    else if (op == "less")
                    {
                        if (Int32.Parse(data.IndexValue) <= threshold)
                        {
                            abnormal = true;
                        }
                    }
                    else if (op == "greater")
                    {
                        if (Int32.Parse(data.IndexValue) >= threshold)
                        {
                            abnormal = true;
                        }
                    }

                    if (abnormal == true)
                    {
                        AlarmInfoModel alarmInfo = new AlarmInfoModel();
                        alarmInfo.AlarmInfo      = data.Id;
                        alarmInfo.DeviceId       = data.DeviceId;
                        alarmInfo.IndexId        = data.IndexId;
                        alarmInfo.IndexName      = data.IndexName;
                        alarmInfo.IndexValue     = data.IndexValue;
                        alarmInfo.ThresholdValue = threshold.ToString();
                        alarmInfo.Timestamp      = DateTime.Now;

                        _alarmInfoDao.Create(alarmInfo);
                    }
                }
            }
        }