| #region << 版 本 注 释 >> | 
| /*---------------------------------------------------------------- | 
|  * 命名空间:WIDESEAWCS_QuartzJob | 
|  * 创建者:胡童庆 | 
|  * 创建时间:2024/8/2 16:13:36 | 
|  * 版本:V1.0.0 | 
|  * 描述:调度服务自动开启 | 
|  * | 
|  * ---------------------------------------------------------------- | 
|  * 修改人: | 
|  * 修改时间: | 
|  * 版本:V1.0.1 | 
|  * 修改说明: | 
|  *  | 
|  *----------------------------------------------------------------*/ | 
| #endregion << 版 本 注 释 >> | 
|   | 
| using Microsoft.AspNetCore.Mvc; | 
| using Microsoft.Extensions.Hosting; | 
| using Microsoft.Extensions.Logging; | 
| using System; | 
| using System.Collections.Generic; | 
| using System.Linq; | 
| using System.Reflection; | 
| using System.Text; | 
| using System.Threading.Tasks; | 
| using WIDESEAWCS_Core; | 
| using WIDESEAWCS_Core.Helper; | 
| using WIDESEAWCS_QuartzJob.DeviceBase; | 
| using WIDESEAWCS_QuartzJob.DTO; | 
| using WIDESEAWCS_QuartzJob.Models; | 
| using WIDESEAWCS_QuartzJob.Service; | 
|   | 
| namespace WIDESEAWCS_QuartzJob.QuartzExtensions | 
| { | 
|     public class QuartzJobHostedService : IHostedService | 
|     { | 
|         private readonly ISchedulerCenter _schedulerCenter; | 
|         private readonly ILogger<QuartzJobHostedService> _logger; | 
|         private readonly IDeviceInfoService _deviceInfoService; | 
|         private readonly IDispatchInfoService _dispatchInfoService; | 
|         private readonly IDeviceProtocolDetailService _deviceProtocolDetailService; | 
|   | 
|         public QuartzJobHostedService(ILogger<QuartzJobHostedService> logger, IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService) | 
|         { | 
|             _logger = logger; | 
|             _deviceInfoService = deviceInfoService; | 
|             _dispatchInfoService = dispatchInfoService; | 
|             _schedulerCenter = schedulerCenter; | 
|             _deviceProtocolDetailService = deviceProtocolDetailService; | 
|         } | 
|   | 
|         /// <summary> | 
|         /// 启动程序自动开启调度服务 | 
|         /// </summary> | 
|         /// <param name="cancellationToken"></param> | 
|         /// <returns></returns> | 
|         public async Task StartAsync(CancellationToken cancellationToken) | 
|         { | 
|             try | 
|             { | 
|                 List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos(); | 
|                 List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos(); | 
|   | 
|                 deviceInfos.ForEach(x => | 
|                 { | 
|                     if (dispatches.Exists(d => d.JobGroup == x.DeviceType)) | 
|                     { | 
|                         #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(x.DeviceCode + "连接成功"); else ConsoleHelper.WriteErrorLine(x.DeviceCode + "连接失败"); | 
|   | 
|                         #endregion | 
|   | 
|                         #region 实例化设备对象 | 
|   | 
|                         List<DeviceProDTO> devicePros = x.ProtocolList.Select(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, | 
|                         }).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); | 
|                     } | 
|                 }); | 
|                 for (int i = 0; i < dispatches.Count; i++) | 
|                 { | 
|                     DeviceInfoDTO? deviceProInfo = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id); | 
|                     dispatches[i].JobParams = deviceProInfo?.Device; | 
|                     WebResponseContent responseContent = await _schedulerCenter.AddScheduleJobAsync(dispatches[i]); | 
|                     if (responseContent.Status) ConsoleHelper.WriteSuccessLine(dispatches[i].JobGroup + "调度服务添加成功"); else ConsoleHelper.WriteErrorLine(dispatches[i].JobGroup + "调度服务添加失败"); | 
|                 } | 
|                 await _schedulerCenter.StartScheduleAsync(); | 
|             } | 
|             catch (Exception ex) | 
|             { | 
|                 _logger.LogError(ex, "调度服务开启异常"); | 
|                 Console.WriteLine("调度服务开启异常" + ex.ToString()); | 
|                 throw; | 
|             } | 
|         } | 
|   | 
|         public Task StopAsync(CancellationToken cancellationToken) | 
|         { | 
|             _logger.LogInformation("Stop QuartzJob Service!"); | 
|             return Task.CompletedTask; | 
|         } | 
|     } | 
| } |