zhangc
7 天以前 4224381232a6c235fe0e0f3c9a483de1cffd23d2
跑库测试代码,与业务无关
2个文件已添加
448 ■■■■■ 已修改文件
zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/AutoRunRebootScheduler.java 299 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/AutoTestDeviationScheduler.java 149 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/AutoRunRebootScheduler.java
New file
@@ -0,0 +1,299 @@
package com.zy.acs.manager.core.scheduler;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.constant.RedisConstant;
import com.zy.acs.common.utils.RedisSupport;
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.service.AreaGovernService;
import com.zy.acs.manager.core.service.MainLockWrapService;
import com.zy.acs.manager.core.service.MainService;
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.manager.service.impl.CodeServiceImpl;
import com.zy.acs.manager.system.service.ConfigService;
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.*;
@Slf4j
@Component
public class AutoRunRebootScheduler {
    private static final AgvModelType DEFAULT_AGV_MODEL = AgvModelType.HEAVY_LOAD_STACKING_ROBOT;
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
    @Autowired
    private AgvService agvService;
    @Autowired
    private BusService busService;
    @Autowired
    private MainService mainService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private LocService locService;
    @Autowired
    private StaService staService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private AreaGovernService areaGovernService;
    @Autowired
    private CodeServiceImpl codeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private MainLockWrapService mainLockWrapService;
    @Autowired
    private AgvDetailService agvDetailService;
    //    @Scheduled(fixedRate = 500) // 固定频率执行,不同步
    @Scheduled(fixedDelay = 1000) // 固定频率执行,同步
//    @Scheduled(cron = "0/1 * * * * ? ")
    private void execute() {
        if (!configService.getVal("TaskAssignRebootMode", Boolean.class)) { return; }
        AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, DEFAULT_AGV_MODEL.toString()));
        if (null == agvModel) { return; }
        Agv agv = agvService.selectByUuid("3");
        // check
        if (taskService.count(new LambdaQueryWrapper<Task>()
                .eq(Task::getAgvId, agv.getId())
                .and(i -> {
                    i.eq(Task::getTaskSts, TaskStsType.WAITING.val())
                            .or()
                            .eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
                            .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val());
                })) > 0) {
            log.info("{}号AGV不可用,已经存在进行中的任务...", agv.getUuid());
            return ;
        }
        if (!agvService.judgeEnable(agv.getId())) {
            log.info("{}号AGV不可用,任务无法计算...", agv.getUuid());
            return ;
        }
        AgvDetail agvDetail = agvDetailService.selectByAgvId(agv.getId());
        if (agvDetail.getPos()==0){
            return;
        }
        Code cacheById = codeService.getCacheById((long) ((Math.random()*74)+1));
        if(cacheById.getStatus()==1&&!agvDetail.getCode$().equals(cacheById.getData())){
            mainLockWrapService.buildMinorTask(agv.getId(), TaskTypeType.MOVE, cacheById.getData(), null);
        }
    }
    private void autoRun(AgvModel agvModel) {
        int availableAgvCount = this.getAvailableAgvCount();
        if (0 == availableAgvCount) { return; }
//        List<String> staPreNos = getStaPrefixes(staGroupList);
        List<String> staPreNos = new ArrayList<>();
        String memo = "DEMO_STA_" + String.join("-", staPreNos);
        // 入库
        //this.runStaToLoc(agvModel, memo);
        // 出库
        //this.runLocToSta(agvModel, memo);
        // 移库
        this.runLocToLoc(agvModel, memo);
    }
    // 入库
    private void runStaToLoc(AgvModel agvModel, String memo) {
        // STOCK STA
        List<Sta> stockList = staService.queryAvailableOutSta(1);
        if (Cools.isEmpty(stockList)) { return; }
        Collections.shuffle(stockList);
        OpenBusSubmitParam param = new OpenBusSubmitParam();
        param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
        for (int i = 0; i < Math.min(agvModel.getBackpack(), stockList.size()) ; i++) {
            Sta stockSta = stockList.get(i);
            String staCode = codeService.getCacheById(stockSta.getCode()).getData();
            Loc idleLoc = null;
            // 所在区域的条码,如果没有area绑定,就全地图
            List<String> codeList = areaGovernService.queryCodesByOneCode(staCode);
            if (Cools.isEmpty(codeList)) {
                idleLoc = locService.selectRandOneByLocSts(LocStsType.IDLE.val(), 1);
            } else {
                Collections.shuffle(codeList);
                for (String codeData : codeList) {
                    Code code = codeService.getCacheByData(codeData);
                    if (null == code) { break; }
                    List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                            .eq(Loc::getCode, code.getId()).eq(Loc::getLocSts, LocStsType.IDLE.val()));
                    if (Cools.isEmpty(locList)) {
                        break;
                    } else if (locList.size() == 1) {
                        idleLoc = locList.get(0);
                    } else {
                        Collections.shuffle(locList);
                        idleLoc = locList.get(0);
                    }
                    if (null != idleLoc) {
                        break;
                    }
                }
            }
            if (null == idleLoc) { break; }
            TaskDto taskDto = new TaskDto();
            taskDto.setOriSta(stockSta.getStaNo());
            taskDto.setDestLoc(idleLoc.getLocNo());
            taskDto.setPriority(100);
            taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(15, 19));
            param.getTaskList().add(taskDto);
        }
        if (Cools.isEmpty(param.getTaskList())) { return; }
        mainService.generateBusAndTask(param, memo);
    }
    // 出库
    private void runLocToSta(AgvModel agvModel, String memo) {
        // IDLE STA
        List<Sta> idleList = staService.queryAvailableInSta(1);
        if (Cools.isEmpty(idleList)) { return; }
        Collections.shuffle(idleList);
        OpenBusSubmitParam param = new OpenBusSubmitParam();
        param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
        for (int i = 0; i < Math.min(agvModel.getBackpack(), idleList.size()) ; i++) {
            Sta idleSta = idleList.get(i);
            String staCode = codeService.getCacheById(idleSta.getCode()).getData();
            Loc stockLoc = null;
            // 所在区域的条码,如果没有area绑定,就全地图
            List<String> codeList = areaGovernService.queryCodesByOneCode(staCode);
            if (Cools.isEmpty(codeList)) {
                stockLoc = locService.selectRandOneByLocSts(LocStsType.STOCK.val(), 1);
            } else {
                Collections.shuffle(codeList);
                for (String codeData : codeList) {
                    Code code = codeService.getCacheByData(codeData);
                    if (null == code) { break; }
                    List<Loc> locList = locService.list(new LambdaQueryWrapper<Loc>()
                            .eq(Loc::getCode, code.getId()).eq(Loc::getLocSts, LocStsType.STOCK.val()));
                    if (Cools.isEmpty(locList)) {
                        break;
                    } else if (locList.size() == 1) {
                        stockLoc = locList.get(0);
                    } else {
                        Collections.shuffle(locList);
                        stockLoc = locList.get(0);
                    }
                    if (null != stockLoc) {
                        break;
                    }
                }
            }
            if (null == stockLoc) { break; }
            TaskDto taskDto = new TaskDto();
            taskDto.setOriLoc(stockLoc.getLocNo());
            taskDto.setDestSta(idleSta.getStaNo());
            taskDto.setPriority(100);
            taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(15, 19));
            param.getTaskList().add(taskDto);
        }
        if (Cools.isEmpty(param.getTaskList())) { return; }
        mainService.generateBusAndTask(param, memo);
    }
    // 移库
    private void runLocToLoc(AgvModel agvModel, String staTaskMemo) {
        String memo = "DEMO_LOC";
        int availableAgvCount = this.getAvailableAgvCount();
        // 最多 ? 组bus运行
        if (availableAgvCount <= busService.count(new LambdaQueryWrapper<Bus>()
                .in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val())
                .in(Bus::getMemo, memo, staTaskMemo)
        )) { return; }
        int maxCapacity = agvModel.getBackpack();
        // STOCK
        List<Loc> stockLocList = locService.selectRandByLocSts(LocStsType.STOCK.val(), maxCapacity);
        if (Cools.isEmpty(stockLocList)) {
            return;
        }
        Collections.shuffle(stockLocList);
        // IDLE
        List<Loc> idleLocList = locService.selectRandByLocSts(LocStsType.IDLE.val(), maxCapacity);
        if (Cools.isEmpty(idleLocList)) {
            return;
        }
        Collections.shuffle(idleLocList);
        OpenBusSubmitParam param = new OpenBusSubmitParam();
        param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
        for (int i = 0; i < Math.min(maxCapacity, Math.min(stockLocList.size(), idleLocList.size())); i++) {
            Loc stockLoc = stockLocList.get(i);
            Loc idleLoc = idleLocList.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);
        }
        if (Cools.isEmpty(param.getTaskList())) { return; }
        mainService.generateBusAndTask(param, memo);
    }
    public static List<String> getStaPrefixes(List<String> staGroupList) {
        Set<String> rowSet = new HashSet<>();
        for (String s : staGroupList) {
            rowSet.add(s.split("-")[0]);
        }
        List<String> result = new ArrayList<>(rowSet);
        result.sort(Comparator.comparingInt(Integer::parseInt));
        return result;
    }
    private int getAvailableAgvCount() {
        int res = 0;
        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
        if (Cools.isEmpty(agvList)) {
            return res;
        }
        for (Agv agv : agvList) {
            if (null == redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agv.getUuid())) {
                continue;
            }
            if (!agv.getStatusBool()) {
                continue;
            }
            res++;
        }
        return res;
    }
}
zy-acs-manager/src/main/java/com/zy/acs/manager/core/scheduler/AutoTestDeviationScheduler.java
New file
@@ -0,0 +1,149 @@
package com.zy.acs.manager.core.scheduler;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.acs.common.constant.RedisConstant;
import com.zy.acs.common.utils.RedisSupport;
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.service.AreaGovernService;
import com.zy.acs.manager.core.service.MainService;
import com.zy.acs.manager.manager.controller.param.OpenBusSubmitParam;
import com.zy.acs.manager.manager.entity.*;
import com.zy.acs.manager.manager.enums.AgvModelType;
import com.zy.acs.manager.manager.enums.BusStsType;
import com.zy.acs.manager.manager.enums.LocStsType;
import com.zy.acs.manager.manager.enums.StatusType;
import com.zy.acs.manager.manager.service.*;
import com.zy.acs.manager.manager.service.impl.CodeServiceImpl;
import com.zy.acs.manager.system.service.ConfigService;
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.*;
@Slf4j
@Component
public class AutoTestDeviationScheduler {
    private static final AgvModelType DEFAULT_AGV_MODEL = AgvModelType.HEAVY_LOAD_STACKING_ROBOT;
    private final RedisSupport redis = RedisSupport.defaultRedisSupport;
    @Autowired
    private AgvService agvService;
    @Autowired
    private BusService busService;
    @Autowired
    private MainService mainService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private LocService locService;
    @Autowired
    private StaService staService;
    @Autowired
    private AgvModelService agvModelService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private AreaGovernService areaGovernService;
    @Autowired
    private CodeServiceImpl codeService;
    /**
     * 循环跑库,测试偏差
     */
    //    @Scheduled(fixedRate = 500) // 固定频率执行,不同步
    //@Scheduled(fixedDelay = 1000) // 固定频率执行,同步
//    @Scheduled(cron = "0/1 * * * * ? ")
    private void execute() {
        if (!configService.getVal("TestDeviationMode", Boolean.class)) { return; }
        AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, DEFAULT_AGV_MODEL.toString()));
        if (null == agvModel) { return; }
        this.autoRun(agvModel);
    }
    private void autoRun(AgvModel agvModel) {
        int availableAgvCount = this.getAvailableAgvCount();
        if (0 == availableAgvCount) { return; }
//        List<String> staPreNos = getStaPrefixes(staGroupList);
        List<String> staPreNos = new ArrayList<>();
        String memo = "DEMO_STA_" + String.join("-", staPreNos);
        // 移库
        this.runLocToLoc(agvModel, memo);
    }
    // 移库
    private void runLocToLoc(AgvModel agvModel, String staTaskMemo) {
        String memo = "DEMO_LOC";
        int availableAgvCount = this.getAvailableAgvCount();
        // 最多 ? 组bus运行
        if (availableAgvCount <= busService.count(new LambdaQueryWrapper<Bus>()
                .in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val())
                .in(Bus::getMemo, memo, staTaskMemo)
        )) { return; }
        int maxCapacity = agvModel.getBackpack();
        // STOCK
        List<Loc> stockLocList = locService.selectRandByLocSts(LocStsType.STOCK.val(), maxCapacity);
        if (Cools.isEmpty(stockLocList)) {
            return;
        }
        Collections.shuffle(stockLocList);
        // IDLE
        List<Loc> idleLocList = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getStatus,1).eq(Loc::getLocSts,LocStsType.IDLE.val()).ne(Loc::getMemo,"1").orderByAsc(Loc::getRow).orderByAsc(Loc::getBay).orderByAsc(Loc::getLev));
        if (Cools.isEmpty(idleLocList)) {
            return;
        }
        OpenBusSubmitParam param = new OpenBusSubmitParam();
        param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
        for (int i = 0; i < Math.min(maxCapacity, Math.min(stockLocList.size(), idleLocList.size())); i++) {
            Loc stockLoc = stockLocList.get(i);
            Loc idleLoc = idleLocList.get(i);
            idleLoc.setMemo("1");
            locService.updateById(idleLoc);
            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);
        }
        if (Cools.isEmpty(param.getTaskList())) { return; }
        mainService.generateBusAndTask(param, memo);
    }
    private int getAvailableAgvCount() {
        int res = 0;
        List<Agv> agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val));
        if (Cools.isEmpty(agvList)) {
            return res;
        }
        for (Agv agv : agvList) {
            if (null == redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agv.getUuid())) {
                continue;
            }
            if (!agv.getStatusBool()) {
                continue;
            }
            res++;
        }
        return res;
    }
}