#
vincentlu
昨天 24de9067a69b6f2d7aaeb2bacd925d995b9120c2
zy-acs-manager/src/main/java/com/zy/acs/manager/core/HandlerController.java
@@ -3,12 +3,13 @@
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.utils.Base62;
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.R;
import com.zy.acs.framework.common.SnowflakeIdWorker;
import com.zy.acs.manager.common.annotation.OperationLog;
import com.zy.acs.manager.common.config.UplinkProperties;
import com.zy.acs.manager.common.domain.param.HandlerPublishParam;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.core.service.*;
@@ -18,6 +19,7 @@
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.system.controller.BaseController;
import com.zy.acs.manager.system.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -64,7 +66,7 @@
    @Autowired
    private StaService staService;
    @Autowired
    private TrafficService trafficService;
    private UplinkProperties uplinkProperties;
    @Autowired
    private ThreadPoolRegulator threadPoolRegulator;
    @Autowired
@@ -73,6 +75,12 @@
    private AvoidWaveCalculator avoidWaveCalculator;
    @Autowired
    private PatrolService patrolService;
    @Autowired
    private StaReserveService staReserveService;
    @Autowired
    private AgvAreaDispatcher agvAreaDispatcher;
    @Autowired
    private ConfigService configService;
    @PreAuthorize("hasAuthority('manager:agv:update')")
    @OperationLog("Locate All Agv")
@@ -136,6 +144,7 @@
            return R.error();
        }
        Boolean maintainLocSts = configService.getVal("maintainLocSts", Boolean.class);
        Date now = new Date();
        Agv agv = agvService.selectByUuid(param.getAgvNo());
        AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
@@ -149,12 +158,30 @@
                    .eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
                            .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                })) > 0) {
            log.info(agv.getUuid() + "号AGV不可用,已经存在进行中的任务...");
            log.info("{}号AGV不可用,已经存在进行中的任务...", agv.getUuid());
            return R.error();
        }
        if (!agvService.judgeEnable(agv.getId())) {
            log.info(agv.getUuid() + "号AGV不可用,任务无法计算...");
            log.info("{}号AGV不可用,任务无法计算...", agv.getUuid());
            return R.error();
        }
        Task task = new Task();
        task.setAgvId(agv.getId());
        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()));
        task.setSeqNum(Base62.encode(snowflakeIdWorker.nextId()));
        task.setTaskType(param.getTaskMode().val());
        task.setTaskSts(TaskStsType.WAITING.val());
        task.setUplinkSts(uplinkProperties.getEnabled() ? TaskUplinkStateType.PENDING.toString() : TaskUplinkStateType.SKIPPED.toString());
        task.setPriority(999);
        task.setIoTime(now);
        task.setStartTime(now);
        task.setMemo("AUTO");
        if (!taskService.save(task)) {
            throw new BusinessException(task.getSeqNum() + "任务保存失败");
        }
        // generate
@@ -162,7 +189,6 @@
        Loc destLoc = null;
        Sta oriSta = null;
        Sta destSta = null;
        Task task = null;
        switch (param.getTaskMode()) {
            case MOVE:
                Code endCode = null;
@@ -175,12 +201,23 @@
                if (null == endCode) {
                    return R.error();
                }
                if (!agvAreaDispatcher.match(agv.getId(), endCode.getId())) {
                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + endCode.getData());
                }
                if (!taskService.removeById(task.getId())) {
                    throw new BusinessException("failed to remove task");
                }
                task = null;
                if (!mainLockWrapService.buildMinorTask(agv.getId(), param.getTaskMode(), endCode.getData(), null)) {
                    return R.error();
                }
                break;
            case TO_CHARGE:
            case TO_STANDBY:
                if (!taskService.removeById(task.getId())) {
                    throw new BusinessException("failed to remove task");
                }
                task = null;
                if (!mainLockWrapService.buildMinorTask(agv.getId(), param.getTaskMode(), null, null)) {
                    return R.error();
                }
@@ -196,13 +233,19 @@
                if (null == oriLoc) {
                    return R.error();
                }
                if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) {
                    throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 不是在库状态");
                if (!agvAreaDispatcher.match(agv.getId(), oriLoc.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + oriLoc.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达起始库位【" + oriLoc.getLocNo() + "】");
                }
                oriLoc.setLocSts(LocStsType.PAKOUT.val());
                oriLoc.setUpdateTime(now);
                if (!locService.updateById(oriLoc)) {
                    throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 修改库位状态失败");
                if (maintainLocSts) {
                    if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) {
                        throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 不是在库状态");
                    }
                    oriLoc.setLocSts(LocStsType.PAKOUT.val());
                    oriLoc.setUpdateTime(now);
                    if (!locService.updateById(oriLoc)) {
                        throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 修改库位状态失败");
                    }
                }
                // destLoc
@@ -215,18 +258,22 @@
                if (null == destLoc) {
                    return R.error();
                }
                if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) {
                    throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 不是空闲状态");
                if (!agvAreaDispatcher.match(agv.getId(), destLoc.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + destLoc.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达目标库位【" + destLoc.getLocNo() + "】");
                }
                destLoc.setLocSts(LocStsType.PAKIN.val());
                destLoc.setUpdateTime(now);
                if (!locService.updateById(destLoc)) {
                    throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 修改库位状态失败");
                if (maintainLocSts) {
                    if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) {
                        throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 不是空闲状态");
                    }
                    destLoc.setLocSts(LocStsType.PAKIN.val());
                    destLoc.setUpdateTime(now);
                    if (!locService.updateById(destLoc)) {
                        throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 修改库位状态失败");
                    }
                }
                // task
                task = new Task();
                task.setOriLoc(oriLoc.getId());
                task.setOriCode(oriLoc.getCode());
                task.setDestLoc(destLoc.getId());
@@ -243,13 +290,19 @@
                if (null == oriLoc) {
                    return R.error();
                }
                if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) {
                    throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 不是在库状态");
                if (!agvAreaDispatcher.match(agv.getId(), oriLoc.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + oriLoc.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达起始库位【" + oriLoc.getLocNo() + "】");
                }
                oriLoc.setLocSts(LocStsType.PAKOUT.val());
                oriLoc.setUpdateTime(now);
                if (!locService.updateById(oriLoc)) {
                    throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 修改库位状态失败");
                if (maintainLocSts) {
                    if (!oriLoc.getLocSts().equals(LocStsType.STOCK.val())) {
                        throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 不是在库状态");
                    }
                    oriLoc.setLocSts(LocStsType.PAKOUT.val());
                    oriLoc.setUpdateTime(now);
                    if (!locService.updateById(oriLoc)) {
                        throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 修改库位状态失败");
                    }
                }
                // destSta
@@ -262,22 +315,20 @@
                if (null == destSta) {
                    return R.error();
                }
                if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 不是无货状态");
                if (!agvAreaDispatcher.match(agv.getId(), destSta.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + destSta.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达目标站点【" + destSta.getStaNo() + "】");
                }
                destSta.setStaSts(StaStsType.READY_RELEASE.val());
                destSta.setUpdateTime(now);
                if (!staService.updateById(destSta)) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 修改站点状态失败");
                if (null == staReserveService.reserveStaIn(destSta, task, 1)) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 预约失败");
                }
                // task
                task = new Task();
                task.setOriLoc(oriLoc.getId());
                task.setOriCode(oriLoc.getCode());
                task.setDestSta(destSta.getId());
                task.setDestCode(destSta.getCode());
                staReserveService.allocateCallBack(task, agv.getId());
                break;
            case STA_TO_LOC:
                // oriSta
@@ -290,13 +341,12 @@
                if (null == oriSta) {
                    return R.error();
                }
                if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 不是有货状态");
                if (!agvAreaDispatcher.match(agv.getId(), oriSta.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + oriSta.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达起始站点【" + oriSta.getStaNo() + "】");
                }
                oriSta.setStaSts(StaStsType.READY_TAKE.val());
                oriSta.setUpdateTime(now);
                if (!staService.updateById(oriSta)) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 修改站点状态失败");
                if (null == staReserveService.reserveStaOut(oriSta, task, 1)) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 预约失败");
                }
                // destLoc
@@ -309,22 +359,28 @@
                if (null == destLoc) {
                    return R.error();
                }
                if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) {
                    throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 不是空闲状态");
                if (!agvAreaDispatcher.match(agv.getId(), destLoc.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + destLoc.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达目标库位【" + destLoc.getLocNo() + "】");
                }
                destLoc.setLocSts(LocStsType.PAKIN.val());
                destLoc.setUpdateTime(now);
                if (!locService.updateById(destLoc)) {
                    throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 修改库位状态失败");
                if (maintainLocSts) {
                    if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) {
                        throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 不是空闲状态");
                    }
                    destLoc.setLocSts(LocStsType.PAKIN.val());
                    destLoc.setUpdateTime(now);
                    if (!locService.updateById(destLoc)) {
                        throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 修改库位状态失败");
                    }
                }
                // task
                task = new Task();
                task.setOriSta(oriSta.getId());
                task.setOriCode(oriSta.getCode());
                task.setDestLoc(destLoc.getId());
                task.setDestCode(destLoc.getCode());
                staReserveService.allocateCallBack(task, agv.getId());
                break;
            case STA_TO_STA:
                // oriSta
@@ -337,14 +393,14 @@
                if (null == oriSta) {
                    return R.error();
                }
                if (!oriSta.getStaSts().equals(StaStsType.STOCK.val())) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 不是有货状态");
                if (!agvAreaDispatcher.match(agv.getId(), oriSta.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + oriSta.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达起始站点【" + oriSta.getStaNo() + "】");
                }
                oriSta.setStaSts(StaStsType.READY_TAKE.val());
                oriSta.setUpdateTime(now);
                if (!staService.updateById(oriSta)) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 修改站点状态失败");
                if (null == staReserveService.reserveStaOut(oriSta, task, 1)) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 预约失败");
                }
                // destSta
                if (!Cools.isEmpty(param.getEndStaNo())) {
@@ -356,44 +412,31 @@
                if (null == destSta) {
                    return R.error();
                }
                if (!destSta.getStaSts().equals(StaStsType.IDLE.val())) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 不是无货状态");
                if (!agvAreaDispatcher.match(agv.getId(), destSta.getCode())) {
//                    throw new BusinessException("AGV " + agv.getUuid() + " failed to reach target code " + destSta.getCode$());
                    throw new BusinessException("AGV【" + agv.getUuid() + "】无法到达目标站点【" + destSta.getStaNo() + "】");
                }
                destSta.setStaSts(StaStsType.READY_RELEASE.val());
                destSta.setUpdateTime(now);
                if (!staService.updateById(destSta)) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 修改站点状态失败");
                if (null == staReserveService.reserveStaIn(destSta, task, 1)) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 预约失败");
                }
                // task
                task = new Task();
                task.setOriSta(oriSta.getId());
                task.setOriCode(oriSta.getCode());
                task.setDestSta(destSta.getId());
                task.setDestCode(destSta.getCode());
                staReserveService.allocateCallBack(task, agv.getId());
                break;
            default:
                break;
        }
        if (null != task) {
            task.setAgvId(agv.getId());
            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()));
            task.setTaskType(param.getTaskMode().val());
            task.setTaskSts(TaskStsType.WAITING.val());
            task.setPriority(999);
            task.setIoTime(now);
            task.setStartTime(now);
            if (!taskService.save(task)) {
                throw new BusinessException(task.getSeqNum() + "任务保存失败");
            if (!taskService.updateById(task)) {
                throw new BusinessException("任务更新失败");
            }
        }
        return R.ok();
    }