package com.zy.acs.manager.core.service; 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.manager.entity.*; 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.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 org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; /** * Created by vincent on 11/9/2024 */ @Slf4j @Service public class PatrolService { private static final int SCHEDULE_TIME_INTERVAL = 100; public static final Map> AGV_PATROL_MAP = new ConcurrentHashMap<>(); private final RedisSupport redis = RedisSupport.defaultRedisSupport; private ScheduledExecutorService scheduler = null; private List CODE_DATA_CACHE = new ArrayList<>(); @Autowired private AgvService agvService; @Autowired private AgvDetailService agvDetailService; @Autowired private TaskService taskService; @Autowired private MainLockWrapService mainLockWrapService; @Autowired private CodeService codeService; @Autowired private MapService mapService; @Autowired private TravelService travelService; @Autowired private AllocateService allocateService; @Autowired private ConfigService configService; private void executePatrolLogic(String agvNo) { this.patrolOfMove(agvNo); } private void patrolOfMove(String agvNo) { Long agvId = agvService.getAgvId(agvNo); if (0 < travelService.count(new LambdaQueryWrapper() .eq(Travel::getAgvId, agvId) .eq(Travel::getState, TravelStateType.RUNNING.toString()))) { return; } if (!agvService.judgeEnable(agvId)) { return; } AgvDetail agvDetail = agvDetailService.selectMajorByAgvId(agvId); String destinationCodeData = this.getDestinationCode(agvNo, agvDetail); if (Cools.isEmpty(destinationCodeData)) { return; } if (mainLockWrapService.buildMinorTask(agvId, TaskTypeType.MOVE, destinationCodeData, null)) { log.info(agvNo + "开始走行演示..."); } } /** * 4个地方调用了buildMinorTask,在什么时候、哪里设置task的lane * ( * HandlerController, 手动 (手动是否需要判断lane) * MaintainScheduler, 自动 (一般不需要考虑 lane) * PatrolService, 自动 (需要预处理 lane) ✔ * TrafficService, 自动 (寻址时已经处理过 lane) ✔ * ) * 评估HandlerController没有调用buildMajorTask,手动创建task的可行性 * agv地图图标变化 */ public String getDestinationCode(String agvNo, AgvDetail agvDetail) { Code startCode = codeService.getCacheById(agvDetail.getRecentCode()); Set notInCodeSet = new HashSet<>(); notInCodeSet.add("00000301"); notInCodeSet.add("00000302"); notInCodeSet.add("00000303"); notInCodeSet.add("00000351"); notInCodeSet.add("00000353"); notInCodeSet.add("00000401"); notInCodeSet.add("00000402"); notInCodeSet.add("00000311"); notInCodeSet.add("00000312"); notInCodeSet.add("00000313"); notInCodeSet.add("00000361"); notInCodeSet.add("00000363"); notInCodeSet.add("00000411"); notInCodeSet.add("00000412"); notInCodeSet.add("00000046"); notInCodeSet.add("00000047"); Collections.shuffle(CODE_DATA_CACHE); for (String endCodeData : CODE_DATA_CACHE) { if (notInCodeSet.contains(endCodeData)) { continue; } Code endCode = codeService.getCacheByData(endCodeData); // valid lane if (!allocateService.validCapacityOfLane(agvNo, endCode)) { continue; } // valid path length List pathList = mapService.validFeasibility(startCode, endCode); if (pathList.size() >= 5) { return endCodeData; } } return CODE_DATA_CACHE.stream().findFirst().orElse(null); } // --------------------------------------------------------------------------- public boolean isPatrolling(String agvNo) { ScheduledFuture scheduledFuture = AGV_PATROL_MAP.get(agvNo); if (scheduledFuture == null) { return false; } return !scheduledFuture.isCancelled() && !scheduledFuture.isDone(); } public R startupPatrol(String agvNo) { if (AGV_PATROL_MAP.containsKey(agvNo)) { return R.error("AGV " + agvNo + " 的跑库任务已经在运行中。"); } Runnable patrolTask = () -> { try { executePatrolLogic(agvNo); } catch (Exception e) { log.error("执行AGV " + agvNo + " 跑库任务时发生异常: " + e.getMessage()); e.printStackTrace(); } }; ScheduledFuture scheduledFuture = scheduler.scheduleAtFixedRate(patrolTask, 0, SCHEDULE_TIME_INTERVAL, TimeUnit.MILLISECONDS); AGV_PATROL_MAP.put(agvNo, scheduledFuture); log.info("已启动AGV " + agvNo + " 的跑库任务。"); return R.ok(); } public R shutdownPatrol(String 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 + " 的跑库任务。"); return R.ok("已停止AGV " + agvNo + " 的跑库任务。"); } else { log.error("未能成功停止AGV " + agvNo + " 的跑库任务。"); return R.error("未能成功停止AGV " + agvNo + " 的跑库任务。"); } } @PostConstruct public void init() { int count = agvService.count(new LambdaQueryWrapper().eq(Agv::getStatus, StatusType.ENABLE.val)); if (count > 0) { this.scheduler = Executors.newScheduledThreadPool(count); } List codeList = codeService.list(new LambdaQueryWrapper().eq(Code::getStatus, StatusType.ENABLE.val)); this.CODE_DATA_CACHE = codeList.stream().map(Code::getData).distinct().collect(Collectors.toList()); } @PreDestroy public void destroy() throws InterruptedException { for (Map.Entry> entry : AGV_PATROL_MAP.entrySet()) { entry.getValue().cancel(true); } scheduler.shutdown(); if (!scheduler.awaitTermination(5, TimeUnit.SECONDS)) { scheduler.shutdownNow(); } } }