#
Junjie
12 小时以前 d16c9af7bb89a7e3abb4071003c63b3a6f6b9e4b
src/main/java/com/zy/core/utils/station/StationRerouteProcessor.java
@@ -75,178 +75,188 @@
    @Autowired
    private StationDispatchRuntimeStateSupport stationDispatchRuntimeStateSupport;
    public void checkStationRunBlock() {
    public void checkStationRunBlock(BasDevp basDevp, Integer stationId) {
        try {
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
                    continue;
                }
                List<Integer> runBlockReassignLocStationList = new ArrayList<>();
                for (StationObjModel stationObjModel : basDevp.getRunBlockReassignLocStationList$()) {
                    runBlockReassignLocStationList.add(stationObjModel.getStationId());
                }
                List<Integer> outOrderStationIds = basDevp.getOutOrderIntList();
                for (StationProtocol stationProtocol : stationThread.getStatus()) {
                    if (stationProtocol.isAutoing()
                            && stationProtocol.isLoading()
                            && stationProtocol.getTaskNo() > 0
                            && stationProtocol.isRunBlock()) {
                        WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                        if (wrkMast == null) {
                            News.info("输送站点号={} 运行阻塞,但无法找到对应任务,工作号={}", stationProtocol.getStationId(), stationProtocol.getTaskNo());
                            continue;
                        }
                        Object lock = redisUtil.get(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo());
                        if (lock != null) {
                            continue;
                        }
                        redisUtil.set(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo(), "lock", 15);
                        if (shouldUseRunBlockDirectReassign(wrkMast, stationProtocol.getStationId(), runBlockReassignLocStationList)) {
                            executeRunBlockDirectReassign(basDevp, stationThread, stationProtocol, wrkMast);
                            continue;
                        }
                        Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
                        RerouteContext context = RerouteContext.create(
                                RerouteSceneType.RUN_BLOCK_REROUTE,
                                basDevp,
                                stationThread,
                                stationProtocol,
                                wrkMast,
                                outOrderStationIds,
                                pathLenFactor,
                                "checkStationRunBlock_reroute"
                        ).withRunBlockCommand()
                                .withSuppressDispatchGuard()
                                .withCancelSessionBeforeDispatch()
                                .withResetSegmentCommandsBeforeDispatch();
                        executeSharedReroute(context);
                    }
                }
            if (basDevp == null || basDevp.getDevpNo() == null || stationId == null) {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void checkStationIdleRecover() {
        try {
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
                    continue;
                }
                for (StationProtocol stationProtocol : stationThread.getStatus()) {
                    if (stationProtocol.isAutoing()
                            && stationProtocol.isLoading()
                            && stationProtocol.getTaskNo() > 0
                            && !stationProtocol.isRunBlock()) {
                        checkStationIdleRecover(basDevp, stationThread, stationProtocol, basDevp.getOutOrderIntList());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void checkStationOutOrder() {
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
                return;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
            if (stationProtocol == null
                    || !stationProtocol.isAutoing()
                    || !stationProtocol.isLoading()
                    || stationProtocol.getTaskNo() <= 0
                    || !stationProtocol.isRunBlock()) {
                return;
            }
            List<Integer> runBlockReassignLocStationList = new ArrayList<>();
            for (StationObjModel stationObjModel : basDevp.getRunBlockReassignLocStationList$()) {
                runBlockReassignLocStationList.add(stationObjModel.getStationId());
            }
            List<Integer> outOrderStationIds = basDevp.getOutOrderIntList();
            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
            if (wrkMast == null) {
                News.info("输送站点号={} 运行阻塞,但无法找到对应任务,工作号={}", stationProtocol.getStationId(), stationProtocol.getTaskNo());
                return;
            }
            Object lock = redisUtil.get(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo());
            if (lock != null) {
                return;
            }
            redisUtil.set(RedisKeyType.CHECK_STATION_RUN_BLOCK_LIMIT_.key + stationProtocol.getTaskNo(), "lock", 30);
            if (shouldUseRunBlockDirectReassign(wrkMast, stationProtocol.getStationId(), runBlockReassignLocStationList)) {
                executeRunBlockDirectReassign(basDevp, stationThread, stationProtocol, wrkMast);
                return;
            }
            Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
            RerouteContext context = RerouteContext.create(
                    RerouteSceneType.RUN_BLOCK_REROUTE,
                    basDevp,
                    stationThread,
                    stationProtocol,
                    wrkMast,
                    outOrderStationIds,
                    pathLenFactor,
                    "checkStationRunBlock_reroute"
            ).withRunBlockCommand()
                    .withSuppressDispatchGuard()
                    .withCancelSessionBeforeDispatch()
                    .withResetSegmentCommandsBeforeDispatch();
            executeSharedReroute(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void checkStationIdleRecover(BasDevp basDevp, Integer stationId) {
        try {
            if (basDevp == null || basDevp.getDevpNo() == null || stationId == null) {
                return;
            }
            if (!isIdleRecoverCandidateStation(basDevp, stationId)) {
                return;
            }
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                return;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            List<StationObjModel> orderList = basDevp.getOutOrderList$();
            List<Integer> outOrderStationIds = basDevp.getOutOrderIntList();
            for (StationObjModel stationObjModel : orderList) {
                StationProtocol stationProtocol = statusMap.get(stationObjModel.getStationId());
                if (stationProtocol == null
                        || !stationProtocol.isAutoing()
                        || !stationProtocol.isLoading()
                        || stationProtocol.getTaskNo() <= 0
                        || stationProtocol.isRunBlock()
                        || !stationProtocol.getStationId().equals(stationProtocol.getTargetStaNo())) {
                    continue;
                }
                WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                if (wrkMast == null
                        || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)
                        || Objects.equals(stationProtocol.getStationId(), wrkMast.getStaNo())) {
                    continue;
                }
                if (stationOutboundDecisionSupport.shouldSkipOutOrderDispatchForExistingRoute(wrkMast.getWrkNo(), stationProtocol.getStationId())) {
                    continue;
                }
                Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
                RerouteContext context = RerouteContext.create(
                        RerouteSceneType.OUT_ORDER,
                        basDevp,
                        stationThread,
                        stationProtocol,
                        wrkMast,
                        outOrderStationIds,
                        pathLenFactor,
                        "checkStationOutOrder"
                ).withDispatchDeviceNo(stationObjModel.getDeviceNo())
                        .withSuppressDispatchGuard()
                        .withOutOrderDispatchLock()
                        .withResetSegmentCommandsBeforeDispatch();
                executeSharedReroute(context);
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
            if (stationProtocol == null
                    || !stationProtocol.isAutoing()
                    || !stationProtocol.isLoading()
                    || stationProtocol.getTaskNo() <= 0
                    || stationProtocol.isRunBlock()) {
                return;
            }
            checkStationIdleRecover(basDevp, stationThread, stationProtocol, basDevp.getOutOrderIntList());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void watchCircleStation() {
        List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
    public void checkStationOutOrder(BasDevp basDevp, StationObjModel stationObjModel) {
        try {
            if (basDevp == null || basDevp.getDevpNo() == null || stationObjModel == null || stationObjModel.getStationId() == null) {
                return;
            }
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
                return;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationObjModel.getStationId());
            if (stationProtocol == null
                    || !stationProtocol.isAutoing()
                    || !stationProtocol.isLoading()
                    || stationProtocol.getTaskNo() <= 0
                    || stationProtocol.isRunBlock()
                    || !stationProtocol.getStationId().equals(stationProtocol.getTargetStaNo())) {
                return;
            }
            List<Integer> outOrderList = basDevp.getOutOrderIntList();
            for (StationProtocol stationProtocol : stationThread.getStatus()) {
                if (!stationProtocol.isAutoing()
                        || !stationProtocol.isLoading()
                        || stationProtocol.getTaskNo() <= 0
                        || !stationOutboundDecisionSupport.isWatchingCircleArrival(stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
                    continue;
                }
                WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                if (wrkMast == null
                        || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)
                        || Objects.equals(stationProtocol.getStationId(), wrkMast.getStaNo())) {
                    continue;
                }
                Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
                RerouteContext context = RerouteContext.create(
                        RerouteSceneType.WATCH_CIRCLE,
                        basDevp,
                        stationThread,
                        stationProtocol,
                        wrkMast,
                        outOrderList,
                        pathLenFactor,
                        "watchCircleStation"
                ).withSuppressDispatchGuard()
                        .withOutOrderDispatchLock()
                        .withResetSegmentCommandsBeforeDispatch();
                executeSharedReroute(context);
            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
            if (wrkMast == null
                    || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)
                    || Objects.equals(stationProtocol.getStationId(), wrkMast.getStaNo())) {
                return;
            }
            if (stationOutboundDecisionSupport.shouldSkipOutOrderDispatchForExistingRoute(wrkMast.getWrkNo(), stationProtocol.getStationId())) {
                return;
            }
            Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
            RerouteContext context = RerouteContext.create(
                    RerouteSceneType.OUT_ORDER,
                    basDevp,
                    stationThread,
                    stationProtocol,
                    wrkMast,
                    basDevp.getOutOrderIntList(),
                    pathLenFactor,
                    "checkStationOutOrder"
            ).withDispatchDeviceNo(stationObjModel.getDeviceNo())
                    .withSuppressDispatchGuard()
                    .withOutOrderDispatchLock()
                    .withResetSegmentCommandsBeforeDispatch();
            executeSharedReroute(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void watchCircleStation(BasDevp basDevp, Integer stationId) {
        try {
            if (basDevp == null || basDevp.getDevpNo() == null || stationId == null) {
                return;
            }
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                return;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationId);
            if (stationProtocol == null
                    || !stationProtocol.isAutoing()
                    || !stationProtocol.isLoading()
                    || stationProtocol.getTaskNo() <= 0
                    || !stationOutboundDecisionSupport.isWatchingCircleArrival(stationProtocol.getTaskNo(), stationProtocol.getStationId())) {
                return;
            }
            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
            if (wrkMast == null
                    || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.STATION_RUN.sts)
                    || Objects.equals(stationProtocol.getStationId(), wrkMast.getStaNo())) {
                return;
            }
            Double pathLenFactor = stationOutboundDecisionSupport.resolveOutboundPathLenFactor(wrkMast);
            RerouteContext context = RerouteContext.create(
                    RerouteSceneType.WATCH_CIRCLE,
                    basDevp,
                    stationThread,
                    stationProtocol,
                    wrkMast,
                    basDevp.getOutOrderIntList(),
                    pathLenFactor,
                    "watchCircleStation"
            ).withSuppressDispatchGuard()
                    .withOutOrderDispatchLock()
                    .withResetSegmentCommandsBeforeDispatch();
            executeSharedReroute(context);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@@ -403,6 +413,27 @@
        return true;
    }
    private boolean isIdleRecoverCandidateStation(BasDevp basDevp, Integer stationId) {
        if (basDevp == null || stationId == null) {
            return false;
        }
        return !containsStation(basDevp.getBarcodeStationList$(), stationId)
                && !containsStation(basDevp.getInStationList$(), stationId)
                && !containsStation(basDevp.getOutStationList$(), stationId);
    }
    private boolean containsStation(List<StationObjModel> stationList, Integer stationId) {
        if (stationList == null || stationList.isEmpty() || stationId == null) {
            return false;
        }
        for (StationObjModel stationObjModel : stationList) {
            if (stationObjModel != null && Objects.equals(stationObjModel.getStationId(), stationId)) {
                return true;
            }
        }
        return false;
    }
    private RerouteExecutionResult executeReroutePlanWithTaskLock(RerouteContext context,
                                                                  RerouteCommandPlan plan,
                                                                  StationProtocol stationProtocol,