| | |
| | | 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.manager.service.impl.TaskServiceImpl; |
| | | import com.zy.acs.manager.system.service.ConfigService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | | @Component |
| | |
| | | private AreaGovernService areaGovernService; |
| | | @Autowired |
| | | private CodeServiceImpl codeService; |
| | | @Autowired |
| | | private AreaAgvService areaAgvService; |
| | | @Autowired |
| | | private AreaService areaService; |
| | | @Autowired |
| | | private TaskServiceImpl taskService; |
| | | |
| | | // @Scheduled(fixedRate = 500) // 固定频率执行,不同步 |
| | | @Scheduled(fixedDelay = 1000) // 固定频率执行,同步 |
| | | // @Scheduled(cron = "0/1 * * * * ? ") |
| | | private void execute() { |
| | | if (!configService.getVal("TaskAssignMode", Boolean.class)) { return; } |
| | | if (!configService.getVal("TaskAssignMode", Boolean.class)) { |
| | | return; |
| | | } |
| | | Boolean maintainLocSts = configService.getVal("maintainLocSts", Boolean.class); |
| | | if (!maintainLocSts) { |
| | | log.error("failed to run auto agv task, the maintainLocSts is false"); |
| | | return; |
| | | } |
| | | AgvModel agvModel = agvModelService.getOne(new LambdaQueryWrapper<AgvModel>().eq(AgvModel::getType, DEFAULT_AGV_MODEL.toString())); |
| | | if (null == agvModel) { return; } |
| | | if (null == agvModel) { |
| | | return; |
| | | } |
| | | |
| | | this.autoRun(agvModel); |
| | | } |
| | | |
| | | private void autoRun(AgvModel agvModel) { |
| | | int availableAgvCount = this.getAvailableAgvCount(); |
| | | if (0 == availableAgvCount) { return; } |
| | | if (0 == availableAgvCount) { |
| | | log.error("failed to run auto agv task, 跑库区域无可用小车"); |
| | | return; |
| | | } |
| | | |
| | | // List<String> staPreNos = getStaPrefixes(staGroupList); |
| | | List<String> staPreNos = new ArrayList<>(); |
| | | String memo = "DEMO_STA_" + String.join("-", staPreNos); |
| | | |
| | | List<String> codes = getTestAreaCodes(); |
| | | if (codes.isEmpty()) { |
| | | log.error("failed to run auto agv task, 未配置跑库区域"); |
| | | return; |
| | | } |
| | | // 入库 |
| | | this.runStaToLoc(agvModel, memo); |
| | | this.runStaToLoc(agvModel, codes, memo); |
| | | // 出库 |
| | | this.runLocToSta(agvModel, memo); |
| | | this.runLocToSta(agvModel, codes, memo); |
| | | // 移库 |
| | | this.runLocToLoc(agvModel, memo); |
| | | this.runLocToLoc(agvModel, codes, memo); |
| | | } |
| | | |
| | | // 入库 |
| | | private void runStaToLoc(AgvModel agvModel, String memo) { |
| | | private void runStaToLoc(AgvModel agvModel, List<String> codes, String memo) { |
| | | |
| | | |
| | | // STOCK STA |
| | | List<Sta> stockList = staService.queryAvailableOutSta(1); |
| | | if (Cools.isEmpty(stockList)) { return; } |
| | | Collections.shuffle(stockList); |
| | | List<Sta> staList = new ArrayList<>(); |
| | | for (Sta sta : stockList) { |
| | | if (sta.getCode$() != null && codes.contains(sta.getCode$())) { |
| | | List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getOriSta, sta.getCode()).or().eq(Task::getDestSta, sta.getCode())); |
| | | if ((sta.getCapacity() - sta.getRsvInCnt()) > (list.isEmpty() ? 0 : list.size())) { |
| | | staList.add(sta); |
| | | } |
| | | } |
| | | } |
| | | if (Cools.isEmpty(staList)) { |
| | | return; |
| | | } |
| | | Collections.shuffle(staList); |
| | | |
| | | OpenBusSubmitParam param = new OpenBusSubmitParam(); |
| | | param.setBatchNo(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(); |
| | | for (int i = 0; i < Math.min(agvModel.getBackpack(), staList.size()); i++) { |
| | | Sta stockSta = staList.get(i); |
| | | |
| | | Loc idleLoc = null; |
| | | Loc idleLoc = getLocByLocSts(LocStsType.IDLE.val(), codes); |
| | | |
| | | // 所在区域的条码,如果没有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; |
| | | } |
| | | |
| | | if (null == idleLoc) { break; } |
| | | |
| | | TaskDto taskDto = new TaskDto(); |
| | | taskDto.setOriSta(stockSta.getStaNo()); |
| | |
| | | |
| | | param.getTasks().add(taskDto); |
| | | } |
| | | if (Cools.isEmpty(param.getTasks())) { return; } |
| | | if (Cools.isEmpty(param.getTasks())) { |
| | | return; |
| | | } |
| | | |
| | | mainService.generateBusAndTask(param, memo); |
| | | } |
| | | |
| | | // 出库 |
| | | private void runLocToSta(AgvModel agvModel, String memo) { |
| | | private void runLocToSta(AgvModel agvModel, List<String> codes, String memo) { |
| | | |
| | | // IDLE STA |
| | | List<Sta> idleList = staService.queryAvailableInSta(1); |
| | | if (Cools.isEmpty(idleList)) { return; } |
| | | Collections.shuffle(idleList); |
| | | List<Sta> staList = new ArrayList<>(); |
| | | for (Sta sta : idleList) { |
| | | if (sta.getCode$() != null && codes.contains(sta.getCode$())) { |
| | | List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getOriSta, sta.getCode()).or().eq(Task::getDestSta, sta.getCode())); |
| | | if ((sta.getCapacity() - sta.getRsvOutCnt()) > (list.isEmpty() ? 0 : list.size())) { |
| | | staList.add(sta); |
| | | } |
| | | } |
| | | } |
| | | if (Cools.isEmpty(staList)) { |
| | | return; |
| | | } |
| | | Collections.shuffle(staList); |
| | | |
| | | OpenBusSubmitParam param = new OpenBusSubmitParam(); |
| | | param.setBatchNo(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(); |
| | | for (int i = 0; i < Math.min(agvModel.getBackpack(), staList.size()); i++) { |
| | | Sta idleSta = staList.get(i); |
| | | |
| | | Loc stockLoc = null; |
| | | Loc stockLoc = getLocByLocSts(LocStsType.STOCK.val(), codes); |
| | | |
| | | // 所在区域的条码,如果没有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; |
| | | } |
| | | |
| | | if (null == stockLoc) { break; } |
| | | |
| | | TaskDto taskDto = new TaskDto(); |
| | | taskDto.setOriLoc(stockLoc.getLocNo()); |
| | |
| | | |
| | | param.getTasks().add(taskDto); |
| | | } |
| | | if (Cools.isEmpty(param.getTasks())) { return; } |
| | | if (Cools.isEmpty(param.getTasks())) { |
| | | return; |
| | | } |
| | | |
| | | mainService.generateBusAndTask(param, memo); |
| | | } |
| | | |
| | | // 移库 |
| | | private void runLocToLoc(AgvModel agvModel, String staTaskMemo) { |
| | | private void runLocToLoc(AgvModel agvModel, List<String> codes, String staTaskMemo) { |
| | | String memo = "DEMO_LOC"; |
| | | |
| | | int availableAgvCount = this.getAvailableAgvCount(); |
| | |
| | | if (availableAgvCount <= busService.count(new LambdaQueryWrapper<Bus>() |
| | | .in(Bus::getBusSts, BusStsType.RECEIVE.val(), BusStsType.PROGRESS.val()) |
| | | .in(Bus::getMemo, memo, staTaskMemo) |
| | | )) { return; } |
| | | )) { |
| | | return; |
| | | } |
| | | |
| | | int maxCapacity = agvModel.getBackpack(); |
| | | |
| | | // STOCK |
| | | List<Loc> stockLocList = locService.selectRandByLocSts(LocStsType.STOCK.val(), maxCapacity); |
| | | List<Loc> stockLocList = getAllLocByLocSts(LocStsType.STOCK.val(), codes); |
| | | if (Cools.isEmpty(stockLocList)) { |
| | | return; |
| | | } |
| | | Collections.shuffle(stockLocList); |
| | | |
| | | // IDLE |
| | | List<Loc> idleLocList = locService.selectRandByLocSts(LocStsType.IDLE.val(), maxCapacity); |
| | | List<Loc> idleLocList = getAllLocByLocSts(LocStsType.IDLE.val(), codes); |
| | | if (Cools.isEmpty(idleLocList)) { |
| | | return; |
| | | } |
| | |
| | | |
| | | param.getTasks().add(taskDto); |
| | | } |
| | | if (Cools.isEmpty(param.getTasks())) { return; } |
| | | if (Cools.isEmpty(param.getTasks())) { |
| | | return; |
| | | } |
| | | |
| | | mainService.generateBusAndTask(param, memo); |
| | | } |
| | | |
| | | |
| | | public Loc getLocByLocSts(Long locSts, List<String> codes) { |
| | | List<Loc> list = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, locSts)); |
| | | Collections.shuffle(list); |
| | | for (Loc loc : list) { |
| | | if (codes.contains(loc.getCode$())) { |
| | | return loc; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public List<Loc> getAllLocByLocSts(Long locSts, List<String> codes) { |
| | | List<Loc> data = new ArrayList<>(); |
| | | List<Loc> list = locService.list(new LambdaQueryWrapper<Loc>().eq(Loc::getLocSts, locSts)); |
| | | Collections.shuffle(list); |
| | | for (Loc loc : list) { |
| | | if (codes.contains(loc.getCode$())) { |
| | | data.add(loc); |
| | | } |
| | | } |
| | | return data; |
| | | } |
| | | |
| | | |
| | | public List<String> getTestAreaCodes() { |
| | | String taskAssignModeArea = configService.getVal("TaskAssignModeArea", String.class); |
| | | if (Cools.isEmpty(taskAssignModeArea)) { |
| | | List<Code> list = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1)); |
| | | return list.stream().map(Code::getData).collect(Collectors.toList()); |
| | | } |
| | | List<Area> list = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getName, taskAssignModeArea)); |
| | | if (list.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return areaGovernService.queryCodes(list.stream().map(Area::getId).collect(Collectors.toList())); |
| | | } |
| | | |
| | | public List<Long> getTestAreaAgvs() { |
| | | String taskAssignModeArea = configService.getVal("TaskAssignModeArea", String.class); |
| | | if (Cools.isEmpty(taskAssignModeArea)) { |
| | | List<Agv> list = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, 1)); |
| | | return list.stream().map(Agv::getId).collect(Collectors.toList()); |
| | | } |
| | | List<Area> list = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getName, taskAssignModeArea)); |
| | | if (list.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | List<AreaAgv> list1 = areaAgvService.list(new LambdaQueryWrapper<AreaAgv>().in(AreaAgv::getAreaId, list.stream().map(Area::getId).collect(Collectors.toList()))); |
| | | return list1.stream().map(AreaAgv::getAgvId).collect(Collectors.toList()); |
| | | } |
| | | |
| | | public static List<String> getStaPrefixes(List<String> staGroupList) { |
| | |
| | | |
| | | 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())) { |
| | | List<Long> testAreaAgvs = getTestAreaAgvs(); |
| | | for (Long agvId : testAreaAgvs) { |
| | | String agvNo = agvService.getAgvNo(agvId); |
| | | Agv agv = agvService.selectByUuid(agvNo); |
| | | if (null == redis.getObject(RedisConstant.AGV_ONLINE_FLAG, agvNo)) { |
| | | continue; |
| | | } |
| | | if (!agv.getStatusBool()) { |