zhangchengsong
2026-04-01 ee3bdbf31d599672fc3427a66655412693e14096
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
using Autofac.Core;
using HslCommunication;
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.Net;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
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.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 IDt_PlatformStationService _DtPlatformStationService;
 
        public CommonStackerCraneJob(ITaskService taskService, ITaskExecuteDetailService taskExecuteDetailService, ITaskRepository taskRepository, IRouterService routerService, IDt_PlatformStationService dt_PlatformStationService)
        {
            _taskService = taskService;
            _taskExecuteDetailService = taskExecuteDetailService;
            _taskRepository = taskRepository;
            _routerService = routerService;
            _DtPlatformStationService= dt_PlatformStationService;
        }
 
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                CommonStackerCrane commonStackerCrane = (CommonStackerCrane)context.JobDetail.JobDataMap.Get("JobParams");
                if (commonStackerCrane != null)
                {
                    //任务完成StackerCraneWorkStatus
                    if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.WorkCompleted)
                    {
                        //判断当前任务是否是盘点
                        Dt_Task? currtask = _taskService.CurrtaskInfo(commonStackerCrane.CurrentTaskNum);
                        if(currtask != null)
                        {
                            string PalletCode = "";
                            //判断任务类型
                            if (currtask.TaskType==  (int)TaskTypeEnum.Inventorybound)
                            {
                                //读取条码,进行配对
                                
                                DeviceProDTO? deviceProDTO = commonStackerCrane.DeviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == "SCBarcode");
                                byte[] bytes = commonStackerCrane.Communicator.Read(deviceProDTO.DeviceProAddress, 64);
                                //读取的托盘条码
                                PalletCode = Encoding.ASCII.GetString(bytes).Replace(" ", "");
                            }
 
                            WebResponseContent webResponse = _taskService.StackCraneTaskCompleted(commonStackerCrane.CurrentTaskNum, PalletCode);
                            if (webResponse.Status)
                            {
                                bool ddjfk = commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                            }
                        }
                    }
 
 
                    if (commonStackerCrane.StackerCraneAutoStatusValue == StackerCraneAutoStatus.Automatic && commonStackerCrane.StackerCraneStatusValue == StackerCraneStatus.Normal)
                    {
                        if (commonStackerCrane.StackerCraneWorkStatusValue == StackerCraneWorkStatus.Standby)
                        {
 
                            Dt_Task? task = GetTask(commonStackerCrane);
                            if (task != null)
                            {
                                StackerCraneTaskCommand? stackerCraneTaskCommand = ConvertToStackerCraneTaskCommand(task);
                                if (stackerCraneTaskCommand != null)
                                {
                                    bool sendFlag = commonStackerCrane.SendCommand(stackerCraneTaskCommand);
                                    if (sendFlag)
                                    {
                                        commonStackerCrane.Communicator.Write("DB105.52", 1);
 
                                        WebResponseContent webResponseContent=_taskService.UpdateTaskStatusToNext(task.TaskNum);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(nameof(CommonStackerCraneJob) + ":" + ex.ToString());
            }
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 任务完成事件订阅的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommonStackerCrane_StackerCraneTaskCompletedEventHandler(object? sender, WIDESEAWCS_QuartzJob.StackerCrane.StackerCraneTaskCompletedEventArgs e)
        {
            CommonStackerCrane? commonStackerCrane = sender as CommonStackerCrane;
            if (commonStackerCrane != null)
            {
                if (commonStackerCrane.GetValue<StackerCraneDBName, short>(StackerCraneDBName.WorkType) != 5 && e.TaskNum !=0)
                {
                    //Console.Out.WriteLine("TaskCompleted" + e.TaskNum);
                    _taskService.StackCraneTaskCompleted(e.TaskNum);
                    commonStackerCrane.SetValue(StackerCraneDBName.WorkType, 5);
                }
            }
        }
 
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <param name="commonStackerCrane">堆垛机对象</param>
        /// <returns></returns>
        private Dt_Task? GetTask(CommonStackerCrane commonStackerCrane)
        {
            try 
            {
                //判断当前堆垛机是否有在执行的任务
                if (_taskService.CutStackerCraneTask(commonStackerCrane.DeviceCode) != null) return null;
 
 
                List<Dt_Task> task = _taskService.QueryStackerCraneTask(commonStackerCrane.DeviceCode);
                if (task.Count > 0)
                {
                    foreach (Dt_Task taskItem in task)
                    {
                        if (taskItem.TaskType == (int)TaskTypeEnum.Outbound)
                        {
                            if (OutTaskStationIsOccupied(taskItem.NextAddress,taskItem.Roadway))
                            {
                                return taskItem;
                            }
                        }
                        else if (taskItem.TaskType == (int)TaskTypeEnum.Inbound || taskItem.TaskType == (int)TaskTypeEnum.Inventorybound)
                        {
                            return taskItem;
                        }
                    }
                }
 
                return null;
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("任务查找", "任务查错误", "错误原因", new { 信息 = ex.Message });
                return null;
            } 
        }
 
        /// <summary>
        /// 出库任务判断出库站台是否被占用
        /// </summary>
        /// <param name="task">任务实体</param>
        /// <returns>如果未被占用,返回传入的任务信息,否则,返回null</returns>
        private bool OutTaskStationIsOccupied(string NextAddress,string Roadway)
        {
            try
            {
                if (Roadway == "C")
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1004");
                    if (device == null) return false;
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "StationNum");
                    DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "HandShake");
                    if (deviceProDTO == null || HandShakeprodto == null) return false;
 
                    int StationNum = conveyorLine.Communicator.Read<ushort>(deviceProDTO.DeviceProAddress);
                    int HandShake = conveyorLine.Communicator.Read<ushort>(HandShakeprodto.DeviceProAddress);
                    if (StationNum == 0 && HandShake == 0 && Roadway == "C")
                    {
                        return true;
                    }
                }
                else
                {
                    IDevice? device = Storage.Devices.FirstOrDefault(x => x.DeviceCode == "1003");
                    if (device == null) return false;
                    CommonConveyorLine conveyorLine = (CommonConveyorLine)device;
                    DeviceProDTO? deviceProDTO = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "StationNum");
                    DeviceProDTO? HandShakeprodto = conveyorLine.DeviceProDTOs.FirstOrDefault(x => x.DeviceChildCode == NextAddress && x.DeviceProParamName == "HandShake");
                    if (deviceProDTO == null || HandShakeprodto == null) return false;
 
                    int StationNum = conveyorLine.Communicator.Read<ushort>(deviceProDTO.DeviceProAddress);
                    int HandShake = conveyorLine.Communicator.Read<ushort>(HandShakeprodto.DeviceProAddress);
                    if ((StationNum == 1 && HandShake == 0 && Roadway == "B") || (StationNum == 0 && HandShake == 0 && Roadway != "B"))
                    {
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取站台信息错误:"+ex.Message);
            }
 
            return false;
        }
 
 
        /// <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.Barcode = task.PalletCode;
            stackerCraneTaskCommand.TaskNum = task.TaskNum;
            stackerCraneTaskCommand.WorkType = 1;
            stackerCraneTaskCommand.TrayType = 0;
            if (task.TaskType == (int)TaskTypeEnum.Inbound)//判断是否是入库任务
            {
                //获取站台编号
                Dt_PlatformStation dt_Platform=_DtPlatformStationService.getStatiomInfo(task.SourceAddress);
 
                string[] targetCodest = dt_Platform.Station_storey.Split("-");
                if (targetCodest.Length == 3)
                {
                    string StartRow = targetCodest[0].Substring(1);
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(StartRow);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(targetCodest[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(targetCodest[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库起点错误,起点:【{task.SourceAddress}】");
                    return null;
                }
                string[] EndCodest = task.NextAddress.Split("-");
                if (EndCodest.Length == 3)
                {
                    string EndRow = EndCodest[0].Substring(1);
                    string SCEndRow = LaneModification(task.Roadway, int.Parse(EndRow));
 
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(SCEndRow );
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(EndCodest[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"入库任务终点错误,起点:【{task.NextAddress}】");
                    return null;
                }
            }
            else if (task.TaskType == (int)TaskTypeEnum.Outbound)
            {
                string[] sourceCodes = task.CurrentAddress.Split("-");
                if (sourceCodes.Length == 3)
                {
                    string StartRow = sourceCodes[0].Substring(1);
                    string SCStartRow = LaneModification(task.Roadway, int.Parse(StartRow));
                    stackerCraneTaskCommand.StartRow = Convert.ToInt16(SCStartRow);
                    stackerCraneTaskCommand.StartColumn = Convert.ToInt16(sourceCodes[1]);
                    stackerCraneTaskCommand.StartLayer = Convert.ToInt16(sourceCodes[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
 
                //获取站台编号
                Dt_PlatformStation dt_Platform = _DtPlatformStationService.getStatiomInfo(task.NextAddress);
                string[] EndCodest = dt_Platform.Station_storey.Split("-");
                if (EndCodest.Length == 3)
                {
                    string EndRow = EndCodest[0].Substring(1);
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(EndRow);
                    stackerCraneTaskCommand.EndColumn = Convert.ToInt16(EndCodest[1]);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
                }
                else
                {
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"出库任务终点错误,起点:【{task.TargetAddress}】");
                    return null;
                }
            }
            else if (task.TaskType == (int)TaskTypeEnum.Inventorybound)
            {
                stackerCraneTaskCommand.WorkType = 6;
 
                stackerCraneTaskCommand.StartRow = Convert.ToInt16(0);
                stackerCraneTaskCommand.StartColumn = Convert.ToInt16(0);
                stackerCraneTaskCommand.StartLayer = Convert.ToInt16(0);
 
                string[] EndCodest = task.NextAddress.Split("-");
                if (EndCodest.Length == 3)
                {
                    string EndRow = EndCodest[0].Substring(1);
                   string SCEnRow=LaneModification(task.Roadway,int.Parse(EndRow));
 
                    stackerCraneTaskCommand.EndRow = Convert.ToInt16(SCEnRow);
                    stackerCraneTaskCommand.EndColumn = (short)(Convert.ToInt16(EndCodest[1]) + 1);
                    stackerCraneTaskCommand.EndLayer = Convert.ToInt16(EndCodest[2]);
                }
                else
                {
                    
                    //数据配置错误
                    _taskService.UpdateTaskExceptionMessage(task.TaskNum, $"移库任务起点错误,起点:【{task.CurrentAddress}】");
                    return null;
                }
 
               
            }
            return stackerCraneTaskCommand;
        }
 
        public string LaneModification(string Roadway,int SCRow)
        {
            if (Roadway == "G")
            {
                return SCRow == 1 ? "3" : "4";
            }else if(Roadway == "C")
            {
                return SCRow == 1 ? "5" : "6";
            }
            else
            {
                return SCRow.ToString();
            }
        }
    }
}