From 8c6fd742db249ad4cc819cf041eb98d880a3ef73 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期四, 02 一月 2025 15:09:07 +0800
Subject: [PATCH] 1

---
 WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs |   76 ++++++++------------------------------
 1 files changed, 16 insertions(+), 60 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs
index 887a448..90cf585 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs
@@ -15,6 +15,7 @@
  *----------------------------------------------------------------*/
 #endregion << 鐗� 鏈� 娉� 閲� >>
 
+using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Hosting;
 using Microsoft.Extensions.Logging;
 using System;
@@ -24,13 +25,18 @@
 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.QuartzNet;
 using WIDESEAWCS_QuartzJob.Service;
 
 namespace WIDESEAWCS_QuartzJob.QuartzExtensions
 {
+    /// <summary>
+    /// 鍚姩绋嬪簭鑷姩寮�鍚皟搴︽湇鍔�
+    /// </summary>
     public class QuartzJobHostedService : IHostedService
     {
         private readonly ISchedulerCenter _schedulerCenter;
@@ -38,14 +44,19 @@
         private readonly IDeviceInfoService _deviceInfoService;
         private readonly IDispatchInfoService _dispatchInfoService;
         private readonly IDeviceProtocolDetailService _deviceProtocolDetailService;
+        private readonly QuartzNetExtension _quartzNetExtension;
 
-        public QuartzJobHostedService(ILogger<QuartzJobHostedService> logger, IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService)
+        /// <summary>
+        /// 鍚姩绋嬪簭鑷姩寮�鍚皟搴︽湇鍔�
+        /// </summary>
+        public QuartzJobHostedService(ILogger<QuartzJobHostedService> logger, IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, QuartzNetExtension quartzNetExtension)
         {
             _logger = logger;
             _deviceInfoService = deviceInfoService;
             _dispatchInfoService = dispatchInfoService;
             _schedulerCenter = schedulerCenter;
             _deviceProtocolDetailService = deviceProtocolDetailService;
+            _quartzNetExtension = quartzNetExtension;
         }
 
         /// <summary>
@@ -55,67 +66,12 @@
         /// <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 });
-                        type.InvokeMember("Connect", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { });
-
-                        #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;
-
-                        DevicesStorage.Devices.Add((IDevice)deviceInstance);
-                    }
-                });
-                for (int i = 0; i < dispatches.Count; i++)
-                {
-                    DeviceInfoDTO? deviceProInfo = deviceInfos.FirstOrDefault(x => x.DeviceType == dispatches[i].JobGroup);
-                    dispatches[i].JobParams = deviceProInfo?.Device;
-                    WebResponseContent responseContent = await _schedulerCenter.AddScheduleJobAsync(dispatches[i]);
-                }
-                await _schedulerCenter.StartScheduleAsync();
-            }
-            catch (Exception ex)
-            {
-                _logger.LogError(ex, "璋冨害鏈嶅姟寮�鍚紓甯�");
-                Console.WriteLine("璋冨害鏈嶅姟寮�鍚紓甯�" + ex.ToString());
-                throw;
-            }
+            await _quartzNetExtension.StartAsync();
         }
 
+        /// <summary>
+        /// 鍚姩绋嬪簭鑷姩寮�鍚皟搴︽湇鍔�
+        /// </summary>
         public Task StopAsync(CancellationToken cancellationToken)
         {
             _logger.LogInformation("Stop QuartzJob Service!");

--
Gitblit v1.9.3