wanshenmean
2026-03-04 17e5dbd7bd0364e27a33f1a7dab91cf33d5dcabc
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs
@@ -1,5 +1,7 @@
using System.Reflection;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
@@ -15,19 +17,21 @@
        private readonly IDeviceInfoService _deviceInfoService;
        private readonly IDispatchInfoService _dispatchInfoService;
        private readonly IDeviceProtocolDetailService _deviceProtocolDetailService;
        private readonly ICacheService _cacheService;
        private readonly Storage _storage;
        /// <summary>
        /// 启动程序自动开启调度服务
        /// </summary>
        /// <returns></returns>
        public QuartzNetExtension(IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, Storage storage)
        public QuartzNetExtension(IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, Storage storage, ICacheService cacheService)
        {
            _deviceInfoService = deviceInfoService;
            _dispatchInfoService = dispatchInfoService;
            _schedulerCenter = schedulerCenter;
            _deviceProtocolDetailService = deviceProtocolDetailService;
            _storage = storage;
            _cacheService = cacheService;
        }
        /// <summary>
@@ -40,6 +44,7 @@
            {
                List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos();
                List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos();
                _cacheService.RemoveByPrefix($"{RedisPrefix.System}");
                deviceInfos.ForEach(x =>
                {
@@ -47,7 +52,7 @@
                    {
                        try
                        {
                            if (!x.DeviceName.Contains("机械手"))
                            if (!x.DevicePlcType.Contains("Socket"))
                            {
                                #region 连接PLC
@@ -93,7 +98,7 @@
                                }).ToList();
                                // 根据设备类型获取设备协议详情
                                List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x.DeviceType);
                                List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceId(x.Id);
                                // 加载设备程序集
                                Assembly assemblyDevice = Assembly.Load($"WIDESEAWCS_QuartzJob");
@@ -107,6 +112,8 @@
                                x.Device = (IDevice)deviceInstance;
                                Storage.Devices.Add((IDevice)deviceInstance);
                                _cacheService.AddObject($"{RedisPrefix.System}:{RedisName.IDevice}:{x.DeviceName}", (IDevice)deviceInstance);
                            }
                        }
                        catch (Exception ex)
@@ -121,14 +128,15 @@
                });
                for (int i = 0; i < dispatches.Count; i++)
                {
                    var targetDevice = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id);
                    var targetDevice = deviceInfos.FirstOrDefault(x => x.DispatchId == dispatches[i].Id);
                    if (targetDevice is null) continue;
                    // 使用模式匹配
                    dispatches[i].JobParams = targetDevice switch
                    {
                        { DeviceName: var name } when name.Contains("机械手")
                        { DevicePlcType: var type } when type.Contains("Socket")
                            => new RobotCraneDevice { DeviceCode = targetDevice.DeviceCode, DeviceName = targetDevice.DeviceName, IPAddress = targetDevice.DeviceIp + ":" + targetDevice.DevicePort },
                        _ => targetDevice.Device
                    };