浏览代码

优化修改

zhuhao 3 年之前
父节点
当前提交
841bf9852b
共有 2 个文件被更改,包括 51 次插入98 次删除
  1. 2 17
      IMCS_CCS/Service/Impl/HttpRequestService.cs
  2. 49 81
      IMCS_CCS/Service/Impl/TaskJobService.cs

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

@@ -1444,27 +1444,12 @@ namespace IMCS.CCS.Services
                 {
                     if (!string.IsNullOrEmpty(actionAddress.Address))
                     {
-                        s7.Write(actionAddress.Address, bool.Parse(actionAddress.Value));
-                        Thread.Sleep(1500);
+                        s7.Write(actionAddress.Address, Convert.ToInt16(actionAddress.Value));
+                        Thread.Sleep(200);
                     }
                 }
                 s7.ConnectClose();
 
-                /*
-                                //string Address1 = ToolUtils.AddressConvertDBW(actionInfo.Address);
-                                s7.Write(actionInfo.Address, true);
-                                //s7.Write(Address1, true);
-                                Thread.Sleep(1500);
-                                s7.Write(actionInfo.Address, false);
-                                Thread.Sleep(1500);
-                                //string Address2 = ToolUtils.AddressConvertDBW(actionInfo.Address2);
-                                //s7.Write(Address2, true);
-                                s7.Write(actionInfo.Address2, true);
-                                Thread.Sleep(1500);
-                                s7.Write(actionInfo.Address2, false);
-                                //s7.Write(Address2, false); */
-
-
                 //插入回调
                 TaskCallback taskCallbackData = new TaskCallback(req.url, ActionTypeEnum.StartCleanMachine.ToString(),
                     EnumHelper.GetDescription(ActionTypeEnum.StartCleanMachine), req.taskId, req.taskNodeId);

+ 49 - 81
IMCS_CCS/Service/Impl/TaskJobService.cs

@@ -7,7 +7,6 @@ using IMCS.CCS.Models.vo;
 using IMCS.CCS.Services;
 using IMCS_CCS.Model.vo;
 using IMCS_CCS.Utils;
-using IMCS_CCS.Utils.DeviceProtocol;
 using Microsoft.Extensions.Configuration;
 using Newtonsoft.Json;
 using S7.Net;
@@ -18,7 +17,6 @@ using System.Threading;
 using System.Threading.Tasks;
 using IMCS.CCS.Models;
 
-
 namespace IMCS.CCS.Service.Impl
 {
     /// <summary>
@@ -109,19 +107,9 @@ namespace IMCS.CCS.Service.Impl
         {
             try
             {
-                List<Device> deviceList = new List<Device>();
-                var deviceListData = await _redisService.Database.StringGetAsync(device_redis_key);
-                if (deviceListData.IsNullOrEmpty)
-                {
-                    deviceList = _deviceService.GetDeviceList();
-                    await _redisService.Database.StringSetAsync(device_redis_key, JsonConvert.SerializeObject(deviceList));
-                }
-                else
-                {
-                    deviceList = JsonConvert.DeserializeObject<List<Device>>(deviceListData);
-                }
+                List<Device> deviceList = await getDeviceList();
 
-                deviceList = deviceList.Where(x => x.ProtocolType.Equals(ProtocalTypeEnum.OPCUA.ToString())).ToList();
+                deviceList = deviceList.Where(x => x.ProtocolType.Equals(ProtocalTypeEnum.OPCUA.ToString()) && x.State).ToList();
 
                 foreach (Device device in deviceList)
                 {
@@ -239,19 +227,9 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> DataCollectS7Job()
         {
             try
-            {
-                List<Device> deviceList = new List<Device>();
-                var deviceListData = await _redisService.Database.StringGetAsync(device_redis_key);
-                if (deviceListData.IsNullOrEmpty)
-                {
-                    deviceList = _deviceService.GetDeviceList();
-                    await _redisService.Database.StringSetAsync(device_redis_key, JsonConvert.SerializeObject(deviceList));
-                }
-                else
-                {
-                    deviceList = JsonConvert.DeserializeObject<List<Device>>(deviceListData);
-                }
-                deviceList = deviceList.Where(x => x.ProtocolType.Equals(ProtocalTypeEnum.S7_1500.ToString())).ToList();
+            { 
+                List<Device> deviceList = await getDeviceList();
+                deviceList = deviceList.Where(x => x.ProtocolType.Equals(ProtocalTypeEnum.S7_1500.ToString()) && x.State).ToList();
                 foreach (Device device in deviceList)
                 {
                     List<CcsTagValue> tagValues = new List<CcsTagValue>();
@@ -350,19 +328,9 @@ namespace IMCS.CCS.Service.Impl
         public async Task<string> DataCollectFanucJob()
         {
             try
-            {
-                List<Device> deviceList = new List<Device>();
-                var deviceListData = await _redisService.Database.StringGetAsync(device_redis_key);
-                if (deviceListData.IsNullOrEmpty)
-                {
-                    deviceList = _deviceService.GetDeviceList();
-                    await _redisService.Database.StringSetAsync(device_redis_key, JsonConvert.SerializeObject(deviceList));
-                }
-                else
-                {
-                    deviceList = JsonConvert.DeserializeObject<List<Device>>(deviceListData);
-                }
-                deviceList = deviceList.Where(x => x.ProtocolType.Equals(ProtocalTypeEnum.FANUC.ToString())).ToList();
+            { 
+                List<Device> deviceList = await getDeviceList();
+                deviceList = deviceList.Where(x => x.ProtocolType.Equals(ProtocalTypeEnum.FANUC.ToString()) && x.State).ToList();
                 foreach (Device device in deviceList)
                 {
                     List<CcsTagValue> tagValues = new List<CcsTagValue>();
@@ -513,8 +481,8 @@ namespace IMCS.CCS.Service.Impl
             {
                 taskList = JsonConvert.DeserializeObject<List<TaskCallback>>(taskCallbackListData);
             }
-            //在线设备
-            List<Device> deviceList = _deviceService.GetDeviceList();
+            //在线设备 
+            List<Device> deviceList = await getDeviceList(); 
 
             taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.GetFlexibleWire.ToString())
                                  || x.OperateType.Equals(ActionTypeEnum.SendFlexibleWire.ToString())
@@ -645,10 +613,10 @@ namespace IMCS.CCS.Service.Impl
             {
                 taskList = JsonConvert.DeserializeObject<List<TaskCallback>>(taskCallbackListData);
             }
+            
+            List<Device> deviceList = await getDeviceList();
             //在线设备
-            List<Device> deviceList = _deviceService.GetDeviceList();
-
-            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.StartLabelMachine.ToString())).ToList();
+            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.StartLabelMachine.ToString()) && x.State).ToList();
             TaskCallback task = taskList.Where(key => deviceList.Any(device => device.Ip == key.IP)).FirstOrDefault();
             if (task == null)
             {
@@ -781,10 +749,9 @@ namespace IMCS.CCS.Service.Impl
             {
                 taskList = JsonConvert.DeserializeObject<List<TaskCallback>>(taskCallbackListData);
             }
-            //在线设备
-            List<Device> deviceList = _deviceService.GetDeviceList();
-
-            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.StartNCProgram.ToString())).ToList();
+            List<Device> deviceList = await getDeviceList();
+            //在线设备 
+            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.StartNCProgram.ToString()) && x.State).ToList();
             TaskCallback task = taskList.Where(key => deviceList.Any(device => device.Ip == key.IP)).FirstOrDefault();
             if (task == null)
             {
@@ -903,10 +870,9 @@ namespace IMCS.CCS.Service.Impl
             {
                 taskList = JsonConvert.DeserializeObject<List<TaskCallback>>(taskCallbackListData);
             }
+            List<Device> deviceList = await getDeviceList();
             //在线设备
-            List<Device> deviceList = _deviceService.GetDeviceList();
-
-            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.StartCleanMachine.ToString())).ToList();
+            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.StartCleanMachine.ToString()) && x.State).ToList();
             TaskCallback task = taskList.Where(key => deviceList.Any(device => device.Ip == key.IP)).FirstOrDefault();
             if (task == null)
             {
@@ -1023,10 +989,9 @@ namespace IMCS.CCS.Service.Impl
             {
                 taskList = JsonConvert.DeserializeObject<List<TaskCallback>>(taskCallbackListData);
             }
+            List<Device> deviceList = await getDeviceList();
             //在线设备
-            List<Device> deviceList = _deviceService.GetDeviceList();
-
-            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.SendThreeCoordinates.ToString())).ToList();
+            taskList = taskList.Where(x => x.OperateType.Equals(ActionTypeEnum.SendThreeCoordinates.ToString()) && x.State).ToList();
             TaskCallback task = taskList.Where(key => deviceList.Any(device => device.Ip == key.IP)).FirstOrDefault();
             if (task == null)
             {
@@ -1247,16 +1212,8 @@ namespace IMCS.CCS.Service.Impl
             try
             {
                 string message = "MonitorEquipmentStatusJob调度成功";
-                List<Device> devices = new List<Device>(); 
-                var deviceData = await _redisService.Database.StringGetAsync(device_redis_key);
-                if (deviceData.IsNullOrEmpty)
-                {
-                    devices = _deviceService.GetDeviceList();
-                }
-                else
-                {
-                    devices = JsonConvert.DeserializeObject<List<Device>>(deviceData);
-                }
+                List<Device> devices = await getDeviceList();
+                //在线设备
                 //ftp不监控
                 devices = devices.Where(x => !x.ProtocolType.Equals(ProtocalTypeEnum.FTP.ToString()) && x.State).ToList();
                 CcsTagValue tagQuery = new CcsTagValue();
@@ -1457,18 +1414,9 @@ namespace IMCS.CCS.Service.Impl
             try
             {
                 string message = "";
-                List<Device> devices = new List<Device>();
-                List<Device> changeDevices = new List<Device>(); 
-                var data = await _redisService.Database.StringGetAsync(device_redis_key);
-                if (data.IsNullOrEmpty)
-                {
-                    devices = _deviceService.GetDeviceAllList();
-                    await _redisService.Database.StringSetAsync(device_redis_key, JsonConvert.SerializeObject(devices));
-                }
-                else
-                {
-                    devices = JsonConvert.DeserializeObject<List<Device>>(data);
-                }
+                List<Device> devices = await getDeviceList();
+                //在线设备
+                List<Device> changeDevices = new List<Device>();  
 
                 //ftp不监控
                 devices = devices.Where(x => !x.ProtocolType.Equals(ProtocalTypeEnum.FTP.ToString())).ToList();
@@ -1500,8 +1448,8 @@ namespace IMCS.CCS.Service.Impl
                                 Device oldDevice = await _deviceService.GetDeviceById(device.Id);
                                 if (oldDevice != null && !oldDevice.State)
                                 {
-                                    device.State = true;
-                                    await _deviceService.UpdateDevice(device);
+                                    oldDevice.State = true;
+                                    await _deviceService.UpdateDevice(oldDevice);
                                     changeDevices.Add(device);
                                 }
                                 s7.Close();
@@ -1514,7 +1462,7 @@ namespace IMCS.CCS.Service.Impl
                         {
                             Device oldDevice = await _deviceService.GetDeviceById(device.Id);
                             if (oldDevice != null && oldDevice.State)
-                            {
+                            { 
                                 device.State = false;
                                 await _deviceService.UpdateDevice(device);
                                 changeDevices.Add(device);
@@ -1741,6 +1689,26 @@ namespace IMCS.CCS.Service.Impl
                 dData = Convert.ToDecimal(strData);
             }
             return dData;
-        } 
+        }
+        /// <summary>
+        /// 获取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);
+            if (deviceListData.IsNullOrEmpty)
+            {
+                deviceList = _deviceService.GetDeviceList();
+                await _redisService.Database.StringSetAsync(device_redis_key, JsonConvert.SerializeObject(deviceList));
+            }
+            else
+            {
+                deviceList = JsonConvert.DeserializeObject<List<Device>>(deviceListData);
+            }
+            return deviceList;
+        }
     }
 }