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().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 staPreNos = getStaPrefixes(staGroupList); List 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() .in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val()) .in(Bus::getMemo, memo, staTaskMemo) )) { return; } int maxCapacity = agvModel.getBackpack(); // STOCK List stockLocList = locService.selectRandByLocSts(LocStsType.STOCK.val(), maxCapacity); if (Cools.isEmpty(stockLocList)) { return; } Collections.shuffle(stockLocList); // IDLE List idleLocList = locService.list(new LambdaQueryWrapper().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 agvList = agvService.list(new LambdaQueryWrapper().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; } }