| package com.zy.acs.manager.core.scheduler; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.zy.acs.manager.core.domain.AgvTaskDto; | 
| import com.zy.acs.manager.core.service.MainService; | 
| import com.zy.acs.manager.core.service.TrafficService; | 
| import com.zy.acs.manager.manager.entity.Bus; | 
| import com.zy.acs.manager.manager.entity.Segment; | 
| import com.zy.acs.manager.manager.entity.Task; | 
| import com.zy.acs.manager.manager.enums.BusStsType; | 
| import com.zy.acs.manager.manager.enums.SegmentStateType; | 
| import com.zy.acs.manager.manager.enums.TaskStsType; | 
| import com.zy.acs.manager.manager.service.ActionService; | 
| import com.zy.acs.manager.manager.service.BusService; | 
| import com.zy.acs.manager.manager.service.SegmentService; | 
| import com.zy.acs.manager.manager.service.TaskService; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.scheduling.annotation.Scheduled; | 
| import org.springframework.stereotype.Component; | 
|   | 
| import java.util.ArrayList; | 
| import java.util.Date; | 
| import java.util.List; | 
| import java.util.concurrent.TimeUnit; | 
| import java.util.concurrent.locks.ReentrantLock; | 
|   | 
| /** | 
|  * to judgement agv idle status when generate action !!! | 
|  * Created by vincent on 2023/6/18 | 
|  */ | 
| @Slf4j | 
| @Component | 
| public class KernelScheduler { | 
|   | 
|     private static final int LOCK_TIMEOUT = 5; | 
|     private final ReentrantLock lock = new ReentrantLock(Boolean.TRUE); | 
|   | 
|     @Autowired | 
|     private BusService busService; | 
|     @Autowired | 
|     private TaskService taskService; | 
|     @Autowired | 
|     private MainService mainService; | 
|     @Autowired | 
|     private ActionService actionService; | 
|     @Autowired | 
|     private SegmentService segmentService; | 
|     @Autowired | 
|     private TrafficService trafficService; | 
|   | 
|     @Scheduled(cron = "0/3 * * * * ? ") | 
|     private void startupBus() throws InterruptedException { | 
|         if (!this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { return; } | 
|         List<Bus> busList = busService.selectBySts(BusStsType.RECEIVE); | 
|         for (Bus bus : busList) { | 
|             mainService.infuseAgvForTask(bus); | 
|         } | 
|         this.lock.unlock(); | 
|     } | 
|   | 
|     @Scheduled(cron = "0/3 * * * * ? ") | 
|     private void calculateSeg() throws InterruptedException { | 
|         if (!this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { return; } | 
|         List<Task> taskList = taskService.selectBySts(TaskStsType.WAITING); | 
|   | 
|         List<AgvTaskDto> taskDtoList = new ArrayList<>(); | 
|         for (Task task : taskList) { | 
|             AgvTaskDto taskDto = new AgvTaskDto(task.getAgvId(), task); | 
|             if (AgvTaskDto.has(taskDtoList, taskDto)) { | 
|                 AgvTaskDto dto = AgvTaskDto.find(taskDtoList, taskDto); | 
|                 assert dto != null; | 
|                 dto.getTaskList().add(task); | 
|             } else { | 
|                 taskDtoList.add(taskDto); | 
|             } | 
|         } | 
|   | 
|         for (AgvTaskDto dto : taskDtoList) { | 
|             mainService.mergeMajorTask(dto.getAgvId(), dto.getTaskList()); | 
|         } | 
|         this.lock.unlock(); | 
|     } | 
|   | 
|     @Scheduled(cron = "0/1 * * * * ? ") | 
|     private void traffic() { | 
|         List<Segment> segments = segmentService.list(new LambdaQueryWrapper<Segment>() | 
|                 .eq(Segment::getState, SegmentStateType.WAITING.toString()) | 
|         ); | 
|         for (Segment segment : segments) { | 
|             trafficService.trigger(segment); | 
|         } | 
|     } | 
|   | 
|     @Scheduled(cron = "0/1 * * * * ? ") | 
|     private void publishAction(){ | 
|         List<String> actionGroupIds = actionService.selectPrepareGroup(); | 
|         for (String actionGroupId : actionGroupIds) { | 
|             mainService.publishAction(actionGroupId); | 
|         } | 
|     } | 
|   | 
|     // patch ---------------------------------------------------------------------------------------------- | 
|   | 
|     @Scheduled(cron = "0/3 * * * * ? ") | 
|     private void busFinishPatch(){ | 
|         List<Bus> busList = busService.selectBySts(BusStsType.PROGRESS); | 
|         for (Bus bus : busList) { | 
|             boolean finish = true; | 
|             List<Task> taskList = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getBusId, bus.getId())); | 
|             for (Task task : taskList) { | 
|                 if (!task.getTaskSts().equals(TaskStsType.COMPLETE.val())) { | 
|                     finish = false; | 
|                     break; | 
|                 } | 
|             } | 
|             if (finish) { | 
|                 bus.setBusSts(BusStsType.FINISH.val()); | 
|                 bus.setEndTime(new Date()); | 
|                 bus.setUpdateTime(new Date()); | 
|                 if (!busService.updateById(bus)) { | 
|                     log.error("Bus [{}] 更新失败 !!!", bus.getUuid()); | 
|                 } | 
|             } | 
|             long cancelNum = taskList.stream().filter(task -> TaskStsType.CANCEL.val() == task.getTaskSts()).count(); | 
|             if (cancelNum == taskList.size()) { | 
|                 bus.setBusSts(BusStsType.CANCEL.val()); | 
|                 bus.setUpdateTime(new Date()); | 
|                 if (!busService.updateById(bus)) { | 
|                     log.error("Bus [{}] 更新失败 !!!", bus.getUuid()); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
| } |