自动化立体仓库 - WCS系统
#
野心家
2025-03-05 c238055f686ee818663cb98634664dc60a798628
src/main/java/com/zy/asrs/service/impl/TaskWrkServiceImpl.java
@@ -74,168 +74,168 @@
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void distribute(String taskNo, Long userId) {
        TaskWrk taskWrk = this.selectByTaskNo(taskNo);
        if (taskWrk == null) {
            throw new CoolException("WMS任务不存在");
        }
        if (taskWrk.getStatus() != 1) {
            throw new CoolException("任务已派发");
        }
        //创建任务
        if (taskWrk.getIoType() == 1) {
            //1.入库
            if (taskWrk.getStartPoint() == null || taskWrk.getTargetPoint() == null) {
                throw new CoolException("未接收到起点和终点,不进行派发");
            }
            startup(taskWrk, userId);
        }else if(taskWrk.getIoType() == 2){
            //2.出库
            stockOut(taskWrk, userId);
        } else if (taskWrk.getIoType() == 3) {
            //3.库格移载
            locMove(taskWrk, userId);
        }
//        TaskWrk taskWrk = this.selectByTaskNo(taskNo);
//        if (taskWrk == null) {
//            throw new CoolException("WMS任务不存在");
//        }
//
//        if (taskWrk.getStatus() != 1) {
//            throw new CoolException("任务已派发");
//        }
//
//        //创建任务
//        if (taskWrk.getIoType() == 1) {
//            //1.入库
//            if (taskWrk.getStartPoint() == null || taskWrk.getTargetPoint() == null) {
//                throw new CoolException("未接收到起点和终点,不进行派发");
//            }
//            startup(taskWrk, userId);
//        }else if(taskWrk.getIoType() == 2){
//            //2.出库
//            stockOut(taskWrk, userId);
//        } else if (taskWrk.getIoType() == 3) {
//            //3.库格移载
//            locMove(taskWrk, userId);
//        }
    }
    @Override
    public void startup(TaskWrk taskWrk, Long userId) {
        //入库任务派发
        StaDesc staDesc = staDescService.queryCrn(taskWrk.getIoType(), Integer.parseInt(taskWrk.getStartPoint()), taskWrk.getCrnNo());
        if (staDesc == null) {
            throw new CoolException("入库路径不存在");
        }
        int workNo = commonService.getWorkNo(WorkNoType.PAKIN.type);//获取入库工作号
        taskWrk.setWrkNo(workNo);//工作号
        taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);//派发状态
        taskWrk.setAssignTime(new Date());//派发时间
        taskWrk.setWrkSts(3);//工作状态 2.吊车入库
        taskWrk.setCrnNo(staDesc.getCrnNo());//堆垛机号
        taskWrk.setModiTime(new Date());
        taskWrk.setModiUser(userId);
        updateById(taskWrk);
        CrnSlave.CrnStn crnStn = Utils.getCrnStnByStaNo(staDesc.getCrnStn(), true);
        if (crnStn == null) {
            throw new CoolException("堆垛机入库站不存在");
        }
        // 命令下发区 --------------------------------------------------------------------------
        CrnCommand crnCommand = new CrnCommand();
        crnCommand.setCrnNo(staDesc.getCrnNo()); // 堆垛机编号
        crnCommand.setTaskNo(taskWrk.getWrkNo().shortValue()); // 工作号
        crnCommand.setAckFinish((short) 0);  // 任务完成确认位
        crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
        crnCommand.setSourcePosX(crnStn.getRow().shortValue());     // 源库位列
        crnCommand.setSourcePosY(crnStn.getBay().shortValue());     // 源库位层
        crnCommand.setSourcePosZ(crnStn.getLev().shortValue());     // 源库位排
        crnCommand.setDestinationPosX(Utils.getRowShort(taskWrk.getTargetPoint()));     // 目标库位列
        crnCommand.setDestinationPosY(Utils.getBayShort(taskWrk.getTargetPoint()));     // 目标库位层
        crnCommand.setDestinationPosZ(Utils.getLevShort(taskWrk.getTargetPoint()));     // 目标库位排
        crnCommand.setCommand((short)1);
        if (!CommandUtils.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(5, crnCommand),false)) {
            log.error("堆垛机命令生成失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
            throw new CoolException("堆垛机命令生成失败");
        }else{
            try{
                HashMap<String, Object> headParam = new HashMap<>();
                headParam.put("taskNo",taskWrk.getTaskNo());
                headParam.put("status",taskWrk.getStatus());
                headParam.put("ioType",taskWrk.getIoType());
                headParam.put("barcode",taskWrk.getBarcode());
//                headParam.put("reportTime",new Date());
                String response;
                response = new HttpHandler.Builder()
                        // .setHeaders(headParam)
                        .setUri(wmsUrl)
                        .setPath(taskStatusFeedbackPath)
                        .setJson(JSON.toJSONString(headParam))
                        .build()
                        .doPost();
                JSONObject jsonObject = JSON.parseObject(response);
                apiLogService.save("wcs派发入库任务上报wms"
                        ,wmsUrl+taskStatusFeedbackPath
                        ,null
                        ,"127.0.0.1"
                        ,JSON.toJSONString(headParam)
                        ,response
                        ,true
                );
            }catch (Exception e){
                log.error("wcs派发入库任务上报wms失败", taskWrk);
//                throw new CoolException("wcs派发入库任务上报wms失败");
            }
        }
//        //入库任务派发
//        StaDesc staDesc = staDescService.queryCrn(taskWrk.getIoType(), Integer.parseInt(taskWrk.getStartPoint()), taskWrk.getCrnNo());
//        if (staDesc == null) {
//            throw new CoolException("入库路径不存在");
//        }
//        int workNo = commonService.getWorkNo(WorkNoType.PAKIN.type);//获取入库工作号
//        taskWrk.setWrkNo(workNo);//工作号
//        taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);//派发状态
//        taskWrk.setAssignTime(new Date());//派发时间
//        taskWrk.setWrkSts(3);//工作状态 2.吊车入库
//        taskWrk.setCrnNo(staDesc.getCrnNo());//堆垛机号
//        taskWrk.setModiTime(new Date());
//        taskWrk.setModiUser(userId);
//        updateById(taskWrk);
//
//        CrnSlave.CrnStn crnStn = Utils.getCrnStnByStaNo(staDesc.getCrnStn(), true);
//        if (crnStn == null) {
//            throw new CoolException("堆垛机入库站不存在");
//        }
//        // 命令下发区 --------------------------------------------------------------------------
//        CrnCommand crnCommand = new CrnCommand();
//        crnCommand.setCrnNo(staDesc.getCrnNo()); // 堆垛机编号
//        crnCommand.setTaskNo(taskWrk.getWrkNo().shortValue()); // 工作号
//        crnCommand.setAckFinish((short) 0);  // 任务完成确认位
//        crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
//        crnCommand.setSourcePosX(crnStn.getRow().shortValue());     // 源库位列
//        crnCommand.setSourcePosY(crnStn.getBay().shortValue());     // 源库位层
//        crnCommand.setSourcePosZ(crnStn.getLev().shortValue());     // 源库位排
//        crnCommand.setDestinationPosX(Utils.getRowShort(taskWrk.getTargetPoint()));     // 目标库位列
//        crnCommand.setDestinationPosY(Utils.getBayShort(taskWrk.getTargetPoint()));     // 目标库位层
//        crnCommand.setDestinationPosZ(Utils.getLevShort(taskWrk.getTargetPoint()));     // 目标库位排
//        crnCommand.setCommand((short)1);
//        if (!CommandUtils.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(5, crnCommand),false)) {
//            log.error("堆垛机命令生成失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
//            throw new CoolException("堆垛机命令生成失败");
//        }else{
//            try{
//                HashMap<String, Object> headParam = new HashMap<>();
//                headParam.put("taskNo",taskWrk.getTaskNo());
//                headParam.put("status",taskWrk.getStatus());
//                headParam.put("ioType",taskWrk.getIoType());
//                headParam.put("barcode",taskWrk.getBarcode());
////                headParam.put("reportTime",new Date());
//                String response;
//                response = new HttpHandler.Builder()
//                        // .setHeaders(headParam)
//                        .setUri(wmsUrl)
//                        .setPath(taskStatusFeedbackPath)
//                        .setJson(JSON.toJSONString(headParam))
//                        .build()
//                        .doPost();
//
//                JSONObject jsonObject = JSON.parseObject(response);
//                apiLogService.save("wcs派发入库任务上报wms"
//                        ,wmsUrl+taskStatusFeedbackPath
//                        ,null
//                        ,"127.0.0.1"
//                        ,JSON.toJSONString(headParam)
//                        ,response
//                        ,true
//                );
//            }catch (Exception e){
//                log.error("wcs派发入库任务上报wms失败", taskWrk);
////                throw new CoolException("wcs派发入库任务上报wms失败");
//            }
//
//        }
    }
    @Override
    public void stockOut(TaskWrk taskWrk, Long userId) {
        //出库任务派发
        StaDesc staDesc = staDescService.queryCrn(taskWrk.getIoType(), Integer.parseInt(taskWrk.getTargetPoint()), taskWrk.getCrnNo());
        if (staDesc == null) {
            return;//不存在路径
        }
        int workNo = commonService.getWorkNo(WorkNoType.PAKOUT.type);//获取出库工作号
        taskWrk.setWrkNo(workNo);//工作号
        taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);//派发状态
        taskWrk.setAssignTime(new Date());//派发时间
        taskWrk.setWrkSts(12);//工作状态 12.吊车入库中
        taskWrk.setCrnNo(staDesc.getCrnNo());//堆垛机号
        taskWrk.setModiTime(new Date());
        taskWrk.setModiUser(userId);
        updateById(taskWrk);
        CrnSlave.CrnStn crnStn = Utils.getCrnStnByStaNo(staDesc.getCrnStn(), false);
        if (crnStn == null) {
            throw new CoolException("堆垛机出库站不存在");
        }
        //生成堆垛机出库命令
        CrnCommand crnCommand = new CrnCommand();
        crnCommand.setCrnNo(taskWrk.getCrnNo()); // 堆垛机编号
        crnCommand.setTaskNo(taskWrk.getWrkNo().shortValue()); // 工作号
        crnCommand.setAckFinish((short) 0);  // 任务完成确认位
        crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
        crnCommand.setSourcePosX(Utils.getBayShort(taskWrk.getStartPoint()));     // 源库位排
        crnCommand.setSourcePosY(Utils.getLevShort(taskWrk.getStartPoint()));     // 源库位列
        crnCommand.setSourcePosZ(Utils.getRowShort(taskWrk.getStartPoint()));     // 源库位层
        crnCommand.setDestinationPosX(crnStn.getBay().shortValue());     // 目标库位排
        crnCommand.setDestinationPosY(crnStn.getLev().shortValue());     // 目标库位列
        crnCommand.setDestinationPosZ(crnStn.getRow().shortValue());     // 目标库位层
        crnCommand.setCommand((short)1);
        if (!CommandUtils.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(5, crnCommand))) {
            log.error("堆垛机命令生成失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
            throw new CoolException("堆垛机命令生成失败");
        }
        //生成输送线命令
        DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
        StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo()).clone();
        staProtocol.setWorkNo(taskWrk.getWrkNo().shortValue());
        staProtocol.setStaNo((short) Integer.parseInt(taskWrk.getTargetPoint()));
        if (!CommandUtils.offer(SlaveType.Devp, crnStn.getDevpPlcId(), new Task(3, staProtocol))) {
            log.error("输送线命令生成失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
            throw new CoolException("输送线命令生成失败");
        }
//        //出库任务派发
//        StaDesc staDesc = staDescService.queryCrn(taskWrk.getIoType(), Integer.parseInt(taskWrk.getTargetPoint()), taskWrk.getCrnNo());
//        if (staDesc == null) {
//            return;//不存在路径
//        }
//        int workNo = commonService.getWorkNo(WorkNoType.PAKOUT.type);//获取出库工作号
//        taskWrk.setWrkNo(workNo);//工作号
//        taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);//派发状态
//        taskWrk.setAssignTime(new Date());//派发时间
//        taskWrk.setWrkSts(12);//工作状态 12.吊车入库中
//        taskWrk.setCrnNo(staDesc.getCrnNo());//堆垛机号
//        taskWrk.setModiTime(new Date());
//        taskWrk.setModiUser(userId);
//        updateById(taskWrk);
//
//        CrnSlave.CrnStn crnStn = Utils.getCrnStnByStaNo(staDesc.getCrnStn(), false);
//        if (crnStn == null) {
//            throw new CoolException("堆垛机出库站不存在");
//        }
//        //生成堆垛机出库命令
//        CrnCommand crnCommand = new CrnCommand();
//        crnCommand.setCrnNo(taskWrk.getCrnNo()); // 堆垛机编号
//        crnCommand.setTaskNo(taskWrk.getWrkNo().shortValue()); // 工作号
//        crnCommand.setAckFinish((short) 0);  // 任务完成确认位
//        crnCommand.setTaskMode(CrnTaskModeType.LOC_MOVE); // 任务模式:  库位移转
//        crnCommand.setSourcePosX(Utils.getBayShort(taskWrk.getStartPoint()));     // 源库位排
//        crnCommand.setSourcePosY(Utils.getLevShort(taskWrk.getStartPoint()));     // 源库位列
//        crnCommand.setSourcePosZ(Utils.getRowShort(taskWrk.getStartPoint()));     // 源库位层
//        crnCommand.setDestinationPosX(crnStn.getBay().shortValue());     // 目标库位排
//        crnCommand.setDestinationPosY(crnStn.getLev().shortValue());     // 目标库位列
//        crnCommand.setDestinationPosZ(crnStn.getRow().shortValue());     // 目标库位层
//        crnCommand.setCommand((short)1);
//        if (!CommandUtils.offer(SlaveType.Crn, taskWrk.getCrnNo(), new Task(5, crnCommand))) {
//            log.error("堆垛机命令生成失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
//            throw new CoolException("堆垛机命令生成失败");
//        }
//
//        //生成输送线命令
//        DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, crnStn.getDevpPlcId());
//        StaProtocol staProtocol = devpThread.getStation().get(crnStn.getStaNo()).clone();
//        staProtocol.setWorkNo(taskWrk.getWrkNo().shortValue());
//        staProtocol.setStaNo((short) Integer.parseInt(taskWrk.getTargetPoint()));
//        if (!CommandUtils.offer(SlaveType.Devp, crnStn.getDevpPlcId(), new Task(3, staProtocol))) {
//            log.error("输送线命令生成失败,堆垛机号={},任务数据={}", taskWrk.getCrnNo(), JSON.toJSON(crnCommand));
//            throw new CoolException("输送线命令生成失败");
//        }
    }
    @Override
    public void locMove(TaskWrk taskWrk, Long userId) {
        //库格移载任务派发
       if (Cools.isEmpty(taskWrk.getTargetPoint())){
           return;
       }
        int workNo = commonService.getWorkNo(WorkNoType.OTHER.type);//获取工作号
        taskWrk.setWrkNo(workNo);//工作号
        taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);//派发状态
        taskWrk.setAssignTime(new Date());//派发时间
        taskWrk.setWrkSts(11);//工作状态 11.生成出库ID
        //taskWrk.setCrnNo(staDesc.getCrnNo());//堆垛机号
        taskWrk.setModiTime(new Date());
        taskWrk.setModiUser(userId);
        updateById(taskWrk);
//        //库格移载任务派发
//       if (Cools.isEmpty(taskWrk.getTargetPoint())){
//           return;
//       }
//        int workNo = commonService.getWorkNo(WorkNoType.OTHER.type);//获取工作号
//        taskWrk.setWrkNo(workNo);//工作号
//        taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);//派发状态
//        taskWrk.setAssignTime(new Date());//派发时间
//        taskWrk.setWrkSts(11);//工作状态 11.生成出库ID
//        //taskWrk.setCrnNo(staDesc.getCrnNo());//堆垛机号
//        taskWrk.setModiTime(new Date());
//        taskWrk.setModiUser(userId);
//        updateById(taskWrk);
    }
    @Override