yanjinhui
2025-03-22 2f0c81709876d76b6b120cf6ac43f05cda6dfe4c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO.Ports;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.WebSocket;
using Newtonsoft.Json;
using Quartz;
using SqlSugar;
using StackExchange.Profiling.Internal;
using WIDESEA_ISerialPortRepository;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class SerialPortJob : JobBase, IJob,IDisposable
    {
        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
 
        public enum CommandType
        {
            None = 0,
            Get = 1,//发信号
            Set = 2,//设值
        }
 
        public enum CommandResult
        {
            SetOK = 0,//parseok
            GetOK = 1,//03
            GetError = 2,//04
        }
        private readonly IPutakeRepository _putakeRepository;
        private readonly IProcessRepository _processRepository;
        private readonly ITorqueOpRepository _orqueOpRepository;
        private WebSocketServer _webSocketContext;
 
        public SerialPortJob(IPutakeRepository putakeRepository, IProcessRepository processRepository, ITorqueOpRepository torqueOpRepository, WebSocketServer webSocketContext)
        {
            _putakeRepository = putakeRepository;
            _processRepository = processRepository;
            _orqueOpRepository = torqueOpRepository;
            _webSocketContext = webSocketContext;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                SerialPortDevice serialPortDevice = (SerialPortDevice)context.JobDetail.JobDataMap.Get("JobParams");
                if (serialPortDevice != null)
                {
 
                    List<DeviceProDTO> deviceProDTOs = serialPortDevice.DeviceProDTOs;
                    foreach (var item in deviceProDTOs)
                    {
                        if (item.DeviceProParamName != CommandType.Get.ToString() && item.DeviceProParamName != CommandType.Set.ToString())
                        {
                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
                            if (deviceProtocolDetail != null)
                            {
                                # region  Setvalue
                                string group = item.DeviceProDataBlock;
                                if (group == "电气")
                                {
                                    //查看步骤
                                    var com = item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue.Replace("[setNum]", (5 * 1000).ToString().PadLeft(7, '0'))+"\r";
                                    serialPortDevice.Communicator.Write(com+"\r");
                                    //打开串口时先设值
                                    
                                     
                                }
                                #endregion
                                item.DeviceProParamName = CommandType.Set.ToString();
 
 
                            }
                        }
 
                        if (CommandType.Get.ToString() == item.DeviceProParamName)
                        {
                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Get));
                            //发送设备号
                            if (deviceProtocolDetail != null)
                            {
                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
                            }
                        }
                        else if (CommandType.Set.ToString() == item.DeviceProParamName)
                        {
                            DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandType) && x.ProtocolDetailType == nameof(CommandType.Set));
                            //设值
                            if (deviceProtocolDetail != null)
                            {
                                serialPortDevice.Communicator.Write(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue, "\r");//打开串口时先设值
                            }
                        }
 
 
 
                        if (serialPortDevice.Communicator.Buffers.Count > 0)
                        {
                            string? receiveData = serialPortDevice.Communicator.ToString(Encoding.Default);
                            if (!string.IsNullOrEmpty(receiveData))
                            {
                                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + receiveData);
 
                                if (item.DeviceProParamName == CommandType.Set.ToString())
                                {
                                    DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.SetOK));
                                    //parseok 
                                    if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
                                    {
                                        item.DeviceProParamName = CommandType.Get.ToString();
 
 
 
 
                                    }
                                }
                                else if (item.DeviceProParamName == CommandType.Get.ToString())
                                {
                                    DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetOK));
                                    //03成功
                                    if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
                                    {
 
                                        string group = item.DeviceProDataBlock; // 设备所属组别
                                        if (group == "电气")
                                        {
 
                                            item.DeviceProParamName = CommandType.Set.ToString();
                                        }
                                        else if (item.DeviceProParamName == CommandType.Get.ToString())
                                        {
                                            //DeviceProtocolDetailDTO? deviceProtocolDetail = serialPortDevice.DeviceProtocolDetailDTOs.FirstOrDefault(x => x.DeviceProParamName == nameof(CommandResult) && x.ProtocolDetailType == nameof(CommandResult.GetError));
                                            //if (deviceProtocolDetail != null && receiveData.Contains(item.DeviceChildCode + deviceProtocolDetail.ProtocalDetailValue))
                                            //{
                                            //    item.DeviceProParamName = CommandType.Set.ToString();
 
 
 
                                            //}
                                        }
                                    }
                                }
                            }
 
 
                            //建一个对象将其传给前端
                            string data = JsonConvert.SerializeObject(serialPortDevice);//这里serialPortDevice是假设有这个对象
                            _webSocketContext.PublishAllClientPayload(data);
                        }
 
                    }
                }
            }
            catch (Exception ex)
            {
                WriteError("CommonConveyorLineJob", "test", ex);
            }
            WriteDebug("CommonConveyorLineJob", "test");
            return Task.CompletedTask;
        }
       
 
 
    }
}