#
vincentlu
2025-12-31 4937f052ec2c91b3fdcbb669aef7991912cde98e
zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
@@ -2,19 +2,19 @@
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.constant.RedisConstant;
import com.zy.acs.common.domain.AgvAction;
import com.zy.acs.common.domain.AgvActionItem;
import com.zy.acs.common.domain.AgvProtocol;
import com.zy.acs.common.domain.BaseResult;
import com.zy.acs.common.domain.protocol.AGV_11_UP;
import com.zy.acs.common.domain.protocol.AGV_70_UP;
import com.zy.acs.common.domain.protocol.IMessageBody;
import com.zy.acs.common.domain.protocol.*;
import com.zy.acs.common.domain.protocol.action.*;
import com.zy.acs.common.enums.AgvBackpackType;
import com.zy.acs.common.enums.AgvCompleteType;
import com.zy.acs.common.enums.AgvDirectionType;
import com.zy.acs.common.enums.AgvSpeedType;
import com.zy.acs.common.utils.GsonUtils;
import com.zy.acs.common.utils.RedisSupport;
import com.zy.acs.common.utils.Utils;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.SnowflakeIdWorker;
@@ -49,6 +49,8 @@
@Slf4j
@Service("mainService")
public class MainService {
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
    @Autowired
    private BusService busService;
@@ -96,6 +98,10 @@
    private LaneService laneService;
    @Autowired
    private ActionSorter actionSorter;
    @Autowired
    private StaReserveService staReserveService;
    @Autowired
    private ConveyorStationService conveyorStationService;
    @SuppressWarnings("all")
    @Transactional
@@ -176,24 +182,14 @@
                    }
                    destSta = staService.getById(task.getDestSta());
                    if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " is not in IDLE status");
                    }
                    destSta.setStaSts(StaStsType.READY_RELEASE.val());
                    destSta.setUpdateTime(now);
                    if (!staService.updateById(destSta)) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " failed to update");
                    if (null == staReserveService.reserveStaIn(destSta, task, 1)) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " failed to reserve");
                    }
                    break;
                case STA_TO_LOC:
                    oriSta = staService.getById(task.getOriSta());
                    if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " is not in STOCK status");
                    }
                    oriSta.setStaSts(StaStsType.READY_TAKE.val());
                    oriSta.setUpdateTime(now);
                    if (!staService.updateById(oriSta)) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " failed to update");
                    if (null == staReserveService.reserveStaOut(oriSta, task, 1)) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " failed to reserve");
                    }
                    destLoc = locService.getById(task.getDestLoc());
@@ -208,23 +204,13 @@
                    break;
                case STA_TO_STA:
                    oriSta = staService.getById(task.getOriSta());
                    if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " is not in STOCK status");
                    }
                    oriSta.setStaSts(StaStsType.READY_TAKE.val());
                    oriSta.setUpdateTime(now);
                    if (!staService.updateById(oriSta)) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " failed to update");
                    if (null == staReserveService.reserveStaOut(oriSta, task, 1)) {
                        throw new BusinessException("oriSta:" + task.getOriSta$() + " failed to reserve");
                    }
                    destSta = staService.getById(task.getDestSta());
                    if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " is not in IDLE status");
                    }
                    destSta.setStaSts(StaStsType.READY_RELEASE.val());
                    destSta.setUpdateTime(now);
                    if (!staService.updateById(destSta)) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " failed to update");
                    if (null == staReserveService.reserveStaIn(destSta, task, 1)) {
                        throw new BusinessException("destSta:" + task.getDestSta$() + " failed to reserve");
                    }
                    break;
                default:
@@ -249,28 +235,47 @@
            );
            if (Cools.isEmpty(taskList)) {
                bus.setBusSts(BusStsType.PROGRESS.val());
                bus.setUpdateTime(now);
                if (!busService.updateById(bus)) {
                    log.error("Bus [{}] failed to Update !!!", bus.getUuid());
                if (bus.getBusSts().equals(BusStsType.RECEIVE.val())) {
                    bus.setBusSts(BusStsType.PROGRESS.val());
                    bus.setUpdateTime(now);
                    if (!busService.updateById(bus)) {
                        log.error("Bus [{}] failed to Update !!!", bus.getUuid());
                    }
                }
                return;
            }
            List<Long> taskIds = taskList.stream().map(Task::getId).distinct().collect(Collectors.toList());
            for (Task task : taskList) {
                Agv agv = allocateService.execute(task);
                if (null == agv) {
//                    log.warn("Task[{}] has an issue, because it failed to check out agv which is idle...", task.getSeqNum());
                    continue;
                }
                task.setAgvId(agv.getId());
                task.setTaskSts(TaskStsType.WAITING.val());
                task.setIoTime(now);
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    throw new BusinessException("seqNum: " + task.getSeqNum() + " failed to update");
                }
            for (Task t : taskList) {
                String resultAgvNo = allocateService.execute(t
                        // inbound
                        , (task, agvNo) -> {
                        }
                        // normal
                        , (task, agvNo) -> {
                            if (Cools.isEmpty(agvNo)) {
                                return;
                            }
                            task.setAgvId(agvService.getAgvId(agvNo));
                            task.setTaskSts(TaskStsType.WAITING.val());
                            task.setIoTime(now);
                            task.setUpdateTime(now);
                            if (!taskService.updateById(task)) {
                                throw new BusinessException("seqNum: " + task.getSeqNum() + " failed to update");
                            }
                        });
//                if (Cools.isEmpty(agvNo)) {
////                    log.warn("Task[{}] has an issue, because it failed to check out agv which is idle...", task.getSeqNum());
//                    continue;
//                }
//                task.setAgvId(agvService.getAgvId(agvNo));
//                task.setTaskSts(TaskStsType.WAITING.val());
//                task.setIoTime(now);
//                task.setUpdateTime(now);
//                if (!taskService.updateById(task)) {
//                    throw new BusinessException("seqNum: " + task.getSeqNum() + " failed to update");
//                }
            }
        } catch (Exception e) {
            log.error("mainService.infuseAgvForTask", e);
@@ -308,7 +313,7 @@
                    return o2.getPriority() - o1.getPriority();
                }
            });
            Integer backpack = agvService.getBackpack(agv);
            Integer backpack = agvService.getBackpack(agvId);
            if (taskList.size() > backpack) {
                taskList = taskList.subList(0, backpack);
            }
@@ -1112,7 +1117,24 @@
                        ));
                        break;
                    case TO_STANDBY:
//                        FuncSta standByFuncSta = funcStaService.query(agvId, lastCode.getId(), 2);
                        // load lift
//                        actionList.add(new Action(
//                                null,    // 编号
//                                null,    // 总线
//                                task.getId(),    // 任务
//                                null,    // 动作号
//                                null,    // 优先级
//                                ActionTypeType.LoadPlatformLift.desc,    // 名称
//                                null,    // 属性值
//                                lastCode.getData(),    // 地面码
//                                String.valueOf(0),   // 动作参数
//                                ActionTypeType.LoadPlatformLift.val(),    // 动作类型
//                                actionPrepareSts,    // 动作进度
//                                agvId,    // AGV
//                                now    // 工作时间
//                        ));
                        // turn
//                        FuncSta standByFuncSta = funcStaService.query(lastCode.getId(), FuncStaType.STANDBY.toString());
//                        Double standByDirection = Double.parseDouble(standByFuncSta.getAngle());
//                        if (!lastDirection.equals(standByDirection)) {
//                            actionList.add(new Action(
@@ -1177,6 +1199,7 @@
            for (Segment item : segmentList) {
                item.setGroupId(groupId);
                item.setState(SegmentStateType.RUNNING.toString());
                item.setStartTime(now);
                item.setUpdateTime(now);
                if (null != algoStartTime) {
                    item.setAlgoTime((int) (now.getTime() - algoStartTime.getTime()));
@@ -1297,6 +1320,13 @@
                        break;
                    case ReadyReleaseToShelvesLoc:
                        agvAction.add(new AgvActionItem<>(ReadyReleaseToShelvesLoc.class)
                                .setVal(action.getVal().intValue())
                                .setQrCode(action.getCode())
                                .bodySync(body -> body.setHeight((short) Double.parseDouble(action.getParams())))
                        );
                        break;
                    case LoadPlatformLift:
                        agvAction.add(new AgvActionItem<>(LoadPlatformLift.class)
                                .setVal(action.getVal().intValue())
                                .setQrCode(action.getCode())
                                .bodySync(body -> body.setHeight((short) Double.parseDouble(action.getParams())))
@@ -1450,6 +1480,86 @@
            }
        }
        // 取放货请求包
        if (msgBody instanceof AGV_06_UP) {
            AGV_06_UP agv_06_up = (AGV_06_UP) msgBody;
            log.info("Agv [{}] 取放货请求包 ===>> {}", protocol.getAgvNo(), JSON.toJSONString(agv_06_up));
            boolean success = false;
            try {
                Code code = codeService.getCacheByData(agv_06_up.getQrCode());
                // query current segment
                Segment currSeg = segmentService.getCurrRunningSeg(agv.getId(), code.getId());
                if (null == currSeg) {
                    log.error("failed to find curr segment [{}]", agv.getUuid());
                } else {
                    // query current task
                    Task currTask = taskService.getById(currSeg.getTaskId());
                    if (currTask == null || !currTask.getTaskSts().equals(TaskStsType.PROGRESS.val())) {
                        log.error("agv[{}] task invalid, task={}", protocol.getAgvNo(), currSeg.getTaskId());
                    } else {
                        Sta sta;
                        TaskPosDto.PosType posType = TaskPosDto.PosType.of(currSeg.getPosType());
                        assert null != posType;
                        switch (posType) {
                            case ORI_LOC:
                            case DEST_LOC:
                                success = true;
                                break;
                            case ORI_STA:
                                // load from sta
                                sta = staService.getById(currTask.getOriSta());
                                // reserve to be waiting
                                if (!staReserveService.waitingStaReserve(sta, currTask, 1, StaReserveType.OUT)) {
                                    break;
                                }
                                // convey plc valid
                                if (!conveyorStationService.allowAgvWork(sta, currTask, currSeg, StaReserveType.OUT)) {
                                    // reserve rollback
                                    staReserveService.rollbackWaitingToReserved(sta, currTask, StaReserveType.OUT);
                                    break;
                                }
                                success = true;
                                break;
                            case DEST_STA:
                                // place to sta
                                sta = staService.getById(currTask.getDestSta());
                                // reserve to be waiting
                                if (!staReserveService.waitingStaReserve(sta, currTask, 1, StaReserveType.IN)) {
                                    break;
                                }
                                // convey plc valid
                                if (!conveyorStationService.allowAgvWork(sta, currTask, currSeg, StaReserveType.IN)) {
                                    // reserve rollback
                                    staReserveService.rollbackWaitingToReserved(sta, currTask, StaReserveType.IN);
                                    break;
                                }
                                success = true;
                                break;
                            default:
                                log.error("agv[{}] has wrong posType [{}], segment [{}]", protocol.getAgvNo(), posType.toString(), currSeg.getId());
                                break;
                        }
                    }
                }
            } catch (Exception e) {
                log.error("agv[{}] failed to deal with action request, qrCode={}", agv.getUuid(), agv_06_up.getVal(), e);
            }
            AGV_06_DOWN agv_06_down = new AGV_06_DOWN();
            agv_06_down.setSerialNo(agv_06_up.getSerialNo());
            agv_06_down.setActionCode(agv_06_up.getActionCode());
            agv_06_down.setResult(success ? 1 : 0);
            redis.push(RedisConstant.AGV_PATH_DOWN_FLAG, AgvProtocol.build(protocol.getAgvNo()).setMessageBody(agv_06_down));
        }
        // 料仓信息包
        if (msgBody instanceof AGV_70_UP) {
            AGV_70_UP agv_70_up = (AGV_70_UP) msgBody;
@@ -1497,14 +1607,13 @@
            }
            if (taskComplete) {
                locService.taskCallBack(task);
                task.setTaskSts(TaskStsType.COMPLETE.val());
                task.setEndTime(now);
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    log.error("Task [{}] 更新失败 !!!", task.getSeqNum());
                } else {
                    taskService.maintainLocAndSta(task);
                    log.info("Task [{}] 作业完毕 ==========>> ", task.getSeqNum());
                }
@@ -1530,6 +1639,7 @@
        // segment
        for (Segment segment : segmentList) {
            segment.setState(SegmentStateType.FINISH.toString());
            segment.setEndTime(now);
            segment.setUpdateTime(now);
            if (!segmentService.updateById(segment)) {
                log.error("Segment [{}] 更新失败 !!!", segment.getGroupId() + " - " + segment.getSerial());