zhang
2025-05-20 1313906bb1eb983d3beece810035e7fc28d6a92f
zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java
@@ -1,17 +1,17 @@
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.constant.LocGroupConstant;
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;
@@ -23,7 +23,6 @@
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;
@@ -69,12 +68,10 @@
    private LocService locService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @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);
        for (Bus bus : busList) {
@@ -106,7 +103,6 @@
        this.lock.unlock();
    }
//    @PostConstruct
    @EventListener(ApplicationReadyEvent.class)
    public void init() {
        try { Thread.sleep(1200); } catch (InterruptedException ignore) {}
@@ -122,7 +118,7 @@
                    for (Segment segment : segments) {
                        long startTime = System.currentTimeMillis();
                        trafficService.trigger(segment);
                        log.info("traffic calculation spend {} ms", System.currentTimeMillis() - startTime);
//                        log.info("traffic calculation spend {} ms", System.currentTimeMillis() - startTime);
                    }
                } catch (Exception e) {
@@ -150,29 +146,6 @@
        this.actionPublicThread.start();
    }
//    @Scheduled(fixedRate = 500) // 500毫秒执行一次,不同步
//    @Scheduled(fixedDelay = 500) // 500毫秒执行一次,同步
//    @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) {
//            long startTime = System.currentTimeMillis();
//            trafficService.trigger(segment);
//            log.info("traffic calculation spend {} ms", System.currentTimeMillis() - startTime);
//        }
//    }
//
//    @Scheduled(cron = "0/1 * * * * ? ")
//    private void publishAction(){
//        List<String> actionGroupIds = actionService.selectPrepareGroup();
//        for (String actionGroupId : actionGroupIds) {
//            long startTime = System.currentTimeMillis();
//            mainService.publishAction(actionGroupId);
//            log.info("publish action calculation spend {} ms", System.currentTimeMillis() - startTime);
//        }
//    }
    // patch ----------------------------------------------------------------------------------------------
@@ -216,77 +189,6 @@
        if (this.actionPublicThread != null) {
            this.actionPublicThread .interrupt();
        }
    }
    // auto loc to loc ----------------------------------------------------------------------------------------------
    @Scheduled(cron = "0/1 * * * * ? ")
    private void autoLocToLoc() {
        if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; }
        this.runLocToLoc(Collections.min(LocGroupConstant.FAR_LEFT_LOC_ROW_LIST), Collections.max(LocGroupConstant.FAR_LEFT_LOC_ROW_LIST));
        this.runLocToLoc(Collections.min(LocGroupConstant.LEFT_LOC_ROW_LIST), Collections.max(LocGroupConstant.LEFT_LOC_ROW_LIST));
        this.runLocToLoc(Collections.min(LocGroupConstant.MIDDLE_LOC_ROW_LIST), Collections.max(LocGroupConstant.MIDDLE_LOC_ROW_LIST));
        this.runLocToLoc(Collections.min(LocGroupConstant.RIGHT_LOC_ROW_LIST), Collections.max(LocGroupConstant.RIGHT_LOC_ROW_LIST));
        this.runLocToLoc(Collections.min(LocGroupConstant.FAR_RIGHT_LOC_ROW_LIST), Collections.max(LocGroupConstant.FAR_RIGHT_LOC_ROW_LIST));
    }
    private void runLocToLoc(Integer startRow, Integer endRow) {
        // 最多两组bus运行
//        if (1 < busService.count(new LambdaQueryWrapper<Bus>().in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val()))) {
//            return;
//        }
//        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) {
            return;
        }
//        int maxCapacity = agvModel.getBackpack() * agvCount;
        int maxCapacity = agvModel.getBackpack();
        // STOCK
        LambdaQueryWrapper<Loc> stockWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, LocStsType.STOCK.val());
        if (null != startRow) { stockWrapper.ge(Loc::getRow, startRow); }
        if (null != endRow) { stockWrapper.le(Loc::getRow, endRow); }
        List<Loc> stockList = locService.list(stockWrapper);
        if (Cools.isEmpty(stockList) || stockList.size() < agvModel.getBackpack()) {
            return;
        }
        Collections.shuffle(stockList);
        if (stockList.size() > maxCapacity) {
            stockList = stockList.subList(0, maxCapacity);
        }
        int numOfStockLocList = stockList.size();
        // IDLE
        LambdaQueryWrapper<Loc> idleWrapper = new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, LocStsType.IDLE.val());
        if (null != startRow) { idleWrapper.ge(Loc::getRow, startRow); }
        if (null != endRow) { idleWrapper.le(Loc::getRow, endRow); }
        List<Loc> idleList = locService.list(idleWrapper);
        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");
    }
}