From a05bbc36d31b8745fad325aa677c29f03d176718 Mon Sep 17 00:00:00 2001
From: 肖洋 <cathay_xy@163.com>
Date: 星期四, 16 一月 2025 10:20:56 +0800
Subject: [PATCH] 更新

---
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/ExceptionPortStationHandler.cs |   41 ++++++
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs                      |  183 +++++++++++++++++++-----------
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/IStationHandler.cs             |   15 ++
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs                                                   |    2 
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/StationHandlerFactory.cs       |   27 ++++
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/EmptyTrayStationHandler.cs     |   33 +++++
 Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/Task/RequestInbound.cs                        |    2 
 7 files changed, 236 insertions(+), 67 deletions(-)

diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs
index 3666ec1..043d29a 100644
--- a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs
@@ -17,6 +17,7 @@
 using WIDESEAWCS_QuartzJob.QuartzExtensions;
 using WIDESEAWCS_QuartzJob.Seed;
 using WIDESEAWCS_SignalR;
+using WIDESEAWCS_Tasks;
 using WIDESEAWCS_WCSServer.Filter;
 
 var builder = WebApplication.CreateBuilder(args);
@@ -81,6 +82,7 @@
 builder.Services.AddIpPolicyRateLimitSetup(builder.Configuration);//IPLimit限流 启动服务
 
 builder.Services.AddScoped<UseServiceDIAttribute>();
+builder.Services.AddScoped<CommonConveyorLineJob>();
 
 builder.Services.AddSession();
 
diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs
index 5104d55..099a9bb 100644
--- a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/CommonConveyorLineJob.cs
@@ -77,79 +77,115 @@
                 CommonConveyorLine conveyorLine = (CommonConveyorLine)context.JobDetail.JobDataMap.Get("JobParams");
                 if (conveyorLine != null)
                 {
+                    ConsoleHelper.WriteColorLine("寮�濮嬶細" + DateTime.Now, ConsoleColor.Cyan);
                     // 鏌ヨ鎵�鏈夊瓙璁惧鐨勪綅缃�
                     List<string> childDeviceCodes = _routerService.QueryAllPositions(conveyorLine.DeviceCode);
+                    // 鑾峰彇鎵�鏈夌珯鐐圭鐞嗗櫒
+                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
 
                     // 骞惰澶勭悊姣忎釜瀛愯澶�
                     var tasks = childDeviceCodes.Select(childDeviceCode => ProcessDeviceAsync(conveyorLine, childDeviceCode)).ToList();
+
+                    // 骞惰澶勭悊姣忎釜绔欑偣绠$悊鍣�
+                    tasks = stationManagers.Select(station => Task.Run(async () =>
+                    {
+                        var (command, commandWrite) = ReadCommands(station, conveyorLine);
+                        if (command == null || commandWrite == null)
+                        {
+                            return;
+                        }
+
+                        IStationHandler handler = StationHandlerFactory.GetHandler(station.stationType, this);
+                        await handler.HandleStationAsync(conveyorLine, station, command, commandWrite);
+                    })).ToList();
+
+                    #region
+                    //foreach (var station in stationManagers)
+                    //{
+                    //    #region
+                    //    //tasks.Add(Task.Run(async () =>
+                    //    //{
+                    //    //    if (station.stationType == 11)
+                    //    //    {
+                    //    //        // 璇诲彇浠诲姟鍛戒护鍜岃澶囧懡浠�
+                    //    //        ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
+                    //    //        ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
+                    //    //        if (command != null && commandWrite != null)
+                    //    //        {
+                    //    //            // 灏嗕氦浜掍俊鍙疯浆鎹负甯冨皵鏁扮粍
+                    //    //            var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
+                    //    //            if (structs[0] == true)
+                    //    //            {
+                    //    //                // 杈撳嚭璀﹀憡淇℃伅
+                    //    //                var log = $"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戠┖鎵樼洏璇锋眰鎵爜鍏ュ簱";
+                    //    //                ConsoleHelper.WriteWarningLine(log);
+
+                    //    //                await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
+                    //    //                WriteInfo(conveyorLine.DeviceName, log);
+
+                    //    //                // 澶勭悊鍏ュ簱璇锋眰
+                    //    //                NGRequestTaskInbound(conveyorLine, command, station, 0);
+                    //    //            }
+                    //    //            else
+                    //    //            {
+                    //    //                // 鍙戦�佸畬鎴愪俊鍙�
+                    //    //                ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
+                    //    //            }
+                    //    //        }
+                    //    //    }
+                    //    //    else if (station.stationType == 5)
+                    //    //    {
+                    //    //        // 璇诲彇浠诲姟鍛戒护鍜岃澶囧懡浠�
+                    //    //        ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
+                    //    //        ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
+                    //    //        if (command != null && commandWrite != null)
+                    //    //        {
+                    //    //            // 灏嗕氦浜掍俊鍙疯浆鎹负甯冨皵鏁扮粍
+                    //    //            var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
+                    //    //            if (structs[0] == true)
+                    //    //            {
+                    //    //                // 妫�鏌ユ槸鍚﹀瓨鍦ㄤ换鍔�
+                    //    //                if (_taskRepository.QueryData(x => x.SourceAddress == station.stationChildCode).Count() > 0)
+                    //    //                {
+                    //    //                    return;
+                    //    //                }
+                    //    //                // 杈撳嚭璀﹀憡淇℃伅
+                    //    //                var log = $"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戝紓甯稿彛璇锋眰鎵爜鍏ュ簱";
+                    //    //                ConsoleHelper.WriteWarningLine(log);
+
+                    //    //                await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
+                    //    //                WriteInfo(conveyorLine.DeviceName, log);
+
+                    //    //                // 澶勭悊鏂颁换鍔�
+                    //    //                await HandleNewTaskAsync(conveyorLine, command, station.stationChildCode, 0);
+                    //    //            }
+                    //    //            else
+                    //    //            {
+                    //    //                // 鍙戦�佸畬鎴愪俊鍙�
+                    //    //                ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
+                    //    //            }
+                    //    //        }
+                    //    //    }
+                    //    //}));
+                    //    #endregion
+
+                    //    tasks.Add(Task.Run(async () =>
+                    //    {
+                    //        var (command, commandWrite) = ReadCommands(station, conveyorLine);
+                    //        if (command == null || commandWrite == null)
+                    //        {
+                    //            return;
+                    //        }
+
+                    //        IStationHandler handler = StationHandlerFactory.GetHandler(station.stationType, this);
+                    //        await handler.HandleStationAsync(conveyorLine, station, command, commandWrite);
+                    //    }));
+                    //}
+                    #endregion
+
                     await Task.WhenAll(tasks);
 
-                    // 鑾峰彇鎵�鏈夌珯鐐圭鐞嗗櫒
-                    List<Dt_StationManager> stationManagers = _stationManagerService.GetAllStationByDeviceCode(conveyorLine.DeviceCode);
-                    foreach (var station in stationManagers)
-                    {
-                        if (station.stationType == 11)
-                        {
-                            // 璇诲彇浠诲姟鍛戒护鍜岃澶囧懡浠�
-                            ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
-                            ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
-                            if (command != null && commandWrite != null)
-                            {
-                                // 灏嗕氦浜掍俊鍙疯浆鎹负甯冨皵鏁扮粍
-                                var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
-                                if (structs[0] == true)
-                                {
-                                    // 杈撳嚭璀﹀憡淇℃伅
-                                    var log = $"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戠┖鎵樼洏璇锋眰鎵爜鍏ュ簱";
-                                    ConsoleHelper.WriteWarningLine(log);
-
-                                    await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
-                                    WriteInfo(conveyorLine.DeviceName, log);
-
-                                    // 澶勭悊鍏ュ簱璇锋眰
-                                    NGRequestTaskInbound(conveyorLine, command, station, 0);
-                                }
-                                else
-                                {
-                                    // 鍙戦�佸畬鎴愪俊鍙�
-                                    ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
-                                }
-                            }
-                        }
-                        else if (station.stationType == 5)
-                        {
-                            // 璇诲彇浠诲姟鍛戒护鍜岃澶囧懡浠�
-                            ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
-                            ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
-                            if (command != null && commandWrite != null)
-                            {
-                                // 灏嗕氦浜掍俊鍙疯浆鎹负甯冨皵鏁扮粍
-                                var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
-                                if (structs[0] == true)
-                                {
-                                    // 妫�鏌ユ槸鍚﹀瓨鍦ㄤ换鍔�
-                                    if (_taskRepository.QueryData(x => x.SourceAddress == station.stationChildCode).Count() > 0)
-                                    {
-                                        return;
-                                    }
-                                    // 杈撳嚭璀﹀憡淇℃伅
-                                    var log = $"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戝紓甯稿彛璇锋眰鎵爜鍏ュ簱";
-                                    ConsoleHelper.WriteWarningLine(log);
-
-                                    await _noticeService.Logs(userTokenIds, new { conveyorLine.DeviceName, log = log, time = DateTime.Now.ToString("G"), color = "red" });
-                                    WriteInfo(conveyorLine.DeviceName, log);
-
-                                    // 澶勭悊鏂颁换鍔�
-                                    await HandleNewTaskAsync(conveyorLine, command, station.stationChildCode, 0);
-                                }
-                                else
-                                {
-                                    // 鍙戦�佸畬鎴愪俊鍙�
-                                    ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
-                                }
-                            }
-                        }
-                    }
+                    ConsoleHelper.WriteColorLine("缁撴潫锛�" + DateTime.Now, ConsoleColor.Cyan);
                 }
             }
             catch (Exception ex)
@@ -853,5 +889,20 @@
         }
 
         #endregion 璁惧NG鍙e叆搴�
+
+        public async Task LogAndWarn(string deviceName, string log, string color = "red")
+        {
+            ConsoleHelper.WriteWarningLine(log);
+            await _noticeService.Logs(userTokenIds, new { deviceName, log = log, time = DateTime.Now.ToString("G"), color = color });
+            WriteInfo(deviceName, log);
+        }
+
+        private (ConveyorLineTaskCommand, ConveyorLineTaskCommandWrite) ReadCommands(Dt_StationManager station, CommonConveyorLine conveyorLine)
+        {
+            ConveyorLineTaskCommand command = conveyorLine.ReadCustomer<ConveyorLineTaskCommand>(station.stationChildCode);
+            ConveyorLineTaskCommandWrite commandWrite = conveyorLine.ReadCustomer<ConveyorLineTaskCommandWrite>(station.stationChildCode, "DeviceCommand");
+            return (command, commandWrite);
+        }
+
     }
 }
\ No newline at end of file
diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/EmptyTrayStationHandler.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/EmptyTrayStationHandler.cs
new file mode 100644
index 0000000..4eb5892
--- /dev/null
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/EmptyTrayStationHandler.cs
@@ -0,0 +1,33 @@
+锘縰sing HslCommunication;
+using MoYu;
+using WIDESEAWCS_ITaskInfoRepository;
+using WIDESEAWCS_Model.Models;
+using WIDESEAWCS_QuartzJob;
+
+namespace WIDESEAWCS_Tasks.ConveyorLineJob
+{
+    public class EmptyTrayStationHandler : IStationHandler
+    {
+        private readonly CommonConveyorLineJob _commonConveyorLineJob;
+
+        public EmptyTrayStationHandler(CommonConveyorLineJob commonConveyorLineJob)
+        {
+            _commonConveyorLineJob = commonConveyorLineJob;
+        }
+
+        public async Task HandleStationAsync(CommonConveyorLine conveyorLine, Dt_StationManager station, ConveyorLineTaskCommand command, ConveyorLineTaskCommandWrite commandWrite)
+        {
+            var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
+            if (structs[0])
+            {
+                var log = $"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戠┖鎵樼洏璇锋眰鎵爜鍏ュ簱";
+                await _commonConveyorLineJob.LogAndWarn(conveyorLine.DeviceName, log);
+                _commonConveyorLineJob.NGRequestTaskInbound(conveyorLine, command, station, 0);
+            }
+            else
+            {
+                _commonConveyorLineJob.ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/ExceptionPortStationHandler.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/ExceptionPortStationHandler.cs
new file mode 100644
index 0000000..64565bc
--- /dev/null
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/ExceptionPortStationHandler.cs
@@ -0,0 +1,41 @@
+锘縰sing HslCommunication;
+using MoYu;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEAWCS_ITaskInfoRepository;
+using WIDESEAWCS_Model.Models;
+using WIDESEAWCS_QuartzJob;
+
+namespace WIDESEAWCS_Tasks.ConveyorLineJob.StationHandler
+{
+    public class ExceptionPortStationHandler : IStationHandler
+    {
+        private readonly CommonConveyorLineJob _commonConveyorLineJob;
+        private readonly ITaskRepository _taskRepository = App.GetService<ITaskRepository>();
+        public ExceptionPortStationHandler(CommonConveyorLineJob commonConveyorLineJob)
+        {
+            _commonConveyorLineJob = commonConveyorLineJob;
+        }
+        public async Task HandleStationAsync(CommonConveyorLine conveyorLine, Dt_StationManager station, ConveyorLineTaskCommand command, ConveyorLineTaskCommandWrite commandWrite)
+        {
+            var structs = BitConverter.GetBytes(commandWrite.WriteInteractiveSignal).Reverse().ToArray().ToBoolArray();
+            if (structs[0])
+            {
+                if (_taskRepository.QueryData(x => x.SourceAddress == station.stationChildCode).Count() > 0)
+                {
+                    return;
+                }
+                var log = $"銆恵conveyorLine.DeviceName}銆戜换鍔″彿锛氥�恵command.TaskNum}銆�,鎵樼洏鏉$爜锛氥�恵command.Barcode}銆戝凡鍒拌揪銆恵station.stationChildCode}銆戝紓甯稿彛璇锋眰鎵爜鍏ュ簱";
+                await _commonConveyorLineJob.LogAndWarn(conveyorLine.DeviceName, log);
+                await _commonConveyorLineJob.HandleNewTaskAsync(conveyorLine, command, station.stationChildCode, 0);
+            }
+            else
+            {
+                _commonConveyorLineJob.ConveyorLineSendFinish(conveyorLine, station.stationChildCode, 0, false);
+            }
+        }
+    }
+}
diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/IStationHandler.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/IStationHandler.cs
new file mode 100644
index 0000000..408ce8e
--- /dev/null
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/IStationHandler.cs
@@ -0,0 +1,15 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEAWCS_Model.Models;
+using WIDESEAWCS_QuartzJob;
+
+namespace WIDESEAWCS_Tasks.ConveyorLineJob
+{
+    public interface IStationHandler
+    {
+        Task HandleStationAsync(CommonConveyorLine conveyorLine, Dt_StationManager station, ConveyorLineTaskCommand command, ConveyorLineTaskCommandWrite commandWrite);
+    }
+}
\ No newline at end of file
diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/StationHandlerFactory.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/StationHandlerFactory.cs
new file mode 100644
index 0000000..877bb8c
--- /dev/null
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/StationHandler/StationHandlerFactory.cs
@@ -0,0 +1,27 @@
+锘縰sing MoYu;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEAWCS_Tasks.ConveyorLineJob.StationHandler;
+
+namespace WIDESEAWCS_Tasks.ConveyorLineJob
+{
+    public class StationHandlerFactory
+    {
+        public static IStationHandler GetHandler(int stationType, CommonConveyorLineJob commonConveyorLineJob)
+        {
+            switch (stationType)
+            {
+                case 11:
+                    return new EmptyTrayStationHandler(commonConveyorLineJob);
+                case 5:
+                    return new ExceptionPortStationHandler(commonConveyorLineJob);
+                default:
+                    throw new ArgumentException($"Unsupported station type: {stationType}");
+            }
+        }
+    }
+
+}
diff --git a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/Task/RequestInbound.cs b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/Task/RequestInbound.cs
index 1c5028a..f90762f 100644
--- a/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/Task/RequestInbound.cs
+++ b/Code Management/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/ConveyorLineJob/Task/RequestInbound.cs
@@ -55,7 +55,7 @@
         /// <summary>
         /// 澶勭悊鏂颁换鍔�
         /// </summary>
-        private async Task HandleNewTaskAsync(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
+        public async Task HandleNewTaskAsync(CommonConveyorLine conveyorLine, ConveyorLineTaskCommand command, string childDeviceCode, int ProtocalDetailValue)
         {
             var stationManager = _stationManagerRepository.QueryFirst(x => x.stationChildCode == childDeviceCode && x.stationPLC == conveyorLine.DeviceCode);
 

--
Gitblit v1.9.3