浏览代码

设备采集优化

zhuhao 2 年之前
父节点
当前提交
3a27f534d5

+ 5 - 0
IMCS_CCS/Entitys/RequestData.cs

@@ -190,6 +190,11 @@ namespace IMCS.CCS.Models
         /// </summary>
         public string alertMsg { get; set; } = "";
 
+        /// <summary>
+        /// 是否报警状态
+        /// </summary>
+        public bool alarmState { get; set; } = false;
+
     }
 
     public class RequestSynStatusData

+ 1 - 0
IMCS_CCS/Repository/DeviceRepository.cs

@@ -24,6 +24,7 @@ namespace IMCS.CCS.Repository
             return  devices.ToList();
 
         }
+        //所有使用设备
         public List<Device> GetDeviceAllList()
         {
             IQueryable<Device> devices = _context.Device;

+ 11 - 5
IMCS_CCS/Service/Impl/DeviceService.cs

@@ -4,6 +4,7 @@ using System.Threading.Tasks;
 using IMCS.CCS.Entitys;
 using IMCS.CCS.Common.Redis;
 using Newtonsoft.Json;
+using IMCS_CCS.Model;
 
 namespace IMCS.CCS.Services
 {
@@ -17,11 +18,12 @@ namespace IMCS.CCS.Services
             _repository = repository;
             _redisService = redisService;
         }
+        //所有使用的设备
         public List<Device> GetDeviceList()
         {
             return  _repository.GetDeviceList();
         }
-
+        //所有使用的设备
         public List<Device> GetDeviceAllList()
         {
             return _repository.GetDeviceAllList();
@@ -53,10 +55,14 @@ namespace IMCS.CCS.Services
         public async Task<bool> UpdateAndCache(Device vo)
         { 
             await _repository.UpdateDevice(vo);
-            
-            string callback_redis_key = "IMCS_CCS:DeviceList";
-            List<Device> devices =  _repository.GetDeviceAllList();
-            await _redisService.Database.StringSetAsync(callback_redis_key, JsonConvert.SerializeObject(devices));
+       
+            //所有使用的设备
+            List<Device> allUseDevices = _repository.GetDeviceAllList();
+            await _redisService.Database.StringSetAsync("IMCS_CCS:" + RedisKeyEnum.ALLUseDeviceList, JsonConvert.SerializeObject(allUseDevices));
+
+            //所有的设备
+            List<Device> allDevices = _repository.GetDevices();
+            await _redisService.Database.StringSetAsync("IMCS_CCS:" + RedisKeyEnum.ALLDeviceList, JsonConvert.SerializeObject(allDevices));
             return true;
         }
     }

+ 4 - 3
IMCS_CCS/Service/Impl/EquipmentMonitorService .cs

@@ -1,6 +1,7 @@
 using IMCS.CCS.Common.Redis;
 using IMCS.CCS.Entitys;
 using IMCS.CCS.Repository;
+using IMCS_CCS.Model;
 using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
@@ -49,9 +50,9 @@ namespace IMCS.CCS.Services
                 await _repository.Update(changeEquipment);
             } 
 
-            string redis_key = "IMCS_CCS:MonitorEquipmentStatusJob";
-            List<EquipmentMonitor> devices = await _repository.GetList();
-            await _redisService.Database.StringSetAsync(redis_key, JsonConvert.SerializeObject(devices));
+            string redis_key = "IMCS_CCS:" + RedisKeyEnum.MonitorEquipmentStatusJob;
+            List<EquipmentMonitor> equipmentMonitors = await _repository.GetList();
+            await _redisService.Database.StringSetAsync(redis_key, JsonConvert.SerializeObject(equipmentMonitors));
             return true;
         }
 

+ 2 - 2
IMCS_CCS/Service/Impl/HttpRequestService.cs

@@ -825,7 +825,7 @@ namespace IMCS.CCS.Services
 
             try
             {
-                List<Device> devices = _deviceService.GetDeviceAllList();
+                List<Device> devices = _deviceService.GetDeviceList();
                 Device deviceFtp = devices.Where(x => x.Ip.Equals(req.url)
                                             && x.ProtocolType.Equals(ProtocalTypeEnum.FTP.ToString())).FirstOrDefault();
                 Device deviceFanuc = devices.Where(x => x.Ip.Equals(req.url)
@@ -973,7 +973,7 @@ namespace IMCS.CCS.Services
 
             try
             {
-                List<Device> devices = _deviceService.GetDeviceAllList();
+                List<Device> devices = _deviceService.GetDeviceList();
                 Device deviceFtp = devices.Where(x => x.Ip.Equals(req.url)
                                             && x.ProtocolType.Equals(ProtocalTypeEnum.FTP.ToString())).FirstOrDefault();
                 Device deviceFanuc = devices.Where(x => x.Ip.Equals(req.url)

+ 77 - 63
IMCS_CCS/Service/Impl/TaskJobService.cs

@@ -17,6 +17,7 @@ using System.Threading;
 using System.Threading.Tasks;
 using IMCS.CCS.Models;
 using System.Net.NetworkInformation;
+using IMCS_CCS.Model;
 
 namespace IMCS.CCS.Service.Impl
 {
@@ -61,9 +62,7 @@ namespace IMCS.CCS.Service.Impl
 
         private string LOG_TITLE_DEVICE = "设备监控";
 
-        private string callback_redis_key = "IMCS_CCS:CallbackTaskList";
-
-        private string device_redis_key = "IMCS_CCS:DeviceList";
+        private string imcs_redis_key; 
 
         public IConfiguration Configuration { get; }
 
@@ -415,7 +414,7 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> CallbackJob1()
         {
             List<TaskCallback> list = new List<TaskCallback>();
-            var taskCallbackListData = await _redisService.Database.StringGetAsync(callback_redis_key);
+            var taskCallbackListData = await _redisService.Database.StringGetAsync(imcs_redis_key + RedisKeyEnum.CallbackTaskList);
             if (taskCallbackListData.IsNullOrEmpty)
             {
                 list = await _taskCallbackService.GetAllList();
@@ -474,7 +473,7 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> CallbackJob2()
         {
             List<TaskCallback> taskList = new List<TaskCallback>();
-            var taskCallbackListData = await _redisService.Database.StringGetAsync(callback_redis_key);
+            var taskCallbackListData = await _redisService.Database.StringGetAsync(imcs_redis_key + RedisKeyEnum.CallbackTaskList);
             if (taskCallbackListData.IsNullOrEmpty)
             {
                 taskList = await _taskCallbackService.GetAllList();
@@ -623,7 +622,7 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> CallbackJob3()
         {
             List<TaskCallback> taskList = new List<TaskCallback>();
-            var taskCallbackListData = await _redisService.Database.StringGetAsync(callback_redis_key);
+            var taskCallbackListData = await _redisService.Database.StringGetAsync(imcs_redis_key + RedisKeyEnum.CallbackTaskList);
             if (taskCallbackListData.IsNullOrEmpty)
             {
                 taskList = await _taskCallbackService.GetAllList();
@@ -759,7 +758,7 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> CallbackJob4()
         {
             List<TaskCallback> taskList = new List<TaskCallback>();
-            var taskCallbackListData = await _redisService.Database.StringGetAsync(callback_redis_key);
+            var taskCallbackListData = await _redisService.Database.StringGetAsync(imcs_redis_key + RedisKeyEnum.CallbackTaskList);
             if (taskCallbackListData.IsNullOrEmpty)
             {
                 taskList = await _taskCallbackService.GetAllList();
@@ -883,7 +882,7 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> CallbackJob5()
         {
             List<TaskCallback> taskList = new List<TaskCallback>();
-            var taskCallbackListData = await _redisService.Database.StringGetAsync(callback_redis_key);
+            var taskCallbackListData = await _redisService.Database.StringGetAsync(imcs_redis_key + RedisKeyEnum.CallbackTaskList);
             if (taskCallbackListData.IsNullOrEmpty)
             {
                 taskList = await _taskCallbackService.GetAllList();
@@ -1008,7 +1007,7 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> CallbackJob6()
         {
             List<TaskCallback> taskList = new List<TaskCallback>();
-            var taskCallbackListData = await _redisService.Database.StringGetAsync(callback_redis_key);
+            var taskCallbackListData = await _redisService.Database.StringGetAsync(imcs_redis_key + RedisKeyEnum.CallbackTaskList);
             if (taskCallbackListData.IsNullOrEmpty)
             {
                 taskList = await _taskCallbackService.GetAllList();
@@ -1240,21 +1239,30 @@ namespace IMCS.CCS.Service.Impl
             try
             {
                 string message = "MonitorEquipmentStatusJob调度成功";
-                List<Device> devices = await getDeviceList();
-                //在线设备
-                //ftp不监控
-                devices = devices.Where(x => !x.ProtocolType.Equals(ProtocalTypeEnum.FTP.ToString())).ToList(); 
-               
+                //查询全部使用设备包括不在线和离线
+                string all_device_redis_key = imcs_redis_key + RedisKeyEnum.ALLDeviceList ;
+                List<Device> devices = new List<Device>(); 
+                var deviceListData = await _redisService.Database.StringGetAsync(all_device_redis_key);
+                if (deviceListData.IsNullOrEmpty)
+                {
+                    devices = _deviceService.GetDevices();
+                    await _redisService.Database.StringSetAsync(all_device_redis_key, JsonConvert.SerializeObject(devices));
+                }
+                else
+                {
+                    devices = JsonConvert.DeserializeObject<List<Device>>(deviceListData);
+                }
+
                 List<EquipmentMonitor> equipmentMonitors = new List<EquipmentMonitor>();
-                string redis_key = "IMCS_CCS:MonitorEquipmentStatusJob";
-                var data = await _redisService.Database.StringGetAsync(redis_key);
+                string equipment_monitors_redis_key = imcs_redis_key + RedisKeyEnum.MonitorEquipmentStatusJob; ;
+                var data = await _redisService.Database.StringGetAsync(equipment_monitors_redis_key);
                 if (data.IsNullOrEmpty)
                 {
                     equipmentMonitors = await _equipmentMonitorService.GetList();
                     //首次设置redis
                     if (equipmentMonitors != null && equipmentMonitors.Count > 0)
                     {
-                        await _redisService.Database.StringSetAsync(redis_key, JsonConvert.SerializeObject(equipmentMonitors));
+                        await _redisService.Database.StringSetAsync(equipment_monitors_redis_key, JsonConvert.SerializeObject(equipmentMonitors));
                     }
                 }
                 else
@@ -1265,33 +1273,33 @@ namespace IMCS.CCS.Service.Impl
                 List<EquipmentMonitor> changeEquipmentMonitors = new List<EquipmentMonitor>();
                 foreach (EquipmentMonitor equipment in equipmentMonitors)
                 {
-                    Device device = devices.Where(x => x.Ip.Equals(equipment.IP)).FirstOrDefault();
-                    if (device != null && !device.State)
+                    Device device = devices.Where(x => x.Ip == equipment.IP).FirstOrDefault();
+                     if (device != null && !device.UseState)
                     {
-                        if (equipment.Status != "线")
+                        if (equipment.Status != "线")
                         {
-                            equipment.Status = "线";
+                            equipment.Status = "线";
                             changeEquipmentMonitors.Add(equipment);
                         }
-
                         continue;
                     }
-                    else if (device != null && device.State && !device.UseState)
+                    else if(device != null && !device.State)
                     {
-                        if (equipment.Status != "线")
+                        if (equipment.Status != "线")
                         {
-                            equipment.Status = "线";
+                            equipment.Status = "线";
                             changeEquipmentMonitors.Add(equipment);
                         }
+
                         continue;
-                    }
+                    } 
                     else if (device != null && equipment.Name == "机械手")
                     {
-                        //缓存取PLCagvalue 值
+                        //缓存取PLC tagvalue 值
                         List<CcsTagValue> tagValues = new List<CcsTagValue>();
-                        string tag_value_redis_key = "IMCS_CCS:" + device.Ip + ":" + device.ProtocolType;
+                        string tag_value_redis_key = imcs_redis_key + device.Ip + ":" + device.ProtocolType;
                         var tagData = await _redisService.Database.StringGetAsync(tag_value_redis_key);
-                        if (data.IsNullOrEmpty)
+                        if (tagData.IsNullOrEmpty)
                         {
                            
                             CcsTagValue tagQuery = new CcsTagValue();
@@ -1301,14 +1309,14 @@ namespace IMCS.CCS.Service.Impl
                             //首次设置redis
                             if (tagValues != null && tagValues.Count > 0)
                             {
-                                await _redisService.Database.StringSetAsync(redis_key, JsonConvert.SerializeObject(tagValues));
+                                await _redisService.Database.StringSetAsync(tag_value_redis_key, JsonConvert.SerializeObject(tagValues));
                             }
                         }
                         else
                         {
                             tagValues = JsonConvert.DeserializeObject<List<CcsTagValue>>(data);
                         }
-                        CcsTagValue ccsTagValue = tagValues.Where(x => x.Ip.Equals(equipment.IP) && x.Address.Equals(equipment.Address)).FirstOrDefault();
+                        CcsTagValue ccsTagValue = tagValues.Where(x => x.Ip == equipment.IP && x.Address == equipment.Address).FirstOrDefault();
                         if (ccsTagValue != null)
                         {
                             if (ccsTagValue.TagValue == "3")
@@ -1322,7 +1330,7 @@ namespace IMCS.CCS.Service.Impl
                                 continue;
                             }
                         }
-                        CcsTagValue ccsTagValue2 = tagValues.Where(x => x.Ip.Equals(equipment.IP) && x.Address.Equals(equipment.Address2)).FirstOrDefault();
+                        CcsTagValue ccsTagValue2 = tagValues.Where(x => x.Ip == equipment.IP && x.Address == equipment.Address2).FirstOrDefault();
                         if (ccsTagValue2 != null)
                         {
                             if (ccsTagValue2.TagValue == "9")
@@ -1361,7 +1369,7 @@ namespace IMCS.CCS.Service.Impl
                     }
 
                 }
-
+                //初始化推送列表
                 List<ProductionStatus> productionStatusList = new List<ProductionStatus>();
                
                 //变化的修改 并且更新缓存
@@ -1371,37 +1379,35 @@ namespace IMCS.CCS.Service.Impl
                     {
                         ProductionStatus productionStatus = new ProductionStatus();
                         productionStatus.id = changeEquipment.Id.ToString();
-                        productionStatus.onlineStatus = syncStatusMap[changeEquipment.Status].ToString();
-                        //equipment.Status == "离线" ? "0" : "1";
-                        //productionStatus.alertMsg = equipment.AlertMsg;
-                        
+                        productionStatus.onlineStatus = syncStatusMap[changeEquipment.Status].ToString(); 
                         productionStatusList.Add(productionStatus);
                        
                     }
                     //修改并重置缓存
                     await _equipmentMonitorService.UpdateAndCache(equipmentMonitors); 
                 }
-                //报警放入推送数组
-                foreach (Device device in devices)
+                //在线设备,报警放入推送数组
+                List<Device> onlineDeviceList = await getDeviceList();
+                foreach (Device device in onlineDeviceList)
                 {
-                    EquipmentMonitor alarmEquipment = equipmentMonitors.Where(x => x.IP == device.Ip && x.Type == device.ProtocolType).FirstOrDefault();
-                    ProductionStatus productionStatus = new ProductionStatus();
-                    productionStatus.id = alarmEquipment.Id.ToString();
+                    EquipmentMonitor alarmEquipment = equipmentMonitors.Where(x => x.IP == device.Ip && x.Type == device.ProtocolType).FirstOrDefault(); 
 
                     if (alarmEquipment.Type == "HEIDEHAIN")
-                    {
-
+                    { 
                         RequestHeidhData hdhReq = new RequestHeidhData();
                         hdhReq.ServerUrl = device.ServerUrl;
                         hdhReq.MachineName = device.UserName;
                         hdhReq.Type = OpcUaActionTypeEnum.Connect.ToString();
                         var Result = await _apiRequestService.RequestAsync(RequsetModeEnum.Post, hdhUrlContext, hdhReq, null);
                         if (Result.IsSuccess)
-                        {
+                        { 
                             ResponseHeidhData responseData = JsonConvert.DeserializeObject<ResponseHeidhData>(Result.Message);
                             if (!string.IsNullOrEmpty(responseData.errorsInfo))
                             {
+                                ProductionStatus productionStatus = new ProductionStatus();
+                                productionStatus.id = alarmEquipment.Id.ToString();
                                 productionStatus.alertMsg = responseData.errorsInfo;
+                                productionStatus.alarmState = true;//报警
                                 productionStatusList.Add(productionStatus);
                             }
                         }
@@ -1413,13 +1419,17 @@ namespace IMCS.CCS.Service.Impl
                         var Result = await _apiRequestService.RequestAsync(RequsetModeEnum.Get, Url + "&fun=AlmInfo", null, null);
                         if (Result.IsSuccess)
                         {
+                            
                             //ResponseFanucData fncReq = new ResponseFanucData();
                             List<AlmInfo> AlmMsg = JsonConvert.DeserializeObject<List<AlmInfo>>(Result.Message);
                             //List<AlmInfo> AlmMsg = fncReq.AlmMsg;                                   
                             if (AlmMsg != null && AlmMsg.Count > 0)
                             {
+                                ProductionStatus productionStatus = new ProductionStatus();
+                                productionStatus.id = alarmEquipment.Id.ToString();
                                 var msgs = from o in AlmMsg select o.msg;
                                 productionStatus.alertMsg = string.Join(",", msgs.ToArray());
+                                productionStatus.alarmState = true;//报警
                                 productionStatusList.Add(productionStatus);
                             }
                         }
@@ -1429,9 +1439,9 @@ namespace IMCS.CCS.Service.Impl
                     {
                         //缓存取PLCagvalue 值
                         List<CcsTagValue> tagValues = new List<CcsTagValue>();
-                        string tag_value_redis_key = "IMCS_CCS:" + device.Ip + ":" + device.ProtocolType;
+                        string tag_value_redis_key = imcs_redis_key + device.Ip + ":" + device.ProtocolType;
                         var tagData = await _redisService.Database.StringGetAsync(tag_value_redis_key);
-                        if (data.IsNullOrEmpty)
+                        if (tagData.IsNullOrEmpty)
                         { 
                             CcsTagValue tagQuery = new CcsTagValue();
                             tagQuery.ProtocolType = device.ProtocolType;
@@ -1440,7 +1450,7 @@ namespace IMCS.CCS.Service.Impl
                             //首次设置redis
                             if (tagValues != null && tagValues.Count > 0)
                             {
-                                await _redisService.Database.StringSetAsync(redis_key, JsonConvert.SerializeObject(tagValues));
+                                await _redisService.Database.StringSetAsync(tag_value_redis_key, JsonConvert.SerializeObject(tagValues));
                             }
                         }
                         else
@@ -1457,12 +1467,17 @@ namespace IMCS.CCS.Service.Impl
                             List<ToolDto> dictList = await _dictionaryService.GetList(ccsDictionary);
                             if (dictList.Count > 0 && dictList[0] != null)
                             {
+
                                 ToolDto dictObj = dictList.Where(x => x.Code.Equals(plcWarnValue.TagValue) && x.state).FirstOrDefault();
                                 if (null != dictObj && !string.IsNullOrEmpty(dictObj.DictValue.Trim()))
                                 {
+                                    ProductionStatus productionStatus = new ProductionStatus();
+                                    productionStatus.id = alarmEquipment.Id.ToString();
                                     productionStatus.alertMsg = dictObj.DictValue.Trim();
+                                    productionStatus.alarmState = true; //报警 
+                                    productionStatusList.Add(productionStatus);
                                 }
-                                productionStatusList.Add(productionStatus);
+                                
                             }
                         }
                     }
@@ -1507,14 +1522,14 @@ namespace IMCS.CCS.Service.Impl
             try
             {
                 string message = "";
-                string all_device_redis_key = "IMCS_CCS:ALLDeviceList";
+                string all_use_device_redis_key = imcs_redis_key + RedisKeyEnum.ALLUseDeviceList ;
                 List<Device> devices = new List<Device>();
-                //查询全部设备包括在线和不在线
-                var deviceListData = await _redisService.Database.StringGetAsync(all_device_redis_key);
+                //查询全部使用设备包括在线和不在线
+                var deviceListData = await _redisService.Database.StringGetAsync(all_use_device_redis_key);
                 if (deviceListData.IsNullOrEmpty)
                 {
-                    devices = _deviceService.GetDevices();
-                    await _redisService.Database.StringSetAsync(device_redis_key, JsonConvert.SerializeObject(devices));
+                    devices = _deviceService.GetDeviceAllList();
+                    await _redisService.Database.StringSetAsync(all_use_device_redis_key, JsonConvert.SerializeObject(devices));
                 }
                 else
                 {
@@ -1571,13 +1586,12 @@ namespace IMCS.CCS.Service.Impl
         /// <returns></returns>
         public void removeRedis()
         {
-            List<Device> devices = _deviceService.GetDeviceAllList();
-            string redis_key = "IMCS_CCS:";
-            _redisService.Database.KeyDelete(device_redis_key);
-            _redisService.Database.KeyDelete(callback_redis_key);
+            List<Device> devices = _deviceService.GetDeviceAllList(); 
+            _redisService.Database.KeyDelete(imcs_redis_key + RedisKeyEnum.ALLUseDeviceList);
+            _redisService.Database.KeyDelete(imcs_redis_key + RedisKeyEnum.CallbackTaskList);
             foreach (Device device in devices)
             {
-                _redisService.Database.KeyDelete(redis_key + device.Ip + ":" + device.ProtocolType);
+                _redisService.Database.KeyDelete(imcs_redis_key + device.Ip + ":" + device.ProtocolType);
             }
         }
 
@@ -1627,18 +1641,18 @@ namespace IMCS.CCS.Service.Impl
             return dData;
         }
         /// <summary>
-        /// 获取redis 设备列表
+        /// 获取redis 设备在线列表
         /// </summary>
         /// <param name="strData"></param>
         /// <returns></returns>
         private async Task<List<Device>> getDeviceList()
         {
             List<Device> deviceList = new List<Device>();
-            var deviceListData = await _redisService.Database.StringGetAsync(device_redis_key);
+            var deviceListData = await _redisService.Database.StringGetAsync(imcs_redis_key + RedisKeyEnum.DeviceList);
             if (deviceListData.IsNullOrEmpty)
             {
-                deviceList = _deviceService.GetDeviceAllList();
-                await _redisService.Database.StringSetAsync(device_redis_key, JsonConvert.SerializeObject(deviceList));
+                deviceList = _deviceService.GetDeviceList();
+                await _redisService.Database.StringSetAsync(imcs_redis_key + RedisKeyEnum.DeviceList, JsonConvert.SerializeObject(deviceList));
             }
             else
             {