#
Junjie
2025-08-13 d65b37f317d7759d41154d33caa15bce61fab3eb
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -62,7 +62,6 @@
 */
@Slf4j
@Service("mainService")
@Transactional
public class MainServiceImpl {
    public static final long COMMAND_TIMEOUT = 5 * 1000;
@@ -650,75 +649,73 @@
     * 堆垛机站出库到出库站
     */
    public synchronized void crnStnToOutStn(Integer mark) {
        List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>()
                .eq("wrk_sts", 12)
        );
        for (CrnSlave crnSlave : slaveProperties.getCrn()) {
            // 遍历堆垛机出库站
            for (CrnSlave.CrnStn crnStn : crnSlave.getCrnOutStn()) {
                // 获取堆垛机出库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
                StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
        for (WrkMast wrkMast : wrkMasts) {
            // 判断工作档条件
            if (wrkMast.getIoType() < 100 || wrkMast.getStaNo() == null || wrkMast.getSourceStaNo() == null) {
                continue;
            }
            Integer devpId = null;
            Integer sourceStaNo = wrkMast.getSourceStaNo();
            for (CrnSlave crnSlave : slaveProperties.getCrn()) {
                for (CrnSlave.CrnStn crnStn : crnSlave.getCrnOutStn()) {
                    if (crnStn.getStaNo().equals(sourceStaNo)) {
                        devpId = crnStn.getDevpPlcId();
                        break;
                    }
                }
                if (staProtocol.isAutoing() && staProtocol.isLoading() && (staProtocol.getWorkNo() == 0 || staProtocol.getStaNo() == null)) {
                    // 查询工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakOutStep2(staProtocol.getSiteId());
                    if (wrkMast == null) {
            }
            if (devpId == null) {
                News.error("" + mark + " - 0" + " - 堆垛机站出库到出库站,未找到对应PLC");
                continue;
            }
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devpId);
            StaProtocol staProtocol = devpThread.getStation().get(sourceStaNo);
            if (staProtocol == null) {
                continue;
            } else {
                staProtocol = staProtocol.clone();
            }
            if (staProtocol.isAutoing() && staProtocol.isLoading() && (staProtocol.getWorkNo() == 0 || staProtocol.getStaNo() == null)) {
                CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, wrkMast.getCrnNo());
                CrnProtocol crnProtocol = crnThread.getCrnProtocol();
                if (crnProtocol.modeType == CrnModeType.AUTO && crnProtocol.getTaskNo().equals(wrkMast.getWrkNo().shortValue())
                        && (crnProtocol.statusType == CrnStatusType.PUTTING || crnProtocol.statusType == CrnStatusType.WAITING)) {
                    //放货中 或 等待确认
                    News.warnNoLog("" + mark + " - 0" + " - 开始执行");
                    // 命令下发区 --------------------------------------------------------------------------
                    LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
                            .eq("loc_no", wrkMast.getSourceLocNo()));
                    // 下发站点信息
                    staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                    staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                    staProtocol.setPalletSize(locMast.getLocType2());
                    if (!MessageQueue.offer(SlaveType.Devp, devpId, new Task(2, staProtocol))) {
                        continue;
                    }
                    // 判断工作档条件
                    if (wrkMast.getIoType() < 100 || wrkMast.getStaNo() == null || wrkMast.getSourceStaNo() == null) {
                        continue;
                    }
                    // 判断吊车是否实际已完成,且电脑状态在move中,以备电脑进行更新工作档
                    CrnThread crnThread = (CrnThread) SlaveConnection.get(SlaveType.Crn, wrkMast.getCrnNo());
                    CrnProtocol crnProtocol = crnThread.getCrnProtocol();
                    if (crnProtocol.statusType == CrnStatusType.FETCHING || crnProtocol.statusType == CrnStatusType.PUTTING) {
                        // 移动中
                        continue;
                    }
                    //  判断堆垛机状态等待确认
                    if (crnProtocol.modeType == CrnModeType.AUTO && crnProtocol.getTaskNo().equals(wrkMast.getWrkNo().shortValue())
                            && crnProtocol.statusType == CrnStatusType.WAITING
                            && crnProtocol.forkPosType == CrnForkPosType.HOME) {
                        News.warnNoLog("" + mark + " - 0" + " - 开始执行");
                        // 命令下发区 --------------------------------------------------------------------------
                        LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
                                .eq("loc_no", wrkMast.getSourceLocNo()));
                        // 下发站点信息
                        staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                        staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                        staProtocol.setPalletSize(locMast.getLocType2());
                        if (!MessageQueue.offer(SlaveType.Devp, crnStn.getDevpPlcId(), new Task(2, staProtocol))) {
                            continue;
                        }
                        // 更新工作档状态为14失败
                        wrkMast.setWrkSts(14L);
                        wrkMast.setCrnEndTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) != 0) {
                            // 复位堆垛机
                            crnThread.setResetFlag(true);
                        } else {
                            News.error("" + mark + " - 1" + " - 更新工作档的工作状态为14失败!!! [工作号:{}]", wrkMast.getWrkNo());
                        }
                    // 更新工作档状态为14失败
                    wrkMast.setWrkSts(14L);
                    wrkMast.setCrnEndTime(new Date());
                    if (wrkMastMapper.updateById(wrkMast) != 0) {
                        News.info("" + mark + " - 1" + " - 更新工作档的工作状态为14成功!!! [工作号:{}]", wrkMast.getWrkNo());
                    } else {
                        News.errorNoLog("" + mark + " - 6" + " - 堆垛机信息不符合入库条件!!!"
                                + " 堆垛机状态:" + crnProtocol.modeType + "==自动AUTO:" + CrnModeType.AUTO
                                + "、堆垛机任务号:" + crnProtocol.getTaskNo() + "==工作档任务号:" + wrkMast.getWrkNo().shortValue()
                                + "、状态枚举:" + crnProtocol.statusType + "==WAITING:90 //任务完成等待WCS确认):" + CrnStatusType.WAITING
                                + "、货叉位置:" + crnProtocol.forkPosType + "==HOME:0  // 货叉原位:" + CrnForkPosType.HOME);
                        News.error("" + mark + " - 1" + " - 更新工作档的工作状态为14失败!!! [工作号:{}]", wrkMast.getWrkNo());
                    }
                }
            }
        }
        News.infoNoLog("" + mark + " - 0" + " - 堆垛机站出库到出库站  ===》执行完成");
    }
@@ -1231,12 +1228,22 @@
                    }
                    if (crnProtocol.getCrnNo() == 1) {
                        //判断堆垛机和当前任务是否处于一个巷道
                        if (Utils.getLaneByLocNo(wrkMast.getSourceLocNo()) != crnProtocol.getCrnLane()) {
                            //判断堆垛机所在巷道是否存在其他任务,如存在则优先执行
                            List<WrkMast> currentWrkMasts = wrkMastService.selectLaneWrkMast(crnProtocol.getCrnLane(), false);
                            if (!currentWrkMasts.isEmpty()) {
                                continue;//当前堆垛机所在巷道存在任务
                        String turnCrnExecuteCurrentChannel = "Y";
                        Config turnCrnExecuteCurrentChannelConfig = configService.selectOne(new EntityWrapper<Config>()
                                .eq("code", "turnCrnExecuteCurrentChannel")
                        );
                        if (turnCrnExecuteCurrentChannelConfig != null) {
                            turnCrnExecuteCurrentChannel = turnCrnExecuteCurrentChannelConfig.getValue();
                        }
                        if(turnCrnExecuteCurrentChannel.equals("Y")) {
                            //判断堆垛机和当前任务是否处于一个巷道
                            if (Utils.getLaneByLocNo(wrkMast.getSourceLocNo()) != crnProtocol.getCrnLane()) {
                                //判断堆垛机所在巷道是否存在其他任务,如存在则优先执行
                                List<WrkMast> currentWrkMasts = wrkMastService.selectLaneWrkMast(crnProtocol.getCrnLane(), false);
                                if (!currentWrkMasts.isEmpty()) {
                                    continue;//当前堆垛机所在巷道存在任务
                                }
                            }
                        }
                    }
@@ -1522,6 +1529,7 @@
            if (wrkMastMapper.updateById(wrkMast) == 0) {
                News.error("" + mark + " - 3" + " - 5" + " - 【库位移转】 修改工作档状态 11.生成出库ID => 12.吊车出库中 失败!!,工作号={}", wrkMast.getWrkNo());
            }
            crnProtocol.setLastIo("O");
        }
        News.infoNoLog("" + mark + " - 3" + " - 0" + " - 堆垛机入出库作业下发:库位移转完成");
@@ -1627,26 +1635,45 @@
                if (crnProtocol.getTaskNo() == 9999) {
                    // 堆垛机复位
                    crnThread.setResetFlag(true);
                } else {
                    // 获取入库待确认工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakInStep3(crnProtocol.getTaskNo().intValue());
                    if (wrkMast == null) {
                        News.error("" + mark + " - 1" + " - 堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                        continue;
                    }
                    continue;
                }
                // 获取待确认工作档
                WrkMast wrkMast = wrkMastMapper.selectPakInStep3(crnProtocol.getTaskNo().intValue());
                if (wrkMast == null) {
                    News.error("" + mark + " - 1" + " - 堆垛机处于等待确认且任务完成状态,但未找到工作档。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                    continue;
                }
                Date now = new Date();
                if(wrkMast.getIoType() < 100){
                    // 入库 + 库位转移  ==> 4.入库完成
                    if (wrkMast.getWrkSts() == 3 || (wrkMast.getWrkSts() == 12 && (wrkMast.getIoType() == 11 || wrkMast.getIoType() == 111))) {
                        wrkMast.setWrkSts(4L);
                    } else {
                        continue;
                    }
                    Date now = new Date();
                    wrkMast.setCrnEndTime(now);
                    wrkMast.setModiTime(now);
                    // 修改成功后复位堆垛机
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        // 堆垛机复位
                        News.warnNoLog("" + mark + " - 2" + " - 修改成功后复位堆垛机 : 堆垛机号={}", crnThread.getCrnProtocol().getCrnNo());
                        crnThread.setResetFlag(true);
                    }
                }else {
                    List<Long> list = new ArrayList<>();
                    list.add(14L);
                    list.add(15L);
                    if (!list.contains(wrkMast.getWrkSts())) {
                        News.error("" + mark + " - 1" + " - 堆垛机处于等待确认但工作档状态未完成。堆垛机号={},工作号={}", crn.getId(), crnProtocol.getTaskNo());
                        continue;
                    }
                    wrkMast.setCrnEndTime(now);
                    if (wrkMastMapper.updateById(wrkMast) != 0) {
                        // 复位堆垛机
                        crnThread.setResetFlag(true);
                    }
                }
@@ -1863,9 +1890,13 @@
                        if (jsonObject.getInteger("code").equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                            LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>()
                                    .eq("loc_no", dto.getLocNo()));
                            // 更新站点信息 且 下发plc命令
                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
                            staProtocol.setStaNo(dto.getStaNo().shortValue());
                            staProtocol.setPalletSize(locMast.getLocType2());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
@@ -1973,7 +2004,7 @@
                    wrkMast1.setIoType(3); // 入出库状态:3.站到站
                    wrkMast1.setIoPri(13D); // 优先级
                    wrkMast1.setSourceStaNo(emptyInSta.getStaNo());
                    wrkMast1.setStaNo(1007);
                    wrkMast1.setStaNo(1004);
                    wrkMast1.setBarcode(barcode);
                    // 操作人员数据
                    wrkMast1.setAppeTime(new Date());
@@ -1986,6 +2017,7 @@
                    // 更新站点信息 且 下发plc命令
                    staProtocol.setWorkNo((short) workNo);
                    staProtocol.setStaNo((short) 2003);
                    staProtocol.setPalletSize((short) 1);
                    devpThread.setPakMk(staProtocol.getSiteId(), false);
                    boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                    if (!result) {