| | |
| | | using System.Reflection; |
| | | using WIDESEAWCS_Common; |
| | | using WIDESEAWCS_Core; |
| | | using WIDESEAWCS_Core.Caches; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_Core.LogHelper; |
| | | using WIDESEAWCS_QuartzJob.DTO; |
| | | using WIDESEAWCS_QuartzJob.Service; |
| | | |
| | |
| | | private readonly IDeviceInfoService _deviceInfoService; |
| | | private readonly IDispatchInfoService _dispatchInfoService; |
| | | private readonly IDeviceProtocolDetailService _deviceProtocolDetailService; |
| | | private readonly ICacheService _cacheService; |
| | | private readonly Storage _storage; |
| | | |
| | | /// <summary> |
| | | /// 启动程序自动开启调度服务 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | public QuartzNetExtension(IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, Storage storage) |
| | | public QuartzNetExtension(IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, Storage storage, ICacheService cacheService) |
| | | { |
| | | _deviceInfoService = deviceInfoService; |
| | | _dispatchInfoService = dispatchInfoService; |
| | | _schedulerCenter = schedulerCenter; |
| | | _deviceProtocolDetailService = deviceProtocolDetailService; |
| | | _storage = storage; |
| | | _cacheService = cacheService; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos(); |
| | | List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos(); |
| | | _cacheService.RemoveByPrefix($"{RedisPrefix.System}"); |
| | | |
| | | deviceInfos.ForEach(x => |
| | | { |
| | |
| | | { |
| | | try |
| | | { |
| | | if (!x.DeviceName.Contains("机械手")) |
| | | if (!x.DevicePlcType.Contains("Socket")) |
| | | { |
| | | #region 连接PLC |
| | | |
| | |
| | | }).ToList(); |
| | | |
| | | // 根据设备类型获取设备协议详情 |
| | | List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x.DeviceType); |
| | | List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceId(x.Id); |
| | | |
| | | // 加载设备程序集 |
| | | Assembly assemblyDevice = Assembly.Load($"WIDESEAWCS_QuartzJob"); |
| | |
| | | x.Device = (IDevice)deviceInstance; |
| | | |
| | | Storage.Devices.Add((IDevice)deviceInstance); |
| | | |
| | | _cacheService.AddObject($"{RedisPrefix.System}:{RedisName.IDevice}:{x.DeviceName}", (IDevice)deviceInstance); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine("调度服务开启异常" + ex.ToString()); |
| | | QuartzLogger.Error($"调度服务开启异常", "QuartzNetExtension", ex); |
| | | } |
| | | } |
| | | else |
| | |
| | | }); |
| | | for (int i = 0; i < dispatches.Count; i++) |
| | | { |
| | | var targetDevice = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id); |
| | | var targetDevice = deviceInfos.FirstOrDefault(x => x.DispatchId == dispatches[i].Id); |
| | | |
| | | if (targetDevice is null) continue; |
| | | |
| | | |
| | | // 使用模式匹配 |
| | | dispatches[i].JobParams = targetDevice switch |
| | | { |
| | | { DeviceName: var name } when name.Contains("机械手") |
| | | { DevicePlcType: var type } when type.Contains("Socket") |
| | | => 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 + "调度服务添加失败"); |
| | | if (responseContent.Status) |
| | | { |
| | | QuartzLogger.Info($"{dispatches[i].Name}调度服务添加成功", "QuartzNetExtension"); |
| | | ConsoleHelper.WriteSuccessLine(dispatches[i].Name + "调度服务添加成功"); |
| | | } |
| | | else |
| | | { |
| | | QuartzLogger.Error($"{dispatches[i].Name}调度服务添加失败", "QuartzNetExtension"); |
| | | ConsoleHelper.WriteErrorLine(dispatches[i].Name + "调度服务添加失败"); |
| | | } |
| | | } |
| | | //await _schedulerCenter.StartScheduleAsync(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Console.WriteLine("调度服务开启异常" + ex.ToString()); |
| | | QuartzLogger.Error($"调度服务开启异常", "QuartzNetExtension", ex); |
| | | throw; |
| | | } |
| | | } |