1
z8018
2025-05-05 b6837f097e9cdb2645368aed4ddb03f580c331e4
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
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;
using WIDESEAWCS_Tasks.Gantry;
 
namespace WIDESEAWCS_Tasks
{
    /// <summary>
    /// 龙门架
    /// </summary>
    [DisallowConcurrentExecution]
    public class GantryJob2 : JobBase, IJob
    {
        private readonly ITaskRepository _taskRepository;
        private readonly ITaskService _taskService;
        private readonly IContainerItemRepository _containerItemRepository;
        private readonly WebSocketServer _webSocketServer;
        private readonly IOrderDetailsService _orderDetailsService;
        public GantryJob2(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)
                        {
 
                            GantryCommand? command = TaskToCommand(task, otherDevice);
                            if (command == null)
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "坐标转换错误");
                                return Task.CompletedTask;
                            }
 
                            if(IsPositionValid(otherDevice, command))
                            {
                                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "坐标超出范围");
                                return Task.CompletedTask;
                            }
 
                            otherDevice.SetValue(GantryDBName.TwoHand, true);
                            otherDevice.SetValue(GantryDBName.TaskNum, task.TaskNum);
                            otherDevice.SetValue(GantryDBName.TakePositionX, command.TakePoX);
                            otherDevice.SetValue(GantryDBName.TakePositionY, command.TakePoY);
                            otherDevice.SetValue(GantryDBName.TakePositionZ, command.TakePoZ);
                            otherDevice.SetValue(GantryDBName.TakePositionR, command.TakePoR);
                            otherDevice.SetValue(GantryDBName.PutPositionX, command.PutPoX);
                            otherDevice.SetValue(GantryDBName.PutPositionY, command.PutPoY);
                            otherDevice.SetValue(GantryDBName.PutPositionZ, command.PutPoZ);
                            otherDevice.SetValue(GantryDBName.PutPositionR, command.PutPoR);
                            otherDevice.SetValue(GantryDBName.Length, command.ItemLength);
                            otherDevice.SetValue(GantryDBName.Width, command.ItemWidth);
                            otherDevice.SetValue(GantryDBName.Height, command.ItemHeight);
                            otherDevice.SetValue(GantryDBName.WorkType, 1);
                            otherDevice.SetValue(GantryDBName.StartCommand, 1);
 
                            task.TaskState = TaskStatusEnum.Gantry_Executing.ObjToInt();
                            _taskRepository.UpdateData(task);
                        }
                    }
                    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);
 
                                _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 GantryCommand? TaskToCommand(Dt_Task task, OtherDevice otherDevice)
        {
            if (TaskValid(task, out string[] takePositions, out string[] putPositions, out Dt_ContainerItem containerItem))
            {
                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;
                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;
                int putPoR = 0;
 
                if (OPositions.HPositions[takePositions[0]].PositionZ == 0)
                {
                    WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "取货Z坐标读取为0");
                    return null;
                }
 
                if (OPositions.HPositions[putPositions[0]].PositionZ == 0)
                {
                    WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}", "放货Z坐标读取为0");
                    return null;
                }
 
                if (Convert.ToInt32(takePositions[4]) == 1 || Convert.ToInt32(takePositions[4]) == 2)
                {
                    takePoX = takePoX * 1000 + OPositions.HPositions[takePositions[0]].PositionX;
                    takePoY = takePoY * 1000 + OPositions.HPositions[takePositions[0]].PositionY;
                    putPoX = putPoX * 1000 + OPositions.HPositions[putPositions[0]].PositionX;
                    putPoY = putPoY * 1000 + 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坐标错误");
                            return null;
                        }
                    }
                    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 * 1000 + OPositions.ZPositions[takePositions[0]].PositionX;
                    takePoY = takePoY * 1000 + OPositions.ZPositions[takePositions[0]].PositionY;
                    putPoX = putPoX * 1000 + OPositions.ZPositions[putPositions[0]].PositionX;
                    putPoY = putPoY * 1000 + 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}");
                }
 
                GantryCommand command = new GantryCommand()
                {
                    TakePoX = takePoX,
                    TakePoY = takePoY,
                    TakePoZ = takePoZ,
                    TakePoR = takePoR,
                    PutPoX = putPoX,
                    PutPoY = putPoY,
                    PutPoZ = putPoZ,
                    PutPoR = putPoR,
                    ItemLength = containerItem.ItemLength,
                    ItemWidth = containerItem.ItemWidth,
                    ItemHeight = containerItem.ItemHeight
                };
 
                return command;
            }
 
            return null;
        }
 
        public bool TaskValid(Dt_Task task, out string[] takePositions, out string[] putPositions, out Dt_ContainerItem containerItem)
        {
            putPositions = null;
            containerItem = new Dt_ContainerItem();
            takePositions = task.CurrentAddress.Split("*");
            if (takePositions.Length != 5)
            {
                //WriteError
                return false;
            }
            putPositions = task.NextAddress.Split("*");
            if (putPositions.Length != 5)
            {
                //WriteError
                return false;
            }
 
            containerItem = _containerItemRepository.QueryFirst(x => x.ItemCode == task.PalletCode);
            if (containerItem == null)
            {
                //WriteError
                return false;
            }
 
            if (LightStatusStorage.LightStatusDic.TryGetValue(putPositions[0], out LightStatusEnum lightStatusDic))
            {
                if (lightStatusDic != LightStatusEnum.LightWorking)
                {
                    LightStatusStorage.LightStatusDic[putPositions[0]] = LightStatusEnum.LightWorking;
                }
            }
            return true;
        }
 
        public bool IsPositionValid(OtherDevice otherDevice, GantryCommand command)
        {
            #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}", "设备协议参数错误,未找到最大最小坐标地址");
                return false;
            }
            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 (command.TakePoX < minX || command.TakePoX > maxX)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"X取货坐标超出范围,取货坐标:{command.TakePoX},最大值:{maxX},最小值:{minX}");
                return false;
            }
 
            if (command.PutPoX < minX || command.PutPoX > maxX)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"X放货坐标超出范围,取货坐标:{command.PutPoX},最大值:{maxX},最小值:{minX}");
                return false;
            }
 
            if (command.TakePoY < minY || command.TakePoY > maxY)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Y取货坐标超出范围,取货坐标:{command.TakePoY},最大值:{maxY},最小值:{minY}");
                return false;
            }
 
            if (command.PutPoY < minY || command.PutPoY > maxY)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Y放货坐标超出范围,取货坐标:{command.PutPoY},最大值:{maxY},最小值:{minY}");
                return false;
            }
 
            if (command.TakePoZ < minZ || command.TakePoZ > maxZ)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Z取货坐标超出范围,取货坐标:{command.TakePoZ},最大值:{maxZ},最小值:{minZ}");
                return false;
            }
 
            if (command.PutPoZ < minZ || command.PutPoZ > maxZ)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"Z放货坐标超出范围,取货坐标:{command.PutPoZ},最大值:{maxZ},最小值:{minZ}");
                return false;
            }
 
            if (command.TakePoR < minR || command.TakePoR > maxR)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"R取货坐标超出范围,取货坐标:{command.TakePoR},最大值:{maxR},最小值:{minR}");
                return false;
            }
 
            if (command.PutPoR < minR || command.PutPoR > maxR)
            {
                WriteError($"{otherDevice.DeviceCode}-{otherDevice.DeviceName}-坐标", $"R放货坐标超出范围,取货坐标:{command.PutPoR},最大值:{maxR},最小值:{minR}");
                return false;
            }
 
            return true;
            #endregion
        }
    }
}