From cd67e854d4715bc92a16dc7bffdbde7be1da9957 Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期四, 27 三月 2025 13:02:37 +0800
Subject: [PATCH] #

---
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java |  231 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 202 insertions(+), 29 deletions(-)

diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java
index a2cf33c..4ef5050 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/KernelScheduler.java
@@ -1,25 +1,30 @@
 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.AgvGroupConstant;
+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.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 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.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;
@@ -33,9 +38,18 @@
 @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;
     @Autowired
     private BusService busService;
     @Autowired
@@ -43,23 +57,34 @@
     @Autowired
     private MainService mainService;
     @Autowired
+    private MainLockWrapService mainLockWrapService;
+    @Autowired
     private ActionService actionService;
     @Autowired
     private SegmentService segmentService;
     @Autowired
     private TrafficService trafficService;
+    @Autowired
+    private ConfigService configService;
+    @Autowired
+    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 (!this.lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) { return; }
         List<Bus> busList = busService.selectBySts(BusStsType.RECEIVE);
         for (Bus bus : busList) {
-            mainService.infuseAgvForTask(bus);
+            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);
@@ -77,28 +102,78 @@
         }
 
         for (AgvTaskDto dto : taskDtoList) {
-            mainService.mergeMajorTask(dto.getAgvId(), dto.getTaskList());
+            mainLockWrapService.buildMajorTask(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);
-        }
+//    @PostConstruct
+    @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);
-        }
-    }
+//    @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 ----------------------------------------------------------------------------------------------
 
@@ -122,7 +197,105 @@
                     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());
+                }
+            }
         }
     }
 
+
+    @PreDestroy
+    public void shutDown(){
+        if (this.trafficCalcThread != null) {
+            this.trafficCalcThread .interrupt();
+        }
+        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(LocGroupConstant.FAR_LEFT_LOC_ROW_LIST, AgvGroupConstant.FIRST_AGV_GROUP);
+        this.runLocToLoc(LocGroupConstant.LEFT_LOC_ROW_LIST, AgvGroupConstant.SECOND_AGV_GROUP);
+        this.runLocToLoc(LocGroupConstant.MIDDLE_LOC_ROW_LIST, AgvGroupConstant.THIRD_AGV_GROUP);
+        this.runLocToLoc(LocGroupConstant.RIGHT_LOC_ROW_LIST, AgvGroupConstant.FOURTH_AGV_GROUP);
+        this.runLocToLoc(LocGroupConstant.FAR_RIGHT_LOC_ROW_LIST, AgvGroupConstant.FIFTH_AGV_GROUP);
+    }
+
+    private void runLocToLoc(List<Integer> locGroupList, List<String> agvGroupList) {
+        Integer startRow = Collections.min(locGroupList);
+        Integer endRow = Collections.max(locGroupList);
+
+        String memo = "DEMO_" + startRow + "-" + endRow;
+
+        int agvCount = agvGroupList.size();
+
+        // 鏈�澶� ? 缁刡us杩愯
+        if (agvCount <= busService.count(new LambdaQueryWrapper<Bus>()
+                .in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val())
+                .eq(Bus::getMemo, memo)
+        )) {
+            return;
+        }
+
+        AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, AgvModelType.CTU_BOX_TRANSPORT_AGV.toString()));
+        if (null == agvModel) {
+            return;
+        }
+        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, memo);
+    }
+
 }

--
Gitblit v1.9.1