Junjie
昨天 ef776e9fd5e4f64e4ad09a3faa12fb7bb646c79c
src/main/java/com/zy/core/plugin/FakeProcess.java
@@ -16,6 +16,7 @@
import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.dispatch.StationCommandDispatcher;
import com.zy.core.enums.*;
import com.zy.core.model.StationObjModel;
import com.zy.core.model.Task;
@@ -25,6 +26,9 @@
import com.zy.core.model.protocol.DualCrnProtocol;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.plugin.api.MainProcessPluginApi;
import com.zy.core.plugin.store.StoreInTaskContext;
import com.zy.core.plugin.store.StoreInTaskGenerationService;
import com.zy.core.plugin.store.StoreInTaskPolicy;
import com.zy.core.properties.SystemProperties;
import com.zy.core.thread.CrnThread;
import com.zy.core.thread.DualCrnThread;
@@ -49,7 +53,7 @@
@Slf4j
@Component
public class FakeProcess implements MainProcessPluginApi {
public class FakeProcess implements MainProcessPluginApi, StoreInTaskPolicy {
    private static final long METHOD_TIMEOUT_MS = 15000; // 15秒超时
    private static final ExecutorService timeoutExecutor = Executors.newCachedThreadPool();
@@ -85,7 +89,13 @@
    @Autowired
    private WmsOperateUtils wmsOperateUtils;
    @Autowired
    private WrkAnalysisService wrkAnalysisService;
    @Autowired
    private DualCrnOperateProcessUtils dualCrnOperateProcessUtils;
    @Autowired
    private StoreInTaskGenerationService storeInTaskGenerationService;
    @Autowired
    private StationCommandDispatcher stationCommandDispatcher;
    /**
     * 带超时保护执行方法
@@ -123,10 +133,6 @@
        stationOperateProcessUtils.stationInExecute();
        // 执行输送站点出库任务
        stationOperateProcessUtils.crnStationOutExecute();
        // 检测输送站点出库任务执行完成
        stationOperateProcessUtils.stationOutExecuteFinish();
        // 检测任务转完成
        stationOperateProcessUtils.checkTaskToComplete();
        // 检测出库排序
        stationOperateProcessUtils.checkStationOutOrder();
        // 监控绕圈站点
@@ -251,7 +257,7 @@
                    StationCommand command = stationThread.getCommand(StationCommandType.MOVE,
                            commonService.getWorkNo(WrkIoType.FAKE_TASK_NO.id), stationId,
                            entity.getBarcodeStation().getStationId(), 0);
                    MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                    stationCommandDispatcher.dispatch(basDevp.getDevpNo(), command, "fake-process", "fake-enable-in");
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_IN_STATION_DATA_LIMIT.key + stationId, "lock", 5);
                }
            }
@@ -346,7 +352,7 @@
                        News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败");
                        continue;
                    }
                    MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                    stationCommandDispatcher.dispatch(basDevp.getDevpNo(), command, "fake-process", "fake-in-task");
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_IN_TASK_LIMIT.key + stationId, "lock", 5);
                }
            }
@@ -425,141 +431,38 @@
     * 入库站,根据条码扫描生成入库工作档
     */
    public synchronized void generateStoreWrkFile() {
        try {
            if (fakeRealTaskRequestWms.equals("N")) {
                return;
            }
        storeInTaskGenerationService.generate(this);
    }
            Object systemConfigMapObj = redisUtil.get(RedisKeyType.SYSTEM_CONFIG_MAP.key);
            if (systemConfigMapObj == null) {
                return;
            }
            HashMap<String, String> systemConfigMap = (HashMap<String, String>) systemConfigMapObj;
    @Override
    public boolean isEnabled() {
        return !"N".equals(fakeRealTaskRequestWms);
    }
            int conveyorStationTaskLimit = 30;
            String conveyorStationTaskLimitStr = systemConfigMap.get("conveyorStationTaskLimit");
            if (conveyorStationTaskLimitStr != null) {
                conveyorStationTaskLimit = Integer.parseInt(conveyorStationTaskLimitStr);
            }
            int currentStationTaskCount = stationOperateProcessUtils.getCurrentStationTaskCount();
            if (currentStationTaskCount > conveyorStationTaskLimit) {
                News.error("输送站点任务已达到上限,上限值:{},站点任务数:{}", conveyorStationTaskLimit, currentStationTaskCount);
                return;
            }
    @Override
    public boolean matchCandidate(StoreInTaskContext context) {
        StationProtocol stationProtocol = context.getStationProtocol();
        return stationProtocol.isAutoing()
                && stationProtocol.isLoading()
                && stationProtocol.getTaskNo() > 0
                && !Cools.isEmpty(stationProtocol.getBarcode());
    }
            List<BasDevp> basDevps = basDevpService.list(new QueryWrapper<>());
            for (BasDevp basDevp : basDevps) {
                StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
                if (stationThread == null) {
                    continue;
                }
    @Override
    public void onRequestPermitGranted(StoreInTaskContext context) {
        redisUtil.set(getGenerateLockKey(context), "lock", 3);
    }
                Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
                List<StationObjModel> list = basDevp.getBarcodeStationList$();
                for (StationObjModel entity : list) {
                    Integer stationId = entity.getStationId();
                    if (!stationMap.containsKey(stationId)) {
                        continue;
                    }
                    StationProtocol stationProtocol = stationMap.get(stationId);
                    if (stationProtocol == null) {
                        continue;
                    }
                    // 满足自动、有物、有工作号,生成入库数据
                    if (stationProtocol.isAutoing()
                            && stationProtocol.isLoading()
                            && stationProtocol.getTaskNo() > 0) {
                        if (Cools.isEmpty(stationProtocol.getBarcode())) {
                            continue;
                        }
                        // 检测任务是否生成
                        List<WrkMast> wrkMasts = wrkMastService
                                .list(new QueryWrapper<WrkMast>()
                                        .eq("barcode", stationProtocol.getBarcode()));
                        if (!wrkMasts.isEmpty()) {
                            continue;
                        }
                        Object lock = redisUtil.get(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId);
                        if (lock != null) {
                            continue;
                        }
                        redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 3);
                        String barcode = stationProtocol.getBarcode();
                        Integer stationIdVal = stationProtocol.getStationId();
                        Integer taskNo = stationProtocol.getTaskNo();
                        // 1. 首先查询是否有已完成的异步响应
                        String response = wmsOperateUtils.queryAsyncInTaskResponse(barcode, stationIdVal, taskNo);
                        if (!Cools.isEmpty(response)) {
                            // 2. 有响应结果,处理响应
                            if (response.equals("FAILED") || response.startsWith("ERROR:")) {
                                // 请求失败,重新发起异步请求
                                News.error("WMS入库请求失败,重新发起请求,barcode={},stationId={},response={}", barcode,
                                        stationIdVal, response);
                                wmsOperateUtils.applyInTaskAsync(barcode, stationIdVal, taskNo,
                                        stationProtocol.getPalletHeight());
                                redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
                                stationProtocol.setSystemWarning("请求入库失败,WMS返回=" + response);
                                continue;
                            }
                            // 解析响应
                            JSONObject jsonObject = JSON.parseObject(response);
                            if (jsonObject.getInteger("code").equals(200)) {
                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                                CreateInTaskParam taskParam = new CreateInTaskParam();
                                taskParam.setTaskNo(dto.getTaskNo());
                                taskParam.setLocNo(dto.getLocNo());
                                taskParam.setTaskPri(dto.getTaskPri());
                                taskParam.setBarcode(barcode);
                                WrkMast wrkMast = commonService.createInTask(taskParam);
                                StationCommand command = stationThread.getCommand(StationCommandType.WRITE_INFO,
                                        wrkMast.getWrkNo(), stationId, stationId, 0);
                                if (command == null) {
                                    News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败");
                                    continue;
                                }
                                MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                                stationProtocol.setSystemWarning("");
                            } else {
                                // 接口返回非200,重新发起请求
                                News.error("WMS入库接口返回非200,重新发起请求,barcode={},stationId={},response={}", barcode,
                                        stationIdVal, response);
                                wmsOperateUtils.applyInTaskAsync(barcode, stationIdVal, taskNo,
                                        stationProtocol.getPalletHeight());
                                redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
                                stationProtocol.setSystemWarning("请求入库失败,WMS返回=" + response);
                            }
                        } else {
                            // 3. 没有响应结果,检查是否有请求正在进行中
                            if (!wmsOperateUtils.isAsyncRequestInProgress(barcode, stationIdVal, taskNo)) {
                                // 没有请求进行中,发起新的异步请求
                                News.info("发起异步WMS入库请求,barcode={},stationId={}", barcode, stationIdVal);
                                wmsOperateUtils.applyInTaskAsync(barcode, stationIdVal, taskNo,
                                        stationProtocol.getPalletHeight());
                                redisUtil.set(RedisKeyType.GENERATE_IN_TASK_LIMIT.key + stationId, "lock", 2);
                                stationProtocol.setSystemWarning("请求入库失败,WMS无返回");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
    @Override
    public void afterTaskCreated(StoreInTaskContext context, WrkMast wrkMast) {
        Integer stationId = context.getStationObjModel().getStationId();
        StationCommand command = context.getStationThread().getCommand(StationCommandType.WRITE_INFO,
                wrkMast.getWrkNo(), stationId, stationId, 0);
        if (command == null) {
            News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败");
            return;
        }
        stationCommandDispatcher.dispatch(context.getBasDevp().getDevpNo(), command, "fake-process", "write-info");
    }
    // 计算所有站点停留时间
@@ -621,7 +524,7 @@
                        continue;
                    }
                    MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                    stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "out-station-reset");
                    redisUtil.set(
                            RedisKeyType.CHECK_OUT_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId(),
                            "lock", 10);
@@ -684,7 +587,7 @@
                WrkMast wrkMast = wrkMastService.selectByWorkNo(stationProtocol.getTaskNo());
                if (wrkMast == null) {
                    MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                    stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "in-station-reset-task-over");
                    redisUtil.set(
                            RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId(),
                            "lock", 10);
@@ -692,7 +595,7 @@
                            JSON.toJSONString(command));
                } else {
                    if (wrkMast.getWrkSts() != WrkStsType.NEW_INBOUND.sts
                            && wrkMast.getWrkSts() != WrkStsType.INBOUND_DEVICE_RUN.sts) {
                            && wrkMast.getWrkSts() != WrkStsType.INBOUND_STATION_RUN.sts) {
                        Integer crnNo = wrkMast.getCrnNo();
                        if (crnNo != null) {
                            CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, crnNo);
@@ -705,7 +608,7 @@
                                continue;
                            }
                            MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                            stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "in-station-reset-crn-fetch");
                            redisUtil.set(RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key
                                    + stationObjModel.getStationId(), "lock", 10);
                            News.info("输送站点重置命令下发成功(crn_fetch),站点号={},命令数据={}", stationObjModel.getStationId(),
@@ -732,7 +635,7 @@
                                continue;
                            }
                            MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                            stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "in-station-reset-dual-crn-fetch");
                            redisUtil.set(RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key
                                    + stationObjModel.getStationId(), "lock", 10);
                            News.info("输送站点重置命令下发成功(crn_fetch),站点号={},命令数据={}", stationObjModel.getStationId(),
@@ -799,20 +702,25 @@
                        // 生成仿真站点数据
                        StationCommand command = stationThread.getCommand(StationCommandType.WRITE_INFO, 9998,
                                wrkMast.getSourceStaNo(), 0, 0);
                        MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                        stationCommandDispatcher.dispatch(stationObjModel.getDeviceNo(), command, "fake-process", "crn-out-complete-write-info");
                        redisUtil.set(RedisKeyType.CRN_OUT_TASK_COMPLETE_STATION_INFO.key + wrkMast.getWrkNo(), JSON.toJSONString(stationObjModel, SerializerFeature.DisableCircularReferenceDetect), 60 * 60 * 24);
                    }
                } else if (wrkMast.getWrkSts() == WrkStsType.LOC_MOVE_RUN.sts) {
                    updateWrkSts = WrkStsType.COMPLETE_LOC_MOVE.sts;
                } else if (wrkMast.getWrkSts() == WrkStsType.CRN_MOVE_RUN.sts) {
                    updateWrkSts = WrkStsType.COMPLETE_CRN_MOVE.sts;
                } else {
                    News.error("堆垛机处于等待确认且任务完成状态,但工作状态异常。堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                    continue;
                }
                Date now = new Date();
                wrkMast.setWrkSts(updateWrkSts);
                wrkMast.setSystemMsg("");
                wrkMast.setIoTime(new Date());
                wrkMast.setIoTime(now);
                wrkMast.setModiTime(now);
                if (wrkMastService.updateById(wrkMast)) {
                    wrkAnalysisService.markCraneComplete(wrkMast, now, updateWrkSts);
                    CrnCommand resetCommand = crnThread.getResetCommand(crnProtocol.getTaskNo(), crnProtocol.getCrnNo());
                    MessageQueue.offer(SlaveType.Crn, crnProtocol.getCrnNo(), new Task(2, resetCommand));
                    News.info("堆垛机任务状态更新成功,堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());