1
z8018
2025-05-06 7da6f394864ba0412c1ac58ba7752862de7d3f3d
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.WebSocket;
using Quartz;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DTO;
 
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 龙门架
    /// </summary>
    [DisallowConcurrentExecution]
    public class GantryJob : JobBase, IJob
    {
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskService _taskService;
        private readonly IContainerItemRepository _containerItemRepository;
        private readonly WebSocketServer _webSocketServer;
        private readonly IOrderDetailsService _orderDetailsService;
        public GantryJob(ITaskRepository taskRepository, ITaskService taskService, IContainerItemRepository containerItemRepository, WebSocketServer webSocketServer, IOrderDetailsService orderDetailsService)
        {
            _taskRepository = taskRepository;
            _taskService = taskService;
            _containerItemRepository = containerItemRepository;
            _webSocketServer = webSocketServer;
            _orderDetailsService = orderDetailsService;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            bool flag = context.JobDetail.JobDataMap.TryGetValue("JobParams", out object? value);
            if (flag && value != null && value is OtherDevice)
            {
                OtherDevice otherDevice = (OtherDevice)value;
                try
                {
                    byte gantryStatus = otherDevice.GetValue<GantryDBName, byte>(GantryDBName.GantryStatus);
                    byte gantryAutoStatus = otherDevice.GetValue<GantryDBName, byte>(GantryDBName.GantryAutoStatus);
                    byte gantryWorkStatus = otherDevice.GetValue<GantryDBName, byte>(GantryDBName.GantryWorkStatus);
 
                    if (gantryStatus == 1 && gantryAutoStatus == 3 && gantryWorkStatus == 0)
                    {
                        // 逻辑处理
                        // 1. 读取任务
                        // 2. 任务执行
                        // 3. 任务完成
                        Dt_Task? task = _taskService.QueryAGantryUnExecuteTask(otherDevice.DeviceCode);
                        if (task != null)
                        {
                            string[] takePositions = task.CurrentAddress.Split("*");
                            if (takePositions.Length != 5)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "取货位置错误");
                                if (task.ExceptionMessage?.Contains("取货位置错误") ?? true)
                                {
                                    task.ExceptionMessage = "取货位置错误";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            string[] putPositions = task.NextAddress.Split("*");
                            if (putPositions.Length != 5)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "放货位置错误");
                                if (task.ExceptionMessage?.Contains("放货位置错误") ?? true)
                                {
                                    task.ExceptionMessage = "放货位置错误";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            Dt_ContainerItem containerItem = _containerItemRepository.QueryFirst(x => x.ItemCode == task.PalletCode);
                            if (containerItem == null)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "板材尺寸获取错误");
                                if (task.ExceptionMessage?.Contains("板材尺寸获取错误") ?? true)
                                {
                                    task.ExceptionMessage = "板材尺寸获取错误";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (!LightStatusStorage.StationStautsDic.TryGetValue(putPositions[0],out bool stationStatus))
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", $"工位【{putPositions[0]}】有无垫板数据错误,{LightStatusStorage.StationStautsDic.Serialize()}");
                                if (task.ExceptionMessage?.Contains($"工位【{putPositions[0]}】有无垫板数据错误") ?? true)
                                {
                                    task.ExceptionMessage = $"工位【{putPositions[0]}】有无垫板数据错误";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
                            if (!stationStatus)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", $"工位【{putPositions[0]}】无垫板,{LightStatusStorage.StationStautsDic.Serialize()}");
                                if (task.ExceptionMessage?.Contains($"工位【{putPositions[0]}】无垫板") ?? true)
                                {
                                    task.ExceptionMessage = $"工位【{putPositions[0]}】无垫板";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
                            
                            int takePoX = Convert.ToInt32(takePositions[1]);
                            int takePoY = Convert.ToInt32(takePositions[2]);
                            int takePoZ = OPositions.HPositions[takePositions[0]].PositionZ + 30 * 1000 - Convert.ToInt32(takePositions[3]) * 1000;
 
                            if (OPositions.HPositions[takePositions[0]].PositionZ == 0)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "读取取货Z坐标读取为0");
                                if (task.ExceptionMessage?.Contains($"读取取货Z坐标读取为0") ?? true)
                                {
                                    task.ExceptionMessage = $"读取取货Z坐标读取为0";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            int takePoR = 0;
 
                            int putPoX = Convert.ToInt32(putPositions[1]);
                            int putPoY = Convert.ToInt32(putPositions[2]);
                            int putPoZ = OPositions.HPositions[putPositions[0]].PositionZ - Convert.ToInt32(putPositions[3]) * 1000;
 
                            if (OPositions.HPositions[takePositions[0]].PositionZ == 0)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "读取放货Z坐标读取为0");
                                if (task.ExceptionMessage?.Contains($"读取放货Z坐标读取为0") ?? true)
                                {
                                    task.ExceptionMessage = $"读取放货Z坐标读取为0";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
                            int putPoR = 0;
 
                            int temp = 1000;
                            if(otherDevice.DeviceCode == "GT02")
                            {
                                temp = -1000;
                            }
 
                            if (Convert.ToInt32(takePositions[4]) == 1 || Convert.ToInt32(takePositions[4]) == 2)
                            {
                                takePoX = takePoX * temp + OPositions.HPositions[takePositions[0]].PositionX;
                                takePoY = takePoY * temp + OPositions.HPositions[takePositions[0]].PositionY;
                                putPoX = putPoX * temp + OPositions.HPositions[putPositions[0]].PositionX;
                                putPoY = putPoY * temp + OPositions.HPositions[putPositions[0]].PositionY;
                                takePoR = OPositions.HPositions[takePositions[0]].PositionR;
 
                                if (Convert.ToInt32(takePositions[4]) == 1)
                                    putPoR = OPositions.HPositions[putPositions[0]].PositionR;
                                else
                                {
                                    if (otherDevice.DeviceCode == "GT03")
                                        putPoR = otherDevice.Communicator.Read<int>("DB10.840");
                                    else if (otherDevice.DeviceCode == "GT02")
                                        putPoR = otherDevice.Communicator.Read<int>("DB10.836");
                                    else if (otherDevice.DeviceCode == "GT01")
                                        putPoR = otherDevice.Communicator.Read<int>("DB10.832");
                                    else
                                    {
                                        WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "R坐标错误");
                                        if (task.ExceptionMessage?.Contains($"R坐标错误") ?? true)
                                        {
                                            task.ExceptionMessage = $"R坐标错误";
                                            _taskRepository.UpdateData(task);
                                        }
                                        return Task.CompletedTask;
                                    }
                                }
 
                                WriteDebug($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"取货位:{takePositions[0]},放货位:{putPositions[0]}{Environment.NewLine}取货坐标:X:{takePoX} Y:{takePoY} Z:{takePoZ} R:{takePoR}{Environment.NewLine}放货坐标:X:{putPoX} Y:{putPoY} Z:{putPoZ} R:{putPoR}{Environment.NewLine}读取PLC取货坐标:X:{OPositions.HPositions[takePositions[0]].PositionX} Y:{OPositions.HPositions[takePositions[0]].PositionY} Z:{OPositions.HPositions[takePositions[0]].PositionZ} R:{OPositions.HPositions[takePositions[0]].PositionR}{Environment.NewLine}读取PLC放货坐标:X:{OPositions.HPositions[putPositions[0]].PositionX} Y:{OPositions.HPositions[putPositions[0]].PositionY} Z:{OPositions.HPositions[putPositions[0]].PositionZ} R:{OPositions.HPositions[putPositions[0]].PositionR}");
                            }
                            else
                            {
                                takePoX = takePoX * temp + OPositions.ZPositions[takePositions[0]].PositionX;
                                takePoY = takePoY * temp + OPositions.ZPositions[takePositions[0]].PositionY;
                                putPoX = putPoX * temp + OPositions.ZPositions[putPositions[0]].PositionX;
                                putPoY = putPoY * temp + OPositions.ZPositions[putPositions[0]].PositionY;
                                takePoR = OPositions.ZPositions[takePositions[0]].PositionR;
                                putPoR = OPositions.ZPositions[putPositions[0]].PositionR;
 
                                WriteDebug($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"取货位:{takePositions[0]},放货位:{putPositions[0]}{Environment.NewLine}取货坐标:X:{takePoX} Y:{takePoY} Z:{takePoZ} R:{takePoR}{Environment.NewLine}放货坐标:X:{putPoX} Y:{putPoY} Z:{putPoZ} R:{putPoR}{Environment.NewLine}读取PLC取货坐标:X:{OPositions.ZPositions[takePositions[0]].PositionX} Y:{OPositions.ZPositions[takePositions[0]].PositionY} Z:{OPositions.ZPositions[takePositions[0]].PositionZ} R:{OPositions.ZPositions[takePositions[0]].PositionR}{Environment.NewLine}读取PLC放货坐标:X:{OPositions.ZPositions[putPositions[0]].PositionX} Y:{OPositions.ZPositions[putPositions[0]].PositionY} Z:{OPositions.ZPositions[putPositions[0]].PositionZ} R:{OPositions.ZPositions[putPositions[0]].PositionR}");
                            }
                            #region
                            List<DeviceProDTO> devicePros = otherDevice.DeviceProDTOs.Where(x => x.DeviceProParamType == "MaxPosition").ToList();
 
                            DeviceProDTO? devicePro = devicePros.OrderBy(x => x.DeviceProOffset).FirstOrDefault();
                            if(devicePro == null)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "设备协议参数错误,未找到最大最小坐标地址");
                                if (task.ExceptionMessage?.Contains($"设备协议参数错误,未找到最大最小坐标地址") ?? true)
                                {
                                    task.ExceptionMessage = $"设备协议参数错误,未找到最大最小坐标地址";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
                            int[] data = otherDevice.Communicator.Read<int>(devicePro.DeviceProAddress, (ushort)(devicePros.Count));
 
                            int maxX = data[0];
                            int minX = data[1];
                            int maxY = data[2];
                            int minY = data[3];
                            int maxZ = data[4];
                            int minZ = data[5];
                            int maxR = data[6];
                            int minR = data[7];
 
                            //int maxX = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MaxX);
                            //int minX = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MinX);
                            //int maxY = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MaxY);
                            //int minY = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MinY);
                            //int maxZ = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MaxZ);
                            //int minZ = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MinZ);
                            //int maxR = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MaxR);
                            //int minR = otherDevice.GetValue<GantryDBName, int>(GantryDBName.MinR);
 
                            
                            if (takePoX < minX || takePoX > maxX)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"X取货坐标超出范围,取货坐标:{takePoX},最大值:{maxX},最小值:{minX}");
                                if (task.ExceptionMessage?.Contains($"X取货坐标超出范围,取货坐标:{takePoX},最大值:{maxX},最小值:{minX}") ?? true)
                                {
                                    task.ExceptionMessage = $"X取货坐标超出范围,取货坐标:{takePoX},最大值:{maxX},最小值:{minX}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (putPoX < minX || putPoX > maxX)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"X放货坐标超出范围,取货坐标:{putPoX},最大值:{maxX},最小值:{minX}");
                                if (task.ExceptionMessage?.Contains($"X放货坐标超出范围,取货坐标:{putPoX},最大值:{maxX},最小值:{minX}") ?? true)
                                {
                                    task.ExceptionMessage = $"X放货坐标超出范围,取货坐标:{putPoX},最大值:{maxX},最小值:{minX}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (takePoY < minY || takePoY > maxY)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Y取货坐标超出范围,取货坐标:{takePoY},最大值:{maxY},最小值:{minY}");
                                if (task.ExceptionMessage?.Contains($"Y取货坐标超出范围,取货坐标:{takePoY},最大值:{maxY},最小值:{minY}") ?? true)
                                {
                                    task.ExceptionMessage = $"Y取货坐标超出范围,取货坐标:{takePoY},最大值:{maxY},最小值:{minY}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (putPoY < minY || putPoY > maxY)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Y放货坐标超出范围,取货坐标:{putPoY},最大值:{maxY},最小值:{minY}");
                                if (task.ExceptionMessage?.Contains($"Y放货坐标超出范围,取货坐标:{putPoY},最大值:{maxY},最小值:{minY}") ?? true)
                                {
                                    task.ExceptionMessage = $"Y放货坐标超出范围,取货坐标:{putPoY},最大值:{maxY},最小值:{minY}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (takePoZ < minZ || takePoZ > maxZ)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Z取货坐标超出范围,取货坐标:{takePoZ},最大值:{maxZ},最小值:{minZ}");
                                if (task.ExceptionMessage?.Contains($"Z取货坐标超出范围,取货坐标:{takePoZ},最大值:{maxZ},最小值:{minZ}") ?? true)
                                {
                                    task.ExceptionMessage = $"Z取货坐标超出范围,取货坐标:{takePoZ},最大值:{maxZ},最小值:{minZ}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (putPoZ < minZ || putPoZ > maxZ)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Z放货坐标超出范围,取货坐标:{putPoZ},最大值:{maxZ},最小值:{minZ}");
                                if (task.ExceptionMessage?.Contains($"Z放货坐标超出范围,取货坐标:{putPoZ},最大值:{maxZ},最小值:{minZ}") ?? true)
                                {
                                    task.ExceptionMessage = $"Z放货坐标超出范围,取货坐标:{putPoZ},最大值:{maxZ},最小值:{minZ}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (takePoR < minR || takePoR > maxR)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"R取货坐标超出范围,取货坐标:{takePoR},最大值:{maxR},最小值:{minR}");
                                if (task.ExceptionMessage?.Contains($"R取货坐标超出范围,取货坐标:{takePoR},最大值:{maxR},最小值:{minR}") ?? true)
                                {
                                    task.ExceptionMessage = $"R取货坐标超出范围,取货坐标:{takePoR},最大值:{maxR},最小值:{minR}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
 
                            if (putPoR < minR || putPoR > maxR)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"R放货坐标超出范围,取货坐标:{putPoR},最大值:{maxR},最小值:{minR}");
                                if (task.ExceptionMessage?.Contains($"R放货坐标超出范围,取货坐标:{putPoR},最大值:{maxR},最小值:{minR}") ?? true)
                                {
                                    task.ExceptionMessage = $"R放货坐标超出范围,取货坐标:{putPoR},最大值:{maxR},最小值:{minR}";
                                    _taskRepository.UpdateData(task);
                                }
                                return Task.CompletedTask;
                            }
                            #endregion
 
                            otherDevice.SetValue(GantryDBName.TwoHand, true);
                            otherDevice.SetValue(GantryDBName.TaskNum, task.TaskNum);
                            otherDevice.SetValue(GantryDBName.TakePositionX, takePoX);
                            otherDevice.SetValue(GantryDBName.TakePositionY, takePoY);
                            otherDevice.SetValue(GantryDBName.TakePositionZ, takePoZ);
                            otherDevice.SetValue(GantryDBName.TakePositionR, takePoR);
                            otherDevice.SetValue(GantryDBName.PutPositionX, putPoX);
                            otherDevice.SetValue(GantryDBName.PutPositionY, putPoY);
                            otherDevice.SetValue(GantryDBName.PutPositionZ, putPoZ);
                            otherDevice.SetValue(GantryDBName.PutPositionR, putPoR);
                            otherDevice.SetValue(GantryDBName.Length, containerItem.ItemLength);
                            otherDevice.SetValue(GantryDBName.Width, containerItem.ItemWidth);
                            otherDevice.SetValue(GantryDBName.Height, containerItem.ItemHeight);
                            otherDevice.SetValue(GantryDBName.WorkType, 1);
                            otherDevice.SetValue(GantryDBName.StartCommand, 1);
 
                            task.TaskState = TaskStatusEnum.Gantry_Executing.ObjToInt();
                            _taskRepository.UpdateData(task);
 
                            if (LightStatusStorage.LightStatusDic.TryGetValue(putPositions[0], out LightStatusEnum lightStatusDic))
                            {
                                if (lightStatusDic != LightStatusEnum.LightWorking)
                                {
                                    LightStatusStorage.LightStatusDic[putPositions[0]] = LightStatusEnum.LightWorking;
                                }
                            }
                        }
                    }
                    else if (gantryWorkStatus == 5)
                    {
                        int currentTaskNum = otherDevice.GetValue<GantryDBName, int>(GantryDBName.TaskNum);
                        if (currentTaskNum > 0)
                        {
                            Dt_Task task = _taskRepository.QueryFirst(x => x.TaskNum == currentTaskNum);
                            if (task != null)
                            {
                                _taskService.TaskComplete(task);
 
                                Task.Run(() =>
                                {
                                    _orderDetailsService.ToMes(task.PalletCode, 4);
                                });
                                
 
                                string[] putPositions = task.NextAddress.Split("*");
                                if (putPositions.Length != 5)
                                {
                                    //WriteError
                                    return Task.CompletedTask;
                                }
                                if (LightStatusStorage.LightStatusDic.TryGetValue(putPositions[0], out LightStatusEnum lightStatusDic))
                                {
                                    if (lightStatusDic != LightStatusEnum.Ready)
                                    {
                                        LightStatusStorage.LightStatusDic[putPositions[0]] = LightStatusEnum.Ready;
                                    }
                                }
                            }
                        }
                        otherDevice.SetValue(GantryDBName.WorkType, 5);
                    }
                }
                catch (Exception ex)
                {
                    WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", ex.Message, ex);
                }
            }
            else
            {
                WriteError(nameof(GantryJob), "参数错误,未传递设备参数或设备类型错误");
            }
            return Task.CompletedTask;
        }
 
        public Dt_Task GetTask()
        {
            return new Dt_Task();
        }
    }
}