From ad64840cc04dac2278ca02f22ddc02b1a218e9cf Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 15 四月 2026 22:44:18 +0800
Subject: [PATCH] feat(机器人任务): 实现换盘任务假电芯补充逻辑

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs |   93 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 81 insertions(+), 12 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs
index 4f2960d..e0211a5 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs
@@ -111,20 +111,15 @@
             // 1. 杩愯妯″紡涓鸿嚜鍔紙2锛�
             // 2. 鎺у埗妯″紡涓哄鎴风鎺у埗锛�1锛�
             // 3. 杩愯鐘舵�佹槸 Running
-            if (latestState.RobotRunMode == 2 /*&& latestState.RobotControlMode == 1*/ && latestState.OperStatus == "Running")
+            if (latestState.RobotRunMode == 2 /*&& latestState.RobotControlMode == 1*/ && latestState.OperStatus == "Running" && latestState.Homed == "Homed")
             {
-                if(latestState.CurrentAction == "Picking" ||  latestState.CurrentAction == "Puting")
-                {
-                    return;
-                }
-
                 // ========== 鍙栬揣瀹屾垚鍚庣殑鏀捐揣澶勭悊 ==========
                 // 鏉′欢锛�
                 // - 褰撳墠鍔ㄤ綔鏄� PickFinished 鎴� AllPickFinished锛堝彇璐у畬鎴愶級
                 // - 鎵嬭噦涓婃湁鐗╂枡锛圧obotArmObject == 1锛�
                 // - 浠诲姟鐘舵�佷负 RobotPickFinish锛堝凡璁板綍鍙栬揣瀹屾垚锛�
                 if ((latestState.CurrentAction == "PickFinished" || latestState.CurrentAction == "AllPickFinished")
-                    && latestState.RobotArmObject == 0
+                    && latestState.RobotArmObject == 1
                     && task.RobotTaskState == TaskRobotStatusEnum.RobotPickFinish.GetHashCode())
                 {
                     _logger.LogInformation("ExecuteAsync锛氭弧瓒虫斁璐ф潯浠讹紝寮�濮嬪鐞嗗彇璐у畬鎴愶紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
@@ -242,22 +237,36 @@
                     || task.RobotTaskType == RobotTaskTypeEnum.ChangePallet.GetHashCode();
             }
 
-            // 濡傛灉鏄粍鐩樹换鍔★紙鍖呮嫭鎹㈢洏锛�
+            // 濡傛灉鏄粍鐩樹换鍔�
             if (task.RobotTaskType == RobotTaskTypeEnum.GroupPallet.GetHashCode())
             {
                 // 鐢熸垚鎵樼洏鏉$爜鍓嶇紑
                 const string prefix = "TRAY";
 
-                // 鐢熸垚涓や釜鎵樼洏鏉$爜锛堢敤浜庣粍鐩樻搷浣滐級
+                // 鐢熸垚涓や釜鎵樼洏鏉$爜锛堢敤浜庣粍鐩樻搷浣滐級锛堟祴璇曠敤锛屽悗缁鍙栫嚎浣撴潯鐮侊級
                 string trayBarcode1 = RobotBarcodeGenerator.GenerateTrayBarcode(prefix);
                 string trayBarcode2 = RobotBarcodeGenerator.GenerateTrayBarcode(prefix);
 
                 // 濡傛灉鏉$爜鐢熸垚鎴愬姛
                 if (!string.IsNullOrEmpty(trayBarcode1) && !string.IsNullOrEmpty(trayBarcode2))
                 {
-                    // 灏嗘潯鐮佹坊鍔犲埌鐘舵�佷腑锛屼緵鍚庣画鏀捐揣鏃朵娇鐢�
-                    stateForUpdate.CellBarcode.Add(trayBarcode1);
-                    stateForUpdate.CellBarcode.Add(trayBarcode2);
+                    if(stateForUpdate.CellBarcode.Contains(trayBarcode1)|| stateForUpdate.CellBarcode.Contains(trayBarcode2))
+                    {
+                        _logger.LogError("HandlePutFinishedStateAsync锛氱敓鎴愮殑鎵樼洏鏉$爜宸插瓨鍦紝鍙兘瀛樺湪閲嶅锛屼换鍔″彿: {TaskNum}", task.RobotTaskNum);
+                        QuartzLogger.Error($"鐢熸垚鐨勬墭鐩樻潯鐮佸凡瀛樺湪锛屽彲鑳藉瓨鍦ㄩ噸澶�", stateForUpdate.RobotCrane.DeviceName);
+
+                        // 鏉$爜閲嶅锛岃褰曢敊璇棩蹇楀苟鍋滄鍚庣画鎿嶄綔(鍚庣画鏀捐揣鏃朵細鐢ㄥ埌杩欎簺鏉$爜淇℃伅锛屼緵鍚庣画鏀捐揣鏃朵娇鐢紝璋冭瘯鍚庡彲鑳戒細鍙栨秷姝ら�昏緫)
+                        return;
+                    }
+                    else
+                    {
+                        _logger.LogInformation("HandlePutFinishedStateAsync锛氱敓鎴愮殑鎵樼洏鏉$爜鍞竴锛岀户缁墽琛岋紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                        QuartzLogger.Info($"鐢熸垚鐨勬墭鐩樻潯鐮佸敮涓�锛岀户缁墽琛�", stateForUpdate.RobotCrane.DeviceName);
+                        // 灏嗘潯鐮佹坊鍔犲埌鐘舵�佷腑锛屼緵鍚庣画鏀捐揣鏃朵娇鐢�
+                        stateForUpdate.CellBarcode.Add(trayBarcode1);
+                        stateForUpdate.CellBarcode.Add(trayBarcode2);
+                    }
+
 
                     // 璁板綍鏃ュ織锛氱敓鎴愭墭鐩樻潯鐮佹垚鍔�
                     _logger.LogInformation("HandlePutFinishedStateAsync锛氱敓鎴愭墭鐩樻潯鐮佹垚鍔�: {Barcode1}+{Barcode2}锛屼换鍔″彿: {TaskNum}", trayBarcode1, trayBarcode2, task.RobotTaskNum);
@@ -273,6 +282,66 @@
                     QuartzLogger.Error($"鐢熸垚鎵樼洏鏉$爜澶辫触", stateForUpdate.RobotCrane.DeviceName);
                 }
             }
+            else if (task.RobotTaskType == RobotTaskTypeEnum.ChangePallet.GetHashCode())
+            {
+                // 鎹㈢洏浠诲姟
+                // 鐩爣锛氭甯哥數鑺姄鍙栧畬鎴愬悗锛岃ˉ鍏呭亣鐢佃姱鑷�48涓�
+                const int targetTotal = 48;
+                const int fakeBatteryPickPosition = 5;  // 鍋囩數鑺姄鍙栦綅缃�
+                const int pickCountPerExecution = 4;     // 姣忔鎶撳彇鏁伴噺
+
+                int targetNormalCount = task.RobotTaskTotalNum;  // 姝e父鐢佃姱鐩爣鏁伴噺
+                int currentCompletedCount = stateForUpdate.RobotTaskTotalNum;  // 宸插畬鎴愭暟閲�
+
+                // 濡傛灉鐩爣鏁伴噺涓�48锛岀洿鎺ヤ笅鍙戞甯镐换鍔�
+                if (targetNormalCount == targetTotal)
+                {
+                    await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate);
+                }
+                // 濡傛灉宸插畬鎴愭暟閲忓皬浜庣洰鏍囨暟閲忥紝缁х画鎶撳彇姝e父鐢佃姱
+                else if (currentCompletedCount < targetNormalCount)
+                {
+                    await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate);
+                }
+                // 姝e父鐢佃姱宸插畬鎴愶紝杩涘叆鍋囩數鑺ˉ鍏呮ā寮�
+                else if (currentCompletedCount == targetNormalCount && !stateForUpdate.IsInFakeBatteryMode)
+                {
+                    // 棣栨杩涘叆鍋囩數鑺ā寮忥紝璁剧疆鏍囧織
+                    stateForUpdate.IsInFakeBatteryMode = true;
+                    _logger.LogInformation("HandlePutFinishedStateAsync锛氭甯哥數鑺姄鍙栧畬鎴愶紝杩涘叆鍋囩數鑺ˉ鍏呮ā寮忥紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                    QuartzLogger.Info($"姝e父鐢佃姱鎶撳彇瀹屾垚锛岃繘鍏ュ亣鐢佃姱琛ュ厖妯″紡", stateForUpdate.RobotCrane?.DeviceName);
+                }
+
+                // 濡傛灉澶勪簬鍋囩數鑺ˉ鍏呮ā寮忥紝璁$畻骞朵笅鍙戣ˉ鏁颁换鍔�
+                if (stateForUpdate.IsInFakeBatteryMode)
+                {
+                    int remaining = targetTotal - currentCompletedCount;
+                    if (remaining > 0)
+                    {
+                        // 璁$畻姣忔鎶撳彇鐨勬暟閲忥紙鏈�澶�4涓級
+                        int pickCount = Math.Min(pickCountPerExecution, remaining);
+
+                        // 鑾峰彇鍙敤鐨勫亣鐢佃姱骞抽潰鐐逛綅
+                        var positions = _taskProcessor.GetNextAvailableFakeBatteryPositions(pickCount);
+                        if (positions.Count == 0)
+                        {
+                            _logger.LogError("HandlePutFinishedStateAsync锛氭棤鍙敤鍋囩數鑺偣浣嶏紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                            QuartzLogger.Error($"鏃犲彲鐢ㄥ亣鐢佃姱鐐逛綅", stateForUpdate.RobotCrane?.DeviceName);
+                            return;
+                        }
+
+                        // 涓嬪彂鍋囩數鑺彇璐ф寚浠�
+                        await _taskProcessor.SendSocketRobotFakeBatteryPickAsync(task, stateForUpdate, positions);
+                    }
+                    else
+                    {
+                        // 鍋囩數鑺ˉ鍏呭畬鎴愶紝閲嶇疆鏍囧織
+                        stateForUpdate.IsInFakeBatteryMode = false;
+                        _logger.LogInformation("HandlePutFinishedStateAsync锛氭崲鐩樹换鍔″畬鎴愶紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                        QuartzLogger.Info($"鎹㈢洏浠诲姟瀹屾垚", stateForUpdate.RobotCrane?.DeviceName);
+                    }
+                }
+            }
             else
             {
                 // 闈炵粍鐩樹换鍔★紝鐩存帴鍙戦�佸彇璐ф寚浠�

--
Gitblit v1.9.3