#
Junjie
2023-11-04 ded3b5c237a484f33dc577a17b3797eebd49db54
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -473,117 +473,155 @@
                        continue;
                    }
                    try {
                        // 访问 WMS 获取入库库位
                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
                        SearchLocParam param = new SearchLocParam();
                        param.setWrkNo(wrkMast.getWrkNo());
                        param.setIoType(wrkMast.getIoType());
                        param.setSourceStaNo(pickSta.getStaNo());
//                        param.setLocType1(locTypeDto.getLocType1());
                        String response = new HttpHandler.Builder()
                                .setUri(wmsUrl)
                                .setPath("/rpc/pakin/loc/v1")
                                .setJson(JSON.toJSONString(param))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
                        Integer code = jsonObject.getInteger("code");
                        if (code.equals(200)) {
                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                    // 保存工作主档历史档
                    if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
                        throw new CoolException(wrkMast.getWrkNo() + "保存工作主档历史档失败");
                    }
//                            // 获取目标站
//                            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
//                                    .eq("type_no", wrkMast.getIoType() - 50)
//                                    .eq("stn_no", pickSta.getStaNo()) // 作业站点 = 拣料出库的目标站
//                                    .eq("crn_no", dto.getCrnNo()); // 堆垛机号
//                            StaDesc staDesc = staDescService.selectOne(wrapper);
//                            if (Cools.isEmpty(staDesc)) {
//                                News.error("入库路径不存在!type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
//                                continue;
//                            }
//                            // 堆垛机站点(目标站)
//                            Integer staNo = staDesc.getCrnStn();
                    String sourceLocNo = wrkMast.getSourceLocNo().trim();
                    LiftStaProtocol liftStaProtocol = NyLiftUtils.getLiftStaByLev(pickSta.getLiftNo(), Utils.getLev(sourceLocNo));//获取回库提升机目标站
                    if (liftStaProtocol == null) {
                        continue;
                    }
                            // 保存工作明细档历史档
//                        if (wrkMastMapper.saveWrkDetlLog(wrkMast.getWrkNo()) == 0) {
//                            throw new CoolException("保存工作明细档历史档失败");
//                        }
                            // 保存工作主档历史档
                            if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
                                throw new CoolException(wrkMast.getWrkNo() + "保存工作主档历史档失败");
                            }
                            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.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(liftStaProtocol.getStaNo());//目标站
                            wrkMast.setLocNo(sourceLocNo); // 目标库位 = 出库时的源库位
                            wrkMast.setShuttleNo(null); // 穿梭车清空
                            wrkMast.setLiftNo(null);// 提升机清空
                            wrkMast.setModiTime(new Date());
                            if (wrkMastMapper.updateById(wrkMast) == 0) {
                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
                            }
                            if (wrkMastMapper.setSteEmpty(wrkMast.getWrkNo()) == 0) {
                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
                            }
                            // 修改库位状态 Q.拣料/盘点/并板再入库
                            LocMast locMast = locMastService.selectById(sourceLocNo);
                            locMast.setLocSts("Q");
                            locMast.setModiTime(new Date());
                            if (!locMastService.updateById(locMast)) {
                                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());
//                            }
                            barcodeThread.setBarcode("");//清理条码
                        } else if (code == 500){
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
                                if (!Cools.isEmpty(errorMsg)) {
                                    MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
                                }
                            }
                            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));
                            // led 异常显示
                            if (ledThread != null) {
                                String errorMsg = jsonObject.getString("msg");
//                                String errorMsg = barcode + "托盘识别异常,请先进行组托!";
                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    wrkMast.setStaNo(liftStaProtocol.getStaNo());//目标站
                    wrkMast.setLocNo(sourceLocNo); // 目标库位 = 出库时的源库位
                    wrkMast.setShuttleNo(null); // 穿梭车清空
                    wrkMast.setLiftNo(null);// 提升机清空
                    wrkMast.setModiTime(new Date());
                    if (wrkMastMapper.updateById(wrkMast) == 0) {
                        throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
                    }
                    if (wrkMastMapper.setSteEmpty(wrkMast.getWrkNo()) == 0) {
                        throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
                    }
                    // 修改库位状态 Q.拣料/盘点/并板再入库
                    LocMast locMast = locMastService.selectById(sourceLocNo);
                    locMast.setLocSts("Q");
                    locMast.setModiTime(new Date());
                    if (!locMastService.updateById(locMast)) {
                        throw new CoolException("修改库位状态失败");
                    }
                    barcodeThread.setBarcode("");//清理条码
//                    try {
//                        // 访问 WMS 获取入库库位
//                        LocTypeDto locTypeDto = new LocTypeDto(staProtocol);
//                        SearchLocParam param = new SearchLocParam();
//                        param.setWrkNo(wrkMast.getWrkNo());
//                        param.setIoType(wrkMast.getIoType());
//                        param.setSourceStaNo(pickSta.getStaNo());
////                        param.setLocType1(locTypeDto.getLocType1());
//                        String response = new HttpHandler.Builder()
//                                .setUri(wmsUrl)
//                                .setPath("/rpc/pakin/loc/v1")
//                                .setJson(JSON.toJSONString(param))
//                                .build()
//                                .doPost();
//                        JSONObject jsonObject = JSON.parseObject(response);
//                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, pickSta.getLed());
//                        Integer code = jsonObject.getInteger("code");
//                        if (code.equals(200)) {
//                            StartupDto dto = jsonObject.getObject("data", StartupDto.class);
//
////                            // 获取目标站
////                            Wrapper<StaDesc> wrapper = new EntityWrapper<StaDesc>()
////                                    .eq("type_no", wrkMast.getIoType() - 50)
////                                    .eq("stn_no", pickSta.getStaNo()) // 作业站点 = 拣料出库的目标站
////                                    .eq("crn_no", dto.getCrnNo()); // 堆垛机号
////                            StaDesc staDesc = staDescService.selectOne(wrapper);
////                            if (Cools.isEmpty(staDesc)) {
////                                News.error("入库路径不存在!type_no={},stn_no={},crn_no={}", wrkMast.getIoType(), pickSta.getStaNo(), wrkMast.getCrnNo());
////                                continue;
////                            }
////                            // 堆垛机站点(目标站)
////                            Integer staNo = staDesc.getCrnStn();
//
//                            // 保存工作明细档历史档
////                        if (wrkMastMapper.saveWrkDetlLog(wrkMast.getWrkNo()) == 0) {
////                            throw new CoolException("保存工作明细档历史档失败");
////                        }
//                            // 保存工作主档历史档
//                            if (wrkMastMapper.saveWrkMastLog(wrkMast.getWrkNo()) == 0) {
//                                throw new CoolException(wrkMast.getWrkNo() + "保存工作主档历史档失败");
//                            }
//
//                            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(liftStaProtocol.getStaNo());//目标站
//                            wrkMast.setLocNo(sourceLocNo); // 目标库位 = 出库时的源库位
//                            wrkMast.setShuttleNo(null); // 穿梭车清空
//                            wrkMast.setLiftNo(null);// 提升机清空
//                            wrkMast.setModiTime(new Date());
//                            if (wrkMastMapper.updateById(wrkMast) == 0) {
//                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
//                            }
//                            if (wrkMastMapper.setSteEmpty(wrkMast.getWrkNo()) == 0) {
//                                throw new CoolException(wrkMast.getWrkNo() + "更新工作档数据状态失败");
//                            }
//
//                            // 修改库位状态 Q.拣料/盘点/并板再入库
//                            LocMast locMast = locMastService.selectById(sourceLocNo);
//                            locMast.setLocSts("Q");
//                            locMast.setModiTime(new Date());
//                            if (!locMastService.updateById(locMast)) {
//                                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());
////                            }
//
//                            barcodeThread.setBarcode("");//清理条码
//
//                        } else if (code == 500){
//                            if (ledThread != null) {
//                                String errorMsg = jsonObject.getString("msg");
//                                if (!Cools.isEmpty(errorMsg)) {
//                                    MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
//                                }
//                            }
//                            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));
//
//                            // led 异常显示
//                            if (ledThread != null) {
//                                String errorMsg = jsonObject.getString("msg");
////                                String errorMsg = barcode + "托盘识别异常,请先进行组托!";
//                                MessageQueue.offer(SlaveType.Led, pickSta.getLed(), new Task(3, errorMsg));
//                            }
//                        }
//
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                    }
                }
            }
@@ -2033,10 +2071,10 @@
    }
    /**
     * AGV补货 => 机械臂拣料
     * AGV补货 => 生成入库通知档
     */
    public void agvRestockByRobot() {
        //检测300站是否自动、有物、工作号
    public void robotGenerateAgvTask() {
//检测300站是否自动、有物、工作号
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 获取入库站信息
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
@@ -2066,7 +2104,7 @@
                    } else if (staProtocol317.isAutoing() && !staProtocol317.isLoading()) {
                        //自动、无物
                        targetSta = (short) 317;
                    }else {
                    } else {
                        continue;//没有空闲站点
                    }
@@ -2125,9 +2163,23 @@
                    }
                }
            }
        }
    }
    /**
     * AGV补货 => 机械臂拣料
     */
    public void agvRestockByRobot() {
        //检测300站是否自动、有物、工作号
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 获取入库站信息
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
            StaProtocol staProtocol303 = devpThread.getStation().get(303);
            StaProtocol staProtocol317 = devpThread.getStation().get(317);
            if (staProtocol303 == null || staProtocol317 == null) {
                continue;
            }
            if (staProtocol303.isAutoing() && staProtocol303.isLoading() && staProtocol303.getWorkNo() != 0) {
                //调度机械臂
                //查询是否有工作档
@@ -2190,7 +2242,7 @@
                String barcode = barcodeThread.getBarcode();
                if(!Cools.isEmpty(barcode)) {
                    //通知AGV取货
                    agvRestockCall("CS-301-001-01@3", barcode);
                    agvRestockCall("301-1", barcode);
                    log.info("通知AGV取货,条码号:" + barcode);
                }
            }
@@ -2204,7 +2256,7 @@
                String barcode = barcodeThread.getBarcode();
                if(!Cools.isEmpty(barcode)) {
                    //通知AGV取货
                    agvRestockCall("CS-302-001-01@3", barcode);
                    agvRestockCall("302-1", barcode);
                    log.info("通知AGV取货,条码号:" + barcode);
                }
            }
@@ -2346,20 +2398,11 @@
            }
            // 命令下发 -------------------------------------------------------------------------------
            if (!commands.isEmpty()) {
                if (led.getId()>3){
                    if (!MessageQueue.offer(SlaveType.Led, led.getId()-3, new Task(1, commands))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId()-3, led.getIp(), led.getPort());
                        continue;
                    }else {
                        ledThread.setLedMk(false);
                    }
                if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
                    log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                    continue;
                }else {
                    if (!MessageQueue.offer(SlaveType.Led, led.getId(), new Task(1, commands))) {
                        log.error("{}号LED命令下发失败!!![ip:{}] [port:{}]", led.getId(), led.getIp(), led.getPort());
                        continue;
                    }else {
                        ledThread.setLedMk(false);
                    }
                    ledThread.setLedMk(false);
                }
            }
@@ -2712,6 +2755,18 @@
                return false;
            }
            //获取源输送站
            LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getSourceStaNo());
            if (liftSta == null) {
                return false;//找不到站点
            }
            //判断提升机是否有其他任务(该任务需要换层必须提前独占提升机)
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(liftSta.getLiftNo());
            if (liftWrkMast != null) {
                return false;//当前提升机存在未完成任务,等待下一次轮询
            }
            //*************尝试锁定目标站路径***************
            List<NavigateNode> targetNodes = NyLiftUtils.getLiftStaNodes(wrkMast.getStaNo());
            if (targetNodes == null) {
@@ -2727,18 +2782,6 @@
                return false;//路径锁定失败
            }
            //*************尝试锁定目标站路径***************
            //获取源输送站
            LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getSourceStaNo());
            if (liftSta == null) {
                return false;//找不到站点
            }
            //判断提升机是否有其他任务(该任务需要换层必须提前独占提升机)
            WrkMast liftWrkMast = wrkMastMapper.selectLiftWrkMast(liftSta.getLiftNo());
            if (liftWrkMast != null) {
                return false;//当前提升机存在未完成任务,等待下一次轮询
            }
            //获取小车到输送站点行走命令
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), liftSta.getLocNo(), NavigationMapType.NORMAL.id);
@@ -3160,43 +3203,26 @@
                return false;
            }
            //跨楼层移动任务
            if (Utils.getLev(wrkMast.getSourceLocNo()) != Utils.getLev(wrkMast.getLocNo())) {
                //获取目标站
                LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
                if (liftSta == null) {
                    return false;//找不到站点
                }
                //*************尝试解锁目标站路径***************
                List<NavigateNode> targetNodes = NyLiftUtils.getLiftStaNodes(liftSta.getStaNo());
                if (targetNodes == null) {
                    return false;//未获取到节点
                }
                //尝试解锁目标站路径
                boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(wrkMast.getLocNo()), targetNodes, false);//所使用的路径进行解锁
                if (!result) {
                    return false;//路径解锁失败
                }
                //*************尝试解锁目标站路径***************
            }
//            //获取目标站对应的输送站点
//            BasDevp targetBasDevp = basDevpService.selectByLevAndLiftNo(Utils.getLev(wrkMast.getLocNo()), wrkMast.getLiftNo());
//            if (targetBasDevp == null) {
//                return false;//缺少站点信息
//            }
//            //跨楼层移动任务
//            if (Utils.getLev(wrkMast.getSourceLocNo()) != Utils.getLev(wrkMast.getLocNo())) {
//                //获取目标站
//                LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
//                if (liftSta == null) {
//                    return false;//找不到站点
//                }
//
//            //获取提升机数据
//            BasLift basLift = basLiftService.selectById(targetBasDevp.getLiftNo());
//            if (basLift == null) {
//                return false;//没有提升机数据
//                //*************尝试解锁目标站路径***************
//                List<NavigateNode> targetNodes = NyLiftUtils.getLiftStaNodes(liftSta.getStaNo());
//                if (targetNodes == null) {
//                    return false;//未获取到节点
//                }
//                //尝试解锁目标站路径
//                boolean result = navigateMapUtils.writeNavigateNodeToRedisMap(Utils.getLev(wrkMast.getLocNo()), targetNodes, false);//所使用的路径进行解锁
//                if (!result) {
//                    return false;//路径解锁失败
//                }
//                //*************尝试解锁目标站路径***************
//            }
//            if (basLift.getPoint() == null) {
//                return false;//没有设置提升机点位坐标
//            }
//            NavigateNode liftNode = new NavigateNode(basLift.getPoint$().getX(), basLift.getPoint$().getY());
//            liftNode.setZ(basLift.getPoint$().getZ());
            //小车已经在目标库位,直接认定小车移动任务完成
            if (shuttleProtocol.getCurrentLocNo().equals(wrkMast.getLocNo())) {
@@ -3225,8 +3251,32 @@
                return false;
            }
            //获取小车到目标库位命令
            NyShuttleOperaResult result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo(), NavigationMapType.NORMAL.id);
            NyShuttleOperaResult result = null;
            //跨楼层移动任务
            if (Utils.getLev(wrkMast.getSourceLocNo()) != Utils.getLev(wrkMast.getLocNo())) {
                //需要将前两个节点作为白名单节点传入
                //获取目标站
                LiftStaProtocol liftSta = NyLiftUtils.getLiftStaByStaNo(wrkMast.getStaNo());
                if (liftSta == null) {
                    return false;//找不到站点
                }
                List<NavigateNode> targetNodes = NyLiftUtils.getLiftStaNodes(liftSta.getStaNo());
                if (targetNodes == null) {
                    return false;//未获取到节点
                }
                //设置计算节点的白名单
                ArrayList<int[]> whiteList = new ArrayList<>();//设置计算节点的白名单
                for (NavigateNode node : targetNodes) {
                    whiteList.add(new int[]{node.getX(), node.getY()});
                }
                result = NyShuttleOperaUtils.getStartToTargetCommandsByWhites(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo(), NavigationMapType.NORMAL.id, whiteList);
            }else {
                //获取小车到目标库位命令
                result = NyShuttleOperaUtils.getStartToTargetCommands(shuttleThread.getSlave().getId(), wrkMast.getWrkNo(), shuttleProtocol.getCurrentLocNo(), wrkMast.getLocNo(), NavigationMapType.NORMAL.id);
            }
            if (result == null) {
                return false;//路径计算失败
            }