#
vincentlu
8 天以前 efabc6ba991acfd01d38bb0bf4e8cfd772416617
zy-acs-manager/src/main/java/com/zy/acs/manager/core/HandlerController.java
@@ -2,24 +2,26 @@
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.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.domain.param.HandlerPublishParam;
import com.zy.acs.manager.common.exception.BusinessException;
import com.zy.acs.manager.core.service.*;
import com.zy.acs.manager.core.service.astart.MapDataDispatcher;
import com.zy.acs.manager.core.service.astart.domain.DynamicNode;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.ActionStsType;
import com.zy.acs.manager.manager.enums.LocStsType;
import com.zy.acs.manager.manager.enums.TaskStsType;
import com.zy.acs.manager.manager.enums.TaskTypeType;
import com.zy.acs.manager.manager.enums.*;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.system.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
@@ -27,6 +29,7 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
/**
 * Created by vincent on 8/1/2024
@@ -35,6 +38,8 @@
@RestController
@RequestMapping("/api/handler")
public class HandlerController extends BaseController {
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
    public static final String APP_KEY = "xltys1995";
@@ -57,6 +62,8 @@
    @Autowired
    private LocService locService;
    @Autowired
    private StaService staService;
    @Autowired
    private TrafficService trafficService;
    @Autowired
    private ThreadPoolRegulator threadPoolRegulator;
@@ -66,6 +73,59 @@
    private AvoidWaveCalculator avoidWaveCalculator;
    @Autowired
    private PatrolService patrolService;
    @Autowired
    private StaReserveService staReserveService;
    @PreAuthorize("hasAuthority('manager:agv:update')")
    @OperationLog("Locate All Agv")
    @PostMapping("/locateAllAgv")
    public synchronized R locateAllAgv() {
        final Integer MAP_DEFAULT_LEV = 1;
        redis.deleteValue(RedisConstant.AGV_MAP_ASTAR_DYNAMIC_FLAG, String.valueOf(MAP_DEFAULT_LEV));
        avoidWaveCalculator.calcDynamicNodeWhenBoot();
        return R.ok();
    }
    @PreAuthorize("hasAuthority('manager:agv:update')")
    @PostMapping("/patrol/batch/startup")
    public synchronized R patrolBatchStartup() {
        List<Agv> list = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
        DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(MapDataDispatcher.MAP_DEFAULT_LEV);
        int result = 0;
        for (Agv agv : list) {
            String agvNo = agv.getUuid();
            boolean inMap = false;
            label: for (int i = 0; i < dynamicMatrix.length; i++) {
                for (int j = 0; j < dynamicMatrix[i].length; j++) {
                    DynamicNode dynamicNode = dynamicMatrix[i][j];
                    String vehicle = dynamicNode.getVehicle();
                    if (agvNo.equals(vehicle)) {
                        inMap = true;
                        break label;
                    }
                }
            }
            if (inMap) {
                patrolService.startupPatrol(agvNo);
                result++;
            }
        }
        return R.ok().add(result);
    }
    @PreAuthorize("hasAuthority('manager:agv:update')")
    @PostMapping("/patrol/batch/shutdown")
    public synchronized R patrolBatchShutdown() {
        List<Agv> list = agvService.list(new LambdaQueryWrapper<Agv>());
        for (String agvNo : list.stream().map(Agv::getUuid).collect(Collectors.toList())) {
            if (patrolService.isPatrolling(agvNo)) {
                patrolService.shutdownPatrol(agvNo);
            }
        }
        return R.ok();
    }
    @RequestMapping(value = "/control/agv", method = {RequestMethod.GET, RequestMethod.POST})
    @Transactional
@@ -91,45 +151,64 @@
                    .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.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() + "任务保存失败");
        }
        // generate
        Loc oriLoc = null;
        Loc destLoc = null;
        Sta oriSta = null;
        Sta destSta = null;
        switch (param.getTaskMode()) {
            case MOVE:
                Code endCode = null;
                if (!Cools.isEmpty(param.getEndCode())) {
                    endCode = codeService.getById(param.getEndCode());
                    endCode = codeService.getCacheById(param.getEndCode());
                }
                if (!Cools.isEmpty(param.getEndCodeStr())) {
                    endCode = codeService.selectByData(param.getEndCodeStr());
                    endCode = codeService.getCacheByData(param.getEndCodeStr());
                }
                if (null == endCode) {
                    return R.error();
                }
                if (!mainLockWrapService.buildMinorTask(agv, agvDetail, param.getTaskMode(), endCode.getData())) {
                if (!mainLockWrapService.buildMinorTask(agv.getId(), param.getTaskMode(), endCode.getData(), null)) {
                    return R.error();
                }
                break;
            case TO_CHARGE:
            case TO_STANDBY:
                if (!mainLockWrapService.buildMinorTask(agv, agvDetail, param.getTaskMode(), null)) {
                if (!mainLockWrapService.buildMinorTask(agv.getId(), param.getTaskMode(), null, null)) {
                    return R.error();
                }
                break;
            case LOC_TO_LOC:
                // oriLoc
                Loc oriLoc = null;
                if (!Cools.isEmpty(param.getStartLocNo())) {
                    oriLoc = locService.getById(param.getStartLocNo());
                }
                if (!Cools.isEmpty(param.getStartLocNoStr())) {
                    oriLoc = locService.selecatByLocNo(param.getStartLocNoStr());
                    oriLoc = locService.selectByLocNo(param.getStartLocNoStr());
                }
                if (null == oriLoc) {
                    return R.error();
@@ -137,13 +216,18 @@
                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
                Loc destLoc = null;
                if (!Cools.isEmpty(param.getEndLocNo())) {
                    destLoc = locService.getById(param.getEndLocNo());
                }
                if (!Cools.isEmpty(param.getEndLocNoStr())) {
                    destLoc = locService.selecatByLocNo(param.getEndLocNoStr());
                    destLoc = locService.selectByLocNo(param.getEndLocNoStr());
                }
                if (null == destLoc) {
                    return R.error();
@@ -151,44 +235,140 @@
                if (!destLoc.getLocSts().equals(LocStsType.IDLE.val())) {
                    throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 不是空闲状态");
                }
                // task
                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.setOriLoc(oriLoc.getId());
                task.setDestLoc(destLoc.getId());
                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() + "任务保存失败");
                destLoc.setLocSts(LocStsType.PAKIN.val());
                destLoc.setUpdateTime(now);
                if (!locService.updateById(destLoc)) {
                    throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 修改库位状态失败");
                }
                // loc
                // task
                task.setOriLoc(oriLoc.getId());
                task.setOriCode(oriLoc.getCode());
                task.setDestLoc(destLoc.getId());
                task.setDestCode(destLoc.getCode());
                break;
            case LOC_TO_STA:
                // oriLoc
                if (!Cools.isEmpty(param.getStartLocNo())) {
                    oriLoc = locService.getById(param.getStartLocNo());
                }
                if (!Cools.isEmpty(param.getStartLocNoStr())) {
                    oriLoc = locService.selectByLocNo(param.getStartLocNoStr());
                }
                if (null == oriLoc) {
                    return R.error();
                }
                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:" + task.getOriLoc$() + " 修改库位状态失败");
                    throw new BusinessException("oriLoc:" + oriLoc.getLocNo() + " 修改库位状态失败");
                }
                // destSta
                if (!Cools.isEmpty(param.getEndStaNo())) {
                    destSta = staService.getById(param.getEndStaNo());
                }
                if (!Cools.isEmpty(param.getEndStaNoStr())) {
                    destSta = staService.selectByStaNo(param.getEndStaNoStr());
                }
                if (null == destSta) {
                    return R.error();
                }
                if (null == staReserveService.reserveStaIn(destSta, task, 1)) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 预约失败");
                }
                // task
                task.setOriLoc(oriLoc.getId());
                task.setOriCode(oriLoc.getCode());
                task.setDestSta(destSta.getId());
                task.setDestCode(destSta.getCode());
                break;
            case STA_TO_LOC:
                // oriSta
                if (!Cools.isEmpty(param.getStartStaNo())) {
                    oriSta = staService.getById(param.getStartStaNo());
                }
                if (!Cools.isEmpty(param.getStartStaNoStr())) {
                    oriSta = staService.selectByStaNo(param.getStartStaNoStr());
                }
                if (null == oriSta) {
                    return R.error();
                }
                if (null == staReserveService.reserveStaOut(oriSta, task, 1)) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 预约失败");
                }
                // destLoc
                if (!Cools.isEmpty(param.getEndLocNo())) {
                    destLoc = locService.getById(param.getEndLocNo());
                }
                if (!Cools.isEmpty(param.getEndLocNoStr())) {
                    destLoc = locService.selectByLocNo(param.getEndLocNoStr());
                }
                if (null == destLoc) {
                    return R.error();
                }
                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:" + task.getDestLoc$() + " 修改库位状态失败");
                    throw new BusinessException("destLoc:" + destLoc.getLocNo() + " 修改库位状态失败");
                }
                // task
                task.setOriSta(oriSta.getId());
                task.setOriCode(oriSta.getCode());
                task.setDestLoc(destLoc.getId());
                task.setDestCode(destLoc.getCode());
                break;
            case STA_TO_STA:
                // oriSta
                if (!Cools.isEmpty(param.getStartStaNo())) {
                    oriSta = staService.getById(param.getStartStaNo());
                }
                if (!Cools.isEmpty(param.getStartStaNoStr())) {
                    oriSta = staService.selectByStaNo(param.getStartStaNoStr());
                }
                if (null == oriSta) {
                    return R.error();
                }
                if (null == staReserveService.reserveStaOut(oriSta, task, 1)) {
                    throw new BusinessException("oriSta:" + oriSta.getStaNo() + " 预约失败");
                }
                // destSta
                if (!Cools.isEmpty(param.getEndStaNo())) {
                    destSta = staService.getById(param.getEndStaNo());
                }
                if (!Cools.isEmpty(param.getEndStaNoStr())) {
                    destSta = staService.selectByStaNo(param.getEndStaNoStr());
                }
                if (null == destSta) {
                    return R.error();
                }
                if (null == staReserveService.reserveStaIn(destSta, task, 1)) {
                    throw new BusinessException("destSta:" + destSta.getStaNo() + " 预约失败");
                }
                // task
                task.setOriSta(oriSta.getId());
                task.setOriCode(oriSta.getCode());
                task.setDestSta(destSta.getId());
                task.setDestCode(destSta.getCode());
                break;
            default:
                break;
        }
        if (!taskService.updateById(task)) {
            throw new BusinessException("任务更新失败");
        }
        return R.ok();
    }