Junjie
12 小时以前 41aeff86351d1dd94fe2408175f96475f227c1b9
src/main/java/com/zy/core/utils/station/StationRegularDispatchProcessor.java
@@ -65,100 +65,11 @@
    public void stationInExecute() {
        try {
            DispatchLimitConfig baseLimitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(null, null);
            int[] currentStationTaskCountRef = new int[]{stationDispatchLoadSupport.countCurrentStationTask()};
            LoadGuardState loadGuardState = stationDispatchLoadSupport.buildLoadGuardState(baseLimitConfig);
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
                    continue;
                }
                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
                List<StationObjModel> stationList = basDevp.getBarcodeStationList$();
                for (StationObjModel entity : stationList) {
                    Integer stationId = entity.getStationId();
                    if (!stationMap.containsKey(stationId)) {
                        continue;
                    }
                    StationProtocol stationProtocol = stationMap.get(stationId);
                    if (stationProtocol == null) {
                        continue;
                    }
                    Object lock = redisUtil.get(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId);
                    if (lock != null) {
                        continue;
                    }
                    if (!stationProtocol.isAutoing()
                            || !stationProtocol.isLoading()
                            || stationProtocol.getTaskNo() <= 0) {
                        continue;
                    }
                    WrkMast wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
                    if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_INBOUND.sts)) {
                        continue;
                    }
                    String locNo = wrkMast.getLocNo();
                    FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
                    if (findCrnNoResult == null) {
                        News.taskInfo(wrkMast.getWrkNo(), "{}工作,未匹配到堆垛机", wrkMast.getWrkNo());
                        continue;
                    }
                    Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationId);
                    if (targetStationId == null) {
                        News.taskInfo(wrkMast.getWrkNo(), "{}站点,搜索入库站点失败", stationId);
                        continue;
                    }
                    DispatchLimitConfig limitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), targetStationId);
                    LoopHitResult loopHitResult = stationDispatchLoadSupport.findPathLoopHit(
                            limitConfig,
                            stationProtocol.getStationId(),
                            targetStationId,
                            loadGuardState
                    );
                    if (stationDispatchLoadSupport.isDispatchBlocked(limitConfig, currentStationTaskCountRef[0], loadGuardState, loopHitResult.isThroughLoop())) {
                        continue;
                    }
                    StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
                    if (command == null) {
                        News.taskInfo(wrkMast.getWrkNo(), "{}工作,获取输送线命令失败", wrkMast.getWrkNo());
                        continue;
                    }
                    Date now = new Date();
                    wrkMast.setWrkSts(WrkStsType.INBOUND_STATION_RUN.sts);
                    wrkMast.setSourceStaNo(stationProtocol.getStationId());
                    wrkMast.setStaNo(targetStationId);
                    wrkMast.setSystemMsg("");
                    wrkMast.setIoTime(now);
                    wrkMast.setModiTime(now);
                    if (wrkMastService.updateById(wrkMast)) {
                        wrkAnalysisService.markInboundStationStart(wrkMast, now);
                        boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "stationInExecute");
                        if (offered && stationMoveCoordinator != null) {
                            stationMoveCoordinator.recordDispatch(
                                    wrkMast.getWrkNo(),
                                    stationProtocol.getStationId(),
                                    "stationInExecute",
                                    command,
                                    false
                            );
                        }
                        News.info("输送站点入库命令下发成功,站点号={},工作号={},命令数据={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
                        redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
                        loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
                        stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
                    }
                    stationInExecute(basDevp, entity);
                }
            }
        } catch (Exception e) {
@@ -166,41 +77,46 @@
        }
    }
    public void stationOutExecuteFinish() {
    public void stationOutExecuteFinish(StationObjModel stationObjModel) {
        try {
            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN.sts));
            for (WrkMast wrkMast : wrkMasts) {
                Integer wrkNo = wrkMast.getWrkNo();
                Integer targetStaNo = wrkMast.getStaNo();
                if (wrkNo == null || targetStaNo == null) {
                    continue;
                }
                boolean complete = false;
                Integer targetDeviceNo = null;
                StationThread stationThread = null;
                BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
                if (basStation != null) {
                    targetDeviceNo = basStation.getDeviceNo();
                    stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
                    if (stationThread != null) {
                        Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
                        StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
                        boolean arrived = stationProtocol != null && wrkNo.equals(stationProtocol.getTaskNo());
                        if (arrived || stationThread.hasRecentArrival(basStation.getStationId(), wrkNo)) {
                            complete = true;
                            if (!arrived) {
                                News.info("输送站点出库到达判定使用最近到站补偿,工作号={},目标站={}", wrkNo, targetStaNo);
                            }
                        }
                    }
                }
                if (complete) {
                    attemptClearTaskPath(stationThread, wrkNo);
                    completeStationRunTask(wrkMast, targetDeviceNo);
                }
            if (stationObjModel == null) {
                return;
            }
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, stationObjModel.getDeviceNo());
            if (stationThread == null) {
                return;
            }
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(stationObjModel.getStationId());
            if (stationProtocol == null) {
                return;
            }
            if (stationProtocol.getTaskNo() <= 0) {
                return;
            }
            WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
            if (wrkMast == null) {
                return;
            }
            if (!wrkMast.getWrkSts().equals(WrkStsType.STATION_RUN.sts)) {
                return;
            }
            Integer wrkNo = wrkMast.getWrkNo();
            if (stationMoveCoordinator != null) {
                stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
            }
            Date now = new Date();
            wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
            wrkMast.setIoTime(now);
            wrkMast.setModiTime(now);
            wrkMastService.updateById(wrkMast);
            wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
            notifyUtils.notify(String.valueOf(SlaveType.Devp), stationObjModel.getDeviceNo(), String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
            redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
            attemptClearTaskPath(stationThread, wrkNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
@@ -210,38 +126,49 @@
        try {
            List<WrkMast> wrkMasts = wrkMastService.list(new QueryWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
            for (WrkMast wrkMast : wrkMasts) {
                Integer wrkNo = wrkMast.getWrkNo();
                Integer targetStaNo = wrkMast.getStaNo();
                checkTaskToComplete(wrkMast);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
                Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkNo);
                if (lock != null) {
                    continue;
                }
    public void checkTaskToComplete(WrkMast wrkMast) {
        try {
            if (wrkMast == null || wrkMast.getWrkNo() == null || wrkMast.getStaNo() == null) {
                return;
            }
            Integer wrkNo = wrkMast.getWrkNo();
            Integer targetStaNo = wrkMast.getStaNo();
                BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
                if (basStation == null) {
                    continue;
                }
            Object lock = redisUtil.get(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkNo);
            if (lock != null) {
                return;
            }
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
                if (stationThread == null) {
                    continue;
                }
            BasStation basStation = basStationService.getOne(new QueryWrapper<BasStation>().eq("station_id", targetStaNo));
            if (basStation == null) {
                return;
            }
                Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
                StationProtocol stationProtocol = statusMap.get(basStation.getStationId());
                if (stationProtocol == null) {
                    continue;
                }
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basStation.getDeviceNo());
            if (stationThread == null) {
                return;
            }
                if (!Objects.equals(stationProtocol.getTaskNo(), wrkNo)) {
                    if (stationMoveCoordinator != null) {
                        stationMoveCoordinator.finishSession(wrkNo);
                    }
                    wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
                    wrkMast.setIoTime(new Date());
                    wrkMastService.updateById(wrkMast);
            Map<Integer, StationProtocol> statusMap = stationThread.getStatusMap();
            StationProtocol stationProtocol = statusMap == null ? null : statusMap.get(basStation.getStationId());
            if (stationProtocol == null) {
                return;
            }
            if (!Objects.equals(stationProtocol.getTaskNo(), wrkNo)) {
                if (stationMoveCoordinator != null) {
                    stationMoveCoordinator.finishSession(wrkNo);
                }
                wrkMast.setWrkSts(WrkStsType.COMPLETE_OUTBOUND.sts);
                wrkMast.setIoTime(new Date());
                wrkMastService.updateById(wrkMast);
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -262,27 +189,103 @@
        }
    }
    private void completeStationRunTask(WrkMast wrkMast, Integer deviceNo) {
        if (wrkMast == null || wrkMast.getWrkNo() == null) {
            return;
        }
        if (stationMoveCoordinator != null) {
            stationMoveCoordinator.finishSession(wrkMast.getWrkNo());
        }
        Date now = new Date();
        wrkMast.setWrkSts(WrkStsType.STATION_RUN_COMPLETE.sts);
        wrkMast.setIoTime(now);
        wrkMast.setModiTime(now);
        wrkMastService.updateById(wrkMast);
        wrkAnalysisService.markOutboundStationComplete(wrkMast, now);
        if (deviceNo != null) {
            notifyUtils.notify(String.valueOf(SlaveType.Devp), deviceNo, String.valueOf(wrkMast.getWrkNo()), wrkMast.getWmsWrkNo(), NotifyMsgType.STATION_OUT_TASK_RUN_COMPLETE, null);
        }
        redisUtil.set(RedisKeyType.STATION_OUT_EXECUTE_COMPLETE_LIMIT.key + wrkMast.getWrkNo(), "lock", 60);
    }
    private boolean offerDevpCommandWithDedup(Integer deviceNo, StationCommand command, String scene) {
        StationCommandDispatchResult dispatchResult = stationCommandDispatcher.dispatch(deviceNo, command, "station-operate-process", scene);
        return dispatchResult.isAccepted();
    }
    public void stationInExecute(BasDevp basDevp, StationObjModel entity) {
        if (basDevp == null || basDevp.getDevpNo() == null || entity == null || entity.getStationId() == null) {
            return;
        }
        Integer stationId = entity.getStationId();
        StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
        if (stationThread == null) {
            return;
        }
        Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
        if (stationMap == null || !stationMap.containsKey(stationId)) {
            return;
        }
        StationProtocol stationProtocol = stationMap.get(stationId);
        if (stationProtocol == null) {
            return;
        }
        Object lock = redisUtil.get(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId);
        if (lock != null) {
            return;
        }
        if (!stationProtocol.isAutoing()
                || !stationProtocol.isLoading()
                || stationProtocol.getTaskNo() <= 0) {
            return;
        }
        WrkMast wrkMast = wrkMastService.getOne(new QueryWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
        if (wrkMast == null || !Objects.equals(wrkMast.getWrkSts(), WrkStsType.NEW_INBOUND.sts)) {
            return;
        }
        String locNo = wrkMast.getLocNo();
        FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
        if (findCrnNoResult == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}工作,未匹配到堆垛机", wrkMast.getWrkNo());
            return;
        }
        Integer targetStationId = commonService.findInStationId(findCrnNoResult, stationId);
        if (targetStationId == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}站点,搜索入库站点失败", stationId);
            return;
        }
        DispatchLimitConfig limitConfig = stationDispatchLoadSupport.getDispatchLimitConfig(stationProtocol.getStationId(), targetStationId);
        int currentStationTaskCount = stationDispatchLoadSupport.countCurrentStationTask();
        LoadGuardState loadGuardState = stationDispatchLoadSupport.buildLoadGuardState(limitConfig);
        LoopHitResult loopHitResult = stationDispatchLoadSupport.findPathLoopHit(
                limitConfig,
                stationProtocol.getStationId(),
                targetStationId,
                loadGuardState
        );
        if (stationDispatchLoadSupport.isDispatchBlocked(limitConfig, currentStationTaskCount, loadGuardState, loopHitResult.isThroughLoop())) {
            return;
        }
        StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
        if (command == null) {
            News.taskInfo(wrkMast.getWrkNo(), "{}工作,获取输送线命令失败", wrkMast.getWrkNo());
            return;
        }
        Date now = new Date();
        wrkMast.setWrkSts(WrkStsType.INBOUND_STATION_RUN.sts);
        wrkMast.setSourceStaNo(stationProtocol.getStationId());
        wrkMast.setStaNo(targetStationId);
        wrkMast.setSystemMsg("");
        wrkMast.setIoTime(now);
        wrkMast.setModiTime(now);
        if (wrkMastService.updateById(wrkMast)) {
            wrkAnalysisService.markInboundStationStart(wrkMast, now);
            boolean offered = offerDevpCommandWithDedup(basDevp.getDevpNo(), command, "stationInExecute");
            if (offered && stationMoveCoordinator != null) {
                stationMoveCoordinator.recordDispatch(
                        wrkMast.getWrkNo(),
                        stationProtocol.getStationId(),
                        "stationInExecute",
                        command,
                        false
                );
            }
            News.info("输送站点入库命令下发成功,站点号={},工作号={},命令数据={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
            redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
            loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
            stationDispatchLoadSupport.saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
        }
    }
}