From 5bf10c1dafe485d506ec534f98e5220a3b83dd17 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期四, 16 四月 2026 23:16:46 +0800
Subject: [PATCH] feat(WCS&WMS): 机械手扫码NG处理与线体条码读取与添加批量MES绑定解绑接口

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs |  336 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 301 insertions(+), 35 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..2ddad09 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Tasks/RobotJob/Workflow/RobotWorkflowOrchestrator.cs
@@ -5,6 +5,7 @@
 using WIDESEAWCS_Core.LogHelper;
 using WIDESEAWCS_ITaskInfoService;
 using WIDESEAWCS_Model.Models;
+using WIDESEAWCS_Tasks.SocketServer;
 using WIDESEAWCS_Tasks.Workflow.Abstractions;
 
 namespace WIDESEAWCS_Tasks.Workflow
@@ -111,20 +112,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);
@@ -157,7 +153,7 @@
         /// </summary>
         /// <remarks>
         /// 褰撳彇璐у畬鎴愬悗锛屽悜鏈哄櫒浜哄彂閫佹斁璐ф寚浠わ紙Putbattery锛夈��
-        /// 鏈哄櫒浜烘敹鍒版寚浠ゅ悗浼氬皢璐х墿鏀剧疆鍒扮洰鏍囧湴鍧�銆�
+        /// 鎹㈢洏浠诲姟浣跨敤鎵规鏍煎紡 SendPutWithBatchAsync銆�
         ///
         /// 鎸囦护鏍煎紡锛歅utbattery,{鐩爣鍦板潃}
         /// 渚嬪锛歅utbattery,B01 琛ㄧず灏嗚揣鐗╂斁缃埌 B01 浣嶇疆
@@ -166,38 +162,113 @@
         /// <param name="ipAddress">鏈哄櫒浜� IP 鍦板潃</param>
         private async Task HandlePickFinishedStateAsync(Dt_RobotTask task, string ipAddress)
         {
-            // 鏋勫缓鏀捐揣鎸囦护锛屾牸寮忥細Putbattery,{鐩爣鍦板潃}
-            string taskString = $"Putbattery,{task.RobotTargetAddress}";
+            string taskString;
 
-            // 閫氳繃瀹㈡埛绔鐞嗗櫒鍙戦�佹寚浠ゅ埌鏈哄櫒浜�
+            var state = _stateManager.GetState(ipAddress);
+
+            // 鎹㈢洏浠诲姟浣跨敤鎵规鏍煎紡
+            if (task.RobotTaskType == RobotTaskTypeEnum.ChangePallet.GetHashCode())
+            {
+                int targetNormalCount = task.RobotTaskTotalNum;
+                int currentCompletedCount = state?.RobotTaskTotalNum ?? 0;
+
+                bool isFlowA = task.RobotSourceAddressLineCode is "11001" or "11010";
+
+                // 娴佸悜A Phase 2锛氭斁鍋囩數鑺埌鐩爣鎵樼洏
+                if (isFlowA && state?.ChangePalletPhase == 2)
+                {
+                    int remaining = 48 - currentCompletedCount;
+                    if (remaining <= 0) return;
+
+                    int batchStart = targetNormalCount + 1 + (state.CurrentBatchIndex - 1);
+                    int putCount = Math.Min(4, remaining);
+                    var (start, end) = _taskProcessor.BuildBatchRange(batchStart, putCount);
+
+                    await _taskProcessor.SendPutWithBatchAsync(task, state, task.RobotTargetAddress, start, end);
+                    return;
+                }
+
+                // 娴佸悜B Phase 4锛氭斁鍋囩數鑺埌5鍙蜂綅
+                if (!isFlowA && state?.ChangePalletPhase == 4)
+                {
+                    int fakeCount = 48 - targetNormalCount;
+                    int completedFake = Math.Max(0, currentCompletedCount - targetNormalCount);
+                    int remainingFake = fakeCount - completedFake;
+
+                    if (remainingFake <= 0) return;
+
+                    var positions = _taskProcessor.GetNextAvailableFakeBatteryPositions(Math.Min(4, remainingFake));
+                    if (positions.Count == 0)
+                    {
+                        _logger.LogError("HandlePickFinishedStateAsync锛氭棤鍙敤鍋囩數鑺偣浣嶏紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                        return;
+                    }
+
+                    int start = positions.Min();
+                    int end = positions.Max();
+
+                    await _taskProcessor.SendPutWithBatchAsync(task, state, "5", start, end);
+                    return;
+                }
+
+                // 娴佸悜B Phase 2锛氭斁姝e父鐢佃姱鍒扮洰鏍囨墭鐩�
+                if (!isFlowA && state?.ChangePalletPhase == 2)
+                {
+                    int remainingNormal = targetNormalCount - currentCompletedCount;
+                    if (remainingNormal <= 0) return;
+
+                    int batchStart = ((currentCompletedCount - 1) / 4) * 4 + 1;
+                    int putCount = Math.Min(4, remainingNormal);
+                    var (start, end) = _taskProcessor.BuildBatchRange(batchStart, putCount);
+
+                    await _taskProcessor.SendPutWithBatchAsync(task, state, task.RobotTargetAddress, start, end);
+                    return;
+                }
+
+                // 榛樿锛氫娇鐢ㄥ師鏈夋牸寮�
+                taskString = $"Putbattery,{task.RobotTargetAddress}";
+            }
+            else
+            {
+                // 闈炴崲鐩樹换鍔★細浣跨敤鍘熸湁鏍煎紡
+                if (state != null && state.IsGroupPallet && task.RobotTaskType == RobotTaskTypeEnum.GroupPallet.GetHashCode())
+                {
+                    // 缁勭洏浠诲姟锛氭斁璐ч渶鍒ゆ柇鏄惁NG锛屽鏋淣G鍒欐斁鍒癗G鍙�
+                    if (state.IsScanNG)
+                    {
+                        taskString = $"Putbattery,NG";
+                    }
+                    else
+                    {
+                        taskString = $"Putbattery,{task.RobotTargetAddress}";
+                    }
+                }
+                else
+                    taskString = $"Putbattery,{task.RobotTargetAddress}";
+            }
+
             bool result = await _clientManager.SendToClientAsync(ipAddress, taskString);
 
             if (result)
             {
-                // 鍙戦�佹垚鍔燂紝璁板綍 Info 鏃ュ織
                 _logger.LogInformation("HandlePickFinishedStateAsync锛氫笅鍙戞斁璐ф寚浠ゆ垚鍔燂紝鎸囦护: {TaskString}锛屼换鍔″彿: {TaskNum}", taskString, task.RobotTaskNum);
                 QuartzLogger.Info($"涓嬪彂鏀捐揣鎸囦护鎴愬姛锛屾寚浠�: {taskString}", task.RobotRoadway);
 
-                // 鏇存柊浠诲姟鐘舵�佷负"鏈哄櫒浜烘墽琛屼腑"
                 task.RobotTaskState = TaskRobotStatusEnum.RobotExecuting.GetHashCode();
 
-                // 鑾峰彇鏈�鏂扮姸鎬佸苟鏇存柊浠诲姟鍏宠仈
                 var stateToUpdate = _stateManager.GetState(ipAddress);
                 if (stateToUpdate != null)
                 {
                     stateToUpdate.CurrentTask = task;
 
-                    // 瀹夊叏鏇存柊鐘舵�佸埌 Redis
                     if (_stateManager.TryUpdateStateSafely(ipAddress, stateToUpdate))
                     {
-                        // 鐘舵�佹洿鏂版垚鍔熷悗锛屾洿鏂颁换鍔¤褰�
                         await _robotTaskService.UpdateRobotTaskAsync(task);
                     }
                 }
             }
             else
             {
-                // 鍙戦�佸け璐ワ紝璁板綍 Error 鏃ュ織
                 _logger.LogError("HandlePickFinishedStateAsync锛氫笅鍙戞斁璐ф寚浠ゅけ璐ワ紝鎸囦护: {TaskString}锛屼换鍔″彿: {TaskNum}", taskString, task.RobotTaskNum);
                 QuartzLogger.Error($"涓嬪彂鏀捐揣鎸囦护澶辫触锛屾寚浠�: {taskString}", task.RobotRoadway);
             }
@@ -242,41 +313,236 @@
                     || 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);
+                // 璇诲彇绾夸綋鐢佃姱鏉$爜
+                string trayBarcode1 = RobotBarcodeGenerator.GenerateTrayBarcode("DB40.990");
+                string trayBarcode2 = RobotBarcodeGenerator.GenerateTrayBarcode("DB40.1020");
 
                 // 濡傛灉鏉$爜鐢熸垚鎴愬姛
                 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);
 
-                    // 璁板綍鏃ュ織锛氱敓鎴愭墭鐩樻潯鐮佹垚鍔�
-                    _logger.LogInformation("HandlePutFinishedStateAsync锛氱敓鎴愭墭鐩樻潯鐮佹垚鍔�: {Barcode1}+{Barcode2}锛屼换鍔″彿: {TaskNum}", trayBarcode1, trayBarcode2, task.RobotTaskNum);
-                    QuartzLogger.Info($"鐢熸垚鎵樼洏鏉$爜鎴愬姛: {trayBarcode1}+{trayBarcode2}", stateForUpdate.RobotCrane.DeviceName);
+                        // 鏉$爜閲嶅锛岃褰曢敊璇棩蹇楀苟鍋滄鍚庣画鎿嶄綔(鍚庣画鏀捐揣鏃朵細鐢ㄥ埌杩欎簺鏉$爜淇℃伅锛屼緵鍚庣画鏀捐揣鏃朵娇鐢紝璋冭瘯鍚庡彲鑳戒細鍙栨秷姝ら�昏緫)
+
+                        // 鍙戦�佸彇璐ф寚浠� 鏍囪鎵爜NG锛屾斁璐ф椂涓嶄娇鐢ㄨ繖浜涙潯鐮侊紝骞舵斁鍏G鍙�
+                        await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate, true);
+                    }
+                    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);
+                    QuartzLogger.Info($"璇诲彇鎵樼洏鏉$爜鎴愬姛: {trayBarcode1}+{trayBarcode2}", stateForUpdate.RobotCrane.DeviceName);
 
                     // 鍙戦�佸彇璐ф寚浠�
-                    await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate);
+                    await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate, false);
                 }
                 else
                 {
-                    // 鏉$爜鐢熸垚澶辫触锛岃褰曢敊璇棩蹇�
-                    _logger.LogError("HandlePutFinishedStateAsync锛氱敓鎴愭墭鐩樻潯鐮佸け璐ワ紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
-                    QuartzLogger.Error($"鐢熸垚鎵樼洏鏉$爜澶辫触", stateForUpdate.RobotCrane.DeviceName);
+                    // 鏉$爜璇诲彇澶辫触锛岃褰曢敊璇棩蹇�
+                    _logger.LogError("HandlePutFinishedStateAsync锛氳鍙栨墭鐩樻潯鐮佸け璐ワ紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                    QuartzLogger.Error($"璇诲彇鎵樼洏鏉$爜澶辫触", stateForUpdate.RobotCrane.DeviceName);
+
+
+                    // 鍙戦�佸彇璐ф寚浠� 鏍囪鎵爜NG锛屾斁璐ф椂涓嶄娇鐢ㄨ繖浜涙潯鐮侊紝骞舵斁鍏G鍙�
+                    await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate, true);
+                }
+            }
+            else if (task.RobotTaskType == RobotTaskTypeEnum.ChangePallet.GetHashCode())
+            {
+                const int targetTotal = 48;
+                int targetNormalCount = task.RobotTaskTotalNum;
+                int currentCompletedCount = stateForUpdate.RobotTaskTotalNum;
+
+                // 鍒ゆ柇娴佸悜锛坣ull-safe锛�
+                bool isFlowA = task.RobotSourceAddressLineCode is "11001" or "11010";
+
+                // 鐩爣鏁伴噺涓�48锛氱洿鎺ヨ蛋鍘熸湁閫昏緫锛屼笉杩涘叆鎵规妯″紡
+                if (targetNormalCount == targetTotal)
+                {
+                    await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate, false);
+                    return;
+                }
+
+                // 鍒濆鍖栨壒娆℃ā寮�
+                if (stateForUpdate.ChangePalletPhase == 0)
+                {
+                    stateForUpdate.ChangePalletPhase = 1;
+                    stateForUpdate.CurrentBatchIndex = 1;
+                    _logger.LogInformation("HandlePutFinishedStateAsync锛氭崲鐩樹换鍔¤繘鍏ユ壒娆℃ā寮忥紝浠诲姟鍙�: {TaskNum}锛屾祦鍚�: {Flow}",
+                        task.RobotTaskNum, isFlowA ? "A" : "B");
+                }
+
+                // ==================== 娴佸悜A锛氳ˉ鍋囩數鑺埌鐩爣鎵樼洏 ====================
+                if (isFlowA)
+                {
+                    // Phase 1: 鍙栧亣鐢佃姱锛堜粠5鍙蜂綅锛屼娇鐢� PositionIndex锛�
+                    if (stateForUpdate.ChangePalletPhase == 1)
+                    {
+                        int remaining = targetTotal - currentCompletedCount;
+                        if (remaining <= 0)
+                        {
+                            stateForUpdate.ChangePalletPhase = 0;
+                            stateForUpdate.CurrentBatchIndex = 1;
+                            stateForUpdate.IsInFakeBatteryMode = false;
+                            _logger.LogInformation("HandlePutFinishedStateAsync锛氭祦鍚慉瀹屾垚锛屼换鍔″彿: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        int pickCount = Math.Min(4, remaining);
+                        var positions = _taskProcessor.GetNextAvailableFakeBatteryPositions(pickCount);
+                        if (positions.Count == 0)
+                        {
+                            _logger.LogError("HandlePutFinishedStateAsync锛氭棤鍙敤鍋囩數鑺偣浣嶏紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        await _taskProcessor.SendSocketRobotFakeBatteryPickAsync(task, stateForUpdate, positions);
+                        stateForUpdate.ChangePalletPhase = 2;
+                    }
+                    // Phase 2: 鏀惧亣鐢佃姱鍒扮洰鏍囨墭鐩橈紙浠� targetNormalCount+1 寮�濮嬮�掑锛�
+                    else if (stateForUpdate.ChangePalletPhase == 2)
+                    {
+                        int remaining = targetTotal - currentCompletedCount;
+                        if (remaining <= 0)
+                        {
+                            stateForUpdate.ChangePalletPhase = 0;
+                            stateForUpdate.CurrentBatchIndex = 1;
+                            stateForUpdate.IsInFakeBatteryMode = false;
+                            _logger.LogInformation("HandlePutFinishedStateAsync锛氭祦鍚慉瀹屾垚锛屼换鍔″彿: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        // 璁$畻鏀捐揣鎵规缂栧彿锛氫粠 targetNormalCount + 1 寮�濮�
+                        int batchStart = targetNormalCount + 1 + (stateForUpdate.CurrentBatchIndex - 1);
+                        int putCount = Math.Min(4, remaining);
+                        var (start, end) = _taskProcessor.BuildBatchRange(batchStart, putCount);
+
+                        await _taskProcessor.SendPutWithBatchAsync(task, stateForUpdate, task.RobotTargetAddress, start, end);
+
+                        stateForUpdate.CurrentBatchIndex += putCount;
+                        stateForUpdate.ChangePalletPhase = 1;
+                    }
+                }
+                // ==================== 娴佸悜B锛氬彇姝e父鐢佃姱 + 鍥炴敹鍋囩數鑺� ====================
+                else
+                {
+                    // Phase 1: 鍙栨甯哥數鑺紙浠庢簮鍦板潃锛屼粠1寮�濮嬮�掑锛�
+                    if (stateForUpdate.ChangePalletPhase == 1)
+                    {
+                        int remainingNormal = targetNormalCount - currentCompletedCount;
+                        if (remainingNormal <= 0)
+                        {
+                            // 姝e父鐢佃姱鍙栧畬锛屽垏鎹㈠埌 Phase 3
+                            stateForUpdate.ChangePalletPhase = 3;
+                            stateForUpdate.CurrentBatchIndex = targetNormalCount + 1;
+                            _logger.LogInformation("HandlePutFinishedStateAsync锛氭甯哥數鑺叏閮ㄥ彇瀹岋紝杩涘叆Phase 3鍥炴敹鍋囩數鑺紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        int pickCount = Math.Min(4, remainingNormal);
+                        var (start, end) = _taskProcessor.BuildBatchRange(stateForUpdate.CurrentBatchIndex, pickCount);
+
+                        await _taskProcessor.SendPickWithBatchAsync(task, stateForUpdate, task.RobotSourceAddress, start, end);
+
+                        stateForUpdate.CurrentBatchIndex += pickCount;
+                        stateForUpdate.ChangePalletPhase = 2;
+                    }
+                    // Phase 2: 鏀炬甯哥數鑺埌鐩爣鎵樼洏锛堟斁璐х紪鍙蜂笌鍙栬揣缂栧彿涓�鑷达級
+                    else if (stateForUpdate.ChangePalletPhase == 2)
+                    {
+                        int remainingNormal = targetNormalCount - currentCompletedCount;
+                        if (remainingNormal <= 0)
+                        {
+                            // 姝e父鐢佃姱鏀惧畬锛屽垏鎹㈠埌 Phase 3
+                            stateForUpdate.ChangePalletPhase = 3;
+                            stateForUpdate.CurrentBatchIndex = targetNormalCount + 1;
+                            _logger.LogInformation("HandlePutFinishedStateAsync锛氭甯哥數鑺叏閮ㄦ斁瀹岋紝杩涘叆Phase 3鍥炴敹鍋囩數鑺紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        // 璁$畻鏈壒鏀捐揣缂栧彿锛氬熀浜� currentCompletedCount 鎺ㄥ鎵规璧峰
+                        int batchStart = ((currentCompletedCount - 1) / 4) * 4 + 1;
+                        int putCount = Math.Min(4, remainingNormal);
+                        var (start, end) = _taskProcessor.BuildBatchRange(batchStart, putCount);
+
+                        await _taskProcessor.SendPutWithBatchAsync(task, stateForUpdate, task.RobotTargetAddress, start, end);
+
+                        stateForUpdate.ChangePalletPhase = 1;
+                    }
+                    // Phase 3: 鍙栧亣鐢佃姱锛堜粠婧愬湴鍧�锛屼粠 targetNormalCount+1 寮�濮嬮�掑锛�
+                    else if (stateForUpdate.ChangePalletPhase == 3)
+                    {
+                        int fakeCount = targetTotal - targetNormalCount;
+                        int completedFake = Math.Max(0, currentCompletedCount - targetNormalCount);
+                        int remainingFake = fakeCount - completedFake;
+
+                        if (remainingFake <= 0)
+                        {
+                            stateForUpdate.ChangePalletPhase = 0;
+                            stateForUpdate.CurrentBatchIndex = 1;
+                            stateForUpdate.IsInFakeBatteryMode = false;
+                            _logger.LogInformation("HandlePutFinishedStateAsync锛氭祦鍚態瀹屾垚锛屼换鍔″彿: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        int pickCount = Math.Min(4, remainingFake);
+                        var (start, end) = _taskProcessor.BuildBatchRange(stateForUpdate.CurrentBatchIndex, pickCount);
+
+                        await _taskProcessor.SendPickWithBatchAsync(task, stateForUpdate, task.RobotSourceAddress, start, end);
+
+                        stateForUpdate.CurrentBatchIndex += pickCount;
+                        stateForUpdate.ChangePalletPhase = 4;
+                    }
+                    // Phase 4: 鏀惧亣鐢佃姱鍒�5鍙蜂綅锛堜娇鐢� PositionIndex锛�
+                    else if (stateForUpdate.ChangePalletPhase == 4)
+                    {
+                        int fakeCount = targetTotal - targetNormalCount;
+                        int completedFake = Math.Max(0, currentCompletedCount - targetNormalCount);
+                        int remainingFake = fakeCount - completedFake;
+
+                        if (remainingFake <= 0)
+                        {
+                            stateForUpdate.ChangePalletPhase = 0;
+                            stateForUpdate.CurrentBatchIndex = 1;
+                            stateForUpdate.IsInFakeBatteryMode = false;
+                            _logger.LogInformation("HandlePutFinishedStateAsync锛氭祦鍚態瀹屾垚锛屼换鍔″彿: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        var positions = _taskProcessor.GetNextAvailableFakeBatteryPositions(Math.Min(4, remainingFake));
+                        if (positions.Count == 0)
+                        {
+                            _logger.LogError("HandlePutFinishedStateAsync锛氭棤鍙敤鍋囩數鑺偣浣嶏紝浠诲姟鍙�: {TaskNum}", task.RobotTaskNum);
+                            return;
+                        }
+
+                        int start = positions.Min();
+                        int end = positions.Max();
+
+                        await _taskProcessor.SendPutWithBatchAsync(task, stateForUpdate, "5", start, end);
+
+                        stateForUpdate.ChangePalletPhase = 3;
+                    }
                 }
             }
             else
             {
                 // 闈炵粍鐩樹换鍔★紝鐩存帴鍙戦�佸彇璐ф寚浠�
-                await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate);
+                await _taskProcessor.SendSocketRobotPickAsync(task, stateForUpdate, false);
             }
         }
     }

--
Gitblit v1.9.3