From 12f30a09bce1c61b2ae90129124cdc467a59b074 Mon Sep 17 00:00:00 2001
From: jianghaiyue <jianghaiyue@zkyt.com>
Date: 星期三, 05 十一月 2025 15:03:17 +0800
Subject: [PATCH] 优化更新

---
 algo-zkd/src/main/java/com/algo/service/RemainingPathProcessor.java |  105 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 80 insertions(+), 25 deletions(-)

diff --git a/algo-zkd/src/main/java/com/algo/service/RemainingPathProcessor.java b/algo-zkd/src/main/java/com/algo/service/RemainingPathProcessor.java
index 0c411d6..87fd7fd 100644
--- a/algo-zkd/src/main/java/com/algo/service/RemainingPathProcessor.java
+++ b/algo-zkd/src/main/java/com/algo/service/RemainingPathProcessor.java
@@ -26,28 +26,38 @@
      * 绯荤粺褰撳墠鏃堕棿鍩哄噯锛堟绉掞級
      */
     private long systemBaseTime;
+    private long unifiedTimestamp;
 
     public RemainingPathProcessor(Map<String, Map<String, Integer>> pathMapping) {
         this.pathMapping = pathMapping;
         this.systemBaseTime = System.currentTimeMillis();
+        this.unifiedTimestamp = this.systemBaseTime / 1000; 
     }
 
     /**
      * 澶勭悊鎵�鏈塁TU鐨勫墿浣欒矾寰勶紝鏋勫缓鏃剁┖鍗犵敤琛�
-     * 馃敡 淇锛氬悓鏃跺鐞嗘湁鍓╀綑璺緞鐨凙GV鍜岄潤姝GV鐨勫綋鍓嶄綅缃崰鐢�
+     * 1. 浣跨敤缁熶竴鏃堕棿鎴充綔涓烘墍鏈堿GV鐨勬椂闂村熀鍑�
+     * 2. 瀵筫rror=1鐨勬晠闅滆溅锛屽叏绋嬪崰鎹椂绌鸿〃锛岀‘淇濆彲浠ョ敓鎴愮粫琛岃矾绾�
      *
      * @param agvStatusList CTU鐘舵�佸垪琛�
+     * @param unifiedTimestamp 缁熶竴鏃堕棿鎴�
      * @return 鏃剁┖鍗犵敤琛紝key涓�"x,y,timeSlot"锛寁alue涓篊TU缂栧彿
      */
-    public Map<String, String> buildSpaceTimeOccupancyMap(List<AGVStatus> agvStatusList) {
+    public Map<String, String> buildSpaceTimeOccupancyMap(List<AGVStatus> agvStatusList, long unifiedTimestamp) {
+        this.unifiedTimestamp = unifiedTimestamp;
+        
         Map<String, String> occupancyMap = new HashMap<>();
 
         for (AGVStatus agv : agvStatusList) {
-            if (agv.hasRemainingPath()) {
-                // 澶勭悊鏈夊墿浣欒矾寰勭殑AGV
+            // 澶勭悊鏁呴殰杞︼細error=1鐨勮溅杈嗗叏绋嬪崰鎹椂绌鸿〃
+            if (agv.getError() == 1) {
+                // 鏁呴殰杞﹀叏绋嬪崰鎹叾褰撳墠浣嶇疆
+                processFaultyAgvOccupancy(agv, occupancyMap);
+            } else if (agv.hasRemainingPath()) {
+                // 澶勭悊鏈夊墿浣欒矾寰勭殑姝e父AGV
                 processRemainingPathOccupancy(agv, occupancyMap);
             } else if (agv.getPosition() != null && !agv.getPosition().isEmpty()) {
-                // 澶勭悊闈欐AGV鐨勫綋鍓嶄綅缃崰鐢�
+                // 澶勭悊闈欐鐨勬甯窤GV鐨勫綋鍓嶄綅缃崰鐢�
                 processStaticAgvOccupancy(agv, occupancyMap);
             }
         }
@@ -56,12 +66,13 @@
     }
     
     /**
-     * 澶勭悊闈欐AGV鐨勪綅缃崰鐢�
+     * 澶勭悊鏁呴殰杞︾殑鏃剁┖鍗犵敤
+     * 鏁呴殰杞�(error=1)鍦ㄦ椂绌鸿〃涓婂叏绋嬪崰鎹叾浣嶇疆
      *
-     * @param agv          CTU鐘舵��
+     * @param agv          鏁呴殰CTU鐘舵��
      * @param occupancyMap 鏃剁┖鍗犵敤琛�
      */
-    private void processStaticAgvOccupancy(AGVStatus agv, Map<String, String> occupancyMap) {
+    private void processFaultyAgvOccupancy(AGVStatus agv, Map<String, String> occupancyMap) {
         String position = agv.getPosition();
         if (position == null || position.isEmpty()) {
             return;
@@ -74,20 +85,38 @@
         }
         
         CTUPhysicalConfig config = agv.getPhysicalConfig();
-        long currentTime = System.currentTimeMillis() / 1000; // 杞崲涓虹
         
-        // 闈欐AGV鍗犵敤褰撳墠浣嶇疆鐨勯暱鏃堕棿娈碉紙鍋囪300绉掞級
-        long occupancyDuration = 300; // 300绉掔殑鍗犵敤鏃堕棿
+        long occupancyDuration = 86400; 
         
-        for (long timeSlot = currentTime; timeSlot < currentTime + occupancyDuration; timeSlot++) {
+        // 浠庣粺涓�鏃堕棿鎴冲紑濮嬪崰鐢�
+        for (long timeSlot = unifiedTimestamp; timeSlot < unifiedTimestamp + occupancyDuration; timeSlot++) {
             String spaceTimeKey = coord[0] + "," + coord[1] + "," + timeSlot;
-            occupancyMap.put(spaceTimeKey, agv.getAgvId());
+            occupancyMap.put(spaceTimeKey, agv.getAgvId() + "_FAULT");
             
-            occupyAdjacentSpaces(coord, timeSlot, agv.getAgvId(), occupancyMap, config);
+            occupyAdjacentSpaces(coord, timeSlot, agv.getAgvId() + "_FAULT", occupancyMap, config);
         }
         
-        System.out.println("  闈欐AGV " + agv.getAgvId() + " 鍗犵敤浣嶇疆 " + position + 
-                         " (鍧愭爣: " + coord[0] + "," + coord[1] + ") " );
+        System.out.println("  鏁呴殰AGV " + agv.getAgvId() + " 鍏ㄧ▼鍗犵敤浣嶇疆 " + position + 
+                         " (鍧愭爣: " + coord[0] + "," + coord[1] + "), 鏃堕棿: " + 
+                         unifiedTimestamp + " ~ " + (unifiedTimestamp + occupancyDuration) + "绉�");
+    }
+    
+    /**
+     * 澶勭悊闈欐AGV鐨勪綅缃崰鐢�
+     * @param agv          CTU鐘舵��
+     * @param occupancyMap 鏃剁┖鍗犵敤琛�
+     */
+    private void processStaticAgvOccupancy(AGVStatus agv, Map<String, String> occupancyMap) {
+        
+        if (!agv.hasRemainingPath()) {
+            System.out.println("  闈欐AGV " + agv.getAgvId());
+            return;
+        }
+        
+        String position = agv.getPosition();
+        if (position != null && !position.isEmpty()) {
+            System.out.println("  闈欐AGV " + agv.getAgvId() + " 鏈夊墿浣欒矾寰勶紝宸插湪鍓╀綑璺緞澶勭悊涓崰鐢ㄤ綅缃�");
+        }
     }
 
     /**
@@ -101,8 +130,11 @@
         List<PathCode> codeList = remainingPath.getCodeList();
         CTUPhysicalConfig config = agv.getPhysicalConfig();
 
-        // 浠庡綋鍓嶄綅缃紑濮嬭绠楁椂绌哄崰鐢�
-        long currentTime = agv.getNextPointArrivalTime();
+        // 浠庣粺涓�鏃堕棿鎴虫垨AGV鐨勪笅涓�鐐瑰埌杈炬椂闂村紑濮嬶紝鍙栬緝澶у��
+        // 濡傛灉AGV宸茬粡鍦ㄦ墽琛岃矾寰勪笖涓嬩竴鐐瑰埌杈炬椂闂村湪鏈潵锛屽垯浣跨敤瀹為檯鏃堕棿
+        // 濡傛灉AGV鍒氬紑濮嬫垨鏃堕棿宸茶繃锛屽垯浣跨敤缁熶竴鏃堕棿鎴�
+        long agvNextArrivalTime = agv.getNextPointArrivalTime() / 1000; // 杞崲涓虹
+        long currentTime = Math.max(unifiedTimestamp, agvNextArrivalTime) * 1000; // 杞洖姣杩涜璁$畻
         int startIndex = agv.getCurrentPathIndex();
 
         for (int i = startIndex; i < codeList.size(); i++) {
@@ -193,8 +225,16 @@
             String currentDirection = currentCode.getDirection();
             String nextDirection = nextCode.getDirection();
 
-            if (!currentDirection.equals(nextDirection)) {
-                stayTime += config.getTurnTime(currentDirection, nextDirection);
+            if (currentDirection != null && nextDirection != null) {
+                if (!currentDirection.equals(nextDirection)) {
+                    stayTime += config.getTurnTime(currentDirection, nextDirection);
+                }
+            } else {
+                if (pathIndex == agv.getCurrentPathIndex()) {
+                    System.out.println("AGV " + agv.getAgvId() + 
+                                     " 鐨勫墿浣欒矾寰勪腑direction瀛楁涓簄ull");
+                }
+                stayTime += 0.5; 
             }
         }
 
@@ -208,7 +248,7 @@
                     stayTime += 2.0; // 鏀捐揣闇�瑕�2绉�
                     break;
                 case "3": // 鍏呯數
-                    stayTime += 10.0; // 鍏呯數鍋滈潬闇�瑕�10绉�
+                    stayTime += 100.0; // 鍏呯數鍋滈潬
                     break;
                 default:
                     stayTime += 1.0; // 鍏朵粬鍔ㄤ綔闇�瑕�1绉�
@@ -402,11 +442,26 @@
         connectedPath.setAgvId(agv.getAgvId());
         connectedPath.setCodeList(remainingCodes);
 
-        // 鐢熸垚鏂扮殑娈佃惤ID
-        String segId = agv.getAgvId() + "_CONNECTED_" + System.currentTimeMillis();
-        if (newPath != null && newPath.getSegId() != null) {
-            segId = newPath.getSegId() + "_EXTENDED";
+        // 浣跨敤鍓╀綑璺緞鐨勫師濮媠egId
+        String segId = remainingPath.getSegId();
+        
+        if (segId != null && !segId.trim().isEmpty()) {
+            // 濡傛灉鏈夋柊璺緞杩炴帴锛屽湪鍘焥egId鍩虹涓婃坊鍔犳墿灞曟爣璁�
+            if (newPath != null && newPath.getCodeList() != null && !newPath.getCodeList().isEmpty()) {
+                // 妫�鏌ユ槸鍚﹀凡缁忔湁鎵╁睍鏍囪锛岄伩鍏嶉噸澶嶆坊鍔�
+                if (!segId.endsWith("_EXTENDED")) {
+                    segId = segId + "_EXTENDED";
+                }
+            }
+        } else {
+            // 濡傛灉鍘熷璺緞娌℃湁segId锛屾牴鎹儏鍐电敓鎴�
+            if (newPath != null && newPath.getSegId() != null && !newPath.getSegId().trim().isEmpty()) {
+                segId = newPath.getSegId() + "_EXTENDED";
+            } else {
+                segId = agv.getAgvId() + "_CONNECTED_" + System.currentTimeMillis();
+            }
         }
+        
         connectedPath.setSegId(segId);
 
         return connectedPath;

--
Gitblit v1.9.1