| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.zy.acs.framework.common.Cools; |
| | | import com.zy.acs.manager.common.utils.CommonUtil; |
| | | import com.zy.acs.manager.core.domain.AgvCntDto; |
| | | import com.zy.acs.manager.core.domain.Lane; |
| | | import com.zy.acs.manager.core.domain.FilterLaneDto; |
| | | import com.zy.acs.manager.core.domain.TaskPosDto; |
| | | import com.zy.acs.manager.manager.entity.*; |
| | | import com.zy.acs.manager.manager.enums.StaTypeType; |
| | | import com.zy.acs.manager.manager.enums.StatusType; |
| | | import com.zy.acs.manager.manager.enums.TaskStsType; |
| | | import com.zy.acs.manager.manager.enums.TaskTypeType; |
| | |
| | | @Slf4j |
| | | @Service |
| | | public class AllocateService { |
| | | |
| | | public static final Integer OUTBOUND_TASKS_ALLOCATE_LIMIT = 5; |
| | | |
| | | @Autowired |
| | | private AgvService agvService; |
| | |
| | | private LaneService laneService; |
| | | @Autowired |
| | | private AgvAreaDispatcher agvAreaDispatcher; |
| | | @Autowired |
| | | private SegmentService segmentService; |
| | | |
| | | /** |
| | | * get available agv list which is idle |
| | | */ |
| | | private List<String> getAvailableAgvNos(List<Long> agvIds) { |
| | | // List<Agv> agvList = new ArrayList<>(); |
| | | // if (Cools.isEmpty(agvNos)) { |
| | | // // global |
| | | // agvList = agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val)); |
| | | // } else { |
| | | // // local |
| | | // for (String agvNo : agvNos) { |
| | | // Agv agv = agvService.selectByUuid(agvNo); |
| | | // if (agv.getStatusBool()) { |
| | | // agvList.add(agv); |
| | | // } |
| | | // } |
| | | // } |
| | | |
| | | private List<String> getAvailableAgvNos(List<Long> agvIds, boolean hasRunning) { |
| | | List<Agv> agvList = Cools.isEmpty(agvIds) |
| | | ? agvService.list(new LambdaQueryWrapper<Agv>().eq(Agv::getStatus, StatusType.ENABLE.val)) |
| | | : agvIds.stream().map(agvService::getById).filter(Agv::getStatusBool).collect(Collectors.toList()); |
| | | |
| | | List<String> result = new ArrayList<>(); |
| | | for (Agv agv : agvList) { |
| | | // 1. without running tasks |
| | | if (0 < taskService.count(new LambdaQueryWrapper<Task>() |
| | | .eq(Task::getAgvId, agv.getId()) |
| | | .and(i -> |
| | | i.eq(Task::getTaskSts, TaskStsType.ASSIGN.val()) |
| | | .or().eq(Task::getTaskSts, TaskStsType.PROGRESS.val()) |
| | | ) |
| | | )) { |
| | | continue; |
| | | if (!hasRunning) { |
| | | // 1. without running tasks |
| | | if (0 < taskService.count(new LambdaQueryWrapper<Task>() |
| | | .eq(Task::getAgvId, agv.getId()) |
| | | .and(i -> i |
| | | .eq(Task::getTaskSts, TaskStsType.ASSIGN.val()) |
| | | .or() |
| | | .eq(Task::getTaskSts, TaskStsType.PROGRESS.val()) |
| | | ) |
| | | )) { |
| | | continue; |
| | | } |
| | | } |
| | | // 2. in idle status |
| | | if (!agvService.judgeEnable(agv.getId(), true)) { |
| | |
| | | } |
| | | |
| | | if (!Cools.isEmpty(result)) { |
| | | // todo: 轮询æé |
| | | Collections.shuffle(result); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | public synchronized Agv execute(Task task) { |
| | | Sta rollerOriSta = getInboundRollerSta(task); |
| | | if (rollerOriSta != null) { |
| | | List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task), true); |
| | | FilterLaneDto filterLaneDto = this.filterThroughLane(task, availableAgvNos); |
| | | if (null != filterLaneDto) { |
| | | String agvNo = this.checkoutAgvForInboundRoller(task, rollerOriSta, filterLaneDto.getActualAvailableAgvNos()); |
| | | if (!Cools.isEmpty(agvNo)) { |
| | | task.setOriLaneHash(filterLaneDto.getOriginLane().getHashCode()); |
| | | task.setDestLaneHash(filterLaneDto.getDestinationLane().getHashCode()); |
| | | return agvService.selectByUuid(agvNo); |
| | | } |
| | | } |
| | | } |
| | | |
| | | Sta rollerDestSta = getOutboundRollerSta(task); |
| | | if (rollerDestSta != null) { |
| | | List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task), true); |
| | | FilterLaneDto filterLaneDto = this.filterThroughLane(task, availableAgvNos); |
| | | if (null != filterLaneDto) { |
| | | String agvNo = this.checkoutAgvForOutboundRoller(task, rollerDestSta, filterLaneDto.getActualAvailableAgvNos()); |
| | | if (!Cools.isEmpty(agvNo)) { |
| | | task.setOriLaneHash(filterLaneDto.getOriginLane().getHashCode()); |
| | | task.setDestLaneHash(filterLaneDto.getDestinationLane().getHashCode()); |
| | | return agvService.selectByUuid(agvNo); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return this.normalExecute(task); |
| | | } |
| | | |
| | | /** |
| | |
| | | * |
| | | * it can break the limit of the number of agv backpack |
| | | */ |
| | | public synchronized Agv execute(Task task) { |
| | | List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task)); |
| | | public synchronized Agv normalExecute(Task task) { |
| | | List<String> availableAgvNos = this.getAvailableAgvNos(agvAreaDispatcher.getAgvNosByTask(task), false); |
| | | // List<String> availableAgvNos = this.getAvailableAgvNos(null); |
| | | if (Cools.isEmpty(availableAgvNos)) { |
| | | // log.warn("No available agv to assign the task[{}]", task.getSeqNum()); |
| | | return null; |
| | | } |
| | | |
| | | // calc lane |
| | | FilterLaneDto filterLaneDto = this.filterThroughLane(task, availableAgvNos); |
| | | if (null == filterLaneDto) { |
| | | return null; |
| | | } |
| | | Lane originLane = filterLaneDto.getOriginLane(); |
| | | Lane destinationLane = filterLaneDto.getDestinationLane(); |
| | | List<String> actualAvailableAgvNos = filterLaneDto.getActualAvailableAgvNos(); |
| | | |
| | | // choose min number of running task |
| | | actualAvailableAgvNos.sort(new Comparator<String>() { |
| | | @Override |
| | | public int compare(String agvNo1, String agvNo2) { |
| | | return calcAllocateWeight(agvNo1, task) - calcAllocateWeight(agvNo2, task); |
| | | } |
| | | }); |
| | | |
| | | if (null != originLane) { |
| | | task.setOriLaneHash(originLane.getHashCode()); |
| | | } |
| | | if (null != destinationLane) { |
| | | task.setDestLaneHash(destinationLane.getHashCode()); |
| | | } |
| | | |
| | | return agvService.selectByUuid(actualAvailableAgvNos.stream().findFirst().orElse(null)); |
| | | } |
| | | |
| | | private String checkoutAgvForInboundRoller(Task task, Sta sta, List<String> availableAgvNos) { |
| | | if (Cools.isEmpty(availableAgvNos, task, sta)) { |
| | | return null; |
| | | } |
| | | |
| | | for (String agvNo : availableAgvNos) { |
| | | Long agvId = agvService.getAgvId(agvNo); |
| | | Code currentCode = agvDetailService.getCurrentCode(agvId); |
| | | if (null == currentCode) { |
| | | continue; |
| | | } |
| | | |
| | | // only checkout the agv which at sta code position |
| | | if (!sta.getCode().equals(currentCode.getId())) { |
| | | continue; |
| | | } |
| | | |
| | | // has running task and within oriSta |
| | | // List<Segment> currSeg = segmentService.getByAgvAndState(agvId, SegmentStateType.WAITING.toString()); |
| | | int taskCnt = taskService.count(new LambdaQueryWrapper<Task>() |
| | | .eq(Task::getAgvId, agvId) |
| | | .eq(Task::getOriSta, sta.getId()) |
| | | .and(wrapper -> wrapper |
| | | .eq(Task::getTaskSts, TaskStsType.ASSIGN.val()) |
| | | .or() |
| | | .eq(Task::getTaskSts, TaskStsType.PROGRESS.val()) |
| | | ) |
| | | ); |
| | | if (taskCnt == 0) { |
| | | break; |
| | | } |
| | | |
| | | // has enough backpack space to load |
| | | Integer backpack = agvService.getBackpack(agvId); |
| | | int countRemainingBackpack = segmentService.countRemainingBackpack(null, agvId); |
| | | if (countRemainingBackpack >= backpack) { |
| | | break; |
| | | } |
| | | |
| | | return agvNo; |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | private String checkoutAgvForOutboundRoller(Task task, Sta sta, List<String> availableAgvNos) { |
| | | if (Cools.isEmpty(availableAgvNos, task, sta)) { |
| | | return null; |
| | | } |
| | | |
| | | List<Task> taskList = taskService.list(new LambdaQueryWrapper<Task>() |
| | | .eq(Task::getDestSta, sta.getId()) |
| | | .eq(Task::getTaskSts, TaskStsType.WAITING.val()) |
| | | .isNotNull(Task::getAgvId) |
| | | ); |
| | | if (Cools.isEmpty(taskList)) { |
| | | return null; |
| | | } |
| | | |
| | | List<AgvCntDto> cntDtoList = new ArrayList<>(); |
| | | for (Task t : taskList) { |
| | | AgvCntDto cntDto = new AgvCntDto(t.getAgvId()); |
| | | if (AgvCntDto.has(cntDtoList, cntDto)) { |
| | | AgvCntDto dto = AgvCntDto.find(cntDtoList, cntDto); |
| | | assert null != dto; |
| | | dto.setCount(dto.getCount() + 1); |
| | | } else { |
| | | cntDtoList.add(cntDto); |
| | | } |
| | | } |
| | | |
| | | cntDtoList.sort(new Comparator<AgvCntDto>() { |
| | | @Override |
| | | public int compare(AgvCntDto o1, AgvCntDto o2) { |
| | | return o1.getCount() - o2.getCount(); |
| | | } |
| | | }); |
| | | |
| | | for (AgvCntDto cntDto : cntDtoList) { |
| | | |
| | | if (cntDto.getAgvId() >= OUTBOUND_TASKS_ALLOCATE_LIMIT) { |
| | | continue; |
| | | } |
| | | return agvService.getAgvNo(cntDto.getAgvId()); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | public FilterLaneDto filterThroughLane(Task task, List<String> availableAgvNos) { |
| | | if (Cools.isEmpty(availableAgvNos, task)) { |
| | | return null; |
| | | } |
| | | |
| | | Integer maxAgvCountInLane = configService.getVal("maxAgvCountInLane", Integer.class); |
| | | |
| | | // checkout lane |
| | |
| | | return null; |
| | | } |
| | | |
| | | // choose min number of running task |
| | | actualAvailableAgvNos.sort(new Comparator<String>() { |
| | | @Override |
| | | public int compare(String agvNo1, String agvNo2) { |
| | | return calcAllocateWeight(agvNo1, task) - calcAllocateWeight(agvNo2, task); |
| | | } |
| | | }); |
| | | |
| | | |
| | | if (null != originLane) { |
| | | task.setOriLaneHash(originLane.getHashCode()); |
| | | } |
| | | if (null != destinationLane) { |
| | | task.setDestLaneHash(destinationLane.getHashCode()); |
| | | } |
| | | |
| | | return agvService.selectByUuid(actualAvailableAgvNos.stream().findFirst().orElse(null)); |
| | | return new FilterLaneDto(originLane, destinationLane, actualAvailableAgvNos); |
| | | } |
| | | |
| | | public List<String> findAgvNosByLane(Lane lane) { |
| | |
| | | return theLastOne.get(theLastOne.size() - 1).getXy(); |
| | | } |
| | | |
| | | |
| | | |
| | | // about roller -------------------------------------------- |
| | | |
| | | private Sta getInboundRollerSta(Task task) { |
| | | TaskTypeType type = TaskTypeType.get(task.getTaskTypeEl()); |
| | | switch (Objects.requireNonNull(type)) { |
| | | case STA_TO_LOC: |
| | | case STA_TO_STA: |
| | | Long oriStaId = task.getOriSta(); |
| | | if (null == oriStaId) { |
| | | return null; |
| | | } |
| | | Sta oriSta = staService.getById(oriStaId); |
| | | if (oriSta == null || Cools.isEmpty(oriSta.getStaType())) { |
| | | return null; |
| | | } |
| | | if (StaTypeType.ROLLER.val() != oriSta.getStaType()) { |
| | | return null; |
| | | } |
| | | return oriSta; |
| | | default: |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private Sta getOutboundRollerSta(Task task) { |
| | | TaskTypeType type = TaskTypeType.get(task.getTaskTypeEl()); |
| | | switch (Objects.requireNonNull(type)) { |
| | | case LOC_TO_STA: |
| | | case STA_TO_STA: |
| | | Long destStaId = task.getDestSta(); |
| | | if (null == destStaId) { |
| | | return null; |
| | | } |
| | | Sta destSta = staService.getById(destStaId); |
| | | if (destSta == null || Cools.isEmpty(destSta.getStaType())) { |
| | | return null; |
| | | } |
| | | if (StaTypeType.ROLLER.val() != destSta.getStaType()) { |
| | | return null; |
| | | } |
| | | return destSta; |
| | | default: |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | } |