From 3e28500e76ffd71b7da90f40d1f3126c00a0e5f0 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 13 四月 2026 14:49:41 +0800
Subject: [PATCH] #站点运行优化

---
 src/main/java/com/zy/core/thread/impl/station/StationSegmentExecutor.java |   74 ++++++++++++++++++++++++++++++++++++
 1 files changed, 73 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/zy/core/thread/impl/station/StationSegmentExecutor.java b/src/main/java/com/zy/core/thread/impl/station/StationSegmentExecutor.java
index eca8680..7d81825 100644
--- a/src/main/java/com/zy/core/thread/impl/station/StationSegmentExecutor.java
+++ b/src/main/java/com/zy/core/thread/impl/station/StationSegmentExecutor.java
@@ -15,6 +15,8 @@
 import com.zy.core.model.protocol.StationProtocol;
 import com.zy.core.move.StationMoveCoordinator;
 import com.zy.core.trace.StationTaskTraceRegistry;
+import com.zy.core.thread.impl.v5.StationV5RuntimeConfigProvider;
+import com.zy.core.thread.support.StationTaskLocationRegistry;
 import com.zy.system.entity.Config;
 import com.zy.system.service.ConfigService;
 
@@ -29,6 +31,7 @@
     private static final String CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = "stationCommandSegmentAdvanceRatio";
     private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d;
     private static final long CURRENT_STATION_TIMEOUT_MS = 1000L * 60L;
+    private static final long TASK_LOCATION_STALE_MS = 2_000L;
 
     private final DeviceConfig deviceConfig;
     private final RedisUtil redisUtil;
@@ -89,6 +92,7 @@
         long lastSeenAt = System.currentTimeMillis();
         int segCursor = 0;
         Integer lastCurrentStationId = null;
+        int lastMatchedPathIndex = -1;
         boolean firstRun = true;
         while (true) {
             try {
@@ -144,12 +148,17 @@
                     break;
                 }
 
-                int currentIndex = effectiveFullPath.indexOf(currentStation.getStationId());
+                int currentIndex = resolveCurrentPathIndex(
+                        effectiveFullPath,
+                        currentStation.getStationId(),
+                        lastMatchedPathIndex
+                );
                 if (currentIndex < 0) {
                     Thread.sleep(500L);
                     firstRun = false;
                     continue;
                 }
+                lastMatchedPathIndex = currentIndex;
 
                 int remaining = effectiveFullPath.size() - currentIndex - 1;
                 if (remaining <= 0) {
@@ -190,6 +199,38 @@
                 break;
             }
         }
+    }
+
+    private int resolveCurrentPathIndex(List<Integer> fullPathStationIds,
+                                        Integer currentStationId,
+                                        int lastMatchedPathIndex) {
+        if (fullPathStationIds == null || fullPathStationIds.isEmpty() || currentStationId == null) {
+            return -1;
+        }
+        if (lastMatchedPathIndex >= 0
+                && lastMatchedPathIndex < fullPathStationIds.size()
+                && equalsInteger(currentStationId, fullPathStationIds.get(lastMatchedPathIndex))) {
+            return lastMatchedPathIndex;
+        }
+
+        int nextIndex = findNextStationIndex(fullPathStationIds, currentStationId, Math.max(lastMatchedPathIndex + 1, 0));
+        if (nextIndex >= 0) {
+            return nextIndex;
+        }
+        return findNextStationIndex(fullPathStationIds, currentStationId, 0);
+    }
+
+    private int findNextStationIndex(List<Integer> path, Integer stationId, int fromIndex) {
+        if (path == null || path.isEmpty() || stationId == null) {
+            return -1;
+        }
+        int startIdx = Math.max(fromIndex, 0);
+        for (int i = startIdx; i < path.size(); i++) {
+            if (equalsInteger(stationId, path.get(i))) {
+                return i;
+            }
+        }
+        return -1;
     }
 
     private boolean sendSegmentWithRetry(StationCommand command,
@@ -272,6 +313,13 @@
     }
 
     private double loadSegmentAdvanceRatio() {
+        if (isV5ThreadImpl()) {
+            StationV5RuntimeConfigProvider configProvider = SpringUtils.getBean(StationV5RuntimeConfigProvider.class);
+            if (configProvider != null) {
+                return configProvider.getSegmentAdvanceRatio();
+            }
+            return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO;
+        }
         try {
             ConfigService configService = SpringUtils.getBean(ConfigService.class);
             if (configService == null) {
@@ -325,6 +373,9 @@
     }
 
     private StationProtocol findCurrentStationByTask(Integer taskNo) {
+        if (isV5ThreadImpl()) {
+            return findCurrentStationByTaskFromRegistry(taskNo);
+        }
         try {
             com.zy.asrs.service.DeviceConfigService deviceConfigService = SpringUtils.getBean(com.zy.asrs.service.DeviceConfigService.class);
             if (deviceConfigService == null) {
@@ -353,6 +404,27 @@
         return null;
     }
 
+    private StationProtocol findCurrentStationByTaskFromRegistry(Integer taskNo) {
+        StationTaskLocationRegistry registry = SpringUtils.getBean(StationTaskLocationRegistry.class);
+        if (registry == null) {
+            return null;
+        }
+        StationTaskLocationRegistry.TaskLocationSnapshot snapshot = registry.findActive(taskNo, TASK_LOCATION_STALE_MS);
+        if (snapshot == null || !snapshot.isLoading()) {
+            return null;
+        }
+        StationProtocol stationProtocol = new StationProtocol();
+        stationProtocol.setTaskNo(snapshot.getTaskNo());
+        stationProtocol.setStationId(snapshot.getStationId());
+        stationProtocol.setRunBlock(snapshot.isRunBlock());
+        stationProtocol.setLoading(true);
+        return stationProtocol;
+    }
+
+    private boolean isV5ThreadImpl() {
+        return deviceConfig != null && "ZyStationV5Thread".equals(deviceConfig.getThreadImpl());
+    }
+
     private List<StationTaskTraceSegmentVo> buildTraceSegments(List<StationCommand> segmentCommands) {
         List<StationTaskTraceSegmentVo> result = new ArrayList<>();
         if (segmentCommands == null) {

--
Gitblit v1.9.1