| | |
| | | using WIDESEAWCS_Core; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_QuartzJob.DTO; |
| | | using WIDESEAWCS_QuartzJob.Models; |
| | | using WIDESEAWCS_QuartzJob.Service; |
| | | |
| | | namespace WIDESEAWCS_QuartzJob.QuartzNet |
| | |
| | | List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos(); |
| | | List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos(); |
| | | |
| | | deviceInfos.ForEach(x => |
| | | deviceInfos.ForEach(delegate (DeviceInfoDTO x) |
| | | { |
| | | if (!Storage.Devices.Exists(d => d.DeviceCode == x.DeviceCode)) |
| | | DeviceInfoDTO x2 = x; |
| | | if (!Storage.Devices.Exists((IDevice d) => d.DeviceCode == x2.DeviceCode) && !x.DeviceName.Contains("å åæº")) |
| | | { |
| | | try |
| | | { |
| | | if (!x.DeviceName.Contains("å åæº")) |
| | | Type type = Assembly.Load("WIDESEAWCS_Communicator").GetType("WIDESEAWCS_Communicator." + x2.DevicePlcType); |
| | | object obj = Activator.CreateInstance(type, x2.DeviceIp, x2.DevicePort, x2.DeviceName); |
| | | if (new bool?((bool)type.InvokeMember("Connect", BindingFlags.InvokeMethod, null, obj, new object[0])).GetValueOrDefault()) |
| | | { |
| | | #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 è¿æ¥PLC |
| | | |
| | | #region å®ä¾å设å¤å¯¹è±¡ |
| | | |
| | | 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); |
| | | (type.Name + x2.DeviceCode + "è¿æ¥æå").WriteSuccessLine(); |
| | | } |
| | | else |
| | | { |
| | | (type.Name + x2.DeviceCode + "è¿æ¥å¤±è´¥").WriteErrorLine(); |
| | | } |
| | | |
| | | List<DeviceProDTO> list = x2.ProtocolList.Select((Dt_DeviceProtocol d) => new DeviceProDTO |
| | | { |
| | | DeviceChildCode = d.DeviceChildCode, |
| | | DeviceDataType = d.DeviceProDataType, |
| | | DeviceId = d.DeviceId, |
| | | DeviceProId = d.Id, |
| | | DeviceProDataBlock = d.DeviceProDataBlock, |
| | | DeviceProDataLength = d.DeviceProDataLength, |
| | | DeviceProOffset = d.DeviceProOffset, |
| | | DeviceProParamDes = d.DeviceProParamDes, |
| | | DeviceProParamName = d.DeviceProParamName, |
| | | DeviceProParamType = d.DeviceProParamType, |
| | | DevicePlcType = x2.DevicePlcType |
| | | }).ToList(); |
| | | List<DeviceProtocolDetailDTO> deviceProtocolDetailsByDeviceType = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x2.DeviceType); |
| | | object obj2 = Activator.CreateInstance(Assembly.Load("WIDESEAWCS_QuartzJob").GetType("WIDESEAWCS_QuartzJob." + x2.DeviceType), obj, list, deviceProtocolDetailsByDeviceType, x2.DeviceCode, x2.DeviceName); |
| | | x2.Device = (IDevice)obj2; |
| | | Storage.Devices.Add((IDevice)obj2); |
| | | } |
| | | catch (Exception ex) |
| | | catch (Exception ex2) |
| | | { |
| | | Console.WriteLine("è°åº¦æå¡å¼å¯å¼å¸¸" + ex.ToString()); |
| | | Console.WriteLine("è°åº¦æå¡å¼å¯å¼å¸¸" + ex2.ToString()); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | x.Device = Storage.Devices.FirstOrDefault(d => d.DeviceCode == x.DeviceCode); |
| | | x2.Device = Storage.Devices.FirstOrDefault((IDevice d) => d.DeviceCode == x2.DeviceCode); |
| | | } |
| | | }); |
| | | for (int i = 0; i < dispatches.Count; i++) |
| | | { |
| | | 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 }, |
| | | => new StackerCraneDevice { DeviceCode = targetDevice.DeviceCode, DeviceName = targetDevice.DeviceName, IPAddress = targetDevice.DeviceIp + ":" + targetDevice.DevicePort }, |
| | | _ => targetDevice.Device |
| | | }; |
| | | WebResponseContent responseContent = await _schedulerCenter.AddScheduleJobAsync(dispatches[i]); |