From 70233af5426b0d1c343ebe87183303a34a9aaa58 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期三, 20 十一月 2024 09:05:38 +0800 Subject: [PATCH] WIDESEAWCS_QuartzJob更新版本 --- WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs | 70 ++++------------------------------ 1 files changed, 9 insertions(+), 61 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs index 31ef5bb..e6571a0 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzExtensions/QuartzJobHostedService.cs @@ -29,6 +29,7 @@ using WIDESEAWCS_QuartzJob.DeviceBase; using WIDESEAWCS_QuartzJob.DTO; using WIDESEAWCS_QuartzJob.Models; +using WIDESEAWCS_QuartzJob.QuartzNet; using WIDESEAWCS_QuartzJob.Service; namespace WIDESEAWCS_QuartzJob.QuartzExtensions @@ -40,14 +41,16 @@ 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) + 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> @@ -57,68 +60,13 @@ /// <returns></returns> public async Task StartAsync(CancellationToken cancellationToken) { - try + if (!HslCommunication.Authorization.SetAuthorizationCode("891c8f18-d6de-409f-81f4-6de405431905")) { - 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, - 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); - } - }); - 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(); + Console.WriteLine("active failed"); + Console.ReadLine(); } - catch (Exception ex) - { - _logger.LogError(ex, "璋冨害鏈嶅姟寮�鍚紓甯�"); - Console.WriteLine("璋冨害鏈嶅姟寮�鍚紓甯�" + ex.ToString()); - throw; - } + + await _quartzNetExtension.StartAsync(); } public Task StopAsync(CancellationToken cancellationToken) -- Gitblit v1.9.3