#
luxiaotao1123
2025-01-08 aa6f20d98b5d8e18ae56f9562a78d403a5417b48
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;
@@ -472,6 +471,7 @@
            // task
            for (Task task : taskList) {
                task.setTaskSts(TaskStsType.ASSIGN.val());
                task.setStartTime(now);
                task.setUpdateTime(now);
                if (!taskService.updateById(task)) {
                    throw new BusinessException(task.getUuid() + "任务更新失败");
@@ -520,7 +520,7 @@
                    }
                    break;
                case MOVE:
                    endCode = codeService.selectByData(destination);
                    endCode = codeService.getCacheByData(destination);
                    break;
                default:
                    return false;
@@ -641,33 +641,35 @@
     * 根据分片生成动作 ( 车辆可能已经做过一些任务了,正在等待下一段任务 )
     */
    @Transactional
    public synchronized void generateAction(Long agvId, List<Segment> segmentList, List<String> pathList) {
    public synchronized void generateAction(Long agvId, List<Segment> segmentList, List<String> pathList, Date algoStartTime) {
        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);
            assert agvDetail != null;
            AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(agvId);
            List<Action> actionList = new ArrayList<>();
            // start node
            Code lastCode = codeService.getById(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;
            long startTime = System.currentTimeMillis();
            for (Segment segment : segmentList) {
                // 分段所属的Task
@@ -688,15 +690,15 @@
                        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)) {
                            nextDirection = workDirection;
                            reverse = true;
                        }
                        if (!lastDirection.equals(nextDirection) || actionList.size() == 0) {
                        if (!lastDirection.equals(nextDirection) || actionList.isEmpty()) {
                            // turn
                            actionList.add(new Action(
                                    null,    // 编号
@@ -709,7 +711,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(nextDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -735,7 +737,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(gap.getDistance()),   // 动作参数
                                actionType.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -761,7 +763,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(workDirection),   // 动作参数
                                ActionTypeType.TurnCorner.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -792,7 +794,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(workDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -801,7 +803,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 +815,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(oriLoc.getOffset()),   // 动作参数
                                ActionTypeType.ReadyTakeFromShelvesLoc.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -829,7 +831,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(backpackType.height),   // 动作参数
                                ActionTypeType.ReadyReleaseToAgvSite.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -850,7 +852,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(workDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -868,7 +870,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(backpackType.height),   // 动作参数
                                ActionTypeType.ReadyTakeFromAgvSite.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -876,7 +878,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 +890,7 @@
                                lastCode.getData(),    // 地面码
                                String.valueOf(destLoc.getOffset()),   // 动作参数
                                ActionTypeType.ReadyReleaseToShelvesLoc.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -913,7 +915,7 @@
                                    lastCode.getData(),    // 地面码
                                    String.valueOf(chargeDirection),   // 动作参数
                                    ActionTypeType.TurnCorner.val(),    // 动作类型
                                    ActionStsType.PREPARE.val(),    // 动作进度
                                    actionPrepareSts,    // 动作进度
                                    agvId,    // AGV
                                    now    // 工作时间
                            ));
@@ -932,7 +934,7 @@
                                lastCode.getData(),    // 地面码
                                null,   // 动作参数
                                ActionTypeType.DockingCharge.val(),    // 动作类型
                                ActionStsType.PREPARE.val(),    // 动作进度
                                actionPrepareSts,    // 动作进度
                                agvId,    // AGV
                                now    // 工作时间
                        ));
@@ -952,7 +954,7 @@
//                                    lastCode.getData(),    // 地面码
//                                    String.valueOf(standByDirection),   // 动作参数
//                                    ActionTypeType.TurnCorner.val(),    // 动作类型
//                                    ActionStsType.PREPARE.val(),    // 动作进度
//                                    actionPrepareSts,    // 动作进度
//                                    agvId,    // AGV
//                                    now    // 工作时间
//                            ));
@@ -966,6 +968,7 @@
                }
            }
            System.out.println("save data: " + (System.currentTimeMillis() - startTime));
            // finish
            actionList.add(new Action(
@@ -979,7 +982,7 @@
                    lastCode.getData(),    // 地面码
                    null,   // 动作参数
                    ActionTypeType.FinishPath.val(),    // 动作类型
                    ActionStsType.PREPARE.val(),    // 动作进度
                    actionPrepareSts,    // 动作进度
                    agvId,    // AGV
                    now    // 工作时间
            ));
@@ -1010,19 +1013,23 @@
                item.setGroupId(groupId);
                item.setState(SegmentStateType.RUNNING.toString());
                item.setUpdateTime(now);
                if (null != algoStartTime) {
                    item.setAlgoTime((int) (now.getTime() - algoStartTime.getTime()));
                }
                if (!segmentService.updateById(item)) {
                    throw new CoolException("更新Segment失败");
                }
            }
            log.info(agv.getUuid() + "号Agv动作组装完成,指令数量:" + newActionList.size());
            log.info(agvNo + "号Agv动作组装完成,指令数量:" + 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);