#
Junjie
17 小时以前 4351d2b4db890de9f5f456f16a68a270788c6434
src/main/java/com/zy/core/thread/impl/ZyStationV5Thread.java
@@ -18,6 +18,7 @@
import com.zy.common.utils.RedisUtil;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.OutputQueue;
import com.zy.core.enums.RedisKeyType;
import com.zy.core.enums.SlaveType;
import com.zy.core.enums.StationCommandType;
import com.zy.core.model.CommandResponse;
@@ -36,14 +37,19 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Data
@Slf4j
public class ZyStationV5Thread implements Runnable, com.zy.core.thread.StationThread {
    private static final int RUN_BLOCK_REROUTE_STATE_EXPIRE_SECONDS = 60 * 60 * 24;
    private List<StationProtocol> statusList = new ArrayList<>();
    private DeviceConfig deviceConfig;
@@ -229,31 +235,75 @@
        if (commandType == StationCommandType.MOVE && !stationId.equals(targetStationId)) {
            List<NavigateNode> nodes = calcPathNavigateNodes(taskNo, stationId, targetStationId);
            List<Integer> path = new ArrayList<>();
            List<Integer> liftTransferPath = new ArrayList<>();
            for (NavigateNode n : nodes) {
                JSONObject v = JSONObject.parseObject(n.getNodeValue());
                if (v == null) {
                    continue;
                }
                Integer stationNo = v.getInteger("stationId");
                if (stationNo == null) {
                    continue;
                }
                path.add(stationNo);
                if (Boolean.TRUE.equals(n.getIsLiftTransferPoint())) {
                    liftTransferPath.add(stationNo);
                }
            }
            if (path.isEmpty()) {
                log.warn("输送线命令生成失败,路径为空,taskNo={}, stationId={}, targetStationId={}",
                        taskNo, stationId, targetStationId);
                return null;
            }
            stationCommand.setNavigatePath(path);
            stationCommand.setLiftTransferPath(liftTransferPath);
            return fillMoveCommandPath(stationCommand, nodes, taskNo, stationId, targetStationId);
        }
        return stationCommand;
    }
    @Override
    public synchronized StationCommand getRunBlockRerouteCommand(Integer taskNo,
                                                                 Integer stationId,
                                                                 Integer targetStationId,
                                                                 Integer palletSize) {
        if (taskNo == null || taskNo <= 0 || stationId == null || targetStationId == null) {
            return null;
        }
        if (Objects.equals(stationId, targetStationId)) {
            return getCommand(StationCommandType.MOVE, taskNo, stationId, targetStationId, palletSize);
        }
        RunBlockRerouteState rerouteState = loadRunBlockRerouteState(taskNo, stationId);
        rerouteState.setTaskNo(taskNo);
        rerouteState.setBlockStationId(stationId);
        rerouteState.setLastTargetStationId(targetStationId);
        rerouteState.setPlanCount((rerouteState.getPlanCount() == null ? 0 : rerouteState.getPlanCount()) + 1);
        rerouteState.setLastPlanTime(System.currentTimeMillis());
        List<List<NavigateNode>> candidatePathList = calcCandidatePathNavigateNodes(taskNo, stationId, targetStationId);
        if (candidatePathList.isEmpty()) {
            saveRunBlockRerouteState(rerouteState);
            log.warn("输送线堵塞重规划失败,候选路径为空,taskNo={}, planCount={}, stationId={}, targetStationId={}",
                    taskNo, rerouteState.getPlanCount(), stationId, targetStationId);
            return null;
        }
        StationCommand rerouteCommand = selectAvailableRerouteCommand(
                rerouteState,
                candidatePathList,
                taskNo,
                stationId,
                targetStationId,
                palletSize
        );
        if (rerouteCommand == null) {
            log.info("输送线堵塞重规划候选路线已全部试过,重置路线历史后重新开始,taskNo={}, planCount={}, stationId={}, targetStationId={}",
                    taskNo, rerouteState.getPlanCount(), stationId, targetStationId);
            rerouteState.resetIssuedRoutes();
            rerouteCommand = selectAvailableRerouteCommand(
                    rerouteState,
                    candidatePathList,
                    taskNo,
                    stationId,
                    targetStationId,
                    palletSize
            );
        }
        if (rerouteCommand != null) {
            saveRunBlockRerouteState(rerouteState);
            log.info("输送线堵塞重规划选中候选路线,taskNo={}, planCount={}, stationId={}, targetStationId={}, route={}",
                    taskNo, rerouteState.getPlanCount(), stationId, targetStationId, JSON.toJSONString(rerouteCommand.getNavigatePath()));
            return rerouteCommand;
        }
        saveRunBlockRerouteState(rerouteState);
        log.warn("输送线堵塞重规划未找到可下发路线,taskNo={}, planCount={}, stationId={}, targetStationId={}, triedRoutes={}",
                taskNo,
                rerouteState.getPlanCount(),
                stationId,
                targetStationId,
                JSON.toJSONString(rerouteState.getIssuedRoutePathList()));
        return null;
    }
    @Override
@@ -314,4 +364,208 @@
        }
        return navigateUtils.calcByStationId(startStationId, targetStationId, taskNo);
    }
    private List<List<NavigateNode>> calcCandidatePathNavigateNodes(Integer taskNo,
                                                                    Integer startStationId,
                                                                    Integer targetStationId) {
        NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class);
        if (navigateUtils == null) {
            return new ArrayList<>();
        }
        return navigateUtils.calcCandidatePathByStationId(startStationId, targetStationId, taskNo);
    }
    private StationCommand buildMoveCommand(Integer taskNo,
                                            Integer stationId,
                                            Integer targetStationId,
                                            Integer palletSize,
                                            List<NavigateNode> nodes) {
        StationCommand stationCommand = new StationCommand();
        stationCommand.setTaskNo(taskNo);
        stationCommand.setStationId(stationId);
        stationCommand.setTargetStaNo(targetStationId);
        stationCommand.setPalletSize(palletSize);
        stationCommand.setCommandType(StationCommandType.MOVE);
        return fillMoveCommandPath(stationCommand, nodes, taskNo, stationId, targetStationId);
    }
    private StationCommand fillMoveCommandPath(StationCommand stationCommand,
                                               List<NavigateNode> nodes,
                                               Integer taskNo,
                                               Integer stationId,
                                               Integer targetStationId) {
        List<Integer> path = new ArrayList<>();
        List<Integer> liftTransferPath = new ArrayList<>();
        for (NavigateNode node : nodes) {
            JSONObject valueObject;
            try {
                valueObject = JSONObject.parseObject(node.getNodeValue());
            } catch (Exception ignore) {
                continue;
            }
            if (valueObject == null) {
                continue;
            }
            Integer stationNo = valueObject.getInteger("stationId");
            if (stationNo == null) {
                continue;
            }
            path.add(stationNo);
            if (Boolean.TRUE.equals(node.getIsLiftTransferPoint())) {
                liftTransferPath.add(stationNo);
            }
        }
        if (path.isEmpty()) {
            log.warn("输送线命令生成失败,路径为空,taskNo={}, stationId={}, targetStationId={}",
                    taskNo, stationId, targetStationId);
            return null;
        }
        stationCommand.setNavigatePath(path);
        stationCommand.setLiftTransferPath(liftTransferPath);
        stationCommand.setTargetStaNo(path.get(path.size() - 1));
        return stationCommand;
    }
    private StationCommand selectAvailableRerouteCommand(RunBlockRerouteState rerouteState,
                                                         List<List<NavigateNode>> candidatePathList,
                                                         Integer taskNo,
                                                         Integer stationId,
                                                         Integer targetStationId,
                                                         Integer palletSize) {
        if (rerouteState == null || candidatePathList == null || candidatePathList.isEmpty()) {
            return null;
        }
        Set<String> issuedRouteSignatureSet = rerouteState.getIssuedRouteSignatureSet();
        for (List<NavigateNode> candidatePath : candidatePathList) {
            StationCommand rerouteCommand = buildMoveCommand(taskNo, stationId, targetStationId, palletSize, candidatePath);
            if (rerouteCommand == null || rerouteCommand.getNavigatePath() == null || rerouteCommand.getNavigatePath().isEmpty()) {
                continue;
            }
            String routeSignature = buildPathSignature(rerouteCommand.getNavigatePath());
            if (Cools.isEmpty(routeSignature) || issuedRouteSignatureSet.contains(routeSignature)) {
                continue;
            }
            issuedRouteSignatureSet.add(routeSignature);
            rerouteState.getIssuedRoutePathList().add(new ArrayList<>(rerouteCommand.getNavigatePath()));
            rerouteState.setLastSelectedRoute(new ArrayList<>(rerouteCommand.getNavigatePath()));
            return rerouteCommand;
        }
        return null;
    }
    private RunBlockRerouteState loadRunBlockRerouteState(Integer taskNo, Integer blockStationId) {
        if (redisUtil == null || taskNo == null || taskNo <= 0 || blockStationId == null || blockStationId <= 0) {
            return new RunBlockRerouteState();
        }
        Object stateObj = redisUtil.get(buildRunBlockRerouteStateKey(taskNo, blockStationId));
        if (stateObj == null) {
            return new RunBlockRerouteState();
        }
        try {
            RunBlockRerouteState state = JSON.parseObject(String.valueOf(stateObj), RunBlockRerouteState.class);
            return state == null ? new RunBlockRerouteState() : state.normalize();
        } catch (Exception ignore) {
            return new RunBlockRerouteState();
        }
    }
    private void saveRunBlockRerouteState(RunBlockRerouteState rerouteState) {
        if (redisUtil == null
                || rerouteState == null
                || rerouteState.getTaskNo() == null
                || rerouteState.getTaskNo() <= 0
                || rerouteState.getBlockStationId() == null
                || rerouteState.getBlockStationId() <= 0) {
            return;
        }
        rerouteState.normalize();
        redisUtil.set(
                buildRunBlockRerouteStateKey(rerouteState.getTaskNo(), rerouteState.getBlockStationId()),
                JSON.toJSONString(rerouteState),
                RUN_BLOCK_REROUTE_STATE_EXPIRE_SECONDS
        );
    }
    private String buildPathSignature(List<Integer> path) {
        if (path == null || path.isEmpty()) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (Integer stationNo : path) {
            if (stationNo == null) {
                continue;
            }
            if (builder.length() > 0) {
                builder.append("->");
            }
            builder.append(stationNo);
        }
        return builder.toString();
    }
    private String buildRunBlockRerouteStateKey(Integer taskNo, Integer blockStationId) {
        return RedisKeyType.STATION_RUN_BLOCK_REROUTE_STATE_.key + taskNo + "_" + blockStationId;
    }
    @Data
    private static class RunBlockRerouteState {
        private Integer taskNo;
        private Integer blockStationId;
        private Integer planCount = 0;
        private Integer lastTargetStationId;
        private Long lastPlanTime;
        private List<List<Integer>> issuedRoutePathList = new ArrayList<>();
        private List<Integer> lastSelectedRoute = new ArrayList<>();
        private Set<String> issuedRouteSignatureSet = new LinkedHashSet<>();
        private RunBlockRerouteState normalize() {
            if (planCount == null || planCount < 0) {
                planCount = 0;
            }
            if (issuedRoutePathList == null) {
                issuedRoutePathList = new ArrayList<>();
            }
            if (lastSelectedRoute == null) {
                lastSelectedRoute = new ArrayList<>();
            }
            if (issuedRouteSignatureSet == null) {
                issuedRouteSignatureSet = new LinkedHashSet<>();
            }
            for (List<Integer> routePath : issuedRoutePathList) {
                if (routePath == null || routePath.isEmpty()) {
                    continue;
                }
                StringBuilder builder = new StringBuilder(buildPathSignatureText(routePath));
                if (builder.length() > 0) {
                    issuedRouteSignatureSet.add(builder.toString());
                }
            }
            return this;
        }
        private void resetIssuedRoutes() {
            this.issuedRoutePathList = new ArrayList<>();
            this.lastSelectedRoute = new ArrayList<>();
            this.issuedRouteSignatureSet = new LinkedHashSet<>();
        }
        private static String buildPathSignatureText(List<Integer> routePath) {
            if (routePath == null || routePath.isEmpty()) {
                return "";
            }
            StringBuilder builder = new StringBuilder();
            for (Integer stationId : routePath) {
                if (stationId == null) {
                    continue;
                }
                if (builder.length() > 0) {
                    builder.append("->");
                }
                builder.append(stationId);
            }
            return builder.toString();
        }
    }
}