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.constant.AgvGroupConstant;
|
import com.zy.acs.manager.core.constant.LocGroupConstant;
|
import com.zy.acs.manager.core.constant.StaGroupConstant;
|
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.LocStsType;
|
import com.zy.acs.manager.manager.enums.StatusType;
|
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.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.util.*;
|
|
@Slf4j
|
@Component
|
public class EfficiencyTestScheduler {
|
|
private final RedisSupport redis = RedisSupport.defaultRedisSupport;
|
|
@Autowired
|
private AgvService agvService;
|
@Autowired
|
private MainService mainService;
|
@Autowired
|
private ConfigService configService;
|
@Autowired
|
private LocService locService;
|
@Autowired
|
private StaService staService;
|
@Autowired
|
private TaskService taskService;
|
@Autowired
|
private AgvModelService agvModelService;
|
@Autowired
|
private SnowflakeIdWorker snowflakeIdWorker;
|
|
|
@Scheduled(fixedDelay = 1000) // 固定频率执行,同步
|
private void execute() {
|
String efficiency = configService.getVal("Efficiency", String.class);
|
// 1/0,23,1/2,1-5,1 1,2,2,6,24,10che
|
// 是否开启,1开启,0关闭 哪些区,1入库还是2出库,一次生成任务数量,最大任务数
|
if (!Cools.isEmpty(efficiency)) {
|
String[] split = efficiency.split(",");
|
if (split.length >= 6 && split[0].equals("1")) {
|
for (char c : split[1].toCharArray()) {
|
switch (c) {
|
case '1':
|
this.autoRun(LocGroupConstant.FAR_RIGHT_LOC_ROW_LIST, StaGroupConstant.FAR_RIGHT_STA_ROW_LIST, AgvGroupConstant.FIFTH_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
|
break;
|
case '2':
|
this.autoRun(LocGroupConstant.RIGHT_LOC_ROW_LIST, StaGroupConstant.RIGHT_STA_ROW_LIST, AgvGroupConstant.FOURTH_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
|
break;
|
case '3':
|
this.autoRun(LocGroupConstant.MIDDLE_LOC_ROW_LIST, StaGroupConstant.MIDDLE_STA_ROW_LIST, AgvGroupConstant.THIRD_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
|
break;
|
case '4':
|
this.autoRun(LocGroupConstant.LEFT_LOC_ROW_LIST, StaGroupConstant.LEFT_STA_ROW_LIST, AgvGroupConstant.SECOND_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
|
break;
|
case '5':
|
this.autoRun(LocGroupConstant.FAR_LEFT_LOC_ROW_LIST, StaGroupConstant.FAR_LEFT_STA_ROW_LIST, AgvGroupConstant.FIRST_AGV_GROUP, Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]),split[5]);
|
break;
|
default:
|
break;
|
}
|
}
|
}
|
}
|
}
|
|
private void autoRun(List<Integer> locGroupList, List<String> staGroupList, List<String> agvGroupList, Integer mode, Integer taskNum, Integer taskMax,String memo) {
|
// int availableAgvCount = this.getAvailableAgvCount(agvGroupList);
|
// if (0 == availableAgvCount) {
|
// return;
|
// }
|
|
List<String> staPreNos = getStaPrefixes(staGroupList);
|
String staTaskMemo = "DEMO_STA_" + String.join("-", staPreNos)+"_"+memo;
|
|
// 最多 ? 个任务运行
|
if (taskMax <= taskService.count(new LambdaQueryWrapper<Task>().in(Task::getTaskSts, TaskStsType.INIT.val(), TaskStsType.WAITING.val(), TaskStsType.ASSIGN.val(), TaskStsType.PROGRESS.val()).in(Task::getMemo, staTaskMemo))) {
|
return;
|
}
|
|
if (mode != null && mode == 1) {
|
// 入库
|
this.runStaToLoc(locGroupList, staGroupList, staTaskMemo, taskNum,"3-1");
|
this.runStaToLoc(locGroupList, staGroupList, staTaskMemo, taskNum,"4-1");
|
} else if (mode == 2) {
|
// 出库
|
this.runLocToSta(locGroupList, staGroupList, staTaskMemo, taskNum,"3-1");
|
this.runLocToSta(locGroupList, staGroupList, staTaskMemo, taskNum,"4-1");
|
}
|
|
}
|
|
|
// 入库
|
private void runStaToLoc(List<Integer> locGroupList, List<String> staGroupList, String memo, Integer taskNum,String sta) {
|
Integer startRow = Collections.min(locGroupList);
|
Integer endRow = Collections.max(locGroupList);
|
|
|
AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, AgvModelType.CTU_BOX_TRANSPORT_AGV.toString()));
|
if (null == agvModel) {
|
return;
|
}
|
|
// STOCK
|
LambdaQueryWrapper<Sta> stockWrapper = new LambdaQueryWrapper<Sta>().eq(Sta::getStatus, StatusType.ENABLE.val).eq(Sta::getInEnable, "Y").in(Sta::getStaNo, staGroupList);
|
List<Sta> stockList = staService.list(stockWrapper);
|
if (Cools.isEmpty(stockList)) {
|
return;
|
}
|
Collections.shuffle(stockList);
|
|
// 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);
|
|
OpenBusSubmitParam param = new OpenBusSubmitParam();
|
param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
|
for (int i = 0; i <taskNum; i++) {
|
Loc idleLoc = idleList.get(i);
|
TaskDto taskDto = new TaskDto();
|
taskDto.setOriSta(sta);
|
taskDto.setDestLoc(idleLoc.getLocNo());
|
taskDto.setPriority(100);
|
taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(11, 19));
|
taskDto.setMemo(memo);
|
param.getTaskList().add(taskDto);
|
}
|
|
mainService.generateBusAndTask(param, memo);
|
}
|
|
|
// 出库
|
private void runLocToSta(List<Integer> locGroupList, List<String> staGroupList, String memo, Integer taskNum,String sta) {
|
Integer startRow = Collections.min(locGroupList);
|
Integer endRow = Collections.max(locGroupList);
|
|
AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, AgvModelType.CTU_BOX_TRANSPORT_AGV.toString()));
|
if (null == agvModel) {
|
return;
|
}
|
|
// STA IDLE
|
LambdaQueryWrapper<Sta> idleWrapper = new LambdaQueryWrapper<Sta>().eq(Sta::getStatus, StatusType.ENABLE.val).eq(Sta::getOutEnable, "Y").in(Sta::getStaNo, staGroupList);
|
List<Sta> idleList = staService.list(idleWrapper);
|
if (Cools.isEmpty(idleList)) {
|
return;
|
}
|
Collections.shuffle(idleList);
|
|
// LOC 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)) {
|
return;
|
}
|
Collections.shuffle(stockList);
|
|
OpenBusSubmitParam param = new OpenBusSubmitParam();
|
param.setBatch(String.valueOf(snowflakeIdWorker.nextId()).substring(13, 19));
|
for (int i = 0; i < taskNum; i++) {
|
Loc stockLoc = stockList.get(i);
|
|
TaskDto taskDto = new TaskDto();
|
taskDto.setOriLoc(stockLoc.getLocNo());
|
taskDto.setDestSta(sta);
|
taskDto.setPriority(100);
|
taskDto.setSeqNum(String.valueOf(snowflakeIdWorker.nextId()).substring(11, 19));
|
taskDto.setMemo(memo);
|
param.getTaskList().add(taskDto);
|
}
|
|
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(List<String> agvNos) {
|
int res = 0;
|
if (Cools.isEmpty(agvNos)) {
|
return res;
|
}
|
for (String agvNo : agvNos) {
|
if (null == redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agvNo)) {
|
continue;
|
}
|
Agv agv = agvService.selectByUuid(agvNo);
|
if (!agv.getStatusBool()) {
|
continue;
|
}
|
res++;
|
}
|
|
return res;
|
}
|
|
}
|