jianghaiyue
2025-11-05 12f30a09bce1c61b2ae90129124cdc467a59b074
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;
    }
    /**
     * 处理所有CTU的剩余路径,构建时空占用表
     * 🔧 修复:同时处理有剩余路径的AGV和静止AGV的当前位置占用
     * 1. 使用统一时间戳作为所有AGV的时间基准
     * 2. 对error=1的故障车,全程占据时空表,确保可以生成绕行路线
     *
     * @param agvStatusList CTU状态列表
     * @param unifiedTimestamp 统一时间戳
     * @return 时空占用表,key为"x,y,timeSlot",value为CTU编号
     */
    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()) {
                // 处理有剩余路径的正常AGV
                processRemainingPathOccupancy(agv, occupancyMap);
            } else if (agv.getPosition() != null && !agv.getPosition().isEmpty()) {
                // 处理静止AGV的当前位置占用
                // 处理静止的正常AGV的当前位置占用
                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字段为null");
                }
                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";
        // 使用剩余路径的原始segId
        String segId = remainingPath.getSegId();
        if (segId != null && !segId.trim().isEmpty()) {
            // 如果有新路径连接,在原segId基础上添加扩展标记
            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;