wanshenmean
2026-02-05 c388f1acccd0b7e76a4366fbaddaca6551826b74
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System.Reflection;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Service;
 
namespace WIDESEAWCS_QuartzJob.QuartzNet
{
    /// <summary>
    /// 启动程序自动开启调度服务
    /// </summary>
    public class QuartzNetExtension
    {
        private readonly ISchedulerCenter _schedulerCenter;
        private readonly IDeviceInfoService _deviceInfoService;
        private readonly IDispatchInfoService _dispatchInfoService;
        private readonly IDeviceProtocolDetailService _deviceProtocolDetailService;
        private readonly Storage _storage;
 
        /// <summary>
        /// 启动程序自动开启调度服务
        /// </summary>
        /// <returns></returns>
        public QuartzNetExtension(IDeviceInfoService deviceInfoService, IDispatchInfoService dispatchInfoService, ISchedulerCenter schedulerCenter, IDeviceProtocolDetailService deviceProtocolDetailService, Storage storage)
        {
            _deviceInfoService = deviceInfoService;
            _dispatchInfoService = dispatchInfoService;
            _schedulerCenter = schedulerCenter;
            _deviceProtocolDetailService = deviceProtocolDetailService;
            _storage = storage;
        }
 
        /// <summary>
        /// 启动程序自动开启调度服务
        /// </summary>
        /// <returns></returns>
        public virtual async Task StartAsync()
        {
            try
            {
                List<DispatchInfoDTO> dispatches = _dispatchInfoService.QueryDispatchInfos();
                List<DeviceInfoDTO> deviceInfos = await _deviceInfoService.QueryDeviceProInfos();
 
                deviceInfos.ForEach(x =>
                {
                    if (!Storage.Devices.Exists(d => d.DeviceCode == x.DeviceCode))
                    {
                        try
                        {
                            if (!x.DevicePlcType.Contains("Socket"))
                            {
                                #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 连接PLC
 
                                #region 实例化设备对象
 
                                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);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("调度服务开启异常" + ex.ToString());
                        }
                    }
                    else
                    {
                        x.Device = Storage.Devices.FirstOrDefault(d => d.DeviceCode == x.DeviceCode);
                    }
                });
                for (int i = 0; i < dispatches.Count; i++)
                {
                    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 + "调度服务添加失败");
                }
                //await _schedulerCenter.StartScheduleAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine("调度服务开启异常" + ex.ToString());
                throw;
            }
        }
    }
}