|
@@ -16,6 +16,7 @@ using System.Linq;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using IMCS.CCS.Models;
|
|
|
+using System.Net.NetworkInformation;
|
|
|
|
|
|
namespace IMCS.CCS.Service.Impl
|
|
|
{
|
|
@@ -1450,199 +1451,37 @@ namespace IMCS.CCS.Service.Impl
|
|
|
devices = devices.Where(x => !x.ProtocolType.Equals(ProtocalTypeEnum.FTP.ToString())).ToList();
|
|
|
foreach (Device device in devices)
|
|
|
{
|
|
|
- if (device.ProtocolType.Equals(ProtocalTypeEnum.S7_1500.ToString()))
|
|
|
+ Ping pingSender = new Ping();
|
|
|
+ PingReply reply = pingSender.Send(device.Ip);
|
|
|
+ if (reply.Status != IPStatus.Success)
|
|
|
{
|
|
|
- try
|
|
|
+ Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
+ if (oldDevice != null && oldDevice.State)
|
|
|
{
|
|
|
-
|
|
|
- Plc s7 = DevicePlcS7.S7(device.ServerUrl, ProtocalTypeEnum.S7_1500.ToString());
|
|
|
-
|
|
|
- if (s7.IsConnected != true)
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && oldDevice.State)
|
|
|
- {
|
|
|
- device.State = false;
|
|
|
- await _deviceService.UpdateDevice(device);
|
|
|
- changeDevices.Add(device);
|
|
|
- }
|
|
|
- s7.Close();
|
|
|
- Log.Instance.WriteLogAdd("plc连接失败,或已离线", LOG_TITLE_DEVICE);
|
|
|
- message = message + ",plc连接失败,或已离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType;
|
|
|
- continue;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && !oldDevice.State)
|
|
|
- {
|
|
|
- oldDevice.State = true;
|
|
|
- await _deviceService.UpdateDevice(oldDevice);
|
|
|
- changeDevices.Add(device);
|
|
|
- }
|
|
|
- s7.Close();
|
|
|
- Log.Instance.WriteLogAdd("plc连接失败,或已离线", LOG_TITLE_DEVICE);
|
|
|
- message = message + ",plc连接成功,ip:" + device.Ip + ",协议类型:" + device.ProtocolType;
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception s7ex)
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && oldDevice.State)
|
|
|
- {
|
|
|
- device.State = false;
|
|
|
- await _deviceService.UpdateDevice(device);
|
|
|
- changeDevices.Add(device);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("plc连接失败,或已离线", LOG_TITLE_DEVICE);
|
|
|
- message = message + ",plc连接失败,plc连不上" + s7ex.Message + ",ip:" + device.Ip + ",协议类型:" + device.ProtocolType;
|
|
|
- }
|
|
|
- }
|
|
|
- else if (device.ProtocolType.Equals(ProtocalTypeEnum.OPCUA.ToString()))
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- //调用opcua是否连接接口
|
|
|
- RequestOpcUaData opcUaReq = new RequestOpcUaData();
|
|
|
- opcUaReq.ServerUrl = device.ServerUrl;
|
|
|
- opcUaReq.UserName = device.UserName;
|
|
|
- opcUaReq.Password = device.Password;
|
|
|
- opcUaReq.Type = OpcUaActionTypeEnum.Connect.ToString();
|
|
|
- var Result = await _apiRequestService.RequestAsync(RequsetModeEnum.Post, opcuacUrlContext, opcUaReq, null);
|
|
|
- ResponseOpcUaData responseOpcUaData = JsonConvert.DeserializeObject<ResponseOpcUaData>(Result.Message);
|
|
|
- if (!Result.IsSuccess || !responseOpcUaData.result)
|
|
|
- {
|
|
|
- device.State = false;
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice!= null && oldDevice.State)
|
|
|
- {
|
|
|
- await _deviceService.UpdateDevice(device);
|
|
|
- changeDevices.Add(device);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("OPCUA连接异常,或已离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType, LOG_TITLE_OPCUA);
|
|
|
- message = message + ",OPCUA连接异常,或以离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType; ;
|
|
|
- continue;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- device.State = true;
|
|
|
- await _deviceService.UpdateDevice(device);
|
|
|
- changeDevices.Add(device);
|
|
|
- Log.Instance.WriteLogAdd("OPCUA连接成功,ip:" + device.Ip + ",协议类型:" + device.ProtocolType, LOG_TITLE_DEVICE);
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception opcex)
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && oldDevice.State)
|
|
|
- {
|
|
|
- device.State = false;
|
|
|
- await _deviceService.UpdateDevice(device);
|
|
|
- changeDevices.Add(device);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("OPCUA连接异常,或以离线" + opcex.Message + ",ip:" + device.Ip + ", 协议类型: " + device.ProtocolType, LOG_TITLE_DEVICE);
|
|
|
- continue;
|
|
|
+ device.State = false;
|
|
|
+ await _deviceService.UpdateDevice(device);
|
|
|
+ changeDevices.Add(device);
|
|
|
}
|
|
|
- }
|
|
|
- else if (device.ProtocolType.Equals(ProtocalTypeEnum.FANUC.ToString()))
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- string Url = fanucUrlContext + "?ip=" + device.Ip + "&port=" + device.Port;
|
|
|
- //调用发那科是否连接接口
|
|
|
- var Result = await _apiRequestService.RequestAsync(RequsetModeEnum.Get, Url + "&fun=Connect", null, null);
|
|
|
|
|
|
- if (Result.IsSuccess && Result.Message == "0")
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice!= null && !oldDevice.State)
|
|
|
- {
|
|
|
- oldDevice.State = true;
|
|
|
- await _deviceService.UpdateDevice(oldDevice);
|
|
|
- changeDevices.Add(oldDevice);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("Fanuc连接成功,ip:" + device.Ip + ",协议类型:" + device.ProtocolType, LOG_TITLE_DEVICE);
|
|
|
- continue;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && oldDevice.State)
|
|
|
- {
|
|
|
- oldDevice.State = false;
|
|
|
- await _deviceService.UpdateDevice(oldDevice);
|
|
|
- changeDevices.Add(oldDevice);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("Fanuc连接异常,或已离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType, LOG_TITLE_OPCUA);
|
|
|
- message = message + ",Fanuc连接异常,或以离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType; ;
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception opcex)
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && oldDevice.State)
|
|
|
- {
|
|
|
- oldDevice.State = false;
|
|
|
- await _deviceService.UpdateDevice(oldDevice);
|
|
|
- changeDevices.Add(oldDevice);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("Fanuc连接异常,或以离线" + opcex.Message + ",ip:" + device.Ip + ", 协议类型: " + device.ProtocolType, LOG_TITLE_DEVICE);
|
|
|
- continue;
|
|
|
- }
|
|
|
+ Log.Instance.WriteLogAdd(device.Ip + "连接失败,或已离线", LOG_TITLE_DEVICE);
|
|
|
+ message = message + device.Ip + ",连接失败,或已离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType;
|
|
|
+ continue;
|
|
|
}
|
|
|
- else if (device.ProtocolType.Equals(ProtocalTypeEnum.HEIDEHAIN.ToString()))
|
|
|
+ else
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- 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);
|
|
|
- ResponseHeidhData responseData = JsonConvert.DeserializeObject<ResponseHeidhData>(Result.Message);
|
|
|
-
|
|
|
- if (!Result.IsSuccess || !responseData.result)
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && oldDevice.State)
|
|
|
- {
|
|
|
- oldDevice.State = false;
|
|
|
- await _deviceService.UpdateDevice(oldDevice);
|
|
|
- changeDevices.Add(oldDevice);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("海德汉连接异常,或已离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType, LOG_TITLE_OPCUA);
|
|
|
- message = message + ",海德汉连接异常,或以离线,ip:" + device.Ip + ",协议类型:" + device.ProtocolType; ;
|
|
|
- continue;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && !oldDevice.State)
|
|
|
- {
|
|
|
- oldDevice.State = true;
|
|
|
- await _deviceService.UpdateDevice(oldDevice);
|
|
|
- changeDevices.Add(oldDevice);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("海德汉连接成功,ip:" + device.Ip + ",协议类型:" + device.ProtocolType, LOG_TITLE_DEVICE);
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception opcex)
|
|
|
+ Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
+ if (oldDevice != null && !oldDevice.State)
|
|
|
{
|
|
|
- Device oldDevice = await _deviceService.GetDeviceById(device.Id);
|
|
|
- if (oldDevice != null && oldDevice.State)
|
|
|
- {
|
|
|
- oldDevice.State = false;
|
|
|
- await _deviceService.UpdateDevice(oldDevice);
|
|
|
- changeDevices.Add(oldDevice);
|
|
|
- }
|
|
|
- Log.Instance.WriteLogAdd("Fanuc连接异常,或以离线" + opcex.Message + ",ip:" + device.Ip + ", 协议类型: " + device.ProtocolType, LOG_TITLE_DEVICE);
|
|
|
- continue;
|
|
|
+ oldDevice.State = true;
|
|
|
+ await _deviceService.UpdateDevice(oldDevice);
|
|
|
+ changeDevices.Add(device);
|
|
|
}
|
|
|
+ Log.Instance.WriteLogAdd("plc连接失败,或已离线", LOG_TITLE_DEVICE);
|
|
|
+ message = message + ",plc连接成功,ip:" + device.Ip + ",协议类型:" + device.ProtocolType;
|
|
|
+ continue;
|
|
|
}
|
|
|
+ Thread.Sleep(50);
|
|
|
+
|
|
|
}
|
|
|
if (changeDevices != null)
|
|
|
{
|