wanshenmean
3 天以前 7278264f027d62664a0209699d0f66a22fd06a8e
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/QuartzNetExtension.cs
@@ -1,14 +1,10 @@
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_Common;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Caches;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.LogHelper;
using WIDESEAWCS_QuartzJob.DTO;
using Microsoft.Extensions.Logging;
using WIDESEAWCS_QuartzJob.QuartzExtensions;
using WIDESEAWCS_QuartzJob.Service;
namespace WIDESEAWCS_QuartzJob.QuartzNet
@@ -22,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>
@@ -47,6 +45,7 @@
            {
                List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos();
                List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos();
                _cacheService.RemoveByPrefix($"{RedisPrefix.System}");
                deviceInfos.ForEach(x =>
                {
@@ -54,66 +53,73 @@
                    {
                        try
                        {
                            #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(type.Name + x.DeviceCode + "连接成功"); else ConsoleHelper.WriteErrorLine(type.Name + x.DeviceCode + "连接失败");
                            #endregion
                            #region 实例化设备对象
                            List<DeviceProDTO> devicePros = x.ProtocolList.Select(d => new DeviceProDTO
                            if (!x.DevicePlcType.Contains("Socket"))
                            {
                                // 设备子编码
                                DeviceChildCode = d.DeviceChildCode,
                                // 设备数据类型
                                DeviceDataType = d.DeviceProDataType,
                                // 设备ID
                                DeviceId = d.DeviceId,
                                // 设备协议ID
                                DeviceProId = d.Id,
                                // 设备协议数据块
                                DeviceProDataBlock = d.DeviceProDataBlock,
                                // 设备协议数据长度
                                DeviceProDataLength = d.DeviceProDataLength,
                                // 设备协议偏移量
                                DeviceProOffset = d.DeviceProOffset,
                                // 设备协议参数描述
                                DeviceProParamDes = d.DeviceProParamDes,
                                // 设备协议参数名称
                                DeviceProParamName = d.DeviceProParamName,
                                // 设备协议参数类型
                                DeviceProParamType = d.DeviceProParamType,
                                // 设备PLC类型
                                DevicePlcType = x.DevicePlcType
                            }).ToList();
                                #region 连接PLC
                            // 根据设备类型获取设备协议详情
                            List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceType(x.DeviceType);
                                // 加载程序集
                                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(type.Name + x.DeviceCode + "连接成功"); else ConsoleHelper.WriteErrorLine(type.Name + x.DeviceCode + "连接失败");
                            // 加载设备程序集
                            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
                                #endregion 连接PLC
                            x.Device = (IDevice)deviceInstance;
                                #region 实例化设备对象
                            Storage.Devices.Add((IDevice)deviceInstance);
                                List<DeviceProDTO> devicePros = x.ProtocolList.Select(d => new DeviceProDTO
                                {
                                    // 设备子编码
                                    DeviceChildCode = d.DeviceChildCode,
                                    // 设备数据类型
                                    DeviceDataType = d.DeviceProDataType,
                                    // 设备ID
                                    DeviceId = d.DeviceId,
                                    // 设备协议ID
                                    DeviceProId = d.Id,
                                    // 设备协议数据块
                                    DeviceProDataBlock = d.DeviceProDataBlock,
                                    // 设备协议数据长度
                                    DeviceProDataLength = d.DeviceProDataLength,
                                    // 设备协议偏移量
                                    DeviceProOffset = d.DeviceProOffset,
                                    // 设备协议参数描述
                                    DeviceProParamDes = d.DeviceProParamDes,
                                    // 设备协议参数名称
                                    DeviceProParamName = d.DeviceProParamName,
                                    // 设备协议参数类型
                                    DeviceProParamType = d.DeviceProParamType,
                                    // 设备PLC类型
                                    DevicePlcType = x.DevicePlcType
                                }).ToList();
                                // 根据设备类型获取设备协议详情
                                List<DeviceProtocolDetailDTO> deviceProtocolDetails = _deviceProtocolDetailService.GetDeviceProtocolDetailsByDeviceId(x.Id);
                                // 加载设备程序集
                                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);
                                _cacheService.AddObject($"{RedisPrefix.System}:{RedisName.IDevice}:{x.DeviceName}", (IDevice)deviceInstance);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("调度服务开启异常" + ex.ToString());
                            QuartzLogger.Error($"调度服务开启异常", "QuartzNetExtension", ex);
                        }
                    }
                    else
@@ -123,18 +129,37 @@
                });
                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.DispatchId == dispatches[i].Id);
                    if (targetDevice is null) continue;
                    // 使用模式匹配
                    dispatches[i].JobParams = targetDevice switch
                    {
                        { DevicePlcType: var type } when type.Contains("Socket")
                            => 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 + "调度服务添加失败");
                    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;
            }
        }
    }
}
}