1
hutongqing
2024-10-29 9ca96199d92168fe221dda9aba56f55520a561d8
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,6 +25,7 @@
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;
@@ -62,13 +64,14 @@
                deviceInfos.ForEach(x =>
                {
                    if(dispatches.Exists(d=>d.JobGroup == x.DeviceType))
                    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[] { });
                        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
@@ -86,6 +89,7 @@
                            DeviceProParamDes = d.DeviceProParamDes,
                            DeviceProParamName = d.DeviceProParamName,
                            DeviceProParamType = d.DeviceProParamType,
                            DevicePlcType = x.DevicePlcType
                        }).ToList();
                        List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x.DeviceType);
@@ -102,9 +106,10 @@
                });
                for (int i = 0; i < dispatches.Count; i++)
                {
                    DeviceInfoDTO? deviceProInfo = deviceInfos.FirstOrDefault(x => x.DeviceType == dispatches[i].JobGroup);
                    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();
            }