Junjie
2023-03-29 32bf433492f540b1392c19757124ebb91d0e74bd
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -609,6 +609,197 @@
     * 入出库  ===>>  四向穿梭车入出库作业下发
     */
    public synchronized void shuttleIoExecute() {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库站
            for (DevpSlave.StaRack rackInStn : devp.getRackInStn()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(rackInStn.getStaNo());
                StaProtocol staProtocol105 = devpThread.getStation().get(105);
                staProtocol105.setWorkNo((short) 752);
                staProtocol105.setStaNo((short) 100);
                StaProtocol staProtocol106 = devpThread.getStation().get(106);
                staProtocol106.setWorkNo((short) 753);
                staProtocol106.setStaNo((short) 100);
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                // 判断是否满足入库条件
                if (true || staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.isInEnable()) {
                    WrkMast wrkMast = wrkMastMapper.selectRackInStep48(workNo, staProtocol.getSiteId());
                    if (wrkMast != null) {
                        if (wrkMast.getWrkSts() == 4) {
                            //寻找最近且空闲的四向穿梭车
                            HashMap<String, Object> searchIdleShuttle = this.searchIdleShuttle(wrkMast);
                            ShuttleThread shuttleThread = (ShuttleThread) searchIdleShuttle.get("result");
                            if (shuttleThread == null) {
                                continue;
                            }
                            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                            wrkMast.setShuttleNo(shuttleProtocol.getShuttleNo().intValue());//给工作档分配四向穿梭车号
                            //分配任务号
                            shuttleProtocol.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //分配源库位
                            shuttleProtocol.setSourceLocNo(wrkMast.getSourceLocNo());
                            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                            //四向穿梭车号
                            assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                            //任务号
                            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //入出库模式
                            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());
                            //源库位(小车当前位置)
                            Short currentCode = shuttleProtocol.getCurrentCode();
                            LocMast currentLocMast = locMastService.queryByQrCode(currentCode.toString());
                            assignCommand.setSourceLocNo(currentLocMast.getLocNo());
                            if (Boolean.parseBoolean(searchIdleShuttle.get("sameLay").toString())) {
                                //同一层
                                //分配目标库位
                                shuttleProtocol.setLocNo(wrkMast.getLocNo());
                                //目标库位
                                assignCommand.setDistLocNo(wrkMast.getLocNo());
                                wrkMast.setWrkSts(9L);//小车入库中
                            }else {
                                //不同层,将目标库位分配成提升机库位号
                                //分配目标库位
                                shuttleProtocol.setLocNo("1800201");
                                //目标库位
                                assignCommand.setDistLocNo("1800201");
                                wrkMast.setWrkSts(5L);//小车迁移状态
                            }
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }else {
                            //状态8,四向穿梭车已在提升机口,等待命令进行入库搬运动作
                            Integer shuttleNo = wrkMast.getShuttleNo();//四向穿梭车号
                            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
                            if (shuttleThread == null) {
                                continue;
                            }
                            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
                            //分配目标库位
                            shuttleProtocol.setLocNo(wrkMast.getLocNo());
                            ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                            //四向穿梭车号
                            assignCommand.setShuttleNo(shuttleNo.shortValue());
                            //任务号
                            assignCommand.setTaskNo(wrkMast.getWrkNo().shortValue());
                            //入出库模式
                            assignCommand.setTaskMode(ShuttleTaskModeType.PAK_IN.id.shortValue());
                            //源库位(小车当前位置)
                            Short currentCode = shuttleProtocol.getCurrentCode();
                            LocMast currentLocMast = locMastService.queryByQrCode(currentCode.toString());
                            assignCommand.setSourceLocNo(currentLocMast.getLocNo());
                            //目标库位
                            assignCommand.setDistLocNo(wrkMast.getLocNo());
                            wrkMast.setWrkSts(9L);//小车入库中
                            if (wrkMastMapper.updateById(wrkMast) > 0) {
                                //下发任务
                                MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
                            }
                        }
                    }
                }
            }
        }
    }
    /**
     * 搜索空闲且最近的四向穿梭车
     */
    public HashMap<String,Object> searchIdleShuttle(WrkMast wrkMast) {
        HashMap<String, Object> map = new HashMap<>();
        String locNo = wrkMast.getWrkSts() < 10 ? wrkMast.getLocNo() : wrkMast.getSourceLocNo();//库位号
        LocMast locMast = locMastService.queryByLoc(locNo);//找到库位记录
        String lay = locNo.substring(locNo.length() - 2, locNo.length());//当前工作档库位层高
        ShuttleThread recentShuttle = null;//当前距离最近的四向穿梭车线程
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车线程
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
            ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol();
            if (shuttleProtocol == null) {
                continue;
            }
            if (!shuttleProtocol.isIdle()) {
                continue;
            }
            Short currentCode = shuttleProtocol.getCurrentCode();//四向穿梭车当前位置二维码
            LocMast shuttleLocMast = locMastService.queryByQrCode(currentCode.toString());//找到二维码对应的库位记录
            String shuttleLocNo = shuttleLocMast.getLocNo();//二维码对应库位号
            String shuttleLocNoLay = shuttleLocNo.substring(shuttleLocNo.length() - 2, shuttleLocNo.length());//库位号对应层高
            if (lay.equals(shuttleLocNoLay)) {
                //当前四向穿梭车和工作档任务在同一层,则调配该车辆
                map.put("sameLay", true);//同一层
                map.put("result", shuttleThread);
                return map;
            }
            //更新当前最近的四向穿梭车
            if (recentShuttle == null) {
                recentShuttle = shuttleThread;
            }else {
                ShuttleProtocol recentShuttleProtocol = recentShuttle.getShuttleProtocol();//目前最近穿梭车
                Short recentShuttleCurrentCode = recentShuttleProtocol.getCurrentCode();//目前最近穿梭车位置二维码
                LocMast recentShuttleLocMast = locMastService.queryByQrCode(recentShuttleCurrentCode.toString());//找到二维码对应的库位记录
                String recentShuttleLocNo = recentShuttleLocMast.getLocNo();//二维码对应库位号
                String recentShuttleLocNoLay = recentShuttleLocNo.substring(recentShuttleLocNo.length() - 2, recentShuttleLocNo.length());//库位号对应层高
                int recentShuttleLocNoLayInt = Integer.parseInt(recentShuttleLocNoLay);
                int layInt = Integer.parseInt(lay);
                int shuttleLocNoLayInt = Integer.parseInt(shuttleLocNoLay);
                int i = Math.abs(layInt - recentShuttleLocNoLayInt);//工作档楼层减最近穿梭车楼层,取绝对值
                int j = Math.abs(layInt - shuttleLocNoLayInt);//工作档楼层减当前穿梭车楼层,取绝对值
                if (i < j) {
                    //更新最近穿梭车
                    recentShuttle = shuttleThread;
                } else if (i == j) {
                    //楼层相同情况
                    //找距离出库点最近的车
                    //当前最近四向穿梭车到提升机路径
                    List<NavigateNode> recentShuttlePath = NavigateUtils.calc(recentShuttleLocNo, "1800201", ShuttleTaskModeType.PAK_IN.id);
                    //当前楼层四向穿梭车到提升机路径
                    List<NavigateNode> shuttlePath = NavigateUtils.calc(shuttleLocNo, "1800201", ShuttleTaskModeType.PAK_IN.id);
                    //判断哪一个路径最短
                    if (shuttlePath.size() < recentShuttlePath.size()) {
                        //如果当前楼层的车路径更小,则更新最近穿梭车
                        recentShuttle = shuttleThread;
                    }
                }
            }
        }
        map.put("sameLay", false);//不同层
        map.put("result", recentShuttle);
        return map;
    }
    /**
     * 四向穿梭车任务完成
     */
    public synchronized void shuttleFinished() {
        for (ShuttleSlave shuttle : slaveProperties.getShuttle()) {
            //获取四向穿梭车信息
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttle.getId());
@@ -617,37 +808,31 @@
                continue;
            }
            // 只有当四向穿梭车空闲 并且无任务时才继续执行
            if (shuttleProtocol.getBusyStatusType() == ShuttleStatusType.IDLE && shuttleProtocol.getTaskNo() == 0) {
                //入出库逻辑
                for (WrkMast wrkSts : wrkMastMapper.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 2))) {
                    //分配任务号
                    shuttleProtocol.setTaskNo(wrkSts.getWrkNo().shortValue());
                    //分配源库位
                    shuttleProtocol.setSourceLocNo(wrkSts.getSourceLocNo());
                    //分配目标库位
                    shuttleProtocol.setLocNo(wrkSts.getLocNo());
                    ShuttleAssignCommand assignCommand = new ShuttleAssignCommand();
                    //四向穿梭车号
                    assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());
                    //任务号
                    assignCommand.setTaskNo(wrkSts.getWrkNo().shortValue());
                    //入出库模式
                    assignCommand.setTaskMode(0);
                    //源库位
                    assignCommand.setSourceLocNo(wrkSts.getSourceLocNo());
                    //目标库位
                    assignCommand.setDistLocNo(wrkSts.getLocNo());
                    //下发任务
                    MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand));
            //四向穿梭车状态为等待确认
            if (shuttleProtocol.getProtocolStatus() == ShuttleProtocolStatusType.WAITING.id && shuttleProtocol.getTaskNo() != 0) {
                //将任务档标记为完成
                WrkMast wrkMast = wrkMastMapper.selectByWorkNo9(shuttleProtocol.getTaskNo().intValue());
                if (wrkMast != null) {
                    wrkMast.setWrkSts(14L);
                    if (wrkMastMapper.updateById(wrkMast) > 0) {
                        //设置四向穿梭车为空闲状态
                        shuttleProtocol.setProtocolStatus(ShuttleProtocolStatusType.IDLE);
                        //任务号清零
                        shuttleProtocol.setTaskNo((short) 0);
                        //源库位清零
                        shuttleProtocol.setSourceLocNo(null);
                        //目标库位清零
                        shuttleProtocol.setLocNo(null);
                        //标记复位
                        shuttleProtocol.setPakMk(true);
                        News.error("四向穿梭车已确认且任务完成状态,复位。堆垛机号={}", shuttleProtocol.getShuttleNo());
                    } else {
                        News.error("四向穿梭车已确认且任务完成状态,复位失败,但未找到工作档。四向穿梭车号={},工作号={}", shuttleProtocol.getShuttleNo(), shuttleProtocol.getTaskNo());
                    }
                }
            }
        }
    }
    /**