From 205cb40876114827a2134c29fde159e063e81454 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期六, 21 三月 2026 09:54:42 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/utils/StationOperateProcessUtils.java        |  106 +++++++++++++++++++-------
 src/main/java/com/zy/core/thread/impl/ZyStationV3Thread.java           |   50 +++++++++---
 src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java           |   62 +++++++++------
 src/main/java/com/zy/core/thread/impl/v5/StationV5SegmentExecutor.java |   11 ++
 4 files changed, 162 insertions(+), 67 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZyStationV3Thread.java b/src/main/java/com/zy/core/thread/impl/ZyStationV3Thread.java
index d5497fe..af286c8 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationV3Thread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationV3Thread.java
@@ -339,7 +339,9 @@
             segCmd.setCommandType(original.getCommandType());
             segCmd.setPalletSize(original.getPalletSize());
             segCmd.setNavigatePath(new ArrayList<>(path.subList(0, currentEndIdx + 1)));
-            sendCommand(segCmd);
+            if (!sendSegmentWithRetry(segCmd, original.getTaskNo())) {
+                return;
+            }
 
             long runTime = System.currentTimeMillis();
             boolean firstRun = true;
@@ -390,18 +392,8 @@
                         nextCmd.setPalletSize(original.getPalletSize());
                         nextCmd.setNavigatePath(new ArrayList<>(path.subList(currentStartIdx, currentEndIdx + 1)));
                         nextCmd.setOriginalNavigatePath(path);
-                        while (true) {
-                            CommandResponse commandResponse = sendCommand(nextCmd);
-                            if (commandResponse == null) {
-                                Thread.sleep(200);
-                                continue;
-                            }
-
-                            if (commandResponse.getResult()) {
-                                break;
-                            }
-
-                            Thread.sleep(200);
+                        if (!sendSegmentWithRetry(nextCmd, original.getTaskNo())) {
+                            break;
                         }
                     }
                     Thread.sleep(500);
@@ -443,4 +435,36 @@
         }
         return null;
     }
+
+    private boolean sendSegmentWithRetry(StationCommand command, Integer taskNo) {
+        while (true) {
+            if (isTaskMoveReset(taskNo)) {
+                return false;
+            }
+            CommandResponse commandResponse = sendCommand(command);
+            if (commandResponse == null) {
+                sleepQuietly(200L);
+                continue;
+            }
+            if (commandResponse.getResult()) {
+                return true;
+            }
+            sleepQuietly(200L);
+        }
+    }
+
+    private boolean isTaskMoveReset(Integer taskNo) {
+        if (taskNo == null || redisUtil == null) {
+            return false;
+        }
+        Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo);
+        return cancel != null;
+    }
+
+    private void sleepQuietly(long millis) {
+        try {
+            Thread.sleep(millis);
+        } catch (Exception ignore) {
+        }
+    }
 }
diff --git a/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java b/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
index 86f57a8..19b3642 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationV4Thread.java
@@ -383,20 +383,8 @@
             }
 
             int segCursor = 0;
-            while (true) {
-                CommandResponse commandResponse = sendCommand(segmentCommands.get(segCursor));
-                if (commandResponse == null) {
-                    try {
-                        Thread.sleep(200);
-                    } catch (Exception ignore) {}
-                    continue;
-                }
-                if (commandResponse.getResult()) {
-                    break;
-                }
-                try {
-                    Thread.sleep(200);
-                } catch (Exception ignore) {}
+            if (!sendSegmentWithRetry(segmentCommands.get(segCursor), original.getTaskNo())) {
+                return;
             }
 
             long runTime = System.currentTimeMillis();
@@ -437,18 +425,8 @@
                     int thresholdSegment = (int) Math.ceil(segLen * segmentAdvanceRatio);
                     if (remainingSegment <= thresholdSegment && segCursor < segmentCommands.size() - 1) {
                         segCursor++;
-                        while (true) {
-                            CommandResponse commandResponse = sendCommand(segmentCommands.get(segCursor));
-                            if (commandResponse == null) {
-                                Thread.sleep(200);
-                                continue;
-                            }
-
-                            if (commandResponse.getResult()) {
-                                break;
-                            }
-
-                            Thread.sleep(200);
+                        if (!sendSegmentWithRetry(segmentCommands.get(segCursor), original.getTaskNo())) {
+                            break;
                         }
                     }
                     Thread.sleep(500);
@@ -535,4 +513,36 @@
         }
         return null;
     }
+
+    private boolean sendSegmentWithRetry(StationCommand command, Integer taskNo) {
+        while (true) {
+            if (isTaskMoveReset(taskNo)) {
+                return false;
+            }
+            CommandResponse commandResponse = sendCommand(command);
+            if (commandResponse == null) {
+                sleepQuietly(200L);
+                continue;
+            }
+            if (commandResponse.getResult()) {
+                return true;
+            }
+            sleepQuietly(200L);
+        }
+    }
+
+    private boolean isTaskMoveReset(Integer taskNo) {
+        if (taskNo == null || redisUtil == null) {
+            return false;
+        }
+        Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo);
+        return cancel != null;
+    }
+
+    private void sleepQuietly(long millis) {
+        try {
+            Thread.sleep(millis);
+        } catch (Exception ignore) {
+        }
+    }
 }
diff --git a/src/main/java/com/zy/core/thread/impl/v5/StationV5SegmentExecutor.java b/src/main/java/com/zy/core/thread/impl/v5/StationV5SegmentExecutor.java
index 96320de..7e749fb 100644
--- a/src/main/java/com/zy/core/thread/impl/v5/StationV5SegmentExecutor.java
+++ b/src/main/java/com/zy/core/thread/impl/v5/StationV5SegmentExecutor.java
@@ -167,6 +167,9 @@
 
     private boolean sendSegmentWithRetry(StationCommand command) {
         while (true) {
+            if (isTaskMoveReset(command == null ? null : command.getTaskNo())) {
+                return false;
+            }
             CommandResponse commandResponse = commandSender.apply(command);
             if (commandResponse == null) {
                 sleepQuietly(200L);
@@ -179,6 +182,14 @@
         }
     }
 
+    private boolean isTaskMoveReset(Integer taskNo) {
+        if (taskNo == null || redisUtil == null) {
+            return false;
+        }
+        Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo);
+        return cancel != null;
+    }
+
     private double loadSegmentAdvanceRatio() {
         try {
             ConfigService configService = SpringUtils.getBean(ConfigService.class);
diff --git a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
index 5bfa108..4ec5e35 100644
--- a/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
+++ b/src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
@@ -40,6 +40,8 @@
     private static final int STATION_IDLE_RECOVER_SECONDS = 10;
     private static final int STATION_IDLE_RECOVER_LIMIT_SECONDS = 10;
     private static final int STATION_IDLE_TRACK_EXPIRE_SECONDS = 60 * 60;
+    private static final long STATION_MOVE_RESET_WAIT_MS = 1000L;
+    private static final String IDLE_RECOVER_CLEARED_MEMO = "idleRecoverRerouteCleared";
 
     @Autowired
     private BasDevpService basDevpService;
@@ -958,10 +960,6 @@
             return;
         }
 
-        if (hasFollowUpMoveCommandAfterStay(idleTrack, stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
-            return;
-        }
-
         OutOrderDispatchDecision dispatchDecision = null;
         Integer moveStaNo;
         if (Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)) {
@@ -975,6 +973,8 @@
         }
 
         redisUtil.set(RedisKeyType.CHECK_STATION_IDLE_RECOVER_LIMIT_.key + stationProtocol.getTaskNo(), "lock", STATION_IDLE_RECOVER_LIMIT_SECONDS);
+        resetSegmentMoveCommandsBeforeReroute(stationProtocol.getTaskNo());
+        int clearedCommandCount = clearIssuedMoveCommandsDuringIdleStay(idleTrack, stationProtocol.getTaskNo(), stationProtocol.getStationId());
 
         StationCommand command = stationThread.getCommand(
                 StationCommandType.MOVE,
@@ -991,8 +991,8 @@
         MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
         syncOutOrderWatchState(wrkMast, stationProtocol.getStationId(), outOrderList, dispatchDecision, command);
         saveStationTaskIdleTrack(new StationTaskIdleTrack(wrkMast.getWrkNo(), stationProtocol.getStationId(), System.currentTimeMillis()));
-        News.info("杈撻�佺珯鐐逛换鍔″仠鐣檣}绉掓湭杩愯锛屽凡閲嶆柊璁$畻璺緞骞堕噸鍚繍琛岋紝绔欑偣鍙�={}锛岀洰鏍囩珯={}锛屽伐浣滃彿={}锛屽懡浠ゆ暟鎹�={}",
-                STATION_IDLE_RECOVER_SECONDS, stationProtocol.getStationId(), moveStaNo, wrkMast.getWrkNo(), JSON.toJSONString(command));
+        News.info("杈撻�佺珯鐐逛换鍔″仠鐣檣}绉掓湭杩愯锛屽凡閲嶆柊璁$畻璺緞骞堕噸鍚繍琛岋紝绔欑偣鍙�={}锛岀洰鏍囩珯={}锛屽伐浣滃彿={}锛屾竻鐞嗘棫鍒嗘鍛戒护鏁�={}锛屽懡浠ゆ暟鎹�={}",
+                STATION_IDLE_RECOVER_SECONDS, stationProtocol.getStationId(), moveStaNo, wrkMast.getWrkNo(), clearedCommandCount, JSON.toJSONString(command));
     }
 
     private boolean canRecoverIdleStationTask(WrkMast wrkMast, Integer currentStationId) {
@@ -1006,41 +1006,91 @@
                 || Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts);
     }
 
-    private boolean hasFollowUpMoveCommandAfterStay(StationTaskIdleTrack idleTrack,
-                                                    Integer taskNo,
-                                                    Integer stationId) {
-        if (idleTrack == null || taskNo == null || stationId == null || idleTrack.firstSeenTime == null) {
-            return false;
+    private void resetSegmentMoveCommandsBeforeReroute(Integer taskNo) {
+        if (redisUtil == null || taskNo == null || taskNo <= 0) {
+            return;
         }
-        if (basStationOptService == null) {
-            return false;
+        String key = RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo;
+        redisUtil.set(key, "cancel", 3);
+        try {
+            Thread.sleep(STATION_MOVE_RESET_WAIT_MS);
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+        } catch (Exception ignore) {
         }
+        redisUtil.del(key);
+    }
 
+    private int clearIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
+                                                      Integer taskNo,
+                                                      Integer stationId) {
+        if (basStationOptService == null) {
+            return 0;
+        }
         List<BasStationOpt> optList;
         try {
-            optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
-                    .select("id", "target_station_id")
-                    .eq("task_no", taskNo)
-                    .eq("source_station_id", stationId)
-                    .eq("mode", String.valueOf(StationCommandType.MOVE))
-                    .eq("send", 1)
-                    .ge("send_time", new Date(idleTrack.firstSeenTime))
-                    .orderByDesc("send_time")
-                    .last("limit 3"));
+            optList = listIssuedMoveCommandsDuringIdleStay(idleTrack, taskNo);
         } catch (Exception e) {
-            return false;
+            return 0;
         }
-
         if (optList == null || optList.isEmpty()) {
-            return false;
+            return 0;
         }
 
+        Date now = new Date();
+        String cleanupMemo = buildIdleRecoverClearedMemo(stationId);
+        int clearedCount = 0;
         for (BasStationOpt opt : optList) {
-            if (opt != null && opt.getTargetStationId() != null && !Objects.equals(opt.getTargetStationId(), stationId)) {
-                return true;
+            if (opt == null || opt.getId() == null) {
+                continue;
             }
+            opt.setSend(0);
+            opt.setUpdateTime(now);
+            opt.setMemo(appendCleanupMemo(opt.getMemo(), cleanupMemo));
+            clearedCount++;
         }
-        return false;
+        if (clearedCount > 0) {
+            basStationOptService.updateBatchById(optList);
+        }
+        return clearedCount;
+    }
+
+    private List<BasStationOpt> listIssuedMoveCommandsDuringIdleStay(StationTaskIdleTrack idleTrack,
+                                                                     Integer taskNo) {
+        if (idleTrack == null || taskNo == null || taskNo <= 0 || idleTrack.firstSeenTime == null || basStationOptService == null) {
+            return Collections.emptyList();
+        }
+        List<BasStationOpt> optList = basStationOptService.list(new QueryWrapper<BasStationOpt>()
+                .select("id", "task_no", "send_time", "target_station_id", "memo", "send")
+                .eq("task_no", taskNo)
+                .eq("mode", String.valueOf(StationCommandType.MOVE))
+                .eq("send", 1)
+                .ge("send_time", new Date(idleTrack.firstSeenTime))
+                .orderByAsc("send_time"));
+        if (optList == null || optList.isEmpty()) {
+            return Collections.emptyList();
+        }
+        return optList;
+    }
+
+    private String buildIdleRecoverClearedMemo(Integer stationId) {
+        if (stationId == null) {
+            return IDLE_RECOVER_CLEARED_MEMO;
+        }
+        return IDLE_RECOVER_CLEARED_MEMO + "(stationId=" + stationId + ")";
+    }
+
+    private String appendCleanupMemo(String memo, String cleanupMemo) {
+        if (Cools.isEmpty(cleanupMemo)) {
+            return memo;
+        }
+        if (Cools.isEmpty(memo)) {
+            return cleanupMemo;
+        }
+        if (memo.contains(cleanupMemo)) {
+            return memo;
+        }
+        return memo + " | " + cleanupMemo;
     }
 
     private StationTaskIdleTrack touchStationTaskIdleTrack(Integer taskNo, Integer stationId) {

--
Gitblit v1.9.1