#
Junjie
2023-10-05 45acd20e06a15d0096776ffe3adae4dc94d22762
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.*;
@@ -224,7 +225,7 @@
                        param.setBarcode(barcode);
                        param.setIoType(1);
                        param.setSourceStaNo(inSta.getStaNo());
//                        param.setLocType1(locTypeDto.getLocType1());
                        param.setLocType1(locTypeDto.getLocType1());
                        String response = new HttpHandler.Builder()
                                .setUri(wmsUrl)
                                .setPath("/rpc/pakin/loc/v1")
@@ -238,14 +239,14 @@
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                            // plc 处理
                            barcodeThread.setBarcode("");
                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
                            staProtocol.setStaNo(dto.getStaNo().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
                                throw new CoolException("更新plc站点信息失败");
                            }
//                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
//                            staProtocol.setStaNo(dto.getStaNo().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            if (!result) {
//                                throw new CoolException("更新plc站点信息失败");
//                            }
                            // 判断重复工作档
                            WrkMast wrkMast = wrkMastMapper.selectPakInStep11(inSta.getStaNo());
@@ -267,10 +268,10 @@
                            }
                            News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        } else if (code == 700) {
                            staProtocol.setWorkNo((short) 32002);
                            staProtocol.setRollback102(1);//102站回退信号
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(5, staProtocol));
//                            staProtocol.setWorkNo((short) 32002);
//                            staProtocol.setRollback102(1);//102站回退信号
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(5, staProtocol));
                            // led 异常显示
                            if (ledThread != null) {
@@ -405,8 +406,8 @@
                }
                if (staProtocol.isAutoing()
                        && staProtocol.isLoading()
                        && (staProtocol.getWorkNo() > 0 && staProtocol.getWorkNo() < 9999)
                        && staProtocol.getStaNo().equals(staProtocol.getSiteId().shortValue())
//                        && (staProtocol.getWorkNo() > 0 && staProtocol.getWorkNo() < 9999)
//                        && staProtocol.getStaNo().equals(staProtocol.getSiteId().shortValue())
                        && staProtocol.isPakMk()){
                    // 获取条码扫描仪信息
@@ -507,12 +508,18 @@
                            }
                            String sourceLocNo = wrkMast.getSourceLocNo().trim();
                            LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(pickSta.getLiftNo(), Utils.getLev(sourceLocNo));//获取回库提升机目标站
                            if (liftStaProtocol == null) {
                                continue;
                            }
                            // 更新工作档数据状态
                            wrkMast.setIoType(wrkMast.getIoType() - 50); // 入出库类型: 103->53,104->54,107->57
                            wrkMast.setWrkSts(2L); // 工作状态: 2.设备上走
                            wrkMast.setSourceStaNo(pickSta.getStaNo()); // 源站
                            wrkMast.setStaNo(dto.getStaNo()); // 目标站
                            wrkMast.setCrnNo(dto.getCrnNo());
//                            wrkMast.setStaNo(dto.getStaNo()); // 目标站
//                            wrkMast.setCrnNo(dto.getCrnNo());
                            wrkMast.setStaNo(liftStaProtocol.getStaNo());//目标站
                            wrkMast.setLocNo(sourceLocNo); // 目标库位 = 出库时的源库位
                            wrkMast.setShuttleNo(null); // 穿梭车清空
                            wrkMast.setModiTime(new Date());
@@ -531,14 +538,16 @@
                                throw new CoolException("修改库位状态失败");
                            }
                            // 更新站点信息 且 下发plc命令
                            staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
                            staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
                                News.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                            }
//                            // 更新站点信息 且 下发plc命令
//                            staProtocol.setWorkNo(wrkMast.getWrkNo().shortValue());
//                            staProtocol.setStaNo(wrkMast.getStaNo().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            if (!result) {
//                                News.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
//                            }
                            barcodeThread.setBarcode("");//清理条码
                        } else if (code == 500){
                            if (ledThread != null) {
@@ -549,10 +558,10 @@
                            }
                            News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v1", JSON.toJSONString(param), response);
                        } else {
                            staProtocol.setWorkNo((short) 32002);
                            staProtocol.setStaNo(pickSta.getBackSta().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            staProtocol.setWorkNo((short) 32002);
//                            staProtocol.setStaNo(pickSta.getBackSta().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            // led 异常显示
                            if (ledThread != null) {
@@ -794,6 +803,12 @@
                return false;//小车已被独占,禁止再派发任务
            }
            //判断小车是否存在移动任务
            WrkMast hasMoveWorking = wrkMastMapper.selectShuttleHasMoveWorking(wrkMast.getShuttleNo());
            if (hasMoveWorking != null) {
                return false;//存在移动任务,禁止执行入库任务
            }
            //判断小车是否到达输送站点库位
            if (!shuttleProtocol.getCurrentLocNo().equals(liftStaProtocol.getLocNo())) {
                //小车不在输送站点位置
@@ -873,7 +888,7 @@
                return false;//找不到站点
            }
            if (liftStaProtocol.getModel() && !liftStaProtocol.getBusy() && !liftStaProtocol.getHasTray()) {
            if (!(liftStaProtocol.getModel() && !liftStaProtocol.getBusy() && !liftStaProtocol.getHasTray())) {
                return false;//站点必须自动、空闲、没有托盘
            }
@@ -1161,6 +1176,12 @@
            //获取提升机命令
            NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftProtocol.getLiftNo().intValue(), NyLiftTaskModelType.MOVE_TRAY.id, startSta, targetSta, wrkMast.getWrkNo());
            if (wrkMast.getIoType() == 53) {
                //拣料再回库,重新分配设备工作号
                Random random = new Random();
                int deviceWrk = Math.abs((liftCommand.getTaskNo().intValue() + random.nextInt(9999)));//获取设备工作号
                liftCommand.setTaskNo((short) deviceWrk);
            }
            ArrayList<NyLiftCommand> commands = new ArrayList<>();
            commands.add(liftCommand);
@@ -1196,6 +1217,17 @@
        //23.小车搬运完成 ==> 24.提升机搬运中
        if (wrkMast.getWrkSts() == 23) {
            NyShuttleThread shuttleThread = (NyShuttleThread) SlaveConnection.get(SlaveType.Shuttle, wrkMast.getShuttleNo());
            if (shuttleThread == null) {
                return false;
            }
            //调度小车避让
            boolean result = Utils.searchEmptyGroupToMoveShuttle(Utils.getLev(wrkMast.getSourceLocNo()), wrkMast.getShuttleNo(), shuttleThread);
            if (!result) {
                return false;
            }
            //获取出库站点(目标站)
            BasDevp basDevp = basDevpService.selectById(wrkMast.getStaNo());
            if (basDevp == null) {
@@ -1286,7 +1318,6 @@
                    && liftProtocol.getTaskNo() != 0
                    && !liftProtocol.getBusy()
            ) {
                boolean clearTaskNoFlag = false;//清零工作号标记
                //将任务档标记为完成
                WrkMast wrkMast = wrkMastMapper.selectByWorkNo324104(liftProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
@@ -1294,7 +1325,7 @@
                        case 3://3.提升机搬运中 ==> 4.提升机搬运完成
                            wrkMast.setWrkSts(4L);
                            wrkMast.setLiftNo(null);//释放提升机
                            if (liftProtocol.getToken() == wrkMast.getWrkNo()) {
                            if (liftProtocol.getToken().equals(wrkMast.getWrkNo())) {
                                //释放提升机令牌
                                liftProtocol.setToken(0);
                            }
@@ -1308,20 +1339,20 @@
                                //释放提升机令牌
                                liftProtocol.setToken(0);
                            }
                            if (wrkMast.getMk() == null || !wrkMast.getMk().equals("Y")) {
                                //不需要用到机械臂,直接转29.出库完成
                                wrkMast.setWrkSts(29L);
                            }
                            break;
                        case 106://106.提升机搬运中 ==> 107.提升机搬运完成
                            wrkMast.setWrkSts(107L);
//                            //清零工作号
//                            clearTaskNoFlag = true;
                            break;
                        default:
                    }
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        if (clearTaskNoFlag) {
                            //清零工作号
                            MessageQueue.offer(SlaveType.Lift, liftSlave.getId(), new Task(4, null));
                        }
                        //设置提升机为空闲状态
                        liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                        //任务指令清零
@@ -1330,8 +1361,19 @@
                    } else {
                        News.error("提升机已确认且任务完成状态,复位失败,但未找到工作档。提升机号={},工作号={}", liftProtocol.getLiftNo(), liftProtocol.getTaskNo());
                    }
                }else {
                    LiftAssignCommand assignCommand = liftProtocol.getAssignCommand();
                    if (assignCommand != null) {
                        if (!assignCommand.getAuto()) {
                            //手动操作
                            //设置提升机为空闲状态
                            liftProtocol.setProtocolStatus(LiftProtocolStatusType.IDLE);
                            //任务指令清零
                            liftProtocol.setAssignCommand(null);
                            News.info("提升机已确认手动操作。提升机号={}", liftProtocol.getLiftNo());
                        }
                    }
                }
            }
        }
    }
@@ -1889,6 +1931,13 @@
                        }
                    }
                    if (!Cools.isEmpty(barcode)) {
                        WrkMast wrkMast = wrkMastMapper.selectByBarcode(barcode);//条码存在工作档
                        if (wrkMast != null) {
                            continue;
                        }
                    }
                    LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, emptyInSta.getLed());
                    try {
@@ -1911,15 +1960,15 @@
                        Integer code = jsonObject.getInteger("code");
                        if (code.equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                            // 更新站点信息 且 下发plc命令
                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
                            staProtocol.setStaNo(dto.getStaNo().shortValue());
                            devpThread.setPakMk(staProtocol.getSiteId(), false);
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            if (!result) {
                                throw new CoolException("更新plc站点信息失败");
                            }
                            barcodeThread.setBarcode("");
//                            // 更新站点信息 且 下发plc命令
//                            staProtocol.setWorkNo(dto.getWorkNo().shortValue());
//                            staProtocol.setStaNo(dto.getStaNo().shortValue());
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//                            if (!result) {
//                                throw new CoolException("更新plc站点信息失败");
//                            }
                        } else {
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
@@ -1937,6 +1986,97 @@
                }
            }
        }
    }
    /**
     * AGV补货 => 机械臂拣料
     */
    public void agvRestockByRobot() {
        //检测300站是否自动、有物、工作号
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 获取入库站信息
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
            StaProtocol staProtocol = devpThread.getStation().get(300);
            if (staProtocol == null) {
                continue;
            }
            if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.getWorkNo() != 0) {
                //查询是否有工作档
                WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol.getWorkNo().intValue());
                if (wrkMast == null) {
                    continue;
                }
                if (wrkMast.getWrkSts() != 25) {//25.提升机搬运完成
                    continue;
                }
                if (wrkMast.getMk() != null && wrkMast.getMk().equals("Y")) {//标记为Y表示需要用到机械臂拣料
                    Short targetSta = null;//目标站
                    //判断机械臂拣料站是否空闲
                    StaProtocol staProtocol303 = devpThread.getStation().get(303);
                    StaProtocol staProtocol317 = devpThread.getStation().get(317);
                    if (staProtocol303.isAutoing() && !staProtocol303.isLoading()) {
                        //自动、无物
                        targetSta = (short) 303;
                    } else if (staProtocol317.isAutoing() && !staProtocol317.isLoading()) {
                        //自动、无物
                        targetSta = (short) 317;
                    }else {
                        continue;//没有空闲站点
                    }
                    targetSta = (short) 317;
                    if (wrkMast.getStaNo() != 303 && wrkMast.getStaNo() != 317) {
                        //覆盖工作档目标站
                        wrkMast.setStaNo(targetSta.intValue());
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            //向300站写入目标站
                            staProtocol = staProtocol.clone();
                            staProtocol.setStaNo(targetSta);//移动到目标站
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            try {
                                Thread.sleep(500);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            continue;
                        }
                    }
                }
            }
            StaProtocol staProtocol303 = devpThread.getStation().get(303);
            StaProtocol staProtocol317 = devpThread.getStation().get(317);
            if (staProtocol303.isAutoing() && staProtocol303.isLoading() && staProtocol303.getWorkNo() != 0) {
                //调度机械臂
//                //查询是否有工作档
//                WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol303.getWorkNo().intValue());
//                if (wrkMast == null) {
//                    continue;
//                }
//                WrkDetl wrkDetl = wrkDetlService.selectById(wrkMast.getWrkNo());
//                if (wrkDetl == null) {
//                    continue;
//                }
//                RobotUtils.sendTask(staProtocol303.getWorkNo().toString(), wrkDetl.getAnfme().intValue(), "303");
                RobotUtils.sendTask(staProtocol303.getWorkNo().toString(), -1, "303");
            }else if (staProtocol317.isAutoing() && staProtocol317.isLoading() && staProtocol317.getWorkNo() != 0) {
                //调度机械臂
//                //查询是否有工作档
//                WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol317.getWorkNo().intValue());
//                if (wrkMast == null) {
//                    continue;
//                }
//                WrkDetl wrkDetl = wrkDetlService.selectById(wrkMast.getWrkNo());
//                if (wrkDetl == null) {
//                    continue;
//                }
//                RobotUtils.sendTask(staProtocol303.getWorkNo().toString(), wrkDetl.getAnfme().intValue(), "317");
                RobotUtils.sendTask(staProtocol303.getWorkNo().toString(), -1, "317");
            }
        }
    }
@@ -2236,7 +2376,8 @@
                }
            } else if (wrkCharge.getWrkSts() == 52) {
                //检测小车是否满电
                if (shuttleProtocol.getPowerPercent() < 100) {
                int maxPower = 95;
                if (shuttleProtocol.getPowerPercent() < maxPower) {
                    continue;
                }
@@ -2252,21 +2393,34 @@
                assignCommand.setSourceLocNo(shuttleProtocol.getCurrentLocNo());//源库位(小车当前位置)
                assignCommand.setCommands(commands);//运行命令
                //下发任务
                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                try {
                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                wrkCharge.setWrkSts(53L);//52.小车去充电中 => 53.小车充电完成
                wrkCharge.setModiTime(new Date());
                if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                    //下发任务
                    MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                    shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE.id);
                    shuttleProtocol.setTaskNo(0);
                    shuttleProtocol.setPakMk(false);
                }
            } else if (wrkCharge.getWrkSts() == 53) {
                boolean result = Utils.searchEmptyGroupToMoveShuttle(Utils.getLev(wrkCharge.getLocNo()), shuttleThread.getSlave().getId(), shuttleThread);
                if (!result) {
                    continue;
                }
                if (shuttleProtocol.getChargState() == 0) {//小车处于未充电状态
                    boolean result = Utils.searchEmptyGroupToMoveShuttle(Utils.getLev(wrkCharge.getLocNo()), shuttleThread.getSlave().getId(), shuttleThread);
                    if (!result) {
                        continue;
                    }
                wrkCharge.setWrkSts(60L);//53.小车充电完成 => 60.充电任务完成
                wrkCharge.setModiTime(new Date());
                wrkChargeMapper.updateById(wrkCharge);
                    wrkCharge.setWrkSts(60L);//53.小车充电完成 => 60.充电任务完成
                    wrkCharge.setModiTime(new Date());
                    if (wrkChargeMapper.updateById(wrkCharge) > 0) {
                    }
                }
            }
        }
@@ -2366,7 +2520,7 @@
            }
            //获取小车到输送站点行走命令
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), liftSta.getLocNo());
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), liftSta.getLocNo(), NavigationMapType.NORMAL.id);
            List<NyShuttleHttpCommand> commands = result.getCommands();
            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
@@ -2478,7 +2632,8 @@
                NyLiftCommand liftCommand = NyLiftUtils.getLiftCommand(liftProtocol.getLiftNo().intValue(), NyLiftTaskModelType.MOVE_CAR.id, sourceLiftSta.getStaNo(), sourceLiftSta.getStaNo(), wrkMast.getWrkNo());
                Random random = new Random();
                liftCommand.setTaskNo((short) Math.abs((liftCommand.getTaskNo().intValue() + random.nextInt(9999))));//更换随机任务号
                int deviceWrk = Math.abs((liftCommand.getTaskNo().intValue() + random.nextInt(9999)));//获取设备工作号
                liftCommand.setTaskNo((short) deviceWrk);//更换随机任务号
                ArrayList<NyLiftCommand> commands = new ArrayList<>();
                commands.add(liftCommand);
@@ -2489,6 +2644,7 @@
                assignCommand.setLiftNo(liftProtocol.getLiftNo());
                assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                assignCommand.setTaskMode(NyLiftTaskModelType.MOVE_CAR.id.shortValue());
                assignCommand.setAuto(false);
                wrkMast.setLiftNo(liftThread.getSlave().getId());//锁定提升机防止被占用
                wrkMast.setModiTime(now);
@@ -2799,8 +2955,17 @@
//            NavigateNode liftNode = new NavigateNode(basLift.getPoint$().getX(), basLift.getPoint$().getY());
//            liftNode.setZ(basLift.getPoint$().getZ());
            //小车已经在目标库位,直接认定小车移动任务完成
            if (shuttleProtocol.getCurrentLocNo().equals(wrkMast.getLocNo())) {
                wrkMast.setWrkSts(111L);//111.小车移动完成
                wrkMast.setLiftNo(null);//释放提升机
                wrkMast.setModiTime(now);
                wrkMastMapper.updateById(wrkMast);
                return false;
            }
            //获取小车到目标库位命令
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo());
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo(), NavigationMapType.NORMAL.id);
            if (result == null) {
                return false;//路径计算失败
            }