From 7a2d709d935075bcdc48e0f3f0ae9f4641663b9f Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 17 三月 2026 09:12:34 +0800
Subject: [PATCH] docs: add repository contributor guidelines

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs |  420 ++++++++++--------------------------------------------------
 1 files changed, 70 insertions(+), 350 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
index e338e6f..f6b1ac5 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/RobotJob.cs
@@ -1,38 +1,65 @@
-锘縰sing HslCommunication;
-using Newtonsoft.Json;
-using Quartz;
-using System.Collections.Concurrent;
-using System.Net.Sockets;
-using System.Text.Json;
-using WIDESEAWCS_Common.HttpEnum;
-using WIDESEAWCS_Core;
+锘縰sing Quartz;
+using WIDESEA_Core;
+using WIDESEAWCS_Core.Caches;
 using WIDESEAWCS_Core.Helper;
-using WIDESEAWCS_Core.Http;
-using WIDESEAWCS_DTO.Stock;
-using WIDESEAWCS_ITaskInfoRepository;
 using WIDESEAWCS_ITaskInfoService;
-using WIDESEAWCS_Model.Models;
 using WIDESEAWCS_QuartzJob;
-using WIDESEAWCS_QuartzJob.Service;
+using WIDESEAWCS_Tasks.Workflow.Abstractions;
+using WIDESEAWCS_Tasks.Workflow;
 using WIDESEAWCS_Tasks.SocketServer;
 
 namespace WIDESEAWCS_Tasks
 {
+    /// <summary>
+    /// 鏈哄櫒浜轰换鍔′綔涓氾細璐熻矗璋冨害涓庣敓鍛藉懆鏈熺鐞嗭紝鍏蜂綋鐘舵�佹満娴佺▼浜ょ粰缂栨帓鍣ㄣ��
+    /// </summary>
     [DisallowConcurrentExecution]
     public class RobotJob : IJob
     {
-        private readonly TcpSocketServer _TcpSocket;
-        private static readonly ConcurrentDictionary<string, RobotSocketState> _socketStates = new();
-        private static int _eventSubscribedFlag;
-        private readonly IRobotTaskService _taskService;
-        private readonly ITaskExecuteDetailService _taskExecuteDetailService;
-        private readonly ITaskRepository _taskRepository;
-        private readonly IRouterService _routerService;
+        private const int MaxTaskTotalNum = 48;
 
-        public RobotJob(TcpSocketServer TcpSocket, IRobotTaskService taskService)
+        private static int _messageSubscribedFlag;
+
+        private readonly RobotClientManager _clientManager;
+        private readonly RobotStateManager _stateManager;
+        private readonly IRobotMessageRouter _messageRouter;
+        private readonly RobotTaskProcessor _taskProcessor;
+        private readonly IRobotWorkflowOrchestrator _workflowOrchestrator;
+
+        public RobotJob(
+            TcpSocketServer tcpSocket,
+            IRobotTaskService robotTaskService,
+            ITaskService taskService,
+            ICacheService cache,
+            HttpClientHelper httpClientHelper)
         {
-            _TcpSocket = TcpSocket;
-            _taskService = taskService;
+            _stateManager = new RobotStateManager(cache);
+
+            // 鏀跺彛 Socket 璁块棶锛屽悗缁嫢鏇挎崲閫氫俊瀹炵幇鍙渶鏇挎崲缃戝叧灞傘��
+            ISocketClientGateway socketGateway = new SocketClientGateway(tcpSocket);
+
+            _taskProcessor = new RobotTaskProcessor(socketGateway, _stateManager, robotTaskService, taskService, httpClientHelper);
+            _clientManager = new RobotClientManager(tcpSocket, _stateManager);
+
+            var simpleCommandHandler = new RobotSimpleCommandHandler(_taskProcessor);
+            var prefixCommandHandler = new RobotPrefixCommandHandler(robotTaskService, _taskProcessor, _stateManager, socketGateway);
+            _messageRouter = new RobotMessageHandler(socketGateway, _stateManager, cache, simpleCommandHandler, prefixCommandHandler);
+
+            _workflowOrchestrator = new RobotWorkflowOrchestrator(_stateManager, _clientManager, _taskProcessor, robotTaskService);
+
+            _clientManager.OnClientDisconnected += OnClientDisconnected;
+
+            // 鍏ㄥ眬鍙闃呬竴娆℃秷鎭簨浠讹紝淇濇寔鍘熸湁琛屼负銆�
+            if (System.Threading.Interlocked.CompareExchange(ref _messageSubscribedFlag, 1, 0) == 0)
+            {
+                tcpSocket.MessageReceived += _messageRouter.HandleMessageReceivedAsync;
+                Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 鏈哄櫒鎵婽CP娑堟伅浜嬩欢宸茶闃�");
+            }
+        }
+
+        private void OnClientDisconnected(object? sender, RobotSocketState state)
+        {
+            Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 瀹㈡埛绔凡鏂紑杩炴帴: {state.IPAddress}");
         }
 
         public async Task Execute(IJobExecutionContext context)
@@ -46,342 +73,35 @@
 
             string ipAddress = robotCrane.IPAddress;
 
-            // 鑾峰彇鎴栧垱寤虹姸鎬�
-            RobotSocketState state = _socketStates.GetOrAdd(ipAddress, _ => new RobotSocketState
-            {
-                IPAddress = ipAddress,
-                RobotCrane = robotCrane
-            });
-
-            // 鏇存柊璁惧淇℃伅
+            RobotSocketState state = _stateManager.GetOrCreateState(ipAddress, robotCrane);
             state.RobotCrane = robotCrane;
 
-            // 妫�鏌ユ槸鍚︽湁璇ュ鎴风杩炴帴
-            var clientIds = _TcpSocket.GetClientIds();
-            if (!clientIds.Contains(ipAddress))
+            try
             {
-                return;
-            }
-
-            // 璁㈤槄涓�娆� message 浜嬩欢锛堝叏灞�涓�娆★級
-            if (Interlocked.CompareExchange(ref _eventSubscribedFlag, 1, 0) == 0)
-            {
-                _TcpSocket.MessageReceived += _TcpSocket_MessageReceived;
-                _TcpSocket.RobotReceived += _TcpSocket_RobotReceived;
-            }
-
-            if (!state.IsEventSubscribed)
-            {
-                _TcpSocket._clients.TryGetValue(ipAddress, out TcpClient client);
-                Task clientTask = _TcpSocket.HandleClientAsync(client, robotCrane.IPAddress, _TcpSocket._cts.Token, state);
-                state.IsEventSubscribed = true;
-            }
-
-            // 鑾峰彇浠诲姟骞剁紦瀛樺埌鐘舵�佷腑
-            Dt_RobotTask? task = GetTask(robotCrane);
-            if (task != null)
-            {
-                state.CurrentTask = task;
-                if (task.RobotTaskTotalNum != 48)
+                if (!_clientManager.EnsureClientSubscribed(ipAddress, robotCrane))
                 {
-                    // 澶勭悊姝e湪鎵ц鐨勪换鍔�
-                    if (state.RobotRunMode == 1 && state.RobotControlMode == 1)
-                    {
-                        await Task.Delay(1000);
-                        if ((state.CurrentAction == "Homed" || state.CurrentAction == "PickFinished" || state.CurrentAction == "PutFinished") && state.OperStatus == "Running")
-                        {
-                            // TODO 璇诲彇绾夸綋鐢垫睜鏉$爜锛屽彂閫佸彇鐢垫睜鎸囦护
-                            if (true)
-                            {
-                                // 妯℃嫙璇诲彇鏉$爜
-                                state.CellBarcode = new string[] { "CellBarcode1", "CellBarcode2", "CellBarcode3", "CellBarcode4" };
+                    return;
+                }
 
-                                string taskString = $"Pickbattery,{task.RobotSourceAddress}";
-                                // 鍙戦�佷换鍔℃寚浠�
-                                bool result = await _TcpSocket.SendToClientAsync(ipAddress, taskString);
-                            }
-                        }
+                var task = _taskProcessor.GetTask(robotCrane);
+                if (task != null)
+                {
+                    var latestState = _stateManager.GetState(ipAddress);
+                    if (latestState == null)
+                    {
+                        return;
+                    }
+
+                    if (latestState.RobotTaskTotalNum < MaxTaskTotalNum)
+                    {
+                        await _workflowOrchestrator.ExecuteAsync(latestState, task, ipAddress);
                     }
                 }
             }
-
-            return;
-        }
-
-        /// <summary>
-        ///  浜嬩欢锛氬鎴风鏂紑杩炴帴鏃惰Е鍙�
-        /// </summary>
-        /// <param name="clientId"></param>
-        /// <returns></returns>
-        private Task<string?> _TcpSocket_RobotReceived(string clientId)
-        {
-            _socketStates.TryRemove(clientId, out _);
-            return Task.FromResult<string?>(null);
-        }
-
-        /// <summary>
-        /// 浜嬩欢锛氭敹鍒版秷鎭椂瑙﹀彂
-        /// </summary>
-        /// <param name="message"></param>
-        /// <param name="isJson"></param>
-        /// <param name="client"></param>
-        /// <param name="state"></param>
-        /// <returns></returns>
-        private async Task<string?> _TcpSocket_MessageReceived(string message, bool isJson, TcpClient client, RobotSocketState state)
-        {
-            WebResponseContent content = new WebResponseContent();
-            string messageLower = message.ToLowerInvariant();
-
-            if (IsSimpleCommand(messageLower, state))
+            catch (Exception)
             {
-                return null;
+                // 寮傚父澶勭悊宸插湪缁勪欢鍐呴儴杩涜锛孞ob 灞備繚鎸佸厹搴曞悶鍚愯涔夈��
             }
-
-            if (IsPrefixCommand(messageLower))
-            {
-                try
-                {
-                    var parts = message.Split(',');
-                    if (parts.Length >= 1)
-                    {
-                        var cmd = parts[0].ToLowerInvariant();
-                        int[] positions = new int[4];
-                        for (int i = 1; i <= 4 && i < parts.Length; i++)
-                        {
-                            int.TryParse(parts[i], out positions[i - 1]);
-                        }
-
-                        if (cmd.StartsWith("pickfinished"))
-                        {
-                            StockDTO stockDTO = new StockDTO
-                            {
-                                SourceLineNo = state.CurrentTask?.RobotSourceAddressLineCode,
-                                SourcePalletNo = state.CurrentTask?.RobotSourceAddressPalletCode,
-                                TargetPalletNo = state.CurrentTask?.RobotTargetAddressPalletCode,
-                                TargetLineNo = state.CurrentTask?.RobotTargetAddressLineCode,
-                                Details = positions
-                                        .Where(x => x > 0)
-                                        .OrderBy(x => x)
-                                        .Select((x, idx) => new StockDetailDTO
-                                        {
-                                            Quantity = state.CurrentTask?.RobotTaskTotalNum ?? 1,
-                                            Channel = x > 0 ? x : throw new ArgumentOutOfRangeException(nameof(x), "Channel must be positive"),
-                                            CellBarcode = state.CellBarcode[idx]
-                                        })
-                                        .ToList()
-                            };
-                            state.LastPickPositions = positions;
-
-                            var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), stockDTO.ToJsonString(), state.CurrentTask?.RobotTaskType == 2 ? nameof(ConfigKey.ChangePalletAsync) : nameof(ConfigKey.SplitPalletAsync));
-                            content = JsonConvert.DeserializeObject<WebResponseContent>(result);
-
-                            if (content.Status)
-                            {
-                                state.CurrentAction = "PickFinished";
-                            }
-                        }
-                        else if (cmd.StartsWith("putfinished"))
-                        {
-                            state.LastPutPositions = positions;
-                            if (state.CurrentTask?.RobotTaskType == 1)
-                            {
-                                // 鍙戦�佹暟鎹粰WMS缁勭洏/鎹㈢洏
-                                StockDTO stockDTO = new StockDTO
-                                {
-                                    SourceLineNo = state.CurrentTask?.RobotSourceAddressLineCode,
-                                    SourcePalletNo = state.CurrentTask?.RobotSourceAddressPalletCode,
-                                    TargetPalletNo = state.CurrentTask?.RobotTargetAddressPalletCode,
-                                    TargetLineNo = state.CurrentTask?.RobotTargetAddressLineCode,
-                                    Details = positions
-                                        .Where(x => x > 0)
-                                        .OrderBy(x => x)
-                                        .Select((x, idx) => new StockDetailDTO
-                                        {
-                                            Quantity = state.CurrentTask?.RobotTaskTotalNum ?? 1,
-                                            Channel = x > 0 ? x : throw new ArgumentOutOfRangeException(nameof(x), "Channel must be positive"),
-                                            CellBarcode = state.CellBarcode[idx]
-                                        })
-                                        .ToList()
-                                };
-                                var result = await HttpRequestHelper.HTTPPostAsync(nameof(Category.WMS), stockDTO.ToJsonString(), nameof(ConfigKey.GroupPalletAsync));
-                                content = JsonConvert.DeserializeObject<WebResponseContent>(result);
-
-                                if (content.Status)
-                                {
-                                    state.CurrentAction = "PutFinished";
-                                }
-                            }
-                        }
-                    }
-                }
-                catch { }
-
-                return null;
-            }
-
-            return null;
-        }
-
-        /// <summary>
-        /// 鏈烘鎵嬬畝鍗曞懡浠ゅ鐞�
-        /// </summary>
-        /// <param name="message"></param>
-        /// <param name="state"></param>
-        /// <returns></returns>
-        private bool IsSimpleCommand(string message, RobotSocketState state)
-        {
-            switch (message)
-            {
-                case "homing":
-                    state.CurrentAction = "Homing";
-                    return true;
-
-                case "homed":
-                    state.CurrentAction = "Homed";
-                    return true;
-
-                case "picking":
-                    state.CurrentAction = "Picking";
-                    return true;
-
-                case "puting":
-                    state.CurrentAction = "Putting";
-                    return true;
-
-                case "allpickfinished":
-                    state.CurrentAction = "AllPickFinished";
-                    if(state.CurrentTask?.RobotTaskType == 2|| state.CurrentTask?.RobotTaskType == 3)
-                    {
-                        // TODO 鏈烘鎵嬪彇璐у畬鎴愶紝鍒ゆ柇鏄惁鎹㈢洏銆佹媶鐩樹换鍔★紝鍒涘缓绌烘墭鐩樺洖搴撲换鍔�
-                    }
-                    return true;
-
-                case "allputfinished":
-                    state.CurrentAction = "AllPutFinished";
-                    if (state.CurrentTask?.RobotTaskType == 1 )
-                    {
-                        // TODO 鏈烘鎵嬪彇璐у畬鎴愶紝鍒ゆ柇鏄惁缁勭洏浠诲姟锛屽垱寤虹粍鐩樺叆搴撲换鍔�
-                    }
-                    return true;
-
-                case "running":
-                    state.OperStatus = "Running";
-                    return true;
-
-                case "pausing":
-                    state.OperStatus = "Pausing";
-                    return true;
-
-                case "warming":
-                    state.OperStatus = "Warming";
-                    return true;
-
-                case "emstoping":
-                    state.OperStatus = "Emstoping";
-                    return true;
-
-                case "runmode,1":
-                    state.RobotRunMode = 1;
-                    return true;
-
-                case "runmode,2":
-                    state.RobotRunMode = 2;
-                    return true;
-
-                case "controlmode,1":
-                    state.RobotControlMode = 1;
-                    return true;
-
-                case "controlmode,2":
-                    state.RobotControlMode = 2;
-                    return true;
-
-                case "armobject,1":
-                    state.RobotArmObject = 1;
-                    return true;
-
-                case "armobject,0":
-                    state.RobotArmObject = 0;
-                    return true;
-
-                default:
-                    return false;
-            }
-        }
-
-        /// <summary>
-        /// 鏈烘鎵嬪墠缂�鍛戒护澶勭悊
-        /// </summary>
-        /// <param name="message"></param>
-        /// <returns></returns>
-        private static bool IsPrefixCommand(string message)
-        {
-            return message.StartsWith("pickfinished") || message.StartsWith("putfinished");
-        }
-
-        private Dt_RobotTask? GetTask(RobotCraneDevice robotCrane)
-        {
-            return _taskService.QueryRobotCraneTask(robotCrane.DeviceCode);
         }
     }
-
-    public class RobotSocketState
-    {
-        public string IPAddress { get; set; } = string.Empty;
-
-        /// <summary>
-        /// 鏄惁宸茶闃呮秷鎭簨浠�
-        /// </summary>
-        public bool IsEventSubscribed { get; set; }
-
-        /// <summary>
-        /// 鏈烘鎵嬭繍琛屾ā寮�
-        /// </summary>
-        public int? RobotRunMode { get; set; }
-
-        /// <summary>
-        /// 鏈烘鎵嬫帶鍒舵ā寮�
-        /// </summary>
-        public int? RobotControlMode { get; set; }
-
-        /// <summary>
-        /// 鏈烘鎵嬫姄鍙栧璞�
-        /// </summary>
-        public int? RobotArmObject { get; set; }
-
-        /// <summary>
-        /// 鏈烘鎵嬭澶囦俊鎭�
-        /// </summary>
-        public RobotCraneDevice? RobotCrane { get; set; }
-
-        /// <summary>
-        /// 褰撳墠鍔ㄤ綔
-        /// </summary>
-        public string? CurrentAction { get; set; }
-
-        /// <summary>
-        /// 褰撳墠鐘舵��
-        /// </summary>
-        public string? OperStatus { get; set; }
-
-        /// <summary>
-        /// 鍙栬揣瀹屾垚浣嶇疆
-        /// </summary>
-        public int[]? LastPickPositions { get; set; }
-
-        /// <summary>
-        /// 鏀捐揣瀹屾垚浣嶇疆
-        /// </summary>
-        public int[]? LastPutPositions { get; set; }
-
-        /// <summary>
-        /// 鎶撳彇浣嶇疆鏉$爜
-        /// </summary>
-        public string[] CellBarcode { get; set; }
-
-        /// <summary>
-        /// 褰撳墠鎶撳彇浠诲姟
-        /// </summary>
-        public Dt_RobotTask? CurrentTask { get; set; }
-    }
-}
\ No newline at end of file
+}

--
Gitblit v1.9.3