#
Administrator
1 天以前 1c8455e647bfb059d52788afaa845c52e7d6213f
src/main/java/com/zy/core/utils/StationOperateProcessUtils.java
@@ -66,6 +66,20 @@
    }
    public void submitStationEnableInTasks(MainProcessLane lane, long minIntervalMs) {
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.STATION_SCAN,
                "submitStationEnableInTasks",
                minIntervalMs,
                new Runnable() {
                    @Override
                    public void run() {
                        submitStationEnableInTasksInternal(lane, minIntervalMs);
                    }
                }
        );
    }
    private void submitStationEnableInTasksInternal(MainProcessLane lane, long minIntervalMs) {
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<BasDevp>());
        for (final BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
@@ -103,18 +117,50 @@
    }
    public void submitStationInTasks(MainProcessLane lane, long minIntervalMs) {
        // 入库下发依赖单轮共享的承载预占状态,必须整体串行,避免多个站点并发时同时通过容量检查。
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.STATION,
                "stationInExecute",
                MainProcessLane.STATION_SCAN,
                "submitStationInTasks",
                minIntervalMs,
                new Runnable() {
                    @Override
                    public void run() {
                        stationInExecute();
                        submitStationInTasksInternal(lane, minIntervalMs);
                    }
                }
        );
    }
    private void submitStationInTasksInternal(MainProcessLane lane, long minIntervalMs) {
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<BasDevp>());
        for (final BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
            if (stationThread == null) {
                continue;
            }
            Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
            if (stationMap == null || stationMap.isEmpty()) {
                continue;
            }
            List<StationObjModel> list = basDevp.getBarcodeStationList$();
            for (final StationObjModel stationObjModel : list) {
                Integer stationId = stationObjModel == null ? null : stationObjModel.getStationId();
                if (stationId == null || !stationMap.containsKey(stationId)) {
                    continue;
                }
                mainProcessTaskSubmitter.submitKeyedSerialTask(
                        lane,
                        stationId,
                        "stationInExecute",
                        minIntervalMs,
                        new Runnable() {
                            @Override
                            public void run() {
                                stationInExecute(basDevp, stationObjModel);
                            }
                        }
                );
            }
        }
    }
    public void submitCrnStationOutTasks(long minIntervalMs) {
@@ -122,18 +168,39 @@
    }
    public void submitCrnStationOutTasks(MainProcessLane lane, long minIntervalMs) {
        // 出库站点下发同样会预占输送线/环线容量,与 stationInExecute 共用 STATION lane 串行。
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.STATION,
                "crnStationOutExecute",
                MainProcessLane.STATION_SCAN,
                "submitCrnStationOutTasks",
                minIntervalMs,
                new Runnable() {
                    @Override
                    public void run() {
                        crnStationOutExecute();
                        submitCrnStationOutTasksInternal(lane, minIntervalMs);
                    }
                }
        );
    }
    private void submitCrnStationOutTasksInternal(MainProcessLane lane, long minIntervalMs) {
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                .eq("wrk_sts", WrkStsType.OUTBOUND_RUN_COMPLETE.sts)
                .isNotNull("crn_no")
        );
        for (final WrkMast wrkMast : wrkMasts) {
            Integer laneKey = getCrnStationOutLaneKey(wrkMast);
            mainProcessTaskSubmitter.submitKeyedSerialTask(
                    lane,
                    laneKey,
                    "crnStationOutExecute",
                    minIntervalMs,
                    new Runnable() {
                        @Override
                        public void run() {
                            crnStationOutExecute(wrkMast);
                        }
                    }
            );
        }
    }
    public void submitDualCrnStationOutTasks(long minIntervalMs) {
@@ -141,6 +208,20 @@
    }
    public void submitDualCrnStationOutTasks(MainProcessLane lane, long minIntervalMs) {
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.STATION_SCAN,
                "submitDualCrnStationOutTasks",
                minIntervalMs,
                new Runnable() {
                    @Override
                    public void run() {
                        submitDualCrnStationOutTasksInternal(lane, minIntervalMs);
                    }
                }
        );
    }
    private void submitDualCrnStationOutTasksInternal(MainProcessLane lane, long minIntervalMs) {
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                .eq("wrk_sts", WrkStsType.OUTBOUND_RUN_COMPLETE.sts)
                .isNotNull("dual_crn_no")
@@ -170,12 +251,23 @@
    }
    public void submitStationOutExecuteFinishTasks(MainProcessLane lane, long minIntervalMs) {
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.STATION_SCAN,
                "submitStationOutExecuteFinishTasks",
                minIntervalMs,
                new Runnable() {
                    @Override
                    public void run() {
                        submitStationOutExecuteFinishTasksInternal(lane, minIntervalMs);
                    }
                }
        );
    }
    private void submitStationOutExecuteFinishTasksInternal(MainProcessLane lane, long minIntervalMs) {
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN.sts));
        for (final WrkMast wrkMast : wrkMasts) {
            Integer laneKey = wrkMast == null ? null : wrkMast.getStaNo();
            if (laneKey == null) {
                laneKey = wrkMast == null ? null : wrkMast.getWrkNo();
            }
            Integer laneKey = wrkMast == null ? null : wrkMast.getWrkNo();
            mainProcessTaskSubmitter.submitKeyedSerialTask(
                    lane,
                    laneKey,
@@ -196,12 +288,23 @@
    }
    public void submitCheckTaskToCompleteTasks(MainProcessLane lane, long minIntervalMs) {
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.STATION_SCAN,
                "submitCheckTaskToCompleteTasks",
                minIntervalMs,
                new Runnable() {
                    @Override
                    public void run() {
                        submitCheckTaskToCompleteTasksInternal(lane, minIntervalMs);
                    }
                }
        );
    }
    private void submitCheckTaskToCompleteTasksInternal(MainProcessLane lane, long minIntervalMs) {
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
        for (final WrkMast wrkMast : wrkMasts) {
            Integer laneKey = wrkMast == null ? null : wrkMast.getStaNo();
            if (laneKey == null) {
                laneKey = wrkMast == null ? null : wrkMast.getWrkNo();
            }
            Integer laneKey = wrkMast == null ? null : wrkMast.getWrkNo();
            mainProcessTaskSubmitter.submitKeyedSerialTask(
                    lane,
                    laneKey,
@@ -222,6 +325,20 @@
    }
    public void submitCheckStationRunBlockTasks(MainProcessLane lane, long minIntervalMs) {
        mainProcessTaskSubmitter.submitSerialTask(
                MainProcessLane.STATION_SCAN,
                "submitCheckStationRunBlockTasks",
                minIntervalMs,
                new Runnable() {
                    @Override
                    public void run() {
                        submitCheckStationRunBlockTasksInternal(lane, minIntervalMs);
                    }
                }
        );
    }
    private void submitCheckStationRunBlockTasksInternal(MainProcessLane lane, long minIntervalMs) {
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<BasDevp>());
        for (final BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
@@ -257,7 +374,7 @@
    }
    //执行输送站点入库任务
    public synchronized void stationInExecute() {
    public void stationInExecute() {
        try {
            DispatchLimitConfig limitConfig = getDispatchLimitConfig();
            int[] currentStationTaskCountRef = new int[]{countCurrentStationTask()};
@@ -385,16 +502,30 @@
                && stationProtocol.getTaskNo() > 0
        ) {
            //检测任务是否生成
            WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
            WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper<WrkMast>()
                    .eq("barcode", stationProtocol.getBarcode())
                    .eq("io_type", WrkIoType.IN.id)
            );
            if (wrkMast == null) {
                logDebugLimited("station_in_wait_wrk_" + stationId + "_" + stationProtocol.getTaskNo(), 3,
                        "[WCS Debug][条码站入库] 条码站已有站点任务号但未找到入库工作档,等待任务生成。stationId={},stationTaskNo={},barcode={},isLoading={},isAutoing={}",
                        stationId, stationProtocol.getTaskNo(), stationProtocol.getBarcode(),
                        stationProtocol.isLoading(), stationProtocol.isAutoing());
                return false;
            }
            if (wrkMast.getWrkSts() == WrkStsType.INBOUND_DEVICE_RUN.sts) {
                logDebugLimited("station_in_already_run_" + wrkMast.getWrkNo(), 5,
                        "[WCS Debug][条码站入库] 入库工作档已处于设备上走状态,等待输送线/PLC状态推进。stationId={},wrkNo={},barcode={},locNo={},wrkSts={}",
                        stationId, wrkMast.getWrkNo(), stationProtocol.getBarcode(),
                        wrkMast.getLocNo(), wrkMast.getWrkSts());
                return false;
            }
            String locNo = wrkMast.getLocNo();
            News.info("[WCS Debug][条码站入库] 准备匹配入库目标站。stationId={},stationTaskNo={},wrkNo={},barcode={},locNo={},wrkSts={}",
                    stationId, stationProtocol.getTaskNo(), wrkMast.getWrkNo(),
                    stationProtocol.getBarcode(), locNo, wrkMast.getWrkSts());
            FindCrnNoResult findCrnNoResult = commonService.findCrnNoByLocNo(locNo);
            if (findCrnNoResult == null) {
                News.taskInfo(wrkMast.getWrkNo(), "{}工作,未匹配到堆垛机", wrkMast.getWrkNo());
@@ -410,10 +541,18 @@
            LoopHitResult loopHitResult = findPathLoopHit(limitConfig, stationProtocol.getStationId(), targetStationId, loadGuardState);
            if (isDispatchBlocked(limitConfig, currentStationTaskCountRef[0], loadGuardState, loopHitResult.isThroughLoop())) {
                logDebugLimited("station_in_dispatch_blocked_" + wrkMast.getWrkNo(), 3,
                        "[WCS Debug][条码站入库] 入库输送命令暂缓,下发容量/环线负载受限。stationId={},targetStationId={},wrkNo={},currentStationTaskCount={},throughLoop={},loopNo={},hitStationId={}",
                        stationId, targetStationId, wrkMast.getWrkNo(), currentStationTaskCountRef[0],
                        loopHitResult.isThroughLoop(), loopHitResult.getLoopNo(), loopHitResult.getHitStationId());
                return true;
            }
            long commandStartMs = System.currentTimeMillis();
            StationCommand command = stationThread.getCommand(StationCommandType.MOVE, wrkMast.getWrkNo(), stationId, targetStationId, 0);
            News.info("[WCS Debug][条码站入库] 获取入库输送命令完成。stationId={},targetStationId={},wrkNo={},costMs={},command={}",
                    stationId, targetStationId, wrkMast.getWrkNo(),
                    System.currentTimeMillis() - commandStartMs, JSON.toJSONString(command));
            if (command == null) {
                News.taskInfo(wrkMast.getWrkNo(), "{}工作,获取输送线命令失败", wrkMast.getWrkNo());
                return false;
@@ -427,6 +566,9 @@
            if (wrkMastService.updateById(wrkMast)) {
                MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                News.info("输送站点入库命令下发成功,站点号={},工作号={},命令数据={}", stationId, wrkMast.getWrkNo(), JSON.toJSONString(command));
                News.info("[WCS Debug][条码站入库] 入库输送命令已入队。stationId={},targetStationId={},wrkNo={},barcode={},locNo={},deviceNo={}",
                        stationId, targetStationId, wrkMast.getWrkNo(), stationProtocol.getBarcode(),
                        locNo, basDevp.getDevpNo());
                redisUtil.set(RedisKeyType.STATION_IN_EXECUTE_LIMIT.key + stationId, "lock", 5);
                loadGuardState.reserveLoopTask(loopHitResult.getLoopNo());
                saveLoopLoadReserve(wrkMast.getWrkNo(), loopHitResult);
@@ -436,7 +578,7 @@
    }
    //执行堆垛机输送站点出库任务
    public synchronized void crnStationOutExecute() {
    public void crnStationOutExecute() {
        try {
            DispatchLimitConfig limitConfig = getDispatchLimitConfig();
            int[] currentStationTaskCountRef = new int[]{countCurrentStationTask()};
@@ -456,6 +598,30 @@
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private Integer getCrnStationOutLaneKey(WrkMast wrkMast) {
        if (wrkMast == null) {
            return null;
        }
        Object infoObj = redisUtil.get(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo());
        if (infoObj != null) {
            try {
                StationObjModel stationObjModel = JSON.parseObject(infoObj.toString(), StationObjModel.class);
                if (stationObjModel != null && stationObjModel.getStationId() != null) {
                    return stationObjModel.getStationId();
                }
            } catch (Exception e) {
                News.error("[WCS Debug][站点出库] 解析出库缓存站点失败,工作号={},cache={}", wrkMast.getWrkNo(), infoObj);
            }
        }
        if (wrkMast.getSourceStaNo() != null) {
            return wrkMast.getSourceStaNo();
        }
        if (wrkMast.getStaNo() != null) {
            return wrkMast.getStaNo();
        }
        return wrkMast.getWrkNo();
    }
    public void crnStationOutExecute(WrkMast wrkMast) {
@@ -556,7 +722,7 @@
    }
    //执行双工位堆垛机输送站点出库任务
    public synchronized void dualCrnStationOutExecute() {
    public void dualCrnStationOutExecute() {
        try {
            List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                    .eq("wrk_sts", WrkStsType.OUTBOUND_RUN_COMPLETE.sts)
@@ -630,7 +796,7 @@
    }
    //检测输送站点出库任务执行完成
    public synchronized void stationOutExecuteFinish() {
    public void stationOutExecuteFinish() {
        try {
            List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN.sts));
            for (WrkMast wrkMast : wrkMasts) {
@@ -683,7 +849,7 @@
    }
    // 检测任务转完成
    public synchronized void checkTaskToComplete() {
    public void checkTaskToComplete() {
        try {
            List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", WrkStsType.STATION_RUN_COMPLETE.sts));
            for (WrkMast wrkMast : wrkMasts) {
@@ -739,7 +905,7 @@
    }
    //检测输送站点是否运行堵塞
    public synchronized void checkStationRunBlock() {
    public void checkStationRunBlock() {
        try {
            List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>());
            for (BasDevp basDevp : basDevps) {
@@ -1017,12 +1183,12 @@
    }
    //获取输送线任务数量
    public synchronized int getCurrentStationTaskCount() {
    public int getCurrentStationTaskCount() {
        return countCurrentStationTask();
    }
    // 检测出库排序
    public synchronized void checkStationOutOrder() {
    public void checkStationOutOrder() {
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getId());
@@ -1142,7 +1308,7 @@
    }
    // 监控绕圈站点
    public synchronized void watchCircleStation() {
    public void watchCircleStation() {
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<BasDevp>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getId());
@@ -1409,6 +1575,20 @@
        redisUtil.expire(RedisKeyType.STATION_CYCLE_LOAD_RESERVE.key, LOOP_LOAD_RESERVE_EXPIRE_SECONDS);
    }
    private void logDebugLimited(String lockKey, int seconds, String format, Object... arguments) {
        String redisKey = RedisKeyType.LOG_LIMIT.key + "wcs_debug_" + lockKey;
        try {
            Object lock = redisUtil.get(redisKey);
            if (lock != null) {
                return;
            }
            redisUtil.set(redisKey, "lock", seconds);
        } catch (Exception e) {
            // 诊断日志不能影响主流程。
        }
        News.info(format, arguments);
    }
    private DispatchLimitConfig getDispatchLimitConfig() {
        DispatchLimitConfig config = new DispatchLimitConfig();
        Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);