1
huangxiaoqiang
2025-04-18 b5625f1b0cbebed336918b5b34e83b2b1da16aeb
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
using HslCommunication;
using HslCommunication.Profinet.OpenProtocol;
using Mapster;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.CodeAnalysis;
using Microsoft.VisualBasic;
using MoYu.Logging;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WIDESEAWCS_Common;
using WIDESEAWCS_Common.TaskEnum;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_DTO.MOM;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_DTO.WMS;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_Tasks.ConveyorLineJob;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
 
namespace WIDESEAWCS_Tasks
{
    public partial class CommonConveyorLineJob
    {
        /// <summary>
        /// 处理出库任务
        /// </summary>
        private void HandleTaskOut(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue, Dt_Task taskOut)
        {
            if (taskOut == null) return;
            //ConveyorLineTaskCommand? taskCommand = MapTaskCommand(taskOut, command);
 
            var next = taskOut.NextAddress;
            var taskCommand = MapTaskCommand(taskOut, command);
            taskOut.NextAddress = next;
 
            bool isOutTray = taskOut.TaskType == (int)TaskOutboundTypeEnum.OutTray;
            bool isOutboundAndOutFinish = taskOut.TaskType == (int)TaskOutboundTypeEnum.Outbound && taskOut.TaskState == (int)TaskOutStatusEnum.SC_OutFinish;
            bool isOutboundAndLineOutExecuting = taskOut.TaskType == (int)TaskOutboundTypeEnum.Outbound && taskOut.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting;
 
            if (isOutTray || isOutboundAndOutFinish || !isOutboundAndLineOutExecuting)
            {
                conveyorLine.SendCommand(taskCommand, childDeviceCode);
 
                var log = $"【{conveyorLine._deviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{childDeviceCode}】请求扫码入库,下一目标地址【{taskCommand.TargetAddress}】";
                ConsoleHelper.WriteWarningLine(log);
 
                _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                WriteInfo(conveyorLine.DeviceName, log);
 
                ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                _taskService.UpdateTaskStatusToNext(taskOut);
            }
            else if (taskOut.TaskType == (int)TaskOutboundTypeEnum.OutTray && taskOut.TaskState == (int)TaskOutStatusEnum.Line_OutExecuting)
            {
                CompleteWmsTask(taskOut, command, conveyorLine, childDeviceCode, ProtocalDetailValue);
            }
        }
 
        /// <summary>
        /// 处理新任务
        /// </summary>
        public async Task HandleNewTaskAsync(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
        {
            var stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == childDeviceCode && x.stationPLC == conveyorLine.DeviceCode);
 
            switch (stationManager.stationType)
            {
                case 5:
                case 1:
                    await RequestWmsTask(conveyorLine, command, childDeviceCode, ProtocalDetailValue, stationManager);
                    break;
 
                case 2:
                case 3:
                case 4:
                case 6:
                    await CreateAndSendEmptyTrayTask(conveyorLine, command, childDeviceCode, ProtocalDetailValue, stationManager);
                    break;
 
                case 7:
                    RequestOutNextAddress(conveyorLine, command, childDeviceCode, ProtocalDetailValue);
                    break;
                case 10:
                    ConveyorLineOutFinish(conveyorLine, command, childDeviceCode, ProtocalDetailValue);
                    break;
                case 20:
                    await JZRequestInBound(conveyorLine, command, childDeviceCode, ProtocalDetailValue, stationManager);
                    break;
                default:
                    break;
            }
        }
 
        /// <summary>
        /// 映射任务命令
        /// </summary>
        private ConveyorLineTaskCommand MapTaskCommand(Dt_Task task, ConveyorLineTaskCommand command)
        {
            // 使用正则表达式匹配类似 -数字 的模式,并替换为空字符串
            task.NextAddress = Regex.Replace(task.NextAddress, @"-(\d+)", "");
            if (Convert.ToInt32(task.NextAddress) > 1999)
            {
                task.NextAddress = (Convert.ToInt32(task.NextAddress) - 1000).ToString();
            }
            var comm = _mapper.Map<ConveyorLineTaskCommand>(task);
            comm.InteractiveSignal = command.InteractiveSignal;
            return comm;
        }
 
        /// <summary>
        /// 完成WMS任务
        /// </summary>
        private void CompleteWmsTask(Dt_Task taskOut, ConveyorLineTaskCommand command, CommonConveyorLine conveyorLine, string childDeviceCode, int ProtocalDetailValue)
        {
            if (command.Barcode == "NoRead")
            {
                var NGAddress = _platFormRepository.QueryFirst(x => x.PlatCode == taskOut.TargetAddress).Capacity;
                taskOut.TargetAddress = NGAddress.ToString();
            }
 
            var keys = new Dictionary<string, object>()
            {
                {"taskNum", taskOut.TaskNum}
            };
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
            var completeTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.CompleteTask)?.ConfigValue;
            if (wmsBase == null || completeTask == null)
            {
                throw new InvalidOperationException("WMS IP 未配置");
            }
            var wmsIpAddress = wmsBase + completeTask;
 
            var result = HttpHelper.GetAsync(wmsIpAddress, keys).Result;
            WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (content.Status)
            {
                ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                _taskService.UpdateTaskStatusToNext(taskOut);
            }
        }
 
        /// <summary>
        /// 创建并发送空托盘任务
        /// </summary>
        public Task CreateAndSendEmptyTrayTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue, Dt_StationManager stationManager)
        {
            if (command.Barcode != "NoRead")
            {
                string isTrue = string.Empty;
 
                ResultTrayCellsStatus result = GetResultTrayCellsStatus(command, stationManager);
 
                List<string> strings = stationManager.Roadway.Split(",").ToList();
                foreach (string item in strings)
                {
                    isTrue = RequestInboundPlatform(item, result.ProductionLine, true);
                    if (isTrue != null)
                    {
                        break;
                    }
                }
 
                WMSTaskDTO taskDTO = null;
                if (isTrue != null && isTrue != string.Empty)
                {
                    taskDTO = new WMSTaskDTO
                    {
                        TaskNum = _taskRepository.GetTaskNo().Result,
                        Grade = 1,
                        PalletCode = command.Barcode,
                        RoadWay = childDeviceCode,
                        SourceAddress = childDeviceCode,
                        TargetAddress = isTrue,
                        TaskState = (int)TaskOutStatusEnum.SC_OutFinish,
                        Id = 2,
                        TaskType = (int)TaskOutboundTypeEnum.OutTray,
                        ProductionLine = result.ProductionLine,
                    };
                }
                else
                {
                    taskDTO = CreateEmptyTrayTaskDto(command.Barcode, childDeviceCode); ;
                }
 
                if (_taskRepository.QueryFirst(x => x.PalletCode == taskDTO.PalletCode) != null)
                {
                    WriteInfo(conveyorLine.DeviceName, "当前托盘存在任务");
                }
                CreateEmptyTryTask(conveyorLine, command, childDeviceCode, ProtocalDetailValue, taskDTO);
            }
 
            return Task.CompletedTask;
        }
        public Task CreateEmptyTryTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue, WMSTaskDTO taskDTO)
        {
            var content = CreateAndSendTask(taskDTO);
            if (content.Status)
            {
                var task = _taskService.QueryConveyorLineTask(conveyorLine.DeviceCode, childDeviceCode);
                if (task != null)
                {
                    var next = task.NextAddress;
                    var taskCommand = MapTaskCommand(task, command);
                    task.NextAddress = next;
 
                    var log = $"【{conveyorLine._deviceName}】任务号:【{command.TaskNum}】,托盘条码:【{command.Barcode}】已到达【{childDeviceCode}】请求扫码入库(空托盘),下一目标地址【{taskCommand.TargetAddress}】";
                    ConsoleHelper.WriteWarningLine(log);
 
                    _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
                    WriteInfo(conveyorLine.DeviceName, log);
 
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
                    ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
                    _taskService.UpdateTaskStatusToNext(task);
                }
            }
            return Task.CompletedTask;
        }
 
        public ResultTrayCellsStatus GetResultTrayCellsStatus(ConveyorLineTaskCommand command, Dt_StationManager stationManager)
        {
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.MOMIP_BASE)?.ConfigValue;
            var ipAddress = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.TrayCellsStatus)?.ConfigValue;
            if (wmsBase == null || ipAddress == null)
            {
                throw new InvalidOperationException("MOM IP 未配置");
            }
            TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
            {
                Software = "WMS",
                TrayBarcode = command.Barcode,
                EquipmentCode = stationManager.stationEquipMOM,
                SessionId = Guid.NewGuid().ToString(),
                EmployeeNo = "MITest",
                SceneType = "4",
                RequestTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
            };
 
            var MOMIpAddress = wmsBase + ipAddress;
 
            var result = HttpHelper.PostAsync(MOMIpAddress, trayCells.ToJsonString()).Result;
            WriteInfo("入站校验", $"【{stationManager.stationChildCode}】入站校验请求参数【{trayCells.ToJsonString()}】");
            WriteInfo("入站校验", "");
            WriteInfo("入站校验", $"【{stationManager.stationChildCode}】入站校验返回参数【{result}】");
            ResultTrayCellsStatus result1 = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(result);
            return result1;
        }
        /// <summary>
        /// 创建空托盘任务DTO
        /// </summary>
        private WMSTaskDTO CreateEmptyTrayTaskDto(string barcode, string childDeviceCode)
        {
            var request = new RequestTaskDto()
            {
                Position = childDeviceCode,
                PalletCode = barcode,
            };
 
            var config = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var wmsBase = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE)?.ConfigValue;
            var requestTrayInTask = config.FirstOrDefault(x => x.ConfigKey == SysConfigKeyConst.RequestTrayInTask)?.ConfigValue;
            if (wmsBase == null || requestTrayInTask == null)
            {
                throw new InvalidOperationException("WMS IP 未配置");
            }
            var wmsIpAddrss = wmsBase + requestTrayInTask;
            var result = HttpHelper.PostAsync(wmsIpAddrss, request.ToJsonString()).Result;
            if (result == null)
                return new WMSTaskDTO();
 
            WebResponseContent content = JsonConvert.DeserializeObject<WebResponseContent>(result);
            if (!content.Status)
                return new WMSTaskDTO();
 
            return JsonConvert.DeserializeObject<WMSTaskDTO>(content.Data.ToString());
        }
 
        /// <summary>
        /// 请求WMS任务
        /// </summary>
        private async Task RequestWmsTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue, Dt_StationManager stationManager)
        {
            ResultTrayCellsStatus result = GetResultTrayCellsStatus(command, stationManager);
 
            if (childDeviceCode == "1435")
            {
                ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
 
                var serialNosError = result.SerialNos.Where(x => x.SerialNoStatus != 1 && x.SerialNoStatus != 4).ToList();
 
                if (serialNosError.Count > 0 || !result.Success || result.SerialNos.Count == 0)
                {
                    WMSTaskDTO taskDTO = new WMSTaskDTO
                    {
                        TaskNum = _taskRepository.GetTaskNo().Result,
                        Grade = 1,
                        PalletCode = command.Barcode,
                        RoadWay = "CHSC4",
                        SourceAddress = childDeviceCode,
                        TargetAddress = "CHSC4",
                        TaskState = (int)TaskInStatusEnum.InNew,
                        Id = 2,
                        TaskType = (int)TaskInboundTypeEnum.InNG,
                        ProductionLine = result.ProductionLine,
                    };
                    var Taskcontent = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
                    if (Taskcontent.Status)
                    {
                        ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                    }
                    ConsoleHelper.WriteSuccessLine($"【{stationManager.stationRemark}】【{stationManager.stationChildCode}】{result.MOMMessage}");
                    return;
                }
                else
                {
                    var Taskcontent = await _taskService.RequestWMSTask(command.Barcode, childDeviceCode);
                    if (Taskcontent.Status)
                    {
                        ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                    }
                }
 
            }
            else
            {
                string isTrue = string.Empty;
 
                ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
 
                if (result.SerialNos.Count == 0)
                {
                    var Traycontent = await _taskService.RequestWMSTask(command.Barcode, childDeviceCode);
                    if (Traycontent.Status)
                    {
                        ConsoleHelper.WriteSuccessLine("二封空框请求回流");
                        ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                    }
                }
 
                List<string> strings = stationManager.Roadway.Split(",").ToList();
                foreach (string item in strings)
                {
                    isTrue = RequestInboundPlatform(item, result.ProductionLine, false);
                    if (isTrue != null)
                    {
                        break;
                    }
                }
                if (isTrue != null && isTrue != string.Empty)
                {
                    WMSTaskDTO taskDTO = new WMSTaskDTO
                    {
                        TaskNum = _taskRepository.GetTaskNo().Result,
                        Grade = 1,
                        PalletCode = command.Barcode,
                        RoadWay = childDeviceCode,
                        SourceAddress = childDeviceCode,
                        TargetAddress = isTrue,
                        TaskState = (int)TaskOutStatusEnum.SC_OutFinish,
                        Id = 2,
                        TaskType = (int)TaskOutboundTypeEnum.OutTray,
                        ProductionLine = result.ProductionLine,
                    };
                    var Taskcontent = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
                    if (Taskcontent.Status)
                    {
                        ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                    }
 
                }
                else
                {
                    var Taskcontent = await _taskService.RequestWMSTask(command.Barcode, childDeviceCode);
                    if (Taskcontent.Status)
                    {
                        ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                    }
                }
            }
        }
 
        /// <summary>
        /// 成化入静置
        /// </summary>
        /// <param name="conveyorLine"></param>
        /// <param name="command"></param>
        /// <param name="childDeviceCode"></param>
        /// <param name="ProtocalDetailValue"></param>
        /// <param name="stationManager"></param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        private async Task JZRequestInBound(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue, Dt_StationManager stationManager)
        {
            try
            {
                StaticVariable.isLineRun = false;
                
                if (StaticVariable.isStackerRun)
                {
                    ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                }
 
                ResultTrayCellsStatus resultTrayCellsStatus = GetResultTrayCellsStatus(command, stationManager);
 
 
                //todo判断是否为空框
                var serialNosError = resultTrayCellsStatus.SerialNos.Where(x => x.SerialNoStatus != 1 && x.SerialNoStatus != 4).ToList();
                if (serialNosError.Count > 0 || !resultTrayCellsStatus.Success)
                {
                    //NG流程
                    var platform = _platFormRepository.QueryFirst(x => x.ProductionLine == resultTrayCellsStatus.ProductionLine && x.DeviceCode == "1005");
                    ConveyorLineTaskCommand conveyorLineTaskCommand = new ConveyorLineTaskCommand()
                    {
                        TaskNum = 1,
                        TargetAddress = Convert.ToInt32(platform.Capacity),
                        Barcode = resultTrayCellsStatus.TrayBarcode,
                        InteractiveSignal = command.InteractiveSignal
                    };
                    conveyorLine.SendCommand(conveyorLineTaskCommand, childDeviceCode);
                    var logMessage = $"MOM数据异常,送至二封【{resultTrayCellsStatus.ProductionLine}】异常口【{Convert.ToInt32(platform.Capacity)}】";
                    LogAndSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, logMessage, conveyorLineTaskCommand.TargetAddress.ToString());
                    return;
                }
                if (resultTrayCellsStatus.SerialNos.Count == 0)
                {
                    var Traycontent = await _taskService.RequestWMSTask(command.Barcode, childDeviceCode);
                    if (Traycontent.Status)
                    {
                        ConsoleHelper.WriteSuccessLine("化成空框请求回流静置");
                        if (StaticVariable.isStackerRun)
                        {
                            ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                        }
                        return;
                    }
                }
                else
                {
                    var configz = _sys_ConfigService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                    var wmsbase = configz.Where(x => x.ConfigKey == SysConfigKeyConst.WMSIP_BASE).FirstOrDefault()?.ConfigValue;
                    var address = configz.Where(x => x.ConfigKey == SysConfigKeyConst.QueryStockInfoForRealTrayJZAsync).FirstOrDefault()?.ConfigValue;
                    if (wmsbase == null || address == null)
                    {
                        throw new InvalidOperationException("WMS IP 未配置");
                    }
                    var wmsIpAddrss = wmsbase + address;
                    var result = await HttpHelper.PostAsync(wmsIpAddrss, new { ProductLine = resultTrayCellsStatus.ProductionLine, PalletCode = command.Barcode }.ToJsonString());
                    var StockInfocontent = JsonConvert.DeserializeObject<WebResponseContent>(result);
                    if (StockInfocontent.Status)
                    {
                        var Taskcontent = await _taskService.RequestWMSTask(command.Barcode, childDeviceCode);
                        ConsoleHelper.WriteErrorLine($"{JsonConvert.SerializeObject(Taskcontent)}");
                        if (Taskcontent.Status)
                        {
                            if (StaticVariable.isStackerRun)
                            {
                                ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                            }
                        }
                        else
                        {
                            WriteInfo(conveyorLine.DeviceName, Taskcontent.Message);
                            return;
                        }
                    }
                    else
                    {
                        string isTrue = string.Empty;
 
                        List<string> strings = stationManager.Roadway.Split(",").ToList();
                        foreach (string item in strings)
                        {
                            isTrue = RequestInboundPlatform(item, resultTrayCellsStatus.ProductionLine, false);
                            if (isTrue != null)
                            {
                                break;
                            }
                        }
                        if (isTrue != null && isTrue != string.Empty)
                        {
                            WMSTaskDTO taskDTO = new WMSTaskDTO
                            {
                                TaskNum = _taskRepository.GetTaskNo().Result,
                                Grade = 1,
                                PalletCode = command.Barcode,
                                RoadWay = isTrue,
                                SourceAddress = childDeviceCode,
                                TargetAddress = isTrue,
                                TaskState = (int)TaskOutStatusEnum.SC_OutFinish,
                                Id = 2,
                                TaskType = (int)TaskOutboundTypeEnum.InToOut,
                                ProductionLine = resultTrayCellsStatus.ProductionLine,
                            };
                            var Taskcontent = _taskService.ReceiveWMSTask(new List<WMSTaskDTO> { taskDTO });
                            if (Taskcontent.Status)
                            {
                                if (StaticVariable.isStackerRun)
                                {
                                    ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                                }
                                return;
                            }
                            else
                            {
                                WriteInfo(conveyorLine.DeviceName, Taskcontent.Message);
                                return;
                            }
                        }
                        else
                        {
                            var Task = await _taskService.RequestWMSTask(command.Barcode, childDeviceCode);
                            ConsoleHelper.WriteErrorLine($"{JsonConvert.SerializeObject(Task)}");
                            if (Task.Status)
                            {
                                if (StaticVariable.isStackerRun)
                                {
                                    ExecuteConveyorLineTask(conveyorLine, command, ProtocalDetailValue, childDeviceCode);
                                }
                            }
                            ConsoleHelper.WriteWarningLine("二封缓存位已满");
                            return;
                        }
 
                    }
                }
            }
            catch (Exception ex)
            {
                ConsoleHelper.WriteWarningLine($"{ex.Message}");
            }
            finally
            {
                StaticVariable.isLineRun = true;
            }       
        }
 
        /// <summary>
        /// 执行输送线任务
        /// </summary>
        /// <param name="conveyorLine">输送线对象</param>
        /// <param name="command">输送线任务命令</param>
        /// <param name="ProtocalDetailValue">协议明细值</param>
        /// <param name="childDeviceCode">子设备编号</param>
        public void ExecuteConveyorLineTask(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, int ProtocalDetailValue, string childDeviceCode)
        {
            // 查询任务信息
            var task = _taskService.QueryBarCodeConveyorLineTask(command.Barcode, childDeviceCode);
 
            if (task == null) return;
 
            // 获取配置值并转换为列表
            var value = _sys_ConfigService.GetByConfigKey(CateGoryConst.CONFIG_SYS_InStation, SysConfigKeyConst.JZNGInBoundStation).ConfigValue;
            var valueList = value.Split(',').ToList();
 
            // 日志模板
            var logMessage = $"【{conveyorLine._deviceName}】任务号:【{task.TaskNum}】,托盘条码:【{task.PalletCode}】已到达【{childDeviceCode}】请求扫码入库(实盘),下一目标地址【{{0}}】";
 
            // 判断任务的起始地址是否在配置列表中
            if (valueList.Contains(task.SourceAddress))
            {
                // 设置目标地址为 "1000"
                conveyorLine.SetValue(ConveyorLineDBName.WriteConveyorLineTargetAddress, "1000", childDeviceCode);
 
                // 记录日志并发送完成信号
                LogAndSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, logMessage, "1000");
            }
            else if (task.Roadway.Contains("JZ"))
            {
                // 查询是否存在静置出库任务
                var outJZTask = _taskRepository.QueryData(x => x.Roadway == task.Roadway &&
                    (x.TaskState == (int)TaskOutStatusEnum.SC_OutExecuting ||
                     x.TaskState == (int)TaskOutStatusEnum.SC_OutFinish || x.TaskState == (int)TaskOutStatusEnum.OutNew));
 
                if (!outJZTask.Any())
                {
                    // 映射任务命令
                    var taskCommand = MapTaskCommand(task, command);
 
                    // 发送任务命令
                    conveyorLine.SendCommand(taskCommand, childDeviceCode);
 
                    // 记录日志并发送完成信号
                    LogAndSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, logMessage, taskCommand.TargetAddress.ToString());
 
                    // 更新任务状态
                    _taskService.UpdateTaskStatusToNext(task);
                }
                else
                {
                    ConsoleHelper.WriteErrorLine("已存在静置出库任务,静置入库任务无法下发至线体");
                }
            }
            else
            {
                // 映射任务命令
                var taskCommand = MapTaskCommand(task, command);
 
                // 发送任务命令
                conveyorLine.SendCommand(taskCommand, childDeviceCode);
 
                // 记录日志并发送完成信号
                LogAndSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, logMessage, taskCommand.TargetAddress.ToString());
 
                // 更新任务状态
                _taskService.UpdateTaskStatusToNext(task);
            }
        }
 
        /// <summary>
        /// 记录日志并发送完成信号
        /// </summary>
        /// <param name="conveyorLine">输送线对象</param>
        /// <param name="childDeviceCode">子设备编号</param>
        /// <param name="ProtocalDetailValue">协议明细值</param>
        /// <param name="logMessage">日志消息模板</param>
        /// <param name="targetAddress">目标地址</param>
        private void LogAndSendFinish(CommonConveyorLine conveyorLine, string childDeviceCode, int ProtocalDetailValue, string logMessage, string targetAddress)
        {
            // 格式化日志消息
            var log = string.Format(logMessage, targetAddress);
            // 输出警告日志
            ConsoleHelper.WriteWarningLine(log);
            // 记录日志
            _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log, time = DateTime.Now.ToString("G"), color = "red" });
            WriteInfo(conveyorLine.DeviceName, log);
            // 发送完成信号
            ConveyorLineSendFinish(conveyorLine, childDeviceCode, ProtocalDetailValue, true);
        }
 
    }
}