|  |  | 
 |  |  | package com.zy.acs.manager.core.scheduler; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.zy.acs.framework.common.Cools; | 
 |  |  | import com.zy.acs.framework.common.SnowflakeIdWorker; | 
 |  |  | import com.zy.acs.manager.common.domain.TaskDto; | 
 |  |  | import com.zy.acs.manager.core.domain.AgvTaskDto; | 
 |  |  | import com.zy.acs.manager.core.service.MainLockWrapService; | 
 |  |  | import com.zy.acs.manager.core.service.MainService; | 
 |  |  | import com.zy.acs.manager.core.service.TrafficService; | 
 |  |  | import com.zy.acs.manager.manager.controller.param.OpenBusSubmitParam; | 
 |  |  | import com.zy.acs.manager.manager.entity.*; | 
 |  |  | import com.zy.acs.manager.manager.enums.*; | 
 |  |  | 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.ActionStsType; | 
 |  |  | 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.*; | 
 |  |  | import com.zy.acs.manager.system.service.ConfigService; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.boot.context.event.ApplicationReadyEvent; | 
 |  |  | import org.springframework.context.event.EventListener; | 
 |  |  | import org.springframework.scheduling.annotation.Scheduled; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  |  | 
 |  |  | import javax.annotation.PreDestroy; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.Collections; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.concurrent.TimeUnit; | 
 |  |  | 
 |  |  | @Component | 
 |  |  | public class KernelScheduler { | 
 |  |  |  | 
 |  |  |     public static int CORE_SCAN_FREQUENCY_MILLISECOND = 15; | 
 |  |  |  | 
 |  |  |     private static final int LOCK_TIMEOUT = 5; | 
 |  |  |  | 
 |  |  |     private final ReentrantLock lock = new ReentrantLock(Boolean.TRUE); | 
 |  |  |  | 
 |  |  |     private Thread trafficCalcThread; | 
 |  |  |  | 
 |  |  |     private Thread actionPublicThread; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private AgvService agvService; | 
 |  |  | 
 |  |  |     private LocService locService; | 
 |  |  |     @Autowired | 
 |  |  |     private AgvModelService agvModelService; | 
 |  |  |     @Autowired | 
 |  |  |     private SnowflakeIdWorker snowflakeIdWorker; | 
 |  |  |  | 
 |  |  |     @Scheduled(cron = "0/3 * * * * ? ") | 
 |  |  |     @Scheduled(cron = "0/1 * * * * ? ") | 
 |  |  |     private void startupBus() throws InterruptedException { | 
 |  |  |         if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; } | 
 |  |  | //        if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; } | 
 |  |  |         if (!this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { return; } | 
 |  |  |         List<Bus> busList = busService.selectBySts(BusStsType.RECEIVE); | 
 |  |  |         List<Bus> busList = busService.selectInSts(BusStsType.RECEIVE, BusStsType.PROGRESS); | 
 |  |  |         for (Bus bus : busList) { | 
 |  |  |             mainService.allocateTask(bus); | 
 |  |  |         } | 
 |  |  |         this.lock.unlock(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Scheduled(cron = "0/3 * * * * ? ") | 
 |  |  |     @Scheduled(cron = "0/1 * * * * ? ") | 
 |  |  |     private void calculateSeg() throws InterruptedException { | 
 |  |  |         if (!this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { return; } | 
 |  |  |         List<Task> taskList = taskService.selectBySts(TaskStsType.WAITING); | 
 |  |  | 
 |  |  |         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); | 
 |  |  |         } | 
 |  |  |     @EventListener(ApplicationReadyEvent.class) | 
 |  |  |     public void init() { | 
 |  |  |         try { Thread.sleep(1200); } catch (InterruptedException ignore) {} | 
 |  |  |         // traffic calculate | 
 |  |  |         this.trafficCalcThread = new Thread(() -> { | 
 |  |  |             while (!Thread.currentThread().isInterrupted()) { | 
 |  |  |                 try { | 
 |  |  |                     Thread.sleep(CORE_SCAN_FREQUENCY_MILLISECOND); | 
 |  |  |  | 
 |  |  |                     List<Segment> segments = segmentService.list(new LambdaQueryWrapper<Segment>() | 
 |  |  |                             .eq(Segment::getState, SegmentStateType.WAITING.toString()) | 
 |  |  |                     ); | 
 |  |  |                     for (Segment segment : segments) { | 
 |  |  |                         long startTime = System.currentTimeMillis(); | 
 |  |  |                         trafficService.trigger(segment); | 
 |  |  | //                        log.info("traffic calculation spend {} ms", System.currentTimeMillis() - startTime); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     log.error("KernelScheduler.trafficCalcThread fail", e); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |         this.trafficCalcThread.start(); | 
 |  |  |         // public action | 
 |  |  |         this.actionPublicThread = new Thread(() -> { | 
 |  |  |             while (!Thread.currentThread().isInterrupted()) { | 
 |  |  |                 try { | 
 |  |  |                     Thread.sleep(CORE_SCAN_FREQUENCY_MILLISECOND); | 
 |  |  |  | 
 |  |  |                     List<String> actionGroupIds = actionService.selectGroupNo(ActionStsType.PREPARE); | 
 |  |  |                     for (String actionGroupId : actionGroupIds) { | 
 |  |  |                         mainService.publishAction(actionGroupId); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     log.error("KernelScheduler.actionPublicThread fail", e); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |         this.actionPublicThread.start(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Scheduled(cron = "0/1 * * * * ? ") | 
 |  |  |     private void publishAction(){ | 
 |  |  |         List<String> actionGroupIds = actionService.selectPrepareGroup(); | 
 |  |  |         for (String actionGroupId : actionGroupIds) { | 
 |  |  |             mainService.publishAction(actionGroupId); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // patch ---------------------------------------------------------------------------------------------- | 
 |  |  |  | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     // auto loc to loc ---------------------------------------------------------------------------------------------- | 
 |  |  |  | 
 |  |  |     @Scheduled(cron = "0/1 * * * * ? ") | 
 |  |  |     private void autoLocToLoc() { | 
 |  |  |         if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; } | 
 |  |  |  | 
 |  |  |         if (0 < busService.count(new LambdaQueryWrapper<Bus>().in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val()))) { | 
 |  |  |             return; | 
 |  |  |     @PreDestroy | 
 |  |  |     public void shutDown(){ | 
 |  |  |         if (this.trafficCalcThread != null) { | 
 |  |  |             this.trafficCalcThread .interrupt(); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         int agvCount = agvService.count(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val)); | 
 |  |  |         AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, AgvModelType.CTU_BOX_TRANSPORT_AGV.toString())); | 
 |  |  |         if (null == agvModel || 0 == agvCount) { | 
 |  |  |             return; | 
 |  |  |         if (this.actionPublicThread != null) { | 
 |  |  |             this.actionPublicThread .interrupt(); | 
 |  |  |         } | 
 |  |  |         int maxCapacity = agvModel.getBackpack() * Math.min(agvCount, 2); | 
 |  |  |  | 
 |  |  |         // STOCK | 
 |  |  |         List<Loc> stockList = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, LocStsType.STOCK.val())); | 
 |  |  |         if (Cools.isEmpty(stockList)) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         Collections.shuffle(stockList); | 
 |  |  |         if (stockList.size() > maxCapacity) { | 
 |  |  |             stockList = stockList.subList(0, maxCapacity); | 
 |  |  |         } | 
 |  |  |         int numOfStockLocList = stockList.size(); | 
 |  |  |  | 
 |  |  |         // IDLE | 
 |  |  |         List<Loc> idleList = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, LocStsType.IDLE.val())); | 
 |  |  |         if (Cools.isEmpty(idleList)) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |         Collections.shuffle(idleList); | 
 |  |  |         if (idleList.size() > numOfStockLocList) { | 
 |  |  |             idleList = idleList.subList(0, numOfStockLocList); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         OpenBusSubmitParam param = new OpenBusSubmitParam(); | 
 |  |  |         param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19)); | 
 |  |  |         for (int i = 0; i < numOfStockLocList; i++) { | 
 |  |  |             Loc stockLoc = stockList.get(i); | 
 |  |  |             Loc idleLoc = idleList.get(i); | 
 |  |  |  | 
 |  |  |             TaskDto taskDto = new TaskDto(); | 
 |  |  |             taskDto.setOriLoc(stockLoc.getLocNo()); | 
 |  |  |             taskDto.setDestLoc(idleLoc.getLocNo()); | 
 |  |  |             taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(15, 19)); | 
 |  |  |  | 
 |  |  |             param.getTaskList().add(taskDto); | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         mainService.generateBusAndTask(param, "autoLocToLoc"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |