1
huanghongfeng
2025-09-12 a1a0c91857b670152ec7d4afa7bc4140953a5e98
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
using Autofac.Core;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Quartz;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.Log;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_ITaskInfoRepository;
using WIDESEAWCS_ITaskInfoService;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_Model.Models.System;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.DeviceBase;
using WIDESEAWCS_QuartzJob.DTO;
using WIDESEAWCS_QuartzJob.Models;
using WIDESEAWCS_QuartzJob.Service;
using WIDESEAWCS_QuartzJob.StackerCrane.Enum;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using WIDESEAWCS_Tasks.StackerCraneJob;
 
namespace WIDESEAWCS_Tasks
{
    [DisallowConcurrentExecution]
    public class CommonStackerCraneJob : IJob
    {
        private readonly ITaskService _taskService;
        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
        private readonly ITaskRepository _taskRepository;
        private readonly IRouterService _routerService;
        private readonly IPlatformStationService _PlatformStationService;
 
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IPlatformStationService platformStation)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _PlatformStationService= platformStation;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonStackerCrane != null)
                {
                    RequestOutIn(commonStackerCrane);
                    if (commonStackerCrane.Communicator.Read<bool>("DB37.32"))  //读取堆垛机允许信号
                    {
                        Dt_Task? task = GetTask(commonStackerCrane);
                        if (task != null)
                        {
                            StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                            if (stackerCraneTaskCommand != null)
                            {
                                commonStackerCrane.Communicator.Write("DB36.0", (string)task.PalletCode);
                                bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                                if (sendFlag)
                                {
                                    _taskService.UpdateTaskStatusToNext(task.TaskNum);
                                }
                            }
                        }
                    }
                    /*else
                    {
                        
                        WriteLog.Write_Log("堆垛机", "堆垛机信号", "允许信号", new { 信息 = "未读取到堆垛机允许信号" });
                    }*/
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
        }
 
 
 
        /// <summary>
        /// 获取任务进行下发
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        public void RequestInbound(CommonStackerCrane commonStackerCrane)
        {
            try
            {
                Dt_Task? task = GetTask(commonStackerCrane);
                if (task != null)
                {
                    StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                    if (stackerCraneTaskCommand != null)
                    {
                        bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                        if (sendFlag)
                        {
                            _taskService.UpdateTaskStatusToNext(task.TaskNum);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 
        /// <summary>
        /// 放货完成信号
        /// </summary>
        /// <param name="conveyorLine">输送线实例对象</param>
        public void RequestOutIn(CommonStackerCrane commonStackerCrane)
        {
            try
            {
                bool StackerStb = commonStackerCrane.Communicator.Read<bool>("DB37.32.2");
                if (StackerStb && commonStackerCrane.CurrentTaskNum !=0)
                {
                    _taskService.StackCraneTaskCompleted(commonStackerCrane.CurrentTaskNum);
 
                    commonStackerCrane.Communicator.Write("DB37.33.6", (bool)true);
                    WriteLog.Write_Log("堆垛机", "堆垛机信号", "堆垛机放货完成信号,正确信息", new { 信息 = $"读取到堆垛机放货完成信号:{StackerStb},任务号:{commonStackerCrane.CurrentTaskNum},写入堆垛机反馈失败信号:true" });
                }
                else
                {
                    commonStackerCrane.Communicator.Write("DB37.33.6", (bool)false);
                    WriteLog.Write_Log("堆垛机", "堆垛机信号", "堆垛机放货完成信号,错误信息", new { 信息 = $"读取到堆垛机放货完成信号:{StackerStb},任务号:{commonStackerCrane.CurrentTaskNum},写入堆垛机反馈失败信号:false" });
                    
                }
               
            }
            catch (Exception ex)
            {
                throw;
            }
        }
 
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="commonStackerCrane">堆垛机对象</param>
        /// <returns></returns>
        private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
        {
            List<Dt_Task> task;
            task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
            if(task.Count > 0)
            {
                foreach (Dt_Task taskItem in task)
                {
                    if (task != null && (taskItem.TaskType == (int)TaskOutboundTypeEnum.Outbound || taskItem.TaskType == (int)TaskOtherTypeEnum.PalletMaintenanceout))
                    {
                        if (OutTaskStationIsOccupied(taskItem) != null) 
                        { 
 
                            return taskItem;
                        }
                    }
                    else if(task != null && (taskItem.TaskType == (int)TaskInboundTypeEnum.Inbound || taskItem.TaskType == (int)TaskOtherTypeEnum.PalletMaintenancein))
                    {
                        return taskItem;
                    }
                    
                }
            }
            return null;
        }
 
        /// <summary>
        /// 出库任务判断出库站台是否被占用
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private Dt_Task? OutTaskStationIsOccupied([NotNull] Dt_Task task)
        {
            IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1002");
            if (device != null)
            {
                CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
                {
                    DeviceProDTO? deviceProDTO = GetDeviceProDTO(conveyorLine, task.NextAddress, "StationFree");
                    if (deviceProDTO != null)
                    {
                        bool B_Event_Test = GetLine(conveyorLine, deviceProDTO.DeviceProAddress);        //判断输送线是否允许放货信号
                        if (B_Event_Test)
                        {
                            return task;
                        }
                    }
                }
                else if (task.TaskType == (int)TaskOtherTypeEnum.PalletMaintenanceout)
                {
                    DeviceProDTO? deviceProDTO3 = GetDeviceProDTO(conveyorLine, "1060", "StationFree");
                    if (deviceProDTO3 != null)
                    {
                        bool B_Event_Test3 = GetLine(conveyorLine, deviceProDTO3.DeviceProAddress);        //判断输送线是否允许放货信号
                        if (B_Event_Test3)
                        {
                            DeviceProDTO? deviceProDTO = GetDeviceProDTO(conveyorLine, task.NextAddress, "StationFree");
                            if (deviceProDTO != null)
                            {
                                bool B_Event_Test = GetLine(conveyorLine, deviceProDTO.DeviceProAddress);        //判断输送线是否允许放货信号
                                
                                if (B_Event_Test)
                                {
                                    WriteLog.Write_Log("检修台任务下发", task.NextAddress + "站台", $"条码:{task.PalletCode}", new { 信息 = $"读取到输送线站台信号为:{B_Event_Test}" });
                                    return task;
                                }
                                else
                                {
                                    //编辑查找逻辑
                                    string StaticAddress = _PlatformStationService.GetOutSCName(task.NextAddress);
                                    DeviceProDTO? deviceProDTO2 = GetDeviceProDTO(conveyorLine, StaticAddress, "StationFree");
                                    bool B_Event2 = GetLine(conveyorLine, deviceProDTO2.DeviceProAddress);
                                    
                                    if (B_Event2)
                                    {
                                        WriteLog.Write_Log("检修台任务下发", task.NextAddress + "站台", $"条码:{task.PalletCode}", new { 信息 = $"读取到输送线站台信号为:{B_Event2}" });
                                        return task;
                                    }
                                }
                            }
                        }
                    }
 
                    
                    
                }
            }
            
            return null;
        }
 
        
        /// <summary>
        /// 任务实体转换成命令Model
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public StackerCraneTaskCommand? ConvertToStackerCraneTaskCommand([NotNull] Dt_Task task)
        {
            StackerCraneTaskCommand stackerCraneTaskCommand = new StackerCraneTaskCommand();
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.InboundGroup || task.TaskType == (int)TaskOtherTypeEnum.PalletMaintenancein)//判断是否是入库任务
            {
                int coty = 1;
                /*if(task.TaskType == (int)TaskOtherTypeEnum.PalletMaintenancein)
                {
                    coty = 2;
                }*/
                //根据当前地址,查找堆垛机货位地址
                task.CurrentAddress = _PlatformStationService.GetSCAdder(int.Parse(task.CurrentAddress), coty);
 
                string[] targetCodest = task.CurrentAddress.Split("-");
                if (targetCodest.Length == 3)
                {
                    targetCodest[0] = (targetCodest[0] == "A") ? "001" : "002";
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(targetCodest[0]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(targetCodest[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(targetCodest[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库起点错误,起点:【{task.SourceAddress}】");
                    return null;
                }
                string[] targetCodes = task.NextAddress.Split("-");
                if (targetCodes.Length == 3)
                {
                    targetCodes[0] = (targetCodes[0] == "A") ? "001" : "002";
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(targetCodes[0]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(targetCodes[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(targetCodes[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
                    return null;
                }
            }
            else if (task.TaskType.GetTaskTypeGroup() == TaskTypeGroup.OutbondGroup || task.TaskType == (int)TaskOtherTypeEnum.PalletMaintenanceout)
            {
                int coty = 1;
                if (task.TaskType == (int)TaskOtherTypeEnum.PalletMaintenanceout)
                {
                    coty = 2;
                }
                task.NextAddress = _PlatformStationService.GetSCAdder(int.Parse(task.NextAddress), coty);
 
                string[] sourceCodes = task.SourceAddress.Split("-");
                
                if (sourceCodes.Length == 3)
                {
                    sourceCodes[0] = (sourceCodes[0] == "A") ? "001" : "002";
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(sourceCodes[0]);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
                string[] sourceCodest = task.NextAddress.Split("-");
                
                if (sourceCodest.Length == 3)
                {
 
                    sourceCodest[0] = (sourceCodest[0] == "A") ? "001" : "002";
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(sourceCodest[0]);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(sourceCodest[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(sourceCodest[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务终点错误,起点:【{task.TargetAddress}】");
                    return null;
                }
            }
            
            return stackerCraneTaskCommand;
        }
 
        //根地址读取输送线信息
        public bool GetLine(CommonConveyorLine conveyorLine, string DeviceProDataBlock)
        {
            return conveyorLine.Communicator.Read<bool>(DeviceProDataBlock);
        }
        //获取输送线实例
        public DeviceProDTO? GetDeviceProDTO(CommonConveyorLine conveyorLine, string SCAddress, string Interactivet)
        {
            return conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == SCAddress && x.DeviceProParamName == Interactivet);
        }
 
        
    }
}