wanshenmean
2026-02-03 3f67529e0f492f30851f091fea7f97a01cb502e5
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs
@@ -1,14 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.Helper;
using System.Reflection;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_QuartzJob.DTO;
using Microsoft.Extensions.Logging;
using WIDESEAWCS_QuartzJob.QuartzExtensions;
using WIDESEAWCS_QuartzJob.Service;
namespace WIDESEAWCS_QuartzJob.QuartzNet
@@ -54,7 +47,10 @@
                    {
                        try
                        {
                            if (!x.DeviceName.Contains("机械手"))
                            {
                            #region 连接PLC
                            // 加载程序集
                            Assembly assembly = Assembly.Load($"WIDESEAWCS_Communicator");
                            // 获取类型
@@ -65,11 +61,11 @@
                            bool? connectResult = (bool)type.InvokeMember("Connect", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { });
                            // 判断连接结果
                            if (connectResult ?? false) ConsoleHelper.WriteSuccessLine(type.Name + x.DeviceCode + "连接成功"); else ConsoleHelper.WriteErrorLine(type.Name + x.DeviceCode + "连接失败");
                            #endregion
                                #endregion 连接PLC
                            #region 实例化设备对象
                            List<DeviceProDTO> devicePros = x.ProtocolList.Select(d => new DeviceProDTO
                            {
                                // 设备子编码
@@ -105,11 +101,13 @@
                            Type typeDevice = assemblyDevice.GetType($"WIDESEAWCS_QuartzJob.{x.DeviceType}");
                            // 创建设备实例
                            object deviceInstance = Activator.CreateInstance(typeDevice, new object[] { obj, devicePros, deviceProtocolDetails, x.DeviceCode, x.DeviceName });
                            #endregion
                                #endregion 实例化设备对象
                            x.Device = (IDevice)deviceInstance;
                            Storage.Devices.Add((IDevice)deviceInstance);
                            }
                        }
                        catch (Exception ex)
                        {
@@ -123,8 +121,17 @@
                });
                for (int i = 0; i < dispatches.Count; i++)
                {
                    DeviceInfoDTO? deviceProInfo = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id);
                    dispatches[i].JobParams = deviceProInfo?.Device;
                    var targetDevice = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id);
                    if (targetDevice is null) continue;
                    // 使用模式匹配
                    dispatches[i].JobParams = targetDevice switch
                    {
                        { DeviceName: var name } when name.Contains("机械手")
                            => 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 + "调度服务添加失败");
                }