From c34f9c17fde14f78b3663803e9776d438e8481b9 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期六, 21 三月 2026 22:30:14 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/thread/impl/ZyStationV5Thread.java |   55 ++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 32 insertions(+), 23 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/ZyStationV5Thread.java b/src/main/java/com/zy/core/thread/impl/ZyStationV5Thread.java
index 212a2ec..f8d7abb 100644
--- a/src/main/java/com/zy/core/thread/impl/ZyStationV5Thread.java
+++ b/src/main/java/com/zy/core/thread/impl/ZyStationV5Thread.java
@@ -31,6 +31,7 @@
 import com.zy.core.network.DeviceConnectPool;
 import com.zy.core.network.ZyStationConnectDriver;
 import com.zy.core.network.entity.ZyStationStatusEntity;
+import com.zy.core.service.StationTaskLoopService;
 import com.zy.core.thread.impl.v5.StationV5SegmentExecutor;
 import com.zy.core.trace.StationTaskTraceRegistry;
 import com.zy.core.utils.DeviceLogRedisKeyBuilder;
@@ -59,7 +60,7 @@
 
     private static final int RUN_BLOCK_REROUTE_STATE_EXPIRE_SECONDS = 60 * 60 * 24;
     private static final int SHORT_PATH_REPEAT_AVOID_THRESHOLD = 2;
-    private static final int LOOP_REPEAT_TRIGGER_COUNT = 2;
+    private static final int LOOP_REPEAT_TRIGGER_COUNT = 3;
     private static final int LOCAL_LOOP_NEIGHBOR_HOP = 3;
 
     private List<StationProtocol> statusList = new ArrayList<>();
@@ -264,20 +265,21 @@
         }
 
         RunBlockRerouteState rerouteState = loadRunBlockRerouteState(taskNo, stationId);
-        TaskLoopRerouteState taskLoopRerouteState = loadTaskLoopRerouteState(taskNo);
-        LoopIdentity currentLoopIdentity = resolveStationLoopIdentity(stationId);
+        StationTaskLoopService taskLoopService = loadStationTaskLoopService();
+        StationTaskLoopService.LoopEvaluation loopEvaluation = taskLoopService == null
+                ? new StationTaskLoopService.LoopEvaluation(taskNo, stationId, StationTaskLoopService.LoopIdentitySnapshot.empty(), 0, 0, false)
+                : taskLoopService.evaluateLoop(taskNo, stationId, true);
         log.info("杈撻�佺嚎鍫靛閲嶈鍒掔幆绾胯瘑鍒紝taskNo={}, stationId={}, scopeType={}, localStationCount={}, sourceLoopStationCount={}",
                 taskNo,
                 stationId,
-                currentLoopIdentity.getScopeType(),
-                currentLoopIdentity.getLocalStationCount(),
-                currentLoopIdentity.getSourceLoopStationCount());
+                loopEvaluation.getLoopIdentity().getScopeType(),
+                loopEvaluation.getLoopIdentity().getLocalStationCount(),
+                loopEvaluation.getLoopIdentity().getSourceLoopStationCount());
         rerouteState.setTaskNo(taskNo);
         rerouteState.setBlockStationId(stationId);
         rerouteState.setLastTargetStationId(targetStationId);
         rerouteState.setPlanCount((rerouteState.getPlanCount() == null ? 0 : rerouteState.getPlanCount()) + 1);
         rerouteState.setLastPlanTime(System.currentTimeMillis());
-        taskLoopRerouteState.setTaskNo(taskNo);
 
         List<List<NavigateNode>> candidatePathList = calcCandidatePathNavigateNodes(taskNo, stationId, targetStationId);
         if (candidatePathList.isEmpty()) {
@@ -289,8 +291,7 @@
 
         StationCommand rerouteCommand = selectAvailableRerouteCommand(
                 rerouteState,
-                taskLoopRerouteState,
-                currentLoopIdentity,
+                loopEvaluation,
                 candidatePathList,
                 taskNo,
                 stationId,
@@ -303,8 +304,7 @@
             rerouteState.resetIssuedRoutes();
             rerouteCommand = selectAvailableRerouteCommand(
                     rerouteState,
-                    taskLoopRerouteState,
-                    currentLoopIdentity,
+                    loopEvaluation,
                     candidatePathList,
                     taskNo,
                     stationId,
@@ -315,10 +315,9 @@
 
         if (rerouteCommand != null) {
             saveRunBlockRerouteState(rerouteState);
-            touchTaskLoopRerouteState(taskLoopRerouteState, currentLoopIdentity);
-            syncTaskTraceLoopAlert(taskNo, stationId, currentLoopIdentity,
-                    resolveCurrentLoopIssuedCount(taskLoopRerouteState, currentLoopIdentity));
-            saveTaskLoopRerouteState(taskLoopRerouteState);
+            if (taskLoopService != null) {
+                taskLoopService.recordLoopIssue(loopEvaluation, "RUN_BLOCK_REROUTE");
+            }
             log.info("杈撻�佺嚎鍫靛閲嶈鍒掗�変腑鍊欓�夎矾绾匡紝taskNo={}, planCount={}, stationId={}, targetStationId={}, route={}",
                     taskNo, rerouteState.getPlanCount(), stationId, targetStationId, JSON.toJSONString(rerouteCommand.getNavigatePath()));
             return rerouteCommand;
@@ -455,8 +454,7 @@
     }
 
     private StationCommand selectAvailableRerouteCommand(RunBlockRerouteState rerouteState,
-                                                         TaskLoopRerouteState taskLoopRerouteState,
-                                                         LoopIdentity currentLoopIdentity,
+                                                         StationTaskLoopService.LoopEvaluation loopEvaluation,
                                                          List<List<NavigateNode>> candidatePathList,
                                                          Integer taskNo,
                                                          Integer stationId,
@@ -467,7 +465,6 @@
         }
 
         Set<String> issuedRouteSignatureSet = rerouteState.getIssuedRouteSignatureSet();
-        int currentLoopIssuedCount = resolveExpectedLoopIssuedCount(taskLoopRerouteState, currentLoopIdentity);
         List<RerouteCandidateCommand> candidateCommandList = new ArrayList<>();
         for (List<NavigateNode> candidatePath : candidatePathList) {
             StationCommand rerouteCommand = buildMoveCommand(taskNo, stationId, targetStationId, palletSize, candidatePath);
@@ -483,9 +480,13 @@
             candidateCommand.setRouteSignature(routeSignature);
             candidateCommand.setPathLength(rerouteCommand.getNavigatePath().size());
             candidateCommand.setIssuedCount(rerouteState.getRouteIssueCountMap().getOrDefault(routeSignature, 0));
-            candidateCommand.setLoopFingerprint(currentLoopIdentity.getLoopFingerprint());
-            candidateCommand.setLoopIssuedCount(currentLoopIssuedCount);
-            candidateCommand.setCurrentLoopHitCount(countCurrentLoopStationHit(rerouteCommand.getNavigatePath(), currentLoopIdentity.getStationIdSet()));
+            candidateCommand.setLoopFingerprint(loopEvaluation.getLoopIdentity().getLoopFingerprint());
+            candidateCommand.setLoopIssuedCount(loopEvaluation.getExpectedLoopIssueCount());
+            candidateCommand.setLoopTriggered(loopEvaluation.isLargeLoopTriggered());
+            candidateCommand.setCurrentLoopHitCount(countCurrentLoopStationHit(
+                    rerouteCommand.getNavigatePath(),
+                    loopEvaluation.getLoopIdentity().getStationIdSet()
+            ));
             candidateCommandList.add(candidateCommand);
         }
         if (candidateCommandList.isEmpty()) {
@@ -550,8 +551,7 @@
                 shortestPathOverused = true;
             }
             if (!Cools.isEmpty(candidateCommand.getLoopFingerprint())
-                    && candidateCommand.getLoopIssuedCount() != null
-                    && isLoopRepeatTriggered(candidateCommand.getLoopIssuedCount())) {
+                    && Boolean.TRUE.equals(candidateCommand.getLoopTriggered())) {
                 currentLoopOverused = true;
             }
         }
@@ -784,6 +784,14 @@
             builder.append(stationNo);
         }
         return builder.toString();
+    }
+
+    private StationTaskLoopService loadStationTaskLoopService() {
+        try {
+            return SpringUtils.getBean(StationTaskLoopService.class);
+        } catch (Exception ignore) {
+            return null;
+        }
     }
 
     private String buildRunBlockRerouteStateKey(Integer taskNo, Integer blockStationId) {
@@ -1161,6 +1169,7 @@
         private Integer issuedCount;
         private String loopFingerprint;
         private Integer loopIssuedCount;
+        private Boolean loopTriggered;
         private Integer currentLoopHitCount;
     }
 

--
Gitblit v1.9.1