From 1ef1063281497f32fcfa4f14b07d99399c0bb765 Mon Sep 17 00:00:00 2001
From: jinglun-cloud <jinglun2019@foxmail.com>
Date: 星期四, 07 五月 2026 15:04:17 +0800
Subject: [PATCH] refactor(设备运动): 重构条码设备运动逻辑,提取运动常量并优化代码结构

---
 src/main/java/com/zy/common/utils/NavigateUtils.java |   57 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NavigateUtils.java b/src/main/java/com/zy/common/utils/NavigateUtils.java
index 8d5ef42..2dbe19c 100644
--- a/src/main/java/com/zy/common/utils/NavigateUtils.java
+++ b/src/main/java/com/zy/common/utils/NavigateUtils.java
@@ -27,6 +27,7 @@
 import com.zy.asrs.service.StationCycleCapacityService;
 import com.zy.asrs.service.StationPathPolicyService;
 import com.zy.core.News;
+import org.springframework.scheduling.annotation.Scheduled;
 import com.zy.core.model.StationObjModel;
 import com.zy.core.model.command.StationCommand;
 import com.zy.core.enums.StationCommandType;
@@ -77,11 +78,23 @@
     @Autowired
     private StationTaskTraceRegistry stationTaskTraceRegistry;
 
-    private final Object runtimeSnapshotLock = new Object();
     private volatile CachedStationPathRuntimeSnapshot cachedRuntimeSnapshot;
 
-    private final Object loopMergeGuardLock = new Object();
     private volatile CachedLoopMergeGuardContext cachedLoopMergeGuardContext;
+
+    @Scheduled(fixedDelay = 1500, initialDelay = 3000)
+    public void refreshStationPathCaches() {
+        try {
+            BaseRuntimeSnapshot snapshot = buildBaseRuntimeSnapshot(null);
+            cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(System.currentTimeMillis(), snapshot);
+        } catch (Exception ignore) {
+        }
+        try {
+            LoopMergeGuardContext context = buildLoopMergeGuardContext();
+            cachedLoopMergeGuardContext = new CachedLoopMergeGuardContext(System.currentTimeMillis(), context);
+        } catch (Exception ignore) {
+        }
+    }
 
     public List<NavigateNode> calcOptimalPathByStationId(Integer startStationId,
                                                          Integer endStationId,
@@ -1691,22 +1704,21 @@
             }
             return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot();
         }
-        synchronized (runtimeSnapshotLock) {
-            cachedSnapshot = cachedRuntimeSnapshot;
-            if (cachedSnapshot != null && now - cachedSnapshot.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
-                if (stepCostMap != null) {
-                    stepCostMap.put("baseSnapshotCacheHit", 0L);
-                }
-                return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot();
-            }
-            long stepStartNs = System.nanoTime();
-            BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(stepCostMap);
+        // 缂撳瓨杩囨湡锛氱敤杩囨湡缂撳瓨鍏滃簳锛園Scheduled 瀹氭椂浠诲姟浼氬緢蹇埛鏂帮級锛岄伩鍏嶅绾跨▼闃诲
+        if (cachedSnapshot != null) {
             if (stepCostMap != null) {
-                stepCostMap.put("buildBaseSnapshot", elapsedMillis(stepStartNs));
+                stepCostMap.put("baseSnapshotStale", 0L);
             }
-            cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(System.currentTimeMillis(), baseRuntimeSnapshot);
-            return baseRuntimeSnapshot;
+            return cachedSnapshot.baseRuntimeSnapshot.toCacheHitSnapshot();
         }
+        // 鏋佺鎯呭喌锛氫粠鏈湁杩囩紦瀛橈紙鍚姩棣栨璋冪敤锛夛紝鍚屾鏋勫缓
+        long stepStartNs = System.nanoTime();
+        BaseRuntimeSnapshot baseRuntimeSnapshot = buildBaseRuntimeSnapshot(stepCostMap);
+        if (stepCostMap != null) {
+            stepCostMap.put("buildBaseSnapshot", elapsedMillis(stepStartNs));
+        }
+        cachedRuntimeSnapshot = new CachedStationPathRuntimeSnapshot(System.currentTimeMillis(), baseRuntimeSnapshot);
+        return baseRuntimeSnapshot;
     }
 
     private BaseRuntimeSnapshot buildBaseRuntimeSnapshot(Map<String, Long> stepCostMap) {
@@ -1774,15 +1786,14 @@
         if (cached != null && now - cached.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
             return new LoopMergeGuardContext(cached.context);
         }
-        synchronized (loopMergeGuardLock) {
-            cached = cachedLoopMergeGuardContext;
-            if (cached != null && now - cached.cacheAtMs <= STATION_PATH_RUNTIME_SNAPSHOT_TTL_MS) {
-                return new LoopMergeGuardContext(cached.context);
-            }
-            LoopMergeGuardContext context = buildLoopMergeGuardContext();
-            cachedLoopMergeGuardContext = new CachedLoopMergeGuardContext(System.currentTimeMillis(), context);
-            return new LoopMergeGuardContext(context);
+        // 缂撳瓨杩囨湡锛氱敤杩囨湡缂撳瓨鍏滃簳锛園Scheduled 瀹氭椂浠诲姟浼氬緢蹇埛鏂帮級
+        if (cached != null) {
+            return new LoopMergeGuardContext(cached.context);
         }
+        // 鏋佺鎯呭喌锛氫粠鏈湁杩囩紦瀛橈紝鍚屾鏋勫缓
+        LoopMergeGuardContext context = buildLoopMergeGuardContext();
+        cachedLoopMergeGuardContext = new CachedLoopMergeGuardContext(System.currentTimeMillis(), context);
+        return new LoopMergeGuardContext(context);
     }
 
     private LoopMergeGuardContext buildLoopMergeGuardContext() {

--
Gitblit v1.9.1