hutongqing
2024-11-27 7ff298c6834275b63b612af49651673689a39660
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core;
using WIDESEAWCS_QuartzJob.DTO;
using Microsoft.Extensions.Logging;
using WIDESEAWCS_QuartzJob.QuartzExtensions;
using WIDESEAWCS_QuartzJob.Service;
 
namespace WIDESEAWCS_QuartzJob.QuartzNet
{
    public class QuartzNetExtension
    {
        private readonly ISchedulerCenter _schedulerCenter;
        private readonly IDeviceInfoService _deviceInfoService;
        private readonly IDispatchInfoService _dispatchInfoService;
        private readonly IDeviceProtocolDetailService _deviceProtocolDetailService;
        private readonly Storage _storage;
        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 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))
                    {
                        #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(x.DeviceCode + "连接成功"); else ConsoleHelper.WriteErrorLine(x.DeviceCode + "连接失败");
                        #endregion
 
                        #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.GetDeviceProtocolDetailsByDeviceType(x.DeviceType);
 
                        // 加载设备程序集
                        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);
                    }
                    else
                    {
                        x.Device = Storage.Devices.FirstOrDefault(d => d.DeviceCode == x.DeviceCode);
                    }
                });
                for (int i = 0; i < dispatches.Count; i++)
                {
                    DeviceInfoDTO? deviceProInfo = deviceInfos.FirstOrDefault(x => x.Id == dispatches[i].Id);
                    dispatches[i].JobParams = deviceProInfo?.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;
            }
        }
    }
}