#
Junjie
2024-12-23 a4d9d3f23e44099972ea6de8af35152ed96ae8ae
src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -114,6 +114,8 @@
    private WrkDetlLogService wrkDetlLogService;
    @Autowired
    private MatService matService;
    @Autowired
    private NavigateMapData navigateMapData;
    /**
@@ -309,105 +311,6 @@
    }
    /**
     * wms入库
     * 入库站,根据条码扫描生成入库工作档,工作状态 1 ==>> 2
     */
    @Deprecated
    public void generateStoreWrkFile0() {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                // 判断是否满足入库条件
                if (staProtocol.isAutoing() && staProtocol.isLoading()
                        && staProtocol.isInEnable()
                        && !staProtocol.isEmptyMk() && (workNo == 0 || (workNo >= 9990 && workNo <= 9999))
                        && staProtocol.isPakMk()) {
                    // 尺寸检测异常
                    boolean back = false;
                    String errMsg = "";
                    if (staProtocol.isFrontErr()) {
                        errMsg = "前超限";
                        back = true;
                    }
                    if (!back && staProtocol.isBackErr()) {
                        errMsg = "后超限";
                        back = true;
                    }
                    if (!back && staProtocol.isHighErr()) {
                        errMsg = "高超限";
                        back = true;
                    }
                    if (!back && staProtocol.isLeftErr()) {
                        errMsg = "左超限";
                        back = true;
                    }
                    if (!back && staProtocol.isRightErr()) {
                        errMsg = "右超限";
                        back = true;
                    }
                    if (!back && staProtocol.isWeightErr()) {
                        errMsg = "超重";
                        back = true;
                    }
                    if (!back && staProtocol.isBarcodeErr()) {
                        errMsg = "扫码失败";
                        back = true;
                    }
                    // 退回
                    if (back) {
                        News.warn("扫码入库失败,{}入库站因{}异常,托盘已被退回", inSta.getStaNo(), errMsg);
                        staProtocol.setWorkNo((short) 32002);
                        staProtocol.setStaNo(inSta.getBackSta().shortValue());
                        devpThread.setPakMk(staProtocol.getSiteId(), false);
                        MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                        // led 异常显示
                        LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, inSta.getLed());
                        if (ledThread != null) {
                            MessageQueue.offer(SlaveType.Led, inSta.getLed(), new Task(3, errMsg));
                        }
                        continue;
                    }
                    // 判断重复工作档
                    WrkMast wrkMast = wrkMastMapper.selectPakInStep11(inSta.getStaNo());
                    if (wrkMast == null) { continue; }
                    // 命令下发区 --------------------------------------------------------------------------
                    // 更新站点信息 且 下发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) {
                        // 更新工作主档
                        wrkMast.setWrkSts(2L); // 工作状态:2.设备上走
                        wrkMast.setModiTime(new Date());
                        if (wrkMastMapper.updateById(wrkMast) == 0) {
                            News.error("更新工作档失败!!! [工作号:{}]", wrkMast.getWrkNo());
                        }
                    } else {
                        News.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
                    }
                }
            }
        }
    }
    /**
     * 拣料、并板再入库
     */
    @Transactional
@@ -527,49 +430,6 @@
                        } catch (Exception e) {
                            e.printStackTrace();
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 拣料、并板再入库(325、331、333、339)
     */
    @Transactional
    public synchronized void stnToCrnStnPick2(){
        try {
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 遍历拣料入库口
                for (DevpSlave.Sta pickSta : devp.getPickInSta2()) {
                    // 获取拣料入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                    StaProtocol staProtocol = devpThread.getStation().get(pickSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    if (staProtocol.isAutoing()
                            && staProtocol.isLoading()
                            && staProtocol.isInEnable()
                            && (staProtocol.getWorkNo() > 0)
                            && staProtocol.isPakMk()) {
                        WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol.getWorkNo().intValue());
                        if (wrkMast == null) {
                            continue;
                        }
                        if (wrkMast.getSteNo() == null) {
                            wrkMast.setSteNo(1);
                            wrkMastMapper.updateById(wrkMast);
                            staProtocol.setStaNo((short) 341);//写入目标站
                            staProtocol.setPakMk(false);
                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                        }
                    }
                }
@@ -888,7 +748,7 @@
//////                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//////                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
//////                            if (!result) {
//////                                News.error("发布命令至输送线队列失败!!! [plc编号:{}]", devp.getId());
//////                                News.error("发布命令至输送线队列失败!!! [plc编号{}]", devp.getId());
//////                            }
////
////                            barcodeThread.setBarcode("");//清理条码
@@ -938,8 +798,7 @@
                    if (basMap == null) {
                        //数据库中也不存在地图数据,从地图文件中获取
                        //载入地图
                        NavigateMapData mapData = new NavigateMapData(i);
                        List<List<MapNode>> lists = mapData.getJsonData(-1, null, null);//获取完整地图(包括入库出库)
                        List<List<MapNode>> lists = navigateMapData.getJsonData(i, -1, null, null);//获取完整地图(包括入库出库)
                        //存入数据库
                        basMap = new BasMap();
@@ -2505,410 +2364,6 @@
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * AGV补货 => 生成入库通知档
     */
    public synchronized void robotGenerateAgvTask() {
        try {
            //检测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;//没有空闲站点
                        }
                        if (wrkMast.getStaNo() != 303 && wrkMast.getStaNo() != 317) {
                            //向AGV发起组托请求
                            try {
                                HashMap<String, Object> param = new HashMap<>();
                                param.put("wrkNo", wrkMast.getWrkNo());
                                String response = new HttpHandler.Builder()
                                        .setUri(wmsUrl)
                                        .setPath("/rpc/replenishment")
                                        .setJson(JSON.toJSONString(param))
                                        .build()
                                        .doPost();
                                JSONObject jsonObject = JSON.parseObject(response);
                                Integer code = jsonObject.getInteger("code");
                                if (code.equals(200)) {//AGV组托成功
                                    //覆盖工作档目标站
                                    wrkMast.setStaNo(targetSta.intValue());
                                    wrkMast.setShuttleNo(null);//释放小车
                                    wrkMast.setLiftNo(null);//释放提升机
                                    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;
                                    }
                                }
                            } catch (Exception e) {
//                                e.printStackTrace();
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            }
//                        //覆盖工作档目标站
//                        wrkMast.setStaNo(targetSta.intValue());
//                        wrkMast.setShuttleNo(null);//释放小车
//                        wrkMast.setLiftNo(null);//释放提升机
//                        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;
//                        }
                        }
                    }
                }
            }
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }
    /**
     * AGV补货 => 机械臂拣料
     */
    public synchronized void agvRestockByRobot() {
        try {
            //检测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) {
                    //调度机械臂
                    //查询是否有工作档
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol303.getWorkNo().intValue());
                    if (wrkMast == null) {
                        continue;
                    }
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
                    if (wrkDetls.isEmpty()) {
                        continue;
                    }
                    if (wrkMast.getInvWh() == null) {
                        boolean result = RobotUtils.sendTask(staProtocol303.getWorkNo().toString(), wrkDetls.size(), "303");
                        if (result) {
                            wrkMast.setInvWh("Y");//标记已经下发机械臂任务
                            wrkMastMapper.updateById(wrkMast);
                        }
                    }
                }
                if (staProtocol317.isAutoing() && staProtocol317.isLoading() && staProtocol317.getWorkNo() != 0) {
                    //调度机械臂
                    //查询是否有工作档
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol317.getWorkNo().intValue());
                    if (wrkMast == null) {
                        continue;
                    }
                    List<WrkDetl> wrkDetls = wrkDetlService.findByWorkNo(wrkMast.getWrkNo());
                    if (wrkDetls.isEmpty()) {
                        continue;
                    }
                    if (wrkMast.getInvWh() == null) {
                        boolean result = RobotUtils.sendTask(staProtocol317.getWorkNo().toString(), wrkDetls.size(), "317");
                        if (result) {
                            wrkMast.setInvWh("Y");//标记已经下发机械臂任务
                            wrkMastMapper.updateById(wrkMast);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * AGV补货(通知AGV取货)
     */
    public synchronized void agvRestockInto() {
        try {
            //检测309和312站是否自动、有物
            for (DevpSlave devp : slaveProperties.getDevp()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol309 = devpThread.getStation().get(309);
                StaProtocol staProtocol312 = devpThread.getStation().get(312);
                if (staProtocol309 == null || staProtocol312 == null) {
                    continue;
                }
                if (staProtocol309.isAutoing() && staProtocol309.isLoading() && staProtocol309.isInEnable()) {
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 9);//308站条码器
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if (!Cools.isEmpty(barcode)) {
                        //通知AGV取货
                        boolean result = agvRestockCall("301-1", barcode);
                        log.info("机械臂通知AGV取货,条码号:" + barcode);
                        if (result) {
                            barcodeThread.setBarcode("");
                        }
                    }
                }
                if (staProtocol312.isAutoing() && staProtocol312.isLoading() && staProtocol312.isInEnable()) {
                    // 获取条码扫描仪信息
                    BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, 10);//311站条码器
                    if (barcodeThread == null) {
                        continue;
                    }
                    String barcode = barcodeThread.getBarcode();
                    if (!Cools.isEmpty(barcode)) {
                        //通知AGV取货
                        boolean result = agvRestockCall("302-1", barcode);
                        log.info("机械臂通知AGV取货,条码号:" + barcode);
                        if (result) {
                            barcodeThread.setBarcode("");
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //通知AGV取货
    private boolean agvRestockCall(String staNo, String barcode) {
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("devNo", staNo);
            param.put("containerCode", barcode);
            String response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath("/rpc/start")
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            News.info("悬挂线,WMS返回结果:" + jsonObject);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {//呼叫AGV
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
    //通知WMS当前拣料站点信息
    private boolean agvCureentCall(String staNo, String barcode) {
        try {
            HashMap<String, Object> param = new HashMap<>();
            param.put("devNo", staNo);
            param.put("containerCode", barcode);
            String response = new HttpHandler.Builder()
                    .setUri(wmsUrl)
                    .setPath("/rpc/current/containerCode")
                    .setJson(JSON.toJSONString(param))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            News.info("悬挂线,WMS返回结果:" + jsonObject);
            Integer code = jsonObject.getInteger("code");
            if (code.equals(200)) {//呼叫AGV
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
    /**
     * AGV补货(悬挂线通知AGV取货)
     */
    public synchronized void agvRestockIntoByHangingWire() {
        try {
            //检测350和351扫码器
            int[] barcodeStaNo = {11, 12,14,16,18,20};//11 => 350站扫码器,12 => 351站扫码器
            for (int staNo : barcodeStaNo) {
                // 获取条码扫描仪信息
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, staNo);
                if (barcodeThread == null) {
                    continue;
                }
                String barcode = barcodeThread.getBarcode();
                if (!Cools.isEmpty(barcode)) {
                    if (barcode.contains("NoRead")) {
                        continue;
                    }
                    String agvStaNo = null;
                    if (staNo == 11) {
                        agvStaNo = "303-1";
                    } else if(staNo == 12){
                        agvStaNo = "304-1";
                    } else if(staNo == 14){
                        agvStaNo = "311-1";
                    } else if(staNo == 16){
                        agvStaNo = "313-1";
                    } else if(staNo == 18){
                        agvStaNo = "315-1";
                    } else if(staNo == 20){
                        agvStaNo = "317-1";
                    }
                    //通知AGV取货
                    boolean result = agvRestockCall(agvStaNo, barcode);
                    if (result) {
                        barcodeThread.setBarcode("");
                    }
                    log.info(barcodeThread.getSlave().getId() + "号扫码器,通知AGV取货,条码号:" + barcode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * AGV拣料站点信息
     */
    public synchronized void agvCurrentContainerCodeInfoWire() {
        try {
            //检测350和351扫码器
            int[] barcodeStaNo = {13, 15,17,19};//11 => 350站扫码器,12 => 351站扫码器
            for (int staNo : barcodeStaNo) {
                // 获取条码扫描仪信息
                BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, staNo);
                if (barcodeThread == null) {
                    continue;
                }
                String barcode = barcodeThread.getBarcode();
                if (!Cools.isEmpty(barcode)) {
                    if (barcode.contains("NoRead")) {
                        continue;
                    }
                    String agvStaNo = null;
                    if (staNo == 13) {
                        agvStaNo = "CS-310";
                    } else if(staNo == 15){
                        agvStaNo = "CS-311";
                    } else if(staNo == 17){
                        agvStaNo = "CS-312";
                    } else if(staNo == 19){
                        agvStaNo = "CS-313";
                    }
                    //通知WMS当前容器码
                    boolean result = agvCureentCall(agvStaNo, barcode);
                    if (result) {
                        barcodeThread.setBarcode("");
                    }
                    log.info(barcodeThread.getSlave().getId() + "号扫码器,通知AGV取货,条码号:" + barcode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 300站拣料
    public void pick300() {
        try {
            //检测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;
                }else {
                    staProtocol = staProtocol.clone();
                }
                if (staProtocol.isAutoing() && staProtocol.isLoading() && staProtocol.getWorkNo() != 0) {
                    //查询是否有工作档
                    WrkMast wrkMast = wrkMastMapper.selectByWorkNo(staProtocol.getWorkNo().intValue());
                    if (wrkMast == null) {
                        continue;
                    }
                    if (wrkMast.getWrkSts() != 29) {//29.出库完成
                        continue;
                    }
                    if (wrkMast.getMk() == null) {
                        Integer sourceStaNo = wrkMast.getSourceStaNo();//源站
                        Integer staNo = wrkMast.getStaNo();//目标站
                        //覆盖工作档目标站
                        wrkMast.setStaNo(sourceStaNo);
                        wrkMast.setSourceStaNo(staNo);
                        wrkMast.setMk("N");
                        if (wrkMastMapper.updateById(wrkMast) > 0) {
                            //向300站写入目标站
                            staProtocol = staProtocol.clone();
                            staProtocol.setStaNo(wrkMast.getStaNo().shortValue());//移动到目标站
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(2, staProtocol));
                            try {
                                Thread.sleep(500);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {