#
Junjie
9 天以前 8280c9eee52dd9fa3501305dfd4d1cab1564a73b
src/main/java/com/zy/core/plugin/FakeProcess.java
@@ -3,20 +3,13 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.zy.asrs.domain.param.CreateInTaskParam;
import com.zy.asrs.domain.param.CreateOutTaskParam;
import com.zy.asrs.entity.BasCrnp;
import com.zy.asrs.entity.BasDevp;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.service.BasCrnpService;
import com.zy.asrs.service.BasDevpService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.WrkMastService;
import com.zy.asrs.utils.Utils;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.common.model.StartupDto;
import com.zy.common.service.CommonService;
import com.zy.common.utils.HttpHandler;
import com.zy.common.utils.RedisUtil;
import com.zy.core.News;
import com.zy.core.cache.MessageQueue;
@@ -28,10 +21,12 @@
import com.zy.core.model.command.StationCommand;
import com.zy.core.model.protocol.CrnProtocol;
import com.zy.core.model.protocol.StationProtocol;
import com.zy.core.properties.SystemProperties;
import com.zy.core.thread.CrnThread;
import com.zy.core.thread.StationThread;
import com.zy.core.utils.CrnOperateProcessUtils;
import com.zy.core.utils.StationOperateProcessUtils;
import com.zy.core.utils.WmsOperateUtils;
import com.zy.system.entity.Config;
import com.zy.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
@@ -46,6 +41,12 @@
    private static Map<Integer,Long> stationStayTimeMap = new HashMap<>();
    private static String enableFake = "N";
    private static String fakeRealTaskRequestWms = "N";
    private static String fakeGenerateInTask = "Y";
    private static String fakeGenerateOutTask = "Y";
    private Thread asyncRunThread = null;
    private Thread asyncFakeRunThread = null;
    @Autowired
    private WrkMastService wrkMastService;
@@ -65,29 +66,14 @@
    private CrnOperateProcessUtils crnOperateUtils;
    @Autowired
    private StationOperateProcessUtils stationOperateProcessUtils;
    @Autowired
    private WmsOperateUtils wmsOperateUtils;
    @Override
    public void run() {
        Config enableFakeConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "enableFake"));
        if (enableFakeConfig != null) {
            enableFake = enableFakeConfig.getValue();
        }
        asyncRun();
        asyncFakeRun();
        //检测入库站是否有任务生成,并仿真生成模拟入库站点数据
        checkInStationHasTask();
        //生成仿真模拟入库任务
        generateFakeInTask();
        //生成仿真模拟出库任务
        generateFakeOutTask();
        //计算所有站点停留时间
        calcAllStationStayTime();
        //检测出库站点停留是否超时
        checkOutStationStayTimeOut();
        //检测入库站点堆垛机是否取走货物
        checkInStationCrnTake();
        //请求生成入库任务
        generateStoreWrkFile();
        //执行堆垛机任务
        crnOperateUtils.crnIoExecute();
        //堆垛机任务执行完成-具备仿真能力
@@ -100,9 +86,108 @@
        stationOperateProcessUtils.stationOutExecuteFinish();
    }
    public void asyncRun() {
        if (asyncRunThread != null) {
            return;
        }
        asyncRunThread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    // 系统运行状态判断
                    if (!SystemProperties.WCS_RUNNING_STATUS.get()) {
                        continue;
                    }
                    //请求生成入库任务
                    generateStoreWrkFile();
                    // 间隔
                    Thread.sleep(50);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        asyncRunThread.setName("asyncRunProcess");
        asyncRunThread.setDaemon(true);
        asyncRunThread.start();
    }
    public void asyncFakeRun() {
        if (asyncFakeRunThread != null) {
            return;
        }
        asyncFakeRunThread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    Config enableFakeConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "enableFake"));
                    if (enableFakeConfig != null) {
                        enableFake = enableFakeConfig.getValue();
                    }
                    Config fakeRealTaskRequestWmsConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "fakeRealTaskRequestWms"));
                    if (fakeRealTaskRequestWmsConfig != null) {
                        fakeRealTaskRequestWms = fakeRealTaskRequestWmsConfig.getValue();
                    }
                    Config fakeGenerateInTaskConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "fakeGenerateInTask"));
                    if (fakeGenerateInTaskConfig != null) {
                        fakeGenerateInTask = fakeGenerateInTaskConfig.getValue();
                    }
                    Config fakeGenerateOutTaskConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "fakeGenerateOutTask"));
                    if (fakeGenerateOutTaskConfig != null) {
                        fakeGenerateOutTask = fakeGenerateOutTaskConfig.getValue();
                    }
                    // 系统运行状态判断
                    if (!SystemProperties.WCS_RUNNING_STATUS.get()) {
                        continue;
                    }
                    //检测入库站是否有任务生成,并仿真生成模拟入库站点数据
                    checkInStationHasTask();
                    //生成仿真模拟入库任务
                    generateFakeInTask();
                    //生成仿真模拟出库任务
                    generateFakeOutTask();
                    //计算所有站点停留时间
                    calcAllStationStayTime();
                    //检测出库站点停留是否超时
                    checkOutStationStayTimeOut();
                    //检测入库站点堆垛机是否取走货物
                    checkInStationCrnTake();
                    //检测输送站点是否运行堵塞
                    stationOperateProcessUtils.checkStationRunBlock();
                    // 间隔
                    Thread.sleep(50);
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupt();
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        asyncFakeRunThread.setName("asyncFakeRunProcess");
        asyncFakeRunThread.setDaemon(true);
        asyncFakeRunThread.start();
    }
    //检测入库站是否有任务生成,并仿真生成模拟入库站点数据
    private synchronized void checkInStationHasTask() {
        if (!enableFake.equals("Y")) {
            return;
        }
        if (!fakeGenerateInTask.equals("Y")) {
            return;
        }
@@ -127,8 +212,8 @@
                    continue;
                }
                Object object = redisUtil.get(RedisKeyType.GENERATE_FAKE_IN_STATION_DATA_LIMIT.key + stationId);
                if (object != null) {
                Object lock = redisUtil.get(RedisKeyType.GENERATE_FAKE_IN_STATION_DATA_LIMIT.key + stationId);
                if(lock != null){
                    continue;
                }
@@ -137,9 +222,9 @@
                        && !stationProtocol.isLoading()
                        && stationProtocol.getTaskNo() == 0
                ) {
                    StationCommand command = stationThread.getMoveCommand(9999, stationId, 0, 0);
                    StationCommand command = stationThread.getMoveCommand(commonService.getWorkNo(WrkIoType.FAKE_TASK_NO.id), stationId, entity.getBarcodeStation().getStationId(), 0);
                    MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_IN_STATION_DATA_LIMIT.key + stationId, "lock", 10);
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_IN_STATION_DATA_LIMIT.key + stationId, "lock", 5);
                }
            }
        }
@@ -148,6 +233,14 @@
    //生成仿真模拟入库任务
    private synchronized void generateFakeInTask() {
        if (!enableFake.equals("Y")) {
            return;
        }
        if (fakeRealTaskRequestWms.equals("Y")) {
            return;
        }
        if (!fakeGenerateInTask.equals("Y")) {
            return;
        }
@@ -160,7 +253,7 @@
            Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
            List<StationObjModel> list = basDevp.getInStationList$();
            List<StationObjModel> list = basDevp.getBarcodeStationList$();
            for (StationObjModel model : list) {
                Integer stationId = model.getStationId();
                if(!stationMap.containsKey(stationId)){
@@ -174,14 +267,18 @@
                Object object = redisUtil.get(RedisKeyType.GENERATE_FAKE_IN_TASK_LIMIT.key + stationId);
                if (object != null) {
                    return;
                    continue;
                }
                //满足自动、有物、工作号9999,生成入库数据
                //满足自动、有物、有工作号,生成入库数据
                if (stationProtocol.isAutoing()
                        && stationProtocol.isLoading()
                        && stationProtocol.getTaskNo() == 9999
                        && stationProtocol.getTaskNo() > 0
                ) {
                    if (Cools.isEmpty(stationProtocol.getBarcode())) {
                        continue;
                    }
                    //检测任务是否生成
                    List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
                    if (!wrkMasts.isEmpty()) {
@@ -212,8 +309,14 @@
                    taskParam.setStaNo(targetStationId);
                    taskParam.setLocNo(locMast.getLocNo());
                    taskParam.setBarcode(stationProtocol.getBarcode());
                    boolean result = commonService.createInTask(taskParam);
                    WrkMast wrkMast = commonService.createInTask(taskParam);
                    StationCommand command = stationThread.getMoveCommand(wrkMast.getWrkNo(), stationId, stationId, 0);
                    if(command == null){
                        News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败");
                        continue;
                    }
                    MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_IN_TASK_LIMIT.key + stationId, "lock", 5);
                }
            }
@@ -223,6 +326,14 @@
    //生成仿真模拟出库任务
    private synchronized void generateFakeOutTask() {
        if (!enableFake.equals("Y")) {
            return;
        }
        if (fakeRealTaskRequestWms.equals("Y")) {
            return;
        }
        if (!fakeGenerateOutTask.equals("Y")) {
            return;
        }
@@ -274,7 +385,7 @@
                    taskParam.setStaNo(stationId);
                    taskParam.setLocNo(locMast.getLocNo());
                    boolean result = commonService.createOutTask(taskParam);
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_OUT_TASK_LIMIT.key + stationId, "lock", 15);
                    redisUtil.set(RedisKeyType.GENERATE_FAKE_OUT_TASK_LIMIT.key + stationId, "lock", 10);
                }
            }
        }
@@ -285,6 +396,10 @@
     * 入库站,根据条码扫描生成入库工作档
     */
    public synchronized void generateStoreWrkFile() {
        if (fakeRealTaskRequestWms.equals("N")) {
            return;
        }
        List<BasDevp> basDevps = basDevpService.selectList(new EntityWrapper<>());
        for (BasDevp basDevp : basDevps) {
            StationThread stationThread = (StationThread) SlaveConnection.get(SlaveType.Devp, basDevp.getDevpNo());
@@ -294,7 +409,7 @@
            Map<Integer, StationProtocol> stationMap = stationThread.getStatusMap();
            List<StationObjModel> list = basDevp.getInStationList$();
            List<StationObjModel> list = basDevp.getBarcodeStationList$();
            for (StationObjModel entity : list) {
                Integer stationId = entity.getStationId();
                if(!stationMap.containsKey(stationId)){
@@ -306,66 +421,49 @@
                    continue;
                }
                //满足自动、有物、工作号9999,生成入库数据
                //满足自动、有物、有工作号,生成入库数据
                if (stationProtocol.isAutoing()
                        && stationProtocol.isLoading()
                        && stationProtocol.getTaskNo() == 9999
                        && stationProtocol.getTaskNo() > 0
                ) {
                    if (Cools.isEmpty(stationProtocol.getBarcode())) {
                        continue;
                    }
                    //检测任务是否生成
                    List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("barcode", stationProtocol.getBarcode()));
                    if (!wrkMasts.isEmpty()) {
                        continue;
                    }
                    String wmsUrl = null;
                    Config wmsSystemUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemUri"));
                    if (wmsSystemUriConfig != null) {
                        wmsUrl = wmsSystemUriConfig.getValue();
                    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", 2);
                    if(wmsUrl == null){
                        News.error("未配置WMS系统URI,配置文件Code编码:wmsSystemUri");
                        return;
                    String response = wmsOperateUtils.applyInTask(stationProtocol.getBarcode(), stationProtocol.getStationId(), stationProtocol.getPalletHeight());
                    if (response == null) {
                        News.error("请求WMS入库接口失败,接口未响应!!!response:{}", response);
                        continue;
                    }
                    JSONObject jsonObject = JSON.parseObject(response);
                    if (jsonObject.getInteger("code").equals(200)) {
                        StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                    String wmsSystemInUrl = null;
                    Config wmsSystemInUrlConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "wmsSystemInUrl"));
                    if (wmsSystemInUrlConfig != null) {
                        wmsSystemInUrl = wmsSystemInUrlConfig.getValue();
                    }
                        CreateInTaskParam taskParam = new CreateInTaskParam();
                        taskParam.setTaskNo(dto.getTaskNo());
                        taskParam.setLocNo(dto.getLocNo());
                        taskParam.setTaskPri(dto.getTaskPri());
                        taskParam.setBarcode(stationProtocol.getBarcode());
                        WrkMast wrkMast = commonService.createInTask(taskParam);
                    if(wmsSystemInUrlConfig == null){
                        News.error("未配置WMS入库接口地址,配置文件Code编码:wmsSystemInUrl");
                        return;
                    }
                    try {
                        HashMap<String, Object> param = new HashMap<>();
                        param.put("barcode", stationProtocol.getBarcode());
                        param.put("sourceStaNo", stationProtocol.getStationId());
                        param.put("locType1", stationProtocol.getPalletHeight());
                        param.put("row", Utils.getInTaskEnableRow());
                        String response = new HttpHandler.Builder()
                                .setUri(wmsUrl)
                                .setPath(wmsSystemInUrl)
                                .setJson(JSON.toJSONString(param))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        if (jsonObject.getInteger("code").equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                            CreateInTaskParam taskParam = new CreateInTaskParam();
                            taskParam.setTaskNo(String.valueOf(dto.getTaskNo()));
                            taskParam.setLocNo(dto.getLocNo());
                            taskParam.setTaskPri(dto.getTaskPri());
                            boolean result = commonService.createInTask(taskParam);
                        } else {
                            News.error("请求WMS接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        StationCommand command = stationThread.getMoveCommand(wrkMast.getWrkNo(), stationId, stationId, 0);
                        if(command == null){
                            News.taskInfo(wrkMast.getWrkNo(), "获取输送线命令失败");
                            continue;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        MessageQueue.offer(SlaveType.Devp, basDevp.getDevpNo(), new Task(2, command));
                    }
                }
            }
@@ -428,7 +526,7 @@
                    MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                    redisUtil.set(RedisKeyType.CHECK_OUT_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId(), "lock",10);
                    News.info("输送站点重置命令下发成功,站点号={},命令数据={}", stationObjModel.getStationId(), JSON.toJSONString(command));
                    News.info("输送站点出库重置命令下发成功,站点号={},命令数据={}", stationObjModel.getStationId(), JSON.toJSONString(command));
                }
            }
        }
@@ -471,7 +569,7 @@
                    if (wrkMast == null) {
                        MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                        redisUtil.set(RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId(), "lock",10);
                        News.info("输送站点重置命令下发成功,站点号={},命令数据={}", stationObjModel.getStationId(), JSON.toJSONString(command));
                        News.info("输送站点重置命令下发成功(task_over),站点号={},命令数据={}", stationObjModel.getStationId(), JSON.toJSONString(command));
                    }else {
                        if (wrkMast.getWrkSts() != WrkStsType.NEW_INBOUND.sts && wrkMast.getWrkSts() != WrkStsType.INBOUND_DEVICE_RUN.sts) {
                            Integer crnNo = wrkMast.getCrnNo();
@@ -480,13 +578,13 @@
                                continue;
                            }
                            CrnProtocol crnProtocol = crnThread.getStatus();
                            if (crnProtocol.getStatusType().equals(CrnStatusType.FETCH_MOVING) || crnProtocol.getStatusType().equals(CrnStatusType.FETCHING)) {
                            if (!crnProtocol.getStatusType().equals(CrnStatusType.PUT_MOVING) && !crnProtocol.getStatusType().equals(CrnStatusType.PUTTING)) {
                                continue;
                            }
                            MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                            redisUtil.set(RedisKeyType.CHECK_IN_STATION_STAY_TIME_OUT_LIMIT.key + stationObjModel.getStationId(), "lock",10);
                            News.info("输送站点重置命令下发成功,站点号={},命令数据={}", stationObjModel.getStationId(), JSON.toJSONString(command));
                            News.info("输送站点重置命令下发成功(crn_fetch),站点号={},命令数据={}", stationObjModel.getStationId(), JSON.toJSONString(command));
                        }
                    }
                }
@@ -512,6 +610,10 @@
                    && crnProtocol.getTaskNo() > 0
                    && crnProtocol.getStatus() == CrnStatusType.WAITING.id
            ) {
                Object lock = redisUtil.get(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo());
                if(lock != null){
                    continue;
                }
                // 获取待确认工作档
                WrkMast wrkMast = wrkMastService.selectByWorkNo(crnProtocol.getTaskNo());
@@ -546,6 +648,8 @@
                        StationCommand command = stationThread.getMoveCommand(9998, wrkMast.getSourceStaNo(), 0, 0);
                        MessageQueue.offer(SlaveType.Devp, stationObjModel.getDeviceNo(), new Task(2, command));
                    }
                }else if(wrkMast.getWrkSts() == WrkStsType.LOC_MOVE_RUN.sts){
                    updateWrkSts = WrkStsType.COMPLETE_LOC_MOVE.sts;
                }else{
                    News.error("堆垛机处于等待确认且任务完成状态,但工作状态异常。堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                    continue;
@@ -559,6 +663,8 @@
                    MessageQueue.offer(SlaveType.Crn, crnProtocol.getCrnNo(), new Task(2, resetCommand));
                    News.info("堆垛机任务状态更新成功,堆垛机号={},工作号={}", basCrnp.getCrnNo(), crnProtocol.getTaskNo());
                }
                redisUtil.set(RedisKeyType.CRN_IO_EXECUTE_FINISH_LIMIT.key + basCrnp.getCrnNo(), "lock",10);
            }
        }
    }