| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Reflection; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using System.Reflection; |
| | | using WIDESEAWCS_Core; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_QuartzJob.DTO; |
| | | using Microsoft.Extensions.Logging; |
| | | using WIDESEAWCS_QuartzJob.QuartzExtensions; |
| | | using WIDESEAWCS_QuartzJob.Service; |
| | | |
| | | namespace WIDESEAWCS_QuartzJob.QuartzNet |
| | |
| | | { |
| | | try |
| | | { |
| | | #region 连接PLC |
| | | // 加载程序集 |
| | | Assembly assembly = Assembly.Load($"WIDESEAWCS_Communicator"); |
| | | // 获取类型 |
| | | Type? type = assembly.GetType($"WIDESEAWCS_Communicator.{x.DevicePlcType}"); |
| | | // 创建实例 |
| | | object? obj = Activator.CreateInstance(type, new object[] { x.DeviceIp, x.DevicePort, x.DeviceName }); |
| | | // 调用连接方法 |
| | | bool? connectResult = (bool)type.InvokeMember("Connect", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { }); |
| | | // 判断连接结果 |
| | | if (connectResult ?? false) ConsoleHelper.WriteSuccessLine(type.Name + x.DeviceCode + "连接成功"); else ConsoleHelper.WriteErrorLine(type.Name + x.DeviceCode + "连接失败"); |
| | | #endregion |
| | | |
| | | |
| | | |
| | | #region 实例化设备对象 |
| | | List<DeviceProDTO> devicePros = x.ProtocolList.Select(d => new DeviceProDTO |
| | | if (!x.DeviceName.Contains("机械手")) |
| | | { |
| | | // 设备子编码 |
| | | DeviceChildCode = d.DeviceChildCode, |
| | | // 设备数据类型 |
| | | DeviceDataType = d.DeviceProDataType, |
| | | // 设备ID |
| | | DeviceId = d.DeviceId, |
| | | // 设备协议ID |
| | | DeviceProId = d.Id, |
| | | // 设备协议数据块 |
| | | DeviceProDataBlock = d.DeviceProDataBlock, |
| | | // 设备协议数据长度 |
| | | DeviceProDataLength = d.DeviceProDataLength, |
| | | // 设备协议偏移量 |
| | | DeviceProOffset = d.DeviceProOffset, |
| | | // 设备协议参数描述 |
| | | DeviceProParamDes = d.DeviceProParamDes, |
| | | // 设备协议参数名称 |
| | | DeviceProParamName = d.DeviceProParamName, |
| | | // 设备协议参数类型 |
| | | DeviceProParamType = d.DeviceProParamType, |
| | | // 设备PLC类型 |
| | | DevicePlcType = x.DevicePlcType |
| | | }).ToList(); |
| | | #region 连接PLC |
| | | |
| | | // 根据设备类型获取设备协议详情 |
| | | List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x.DeviceType); |
| | | // 加载程序集 |
| | | Assembly assembly = Assembly.Load($"WIDESEAWCS_Communicator"); |
| | | // 获取类型 |
| | | Type? type = assembly.GetType($"WIDESEAWCS_Communicator.{x.DevicePlcType}"); |
| | | // 创建实例 |
| | | object? obj = Activator.CreateInstance(type, new object[] { x.DeviceIp, x.DevicePort, x.DeviceName }); |
| | | // 调用连接方法 |
| | | bool? connectResult = (bool)type.InvokeMember("Connect", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { }); |
| | | // 判断连接结果 |
| | | if (connectResult ?? false) ConsoleHelper.WriteSuccessLine(type.Name + x.DeviceCode + "连接成功"); else ConsoleHelper.WriteErrorLine(type.Name + x.DeviceCode + "连接失败"); |
| | | |
| | | // 加载设备程序集 |
| | | Assembly assemblyDevice = Assembly.Load($"WIDESEAWCS_QuartzJob"); |
| | | // 获取设备类型对应的类型 |
| | | Type typeDevice = assemblyDevice.GetType($"WIDESEAWCS_QuartzJob.{x.DeviceType}"); |
| | | // 创建设备实例 |
| | | object deviceInstance = Activator.CreateInstance(typeDevice, new object[] { obj, devicePros, deviceProtocolDetails, x.DeviceCode, x.DeviceName }); |
| | | #endregion |
| | | #endregion 连接PLC |
| | | |
| | | x.Device = (IDevice)deviceInstance; |
| | | #region 实例化设备对象 |
| | | |
| | | Storage.Devices.Add((IDevice)deviceInstance); |
| | | List<DeviceProDTO> devicePros = x.ProtocolList.Select(d => new DeviceProDTO |
| | | { |
| | | // 设备子编码 |
| | | DeviceChildCode = d.DeviceChildCode, |
| | | // 设备数据类型 |
| | | DeviceDataType = d.DeviceProDataType, |
| | | // 设备ID |
| | | DeviceId = d.DeviceId, |
| | | // 设备协议ID |
| | | DeviceProId = d.Id, |
| | | // 设备协议数据块 |
| | | DeviceProDataBlock = d.DeviceProDataBlock, |
| | | // 设备协议数据长度 |
| | | DeviceProDataLength = d.DeviceProDataLength, |
| | | // 设备协议偏移量 |
| | | DeviceProOffset = d.DeviceProOffset, |
| | | // 设备协议参数描述 |
| | | DeviceProParamDes = d.DeviceProParamDes, |
| | | // 设备协议参数名称 |
| | | DeviceProParamName = d.DeviceProParamName, |
| | | // 设备协议参数类型 |
| | | DeviceProParamType = d.DeviceProParamType, |
| | | // 设备PLC类型 |
| | | DevicePlcType = x.DevicePlcType |
| | | }).ToList(); |
| | | |
| | | // 根据设备类型获取设备协议详情 |
| | | List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x.DeviceType); |
| | | |
| | | // 加载设备程序集 |
| | | Assembly assemblyDevice = Assembly.Load($"WIDESEAWCS_QuartzJob"); |
| | | // 获取设备类型对应的类型 |
| | | Type typeDevice = assemblyDevice.GetType($"WIDESEAWCS_QuartzJob.{x.DeviceType}"); |
| | | // 创建设备实例 |
| | | object deviceInstance = Activator.CreateInstance(typeDevice, new object[] { obj, devicePros, deviceProtocolDetails, x.DeviceCode, x.DeviceName }); |
| | | |
| | | #endregion 实例化设备对象 |
| | | |
| | | x.Device = (IDevice)deviceInstance; |
| | | |
| | | Storage.Devices.Add((IDevice)deviceInstance); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | }); |
| | | for (int i = 0; i < dispatches.Count; i++) |
| | | { |
| | | DeviceInfoDTO? deviceProInfo = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id); |
| | | dispatches[i].JobParams = deviceProInfo?.Device; |
| | | var targetDevice = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id); |
| | | |
| | | if (targetDevice is null) continue; |
| | | |
| | | // 使用模式匹配 |
| | | dispatches[i].JobParams = targetDevice switch |
| | | { |
| | | { DeviceName: var name } when name.Contains("机械手") |
| | | => new RobotCraneDevice { DeviceCode = targetDevice.DeviceCode, DeviceName = targetDevice.DeviceName, IPAddress = targetDevice.DeviceIp + ":" + targetDevice.DevicePort }, |
| | | _ => targetDevice.Device |
| | | }; |
| | | WebResponseContent responseContent = await _schedulerCenter.AddScheduleJobAsync(dispatches[i]); |
| | | if (responseContent.Status) ConsoleHelper.WriteSuccessLine(dispatches[i].Name + "调度服务添加成功"); else ConsoleHelper.WriteErrorLine(dispatches[i].Name + "调度服务添加失败"); |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |