| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.zy.acs.common.utils.RedisSupport; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.framework.common.R; |
| | | import com.zy.acs.manager.core.constant.AgvAreaDispatcher; |
| | | import com.zy.acs.manager.core.cache.CoreCache; |
| | | import com.zy.acs.manager.core.domain.CodeStepDto; |
| | | import com.zy.acs.manager.core.domain.type.JobType; |
| | | import com.zy.acs.manager.manager.entity.Agv; |
| | | import com.zy.acs.manager.manager.entity.AgvDetail; |
| | | import com.zy.acs.manager.manager.entity.Code; |
| | | import com.zy.acs.manager.manager.entity.Task; |
| | | import com.zy.acs.manager.manager.entity.Travel; |
| | | import com.zy.acs.manager.manager.enums.StatusType; |
| | | import com.zy.acs.manager.manager.enums.TaskStsType; |
| | | import com.zy.acs.manager.manager.enums.TaskTypeType; |
| | | import com.zy.acs.manager.manager.service.AgvDetailService; |
| | | import com.zy.acs.manager.manager.service.AgvService; |
| | | import com.zy.acs.manager.manager.service.CodeService; |
| | | import com.zy.acs.manager.manager.service.TaskService; |
| | | import com.zy.acs.manager.manager.enums.TravelStateType; |
| | | import com.zy.acs.manager.manager.service.*; |
| | | import com.zy.acs.manager.system.service.ConfigService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.PreDestroy; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * Created by vincent on 11/9/2024 |
| | |
| | | @Service |
| | | public class PatrolService { |
| | | |
| | | private static final int SCHEDULE_TIME_INTERVAL = 5; |
| | | private static final int SCHEDULE_TIME_INTERVAL = 100; |
| | | |
| | | private static final Map<String, ScheduledFuture<?>> AGV_PATROL_MAP = new ConcurrentHashMap<>(); |
| | | public static final Map<String, ScheduledFuture<?>> AGV_PATROL_MAP = new ConcurrentHashMap<>(); |
| | | |
| | | private final RedisSupport redis = RedisSupport.defaultRedisSupport; |
| | | |
| | | private ScheduledExecutorService scheduler = null; |
| | | |
| | | private List<String> CODE_DATA_CACHE = new ArrayList<>(); |
| | | |
| | | private List<String> LOC_CODE_DATA_CACHE = new ArrayList<>(); |
| | | |
| | | private List<String> CONVEYOR_DROP_CODE_DATA_CACHE = new ArrayList<String>(){{ |
| | | add("00000048"); |
| | | }}; |
| | | |
| | | private List<String> CONVEYOR_PICK_CODE_DATA_CACHE = new ArrayList<String>(){{ |
| | | add("00000146"); |
| | | }}; |
| | | |
| | | @Autowired |
| | | private AgvService agvService; |
| | |
| | | @Autowired |
| | | private MapService mapService; |
| | | @Autowired |
| | | private LaneService laneService; |
| | | private TravelService travelService; |
| | | @Autowired |
| | | private AllocateService allocateService; |
| | | @Autowired |
| | | private ConfigService configService; |
| | | @Autowired |
| | | private AgvModelService agvModelService; |
| | | |
| | | // pick & drop logic -------------------------------------------------- |
| | | |
| | | private void executeUShapeConveyor(String agvNo) { |
| | | Long agvId = agvService.getAgvId(agvNo); |
| | | if (0 < travelService.count(new LambdaQueryWrapper<Travel>() |
| | | .eq(Travel::getAgvId, agvId) |
| | | .eq(Travel::getState, TravelStateType.RUNNING.toString()))) { |
| | | return; |
| | | } |
| | | if (!agvService.judgeEnable(agvId)) { |
| | | return; |
| | | } |
| | | |
| | | JobType jobType = CoreCache.AGV_MOCK_JOB_CACHE.get(agvNo); |
| | | if (null == jobType) { |
| | | CoreCache.AGV_MOCK_JOB_CACHE.put(agvNo, JobType.LOC_PICK); |
| | | CoreCache.AGV_BACKPACK_USED_CACHE.put(agvId, 0); |
| | | jobType = JobType.LOC_PICK; |
| | | } |
| | | |
| | | String destinationCodeData = null; |
| | | switch (jobType) { |
| | | case LOC_PICK: |
| | | // 5, 4, 3, 2, 1 |
| | | int locPickRemaining = agvService.getBackpackRemainingCapacity(agvId); |
| | | if (0 < locPickRemaining) { |
| | | destinationCodeData = this.getLocCode(agvNo, null); |
| | | } |
| | | break; |
| | | case CONVEYOR_DROP: |
| | | destinationCodeData = this.getConveyorDropCode(agvNo); |
| | | break; |
| | | case CONVEYOR_PICK: |
| | | destinationCodeData = this.getConveyorPickCode(agvNo); |
| | | break; |
| | | case LOCK_DROP: |
| | | // 0, 1, 2, 3, 4 |
| | | int locDropRemaining = agvService.getBackpackRemainingCapacity(agvId); |
| | | if (locDropRemaining < agvModelService.getByAgvId(agvId).getBackpack()) { |
| | | destinationCodeData = this.getLocCode(agvNo, null); |
| | | } |
| | | break; |
| | | default: |
| | | log.warn("AGV {}: 未知阶段: {}", agvNo, jobType); |
| | | break; |
| | | } |
| | | if (Cools.isEmpty(destinationCodeData)) { |
| | | return; |
| | | } |
| | | |
| | | if (mainLockWrapService.buildMinorTask(agvId, TaskTypeType.MOVE, destinationCodeData, null)) { |
| | | log.info( "{}开始作业演示...", agvNo); |
| | | |
| | | CoreCache.AGV_MOCK_STEP_CACHE.put(agvId, CodeStepDto.build(destinationCodeData, jobType)); |
| | | } |
| | | } |
| | | |
| | | public String getLocCode(String agvNo, AgvDetail agvDetail) { |
| | | if (LOC_CODE_DATA_CACHE.isEmpty()) { |
| | | return null; |
| | | } |
| | | Collections.shuffle(LOC_CODE_DATA_CACHE); |
| | | for (String locCodeData : LOC_CODE_DATA_CACHE) { |
| | | Code locCode = codeService.getCacheByData(locCodeData); |
| | | |
| | | // valid lane |
| | | if (!allocateService.validCapacityOfLane(agvNo, locCode)) { |
| | | continue; |
| | | } |
| | | |
| | | return locCodeData; |
| | | } |
| | | return LOC_CODE_DATA_CACHE.stream().findFirst().orElse(null); |
| | | } |
| | | |
| | | public String getConveyorDropCode(String agvNo) { |
| | | return CONVEYOR_DROP_CODE_DATA_CACHE.stream().findFirst().orElse(null); |
| | | } |
| | | |
| | | public String getConveyorPickCode(String agvNo) { |
| | | return CONVEYOR_PICK_CODE_DATA_CACHE.stream().findFirst().orElse(null); |
| | | } |
| | | |
| | | |
| | | // pure move logic -------------------------------------------------- |
| | | |
| | | private void executePatrolLogic(String agvNo) { |
| | | this.patrolOfMove(agvNo); |
| | | } |
| | | |
| | | private void patrolOfMove(String agvNo) { |
| | | Agv agv = agvService.selectByUuid(agvNo); |
| | | AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId()); |
| | | if (taskService.count(new LambdaQueryWrapper<Task>() |
| | | .eq(Task::getAgvId, agv.getId()) |
| | | .and(i -> { |
| | | i.eq(Task::getTaskSts, TaskStsType.WAITING.val()) |
| | | .or().eq(Task::getTaskSts, TaskStsType.ASSIGN.val()) |
| | | .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val()); |
| | | })) > 0) { |
| | | Long agvId = agvService.getAgvId(agvNo); |
| | | if (0 < travelService.count(new LambdaQueryWrapper<Travel>() |
| | | .eq(Travel::getAgvId, agvId) |
| | | .eq(Travel::getState, TravelStateType.RUNNING.toString()))) { |
| | | return; |
| | | } |
| | | if (!agvService.judgeEnable(agv.getId())) { |
| | | if (!agvService.judgeEnable(agvId)) { |
| | | return; |
| | | } |
| | | Code destinationCode = this.getDestinationCode(agv, agvDetail); |
| | | if (null == destinationCode) { |
| | | AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(agvId); |
| | | String destinationCodeData = this.getDestinationCode(agvNo, agvDetail); |
| | | if (Cools.isEmpty(destinationCodeData)) { |
| | | return; |
| | | } |
| | | if (mainLockWrapService.buildMinorTask(agv, TaskTypeType.MOVE, destinationCode.getData(), null)) { |
| | | log.info(agv.getUuid() + "开始走行演示..."); |
| | | if (mainLockWrapService.buildMinorTask(agvId, TaskTypeType.MOVE, destinationCodeData, null)) { |
| | | log.info( "{}开始走行演示...", agvNo); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 4个地方调用了buildMinorTask,在什么时候、哪里设置task的lane |
| | | * ( |
| | | * HandlerController, 手动 (手动是否需要判断lane) |
| | | * MaintainScheduler, 自动 (一般不需要考虑 lane) |
| | | * PatrolService, 自动 (需要预处理 lane) ✔ |
| | | * TrafficService, 自动 (寻址时已经处理过 lane) ✔ |
| | | * HandlerController, 手动 (手动是否需要判断lane) |
| | | * MaintainScheduler, 自动 (一般不需要考虑 lane) |
| | | * PatrolService, 自动 (需要预处理 lane) ✔ |
| | | * TrafficService, 自动 (寻址时已经处理过 lane) ✔ |
| | | * ) |
| | | * 评估HandlerController没有调用buildMajorTask,手动创建task的可行性 |
| | | * agv地图图标变化 |
| | | */ |
| | | public Code getDestinationCode(Agv agv, AgvDetail agvDetail) { |
| | | Integer maxAgvCountInLane = configService.getVal("maxAgvCountInLane", Integer.class); |
| | | public String getDestinationCode(String agvNo, AgvDetail agvDetail) { |
| | | |
| | | Code startCode = codeService.getById(agvDetail.getRecentCode()); |
| | | Code startCode = codeService.getCacheById(agvDetail.getRecentCode()); |
| | | |
| | | List<String> notInCodeList = new ArrayList<>(); |
| | | notInCodeList.add("00000301"); |
| | | notInCodeList.add("00000302"); |
| | | notInCodeList.add("00000303"); |
| | | notInCodeList.add("00000351"); |
| | | notInCodeList.add("00000353"); |
| | | notInCodeList.add("00000402"); |
| | | List<String> codeList = AgvAreaDispatcher.AGV_AREA.get(agvNo); |
| | | List<String> disableCodeList = AgvAreaDispatcher.AGV_DISABLE_AREA.get(agvNo); |
| | | if (!Cools.isEmpty(codeList)) { |
| | | Collections.shuffle(codeList); |
| | | } |
| | | |
| | | notInCodeList.add("00000311"); |
| | | notInCodeList.add("00000312"); |
| | | notInCodeList.add("00000313"); |
| | | notInCodeList.add("00000361"); |
| | | notInCodeList.add("00000363"); |
| | | notInCodeList.add("00000412"); |
| | | List<Code> list = codeService.list(new LambdaQueryWrapper<Code>().notIn(Code::getData, notInCodeList)); |
| | | for (String endCodeData : codeList) { |
| | | if (disableCodeList.contains(endCodeData)) { continue; } |
| | | Code endCode = codeService.getCacheByData(endCodeData); |
| | | |
| | | Collections.shuffle(list); |
| | | |
| | | for (Code endCode : list) { |
| | | // valid lane |
| | | if (!allocateService.validCapacityOfLane(agv, endCode)) { |
| | | if (!allocateService.validCapacityOfLane(agvNo, endCode)) { |
| | | continue; |
| | | } |
| | | |
| | | // valid path length |
| | | List<String> pathList = mapService.validFeasibility(startCode, endCode); |
| | | if (pathList.size() >= 5) { |
| | | return endCode; |
| | | return endCodeData; |
| | | } |
| | | } |
| | | |
| | | return list.stream().findFirst().orElse(null); |
| | | return codeList.stream().findFirst().orElse(null); |
| | | } |
| | | |
| | | |
| | | // --------------------------------------------------------------------------- |
| | | |
| | |
| | | Runnable patrolTask = () -> { |
| | | try { |
| | | executePatrolLogic(agvNo); |
| | | // executeUShapeConveyor(agvNo); |
| | | } catch (Exception e) { |
| | | log.error("执行AGV " + agvNo + " 跑库任务时发生异常: " + e.getMessage()); |
| | | e.printStackTrace(); |
| | | log.error("执行AGV{}跑库任务时发生异常", agvNo, e); |
| | | } |
| | | }; |
| | | |
| | | ScheduledFuture<?> scheduledFuture = scheduler.scheduleAtFixedRate(patrolTask, 0, SCHEDULE_TIME_INTERVAL, TimeUnit.SECONDS); |
| | | ScheduledFuture<?> scheduledFuture = scheduler.scheduleAtFixedRate(patrolTask, 0, SCHEDULE_TIME_INTERVAL, TimeUnit.MILLISECONDS); |
| | | |
| | | AGV_PATROL_MAP.put(agvNo, scheduledFuture); |
| | | log.info("已启动AGV " + agvNo + " 的跑库任务。"); |
| | | log.info("已启动AGV{}的跑库任务。", agvNo); |
| | | return R.ok(); |
| | | } |
| | | |
| | | |
| | | public R shutdownPatrol(String agvNo) { |
| | | Long agvId = agvService.getAgvId(agvNo); |
| | | ScheduledFuture<?> scheduledFuture = AGV_PATROL_MAP.get(agvNo); |
| | | if (scheduledFuture == null) { |
| | | return R.error("AGV " + agvNo + " 没有正在运行的跑库任务。"); |
| | |
| | | boolean cancelled = scheduledFuture.cancel(true); |
| | | if (cancelled) { |
| | | AGV_PATROL_MAP.remove(agvNo); |
| | | log.info("已停止AGV " + agvNo + " 的跑库任务。"); |
| | | CoreCache.AGV_BACKPACK_USED_CACHE.remove(agvId); |
| | | CoreCache.AGV_MOCK_STEP_CACHE.remove(agvId); |
| | | CoreCache.AGV_MOCK_JOB_CACHE.remove(agvNo); |
| | | log.info("已停止AGV {} 的跑库任务。", agvNo); |
| | | return R.ok("已停止AGV " + agvNo + " 的跑库任务。"); |
| | | } else { |
| | | log.error("未能成功停止AGV " + agvNo + " 的跑库任务。"); |
| | |
| | | if (count > 0) { |
| | | this.scheduler = Executors.newScheduledThreadPool(count); |
| | | } |
| | | // init all code data |
| | | List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, StatusType.ENABLE.val)); |
| | | this.CODE_DATA_CACHE = codeList.stream().map(Code::getData).distinct().collect(Collectors.toList()); |
| | | |
| | | // int all loc code data |
| | | List<Code> locCodeList = codeService.getAllLocCode(); |
| | | this.LOC_CODE_DATA_CACHE = locCodeList.stream().map(Code::getData).distinct().collect(Collectors.toList()); |
| | | |
| | | } |
| | | |
| | | @PreDestroy |