wanshenmean
6 天以前 5171d3f59b89389bf75293afd210cfa6de4ccff7
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs
@@ -1,6 +1,9 @@
using System.Reflection;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
@@ -15,19 +18,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 +45,7 @@
            {
                List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos();
                List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos();
                _cacheService.RemoveByPrefix($"{RedisPrefix.System}");
                deviceInfos.ForEach(x =>
                {
@@ -107,11 +113,13 @@
                                x.Device = (IDevice)deviceInstance;
                                Storage.Devices.Add((IDevice)deviceInstance);
                                _cacheService.AddObject($"{RedisPrefix.System}:{RedisName.IDevice}:{x.DeviceName}", (IDevice)deviceInstance);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("调度服务开启异常" + ex.ToString());
                            QuartzLogger.Error($"调度服务开启异常", "QuartzNetExtension", ex);
                        }
                    }
                    else
@@ -125,6 +133,7 @@
                    if (targetDevice is null) continue;
                    // 使用模式匹配
                    dispatches[i].JobParams = targetDevice switch
                    {
@@ -133,13 +142,22 @@
                        _ => targetDevice.Device
                    };
                    WebResponseContent responseContent = await _schedulerCenter.AddScheduleJobAsync(dispatches[i]);
                    if (responseContent.Status) ConsoleHelper.WriteSuccessLine(dispatches[i].Name + "调度服务添加成功"); else ConsoleHelper.WriteErrorLine(dispatches[i].Name + "调度服务添加失败");
                    if (responseContent.Status)
                    {
                        QuartzLogger.Info($"{dispatches[i].Name}调度服务添加成功", "QuartzNetExtension");
                        ConsoleHelper.WriteSuccessLine(dispatches[i].Name + "调度服务添加成功");
                    }
                    else
                    {
                        QuartzLogger.Error($"{dispatches[i].Name}调度服务添加失败", "QuartzNetExtension");
                        ConsoleHelper.WriteErrorLine(dispatches[i].Name + "调度服务添加失败");
                    }
                }
                //await _schedulerCenter.StartScheduleAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine("调度服务开启异常" + ex.ToString());
                QuartzLogger.Error($"调度服务开启异常", "QuartzNetExtension", ex);
                throw;
            }
        }