#
luxiaotao1123
2025-01-15 fd6d26f8ffe6a37533e741337f3eca9134d934c7
zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
@@ -1,7 +1,6 @@
package com.zy.acs.manager.core.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.domain.AgvAction;
import com.zy.acs.common.domain.AgvActionItem;
@@ -32,6 +31,7 @@
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.manager.service.impl.WebsocketServiceImpl;
import com.zy.acs.manager.manager.utils.ActionSorter;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -94,6 +94,8 @@
    private AgvModelService agvModelService;
    @Autowired
    private LaneService laneService;
    @Autowired
    private ActionSorter actionSorter;
    @SuppressWarnings("all")
@@ -248,7 +250,7 @@
        try {
            // valid -----------------------------------------------
            Agv agv = agvService.getById(agvId);
            if (!agvService.judgeEnable(agv.getId(), true)) {
            if (!agvService.judgeEnable(agv.getId(), false)) {
                return;
            }
            if (!Cools.isEmpty(taskService.selectInSts(agvId, TaskStsType.ASSIGN, TaskStsType.PROGRESS))) {
@@ -299,8 +301,8 @@
                        oriLoc = locService.getById(task.getOriLoc());
                        destLoc = locService.getById(task.getDestLoc());
                        startCode = codeService.getById(oriLoc.getCode());
                        endCode = codeService.getById(destLoc.getCode());
                        startCode = codeService.getCacheById(oriLoc.getCode());
                        endCode = codeService.getCacheById(destLoc.getCode());
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_LOC, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_LOC, sameGroupXy);
@@ -309,8 +311,8 @@
                        oriLoc = locService.getById(task.getOriLoc());
                        destSta = staService.getById(task.getDestSta());
                        startCode = codeService.getById(oriLoc.getCode());
                        endCode = codeService.getById(destSta.getCode());
                        startCode = codeService.getCacheById(oriLoc.getCode());
                        endCode = codeService.getCacheById(destSta.getCode());
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_LOC, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_STA, sameGroupXy);
@@ -319,8 +321,8 @@
                        oriSta = staService.getById(task.getOriSta());
                        destLoc = locService.getById(task.getDestLoc());
                        startCode = codeService.getById(oriSta.getCode());
                        endCode = codeService.getById(destLoc.getCode());
                        startCode = codeService.getCacheById(oriSta.getCode());
                        endCode = codeService.getCacheById(destLoc.getCode());
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_STA, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_LOC, sameGroupXy);
@@ -329,8 +331,8 @@
                        oriSta = staService.getById(task.getOriSta());
                        destSta = staService.getById(task.getDestSta());
                        startCode = codeService.getById(oriSta.getCode());
                        endCode = codeService.getById(destSta.getCode());
                        startCode = codeService.getCacheById(oriSta.getCode());
                        endCode = codeService.getCacheById(destSta.getCode());
                        TaskPosDto.packagePosGroup(groups, task, startCode, TaskPosDto.PosType.ORI_STA, sameGroupXy);
                        TaskPosDto.packagePosGroup(groups, task, endCode, TaskPosDto.PosType.DEST_STA, sameGroupXy);
@@ -395,7 +397,7 @@
            }
            // re-order by agv current position
            Code currCode = codeService.getById(agvDetail.getRecentCode());
            Code currCode = codeService.getCacheById(agvDetail.getRecentCode());
            Double[] currPosition = new Double[] {currCode.getX(), currCode.getY()};
            List<List<TaskPosDto>> pickGroups = new ArrayList<>();
@@ -491,37 +493,38 @@
     * 充电 回待机位任务
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW) // although there is a Transactional here that the lock is isolated, but we can't join the caller's Transactional
    public boolean buildMinorTask(Agv agv, TaskTypeType taskType, String destination, Jam jam) {
        if (Cools.isEmpty(agv, taskType)) { return false; }
    public boolean buildMinorTask(Long agvId, TaskTypeType taskType, String destination, Jam jam) {
        if (Cools.isEmpty(agvId, taskType)) { return false; }
        try {
            AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
            if (!agvService.judgeEnable(agv.getId())) {
            String agvNo = agvService.getAgvNo(agvId);
            if (!agvService.judgeEnable(agvId)) {
                return false;
            }
            if (!Cools.isEmpty(taskService.selectInSts(agv.getId(), TaskStsType.ASSIGN, TaskStsType.PROGRESS))) {
                throw new CoolException("AGV[" + agv.getUuid() + "] failed to assign,because already has the task in running...");
            if (!Cools.isEmpty(taskService.selectInSts(agvId, TaskStsType.ASSIGN, TaskStsType.PROGRESS))) {
                throw new CoolException("AGV[" + agvNo + "] failed to assign,because already has the task in running...");
            }
            if (!Cools.isEmpty(segmentService.getByAgvAndState(agv.getId(), SegmentStateType.WAITING.toString()))
                    || !Cools.isEmpty(segmentService.getByAgvAndState(agv.getId(), SegmentStateType.RUNNING.toString()))) {
                throw new CoolException("AGV[" + agv.getUuid() + "] failed to assign,because already has the segment in running...");
            if (!Cools.isEmpty(segmentService.getByAgvAndState(agvId, SegmentStateType.WAITING.toString()))
                    || !Cools.isEmpty(segmentService.getByAgvAndState(agvId, SegmentStateType.RUNNING.toString()))) {
                throw new CoolException("AGV[" + agvNo + "] failed to assign,because already has the segment in running...");
            }
            Date now = new Date();
            AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(agvId);
            Code endCode = null;
            switch (taskType) {
                case TO_CHARGE:
                case TO_STANDBY:
                    List<FuncSta> idleFunStaList = funcStaService.findInIdleStatus(FuncStaType.query(taskType), agv.getId());
                    List<FuncSta> idleFunStaList = funcStaService.findInIdleStatus(FuncStaType.query(taskType), agvId);
                    if (!Cools.isEmpty(idleFunStaList)) {
                        FuncSta funcSta = funcStaService.checkoutClosestFunSta(agvDetail.getRecentCode(), idleFunStaList);
                        endCode = codeService.getById(funcSta.getCode());
                        endCode = codeService.getCacheById(funcSta.getCode());
                    }
                    if (null == endCode) {
                        log.warn("AGV[{}] failed to search destination,there hadn't any idle funSta,TaskTypeType:{}", agv.getUuid(), taskType.toString());
                        log.warn("AGV[{}] failed to search destination,there hadn't any idle funSta,TaskTypeType:{}", agvNo, taskType.toString());
                    }
                    break;
                case MOVE:
                    endCode = codeService.selectByData(destination);
                    endCode = codeService.getCacheByData(destination);
                    break;
                default:
                    return false;
@@ -530,12 +533,12 @@
            if (null == endCode) {
                return false;
            }
            if (!allocateService.validCapacityOfLane(agv, endCode)) {
                throw new BusinessException("the lane with code:" + endCode.getData() + " is full of AGV[" + agv.getUuid() + "]!!!");
            if (!allocateService.validCapacityOfLane(agvNo, endCode)) {
                throw new BusinessException("the lane with code:" + endCode.getData() + " is full of AGV[" + agvNo + "]!!!");
            }
            Task task = new Task();
            task.setAgvId(agv.getId());
            task.setAgvId(agvId);
            task.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            List<Task> lastTasks = taskService.list(new LambdaQueryWrapper<Task>().orderByDesc(Task::getId));
            task.setSeqNum(Utils.generateSeqNum(Cools.isEmpty(lastTasks)?null:lastTasks.get(0).getSeqNum()));
@@ -559,7 +562,7 @@
            Travel travel = new Travel();
            travel.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            travel.setTravelId(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            travel.setAgvId(agv.getId());
            travel.setAgvId(agvId);
//            travel.setTaskContent(JSON.toJSONString(list));
            travel.setTaskIds(GsonUtils.toJson(Utils.singletonList(task.getId())));
            travel.setState(TravelStateType.RUNNING.toString());
@@ -589,7 +592,7 @@
            Segment segment = new Segment();
            segment.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
            segment.setTravelId(travel.getId());
            segment.setAgvId(agv.getId());
            segment.setAgvId(agvId);
            segment.setTaskId(task.getId());
            segment.setSerial(segSerial);
            segment.setEndNode(endCode.getId());
@@ -646,25 +649,27 @@
        try {
            if (Cools.isEmpty(agvId, segmentList)) { return; }
            Date now = new Date();
            JSONObject storeDirection = configService.getVal("storeDirection", JSONObject.class);
            Agv agv = agvService.getById(agvId);
            if (!agvService.judgeEnable(agv.getId())) {
                throw new CoolException("AGV[" + agv.getUuid() + "]当前不可用...");
            }
            long actionPrepareSts = ActionStsType.PREPARE.val();
//            JSONObject storeDirection = configService.getVal("storeDirection", JSONObject.class);
            int angleOffsetVal = configService.getVal("mapAngleOffsetVal", Integer.class);
            String agvNo = agvService.getAgvNo(agvId);
//            if (!agvService.judgeEnable(agvId)) {
//                throw new CoolException("AGV[" + agvNo + "]当前不可用...");
//            }
            AgvModel agvModel = agvModelService.getById(agv.getAgvModel());
            AgvModel agvModel = agvModelService.getByAgvId(agvId);
            Double workDirection = agvModel.getWorkDirection();
            AgvSpeedType agvSpeedType = AgvSpeedType.query(agvModel.getTravelSpeed());
            assert agvSpeedType != null;
            AgvDetail agvDetail = agvDetailService.selectByAgvId(agvId);
            AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(agvId);
            List<Action> actionList = new ArrayList<>();
            // start node
            Code lastCode = codeService.getById(agvDetail.getRecentCode());
            Code lastCode = codeService.getCacheById(agvDetail.getRecentCode());
            Double lastDirection = agvDetail.getAgvAngle();
            if (!lastCode.getData().equals(pathList.get(0))) {
                throw new CoolException("AGV[" + agv.getUuid() + "]定位偏移...");
                throw new CoolException("AGV[" + agvNo + "]定位偏移...");
            }
            boolean first = true;
@@ -674,7 +679,7 @@
                Task task = taskService.getById(segment.getTaskId());
                // 节点条码
                Code code = codeService.getById(segment.getEndNode());
                Code code = codeService.getCacheById(segment.getEndNode());
                // 需要走行
                if (!lastCode.getData().equals(code.getData())) {
@@ -688,8 +693,8 @@
                        String next = pathListPart.get(i);
                        Code nextCode = codeService.selectByData(next);
                        Double nextDirection = mapService.calculateDirection(lastCode, nextCode);
                        Code nextCode = codeService.getCacheByData(next);
                        Double nextDirection = mapService.calculateDirection(lastCode, nextCode, angleOffsetVal);
                        boolean reverse = false;
                        if (nextDirection.equals((workDirection + 180) % 360)) {
@@ -709,7 +714,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(nextDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -735,7 +740,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(gap.getDistance()),   // 动作参数
                                actionType.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -761,7 +766,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(workDirection),   // 动作参数
                                ActionTypeType.TurnCorner.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -792,7 +797,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(workDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -801,7 +806,7 @@
                        // 货架取货
                        Loc oriLoc = locService.getById(task.getOriLoc());
                        // 计算左右方向
                        agvDirectionType = mapService.calculateAgvWorkDirection(storeDirection, oriLoc, lastCode);
                        agvDirectionType = mapService.calculateAgvWorkDirection(oriLoc, lastCode);
                        actionList.add(new Action(
                                null,    // 编号
                                task.getBusId(),    // 总线
@@ -813,7 +818,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(oriLoc.getOffset()),   // 动作参数
                                ActionTypeType.ReadyTakeFromShelvesLoc.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -829,7 +834,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(backpackType.height),   // 动作参数
                                ActionTypeType.ReadyReleaseToAgvSite.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -850,7 +855,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(workDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -868,7 +873,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(backpackType.height),   // 动作参数
                                ActionTypeType.ReadyTakeFromAgvSite.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -876,7 +881,7 @@
                        // 货架放货
                        Loc destLoc = locService.getById(task.getDestLoc());
                        // 计算左右方向
                        agvDirectionType = mapService.calculateAgvWorkDirection(storeDirection, destLoc, lastCode);
                        agvDirectionType = mapService.calculateAgvWorkDirection(destLoc, lastCode);
                        actionList.add(new Action(
                                null,    // 编号
                                task.getBusId(),    // 总线
@@ -888,7 +893,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(destLoc.getOffset()),   // 动作参数
                                ActionTypeType.ReadyReleaseToShelvesLoc.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -913,7 +918,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(chargeDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -932,7 +937,7 @@
                                lastCode.getData(),    // 地面码
                                null,   // 动作参数
                                ActionTypeType.DockingCharge.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -952,7 +957,7 @@
//                                    lastCode.getData(),    // 地面码
//                                    String.valueOf(standByDirection),   // 动作参数
//                                    ActionTypeType.TurnCorner.val(),    // 动作类型
//                                    ActionStsType.PREPARE.val(),    // 动作进度
//                                    actionPrepareSts,    // 动作进度
//                                    agvId,    // AGV
//                                    now    // 工作时间
//                            ));
@@ -979,30 +984,24 @@
                    lastCode.getData(),    // 地面码
                    null,   // 动作参数
                    ActionTypeType.FinishPath.val(),    // 动作类型
                    ActionStsType.PREPARE.val(),    // 动作进度
                    actionPrepareSts,    // 动作进度
                    agvId,    // AGV
                    now    // 工作时间
            ));
            List<Action> newActionList = new ArrayList<>(actionList);
//            List<Action> optimizeList = actionService.optimizeSort(actionList);
//            List<Action> newActionList = new ArrayList<>(optimizeList);
            List<Action> newActionList = actionSorter.optimizeSort(actionList);
            String groupId = String.valueOf(snowflakeIdWorker.nextId()).substring(3);
            // save action
            int i = newActionList.size();
            for (Action action : newActionList) {
                action.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
//                action.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3));
                action.setGroupId(groupId);
                action.setPriority(i);
                if (!actionService.save(action)) {
                    throw new BusinessException(action.getName() + "动作保存失败");
                }
                i -= 1;
            }
            if (!actionService.saveBatch(newActionList)) {
                throw new BusinessException("group[" + groupId + "] 动作保存失败");
            }
            // update segment
@@ -1018,14 +1017,15 @@
                }
            }
            log.info(agv.getUuid() + "号Agv动作组装完成,指令数量:" + newActionList.size());
            log.info("{}号Agv动作组装完成,指令数量:{}", agvNo, newActionList.size());
        } catch (Exception e) {
            log.error("mainService.generateAction", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            if (!Cools.isEmpty(pathList)) {
                pathList.remove(0);
                mapDataDispatcher.clearDynamicMatrixByCodeList(null, pathList);
                List<int[]> codeMatrixIdxList = mapDataDispatcher.getCodeMatrixIdxList(null, pathList);
                mapDataDispatcher.clearDynamicMatrixByCodeList(null, codeMatrixIdxList);
            }
            throw new RuntimeException("generateAction method caught an exception, rolling back transaction.", e);
@@ -1070,8 +1070,8 @@
                }
            }
            Agv agv = agvService.getById(actionList.get(0).getAgvId());
            AgvAction agvAction = new AgvAction(agv.getUuid(), actionGroupId);
            String agvNo = agvService.getAgvNo(actionList.get(0).getAgvId());
            AgvAction agvAction = new AgvAction(agvNo, actionGroupId);
            for (Action action : actionList) {
                switch (Objects.requireNonNull(ActionTypeType.get(action.getActionTypeEl()))) {