| | |
| | | import com.zy.asrs.domain.path.StationPathProfileConfig; |
| | | import com.zy.asrs.domain.path.StationPathResolvedPolicy; |
| | | import com.zy.asrs.domain.path.StationPathRuleConfig; |
| | | import com.zy.asrs.domain.vo.StationTaskTraceSegmentVo; |
| | | import com.zy.asrs.entity.BasStationOpt; |
| | | import com.zy.asrs.domain.vo.StationTaskTraceVo; |
| | | import com.zy.asrs.domain.vo.StationCycleCapacityVo; |
| | | import com.zy.asrs.domain.vo.StationCycleLoopVo; |
| | | import com.zy.asrs.entity.BasDevp; |
| | | import com.zy.asrs.entity.BasStation; |
| | | import com.zy.asrs.service.BasStationOptService; |
| | | import com.zy.asrs.service.BasDevpService; |
| | | import com.zy.asrs.service.BasStationService; |
| | | import com.zy.asrs.service.StationCycleCapacityService; |
| | | import com.zy.asrs.service.StationPathPolicyService; |
| | | import com.zy.core.News; |
| | | import com.zy.core.model.StationObjModel; |
| | | import com.zy.core.model.command.StationCommand; |
| | | import com.zy.core.enums.StationCommandType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | |
| | | import com.zy.core.thread.StationThread; |
| | | import com.zy.core.trace.StationTaskTraceRegistry; |
| | | |
| | | import java.util.Date; |
| | | import java.util.LinkedHashMap; |
| | | |
| | | @Component |
| | | public class NavigateUtils { |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private BasStationService basStationService; |
| | | @Autowired |
| | | private BasStationOptService basStationOptService; |
| | | @Autowired |
| | | private StationPathPolicyService stationPathPolicyService; |
| | | @Autowired |
| | |
| | | } |
| | | } |
| | | |
| | | for (StationTaskTraceVo traceVo : loadActiveTraceList(currentTaskNo)) { |
| | | for (StationTaskTraceVo traceVo : loadActiveTraceList(currentTaskNo, statusMap)) { |
| | | if (traceVo == null) { |
| | | continue; |
| | | } |
| | |
| | | return snapshot; |
| | | } |
| | | |
| | | private List<StationTaskTraceVo> loadActiveTraceList(Integer currentTaskNo) { |
| | | if (stationTaskTraceRegistry == null) { |
| | | return Collections.emptyList(); |
| | | } |
| | | List<StationTaskTraceVo> traceList; |
| | | try { |
| | | traceList = stationTaskTraceRegistry.listLatestTraces(); |
| | | } catch (Exception ignore) { |
| | | return Collections.emptyList(); |
| | | } |
| | | if (traceList == null || traceList.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | |
| | | List<StationTaskTraceVo> result = new ArrayList<>(); |
| | | for (StationTaskTraceVo traceVo : traceList) { |
| | | if (!isPlanningActiveTrace(traceVo)) { |
| | | continue; |
| | | private List<StationTaskTraceVo> loadActiveTraceList(Integer currentTaskNo, Map<Integer, StationProtocol> statusMap) { |
| | | Map<Integer, StationTaskTraceVo> traceMap = new LinkedHashMap<>(); |
| | | if (stationTaskTraceRegistry != null) { |
| | | try { |
| | | List<StationTaskTraceVo> traceList = stationTaskTraceRegistry.listLatestTraces(); |
| | | if (traceList != null) { |
| | | for (StationTaskTraceVo traceVo : traceList) { |
| | | if (!isPlanningActiveTrace(traceVo)) { |
| | | continue; |
| | | } |
| | | if (currentTaskNo != null && currentTaskNo.equals(traceVo.getTaskNo())) { |
| | | continue; |
| | | } |
| | | if (traceVo.getTaskNo() != null) { |
| | | traceMap.put(traceVo.getTaskNo(), traceVo); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ignore) { |
| | | } |
| | | if (currentTaskNo != null && currentTaskNo.equals(traceVo.getTaskNo())) { |
| | | continue; |
| | | } |
| | | result.add(traceVo); |
| | | } |
| | | return result; |
| | | Map<Integer, StationTaskTraceVo> fallbackTraceMap = loadFallbackActiveTraceMap(currentTaskNo, statusMap, traceMap.keySet()); |
| | | if (!fallbackTraceMap.isEmpty()) { |
| | | traceMap.putAll(fallbackTraceMap); |
| | | } |
| | | return new ArrayList<>(traceMap.values()); |
| | | } |
| | | |
| | | private boolean isPlanningActiveTrace(StationTaskTraceVo traceVo) { |
| | |
| | | return StationTaskTraceRegistry.STATUS_WAITING.equals(status) |
| | | || StationTaskTraceRegistry.STATUS_RUNNING.equals(status) |
| | | || StationTaskTraceRegistry.STATUS_REROUTED.equals(status); |
| | | } |
| | | |
| | | private Map<Integer, StationTaskTraceVo> loadFallbackActiveTraceMap(Integer currentTaskNo, |
| | | Map<Integer, StationProtocol> statusMap, |
| | | Set<Integer> existingTaskNoSet) { |
| | | if (basStationOptService == null || statusMap == null || statusMap.isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | Map<Integer, StationProtocol> activeTaskProtocolMap = new LinkedHashMap<>(); |
| | | for (StationProtocol protocol : statusMap.values()) { |
| | | if (protocol == null || protocol.getTaskNo() == null || protocol.getTaskNo() <= 0) { |
| | | continue; |
| | | } |
| | | if (!Boolean.TRUE.equals(protocol.isLoading())) { |
| | | continue; |
| | | } |
| | | if (currentTaskNo != null && currentTaskNo.equals(protocol.getTaskNo())) { |
| | | continue; |
| | | } |
| | | if (existingTaskNoSet != null && existingTaskNoSet.contains(protocol.getTaskNo())) { |
| | | continue; |
| | | } |
| | | activeTaskProtocolMap.putIfAbsent(protocol.getTaskNo(), protocol); |
| | | } |
| | | if (activeTaskProtocolMap.isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | List<Integer> taskNoList = new ArrayList<>(activeTaskProtocolMap.keySet()); |
| | | int limit = Math.max(50, taskNoList.size() * 8); |
| | | List<BasStationOpt> optList; |
| | | try { |
| | | optList = basStationOptService.list(new QueryWrapper<BasStationOpt>() |
| | | .select("id", "task_no", "send_time", "command", "mode", "send", "target_station_id") |
| | | .in("task_no", taskNoList) |
| | | .eq("send", 1) |
| | | .orderByDesc("send_time") |
| | | .last("limit " + limit)); |
| | | } catch (Exception ignore) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | if (optList == null || optList.isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | |
| | | Map<Integer, List<FallbackMoveCommand>> fallbackCommandMap = new LinkedHashMap<>(); |
| | | for (BasStationOpt opt : optList) { |
| | | FallbackMoveCommand moveCommand = parseFallbackMoveCommand(opt); |
| | | if (moveCommand == null || moveCommand.taskNo == null) { |
| | | continue; |
| | | } |
| | | if (!activeTaskProtocolMap.containsKey(moveCommand.taskNo)) { |
| | | continue; |
| | | } |
| | | fallbackCommandMap.computeIfAbsent(moveCommand.taskNo, key -> new ArrayList<>()).add(moveCommand); |
| | | } |
| | | |
| | | Map<Integer, StationTaskTraceVo> result = new LinkedHashMap<>(); |
| | | for (Map.Entry<Integer, StationProtocol> entry : activeTaskProtocolMap.entrySet()) { |
| | | Integer taskNo = entry.getKey(); |
| | | StationProtocol protocol = entry.getValue(); |
| | | StationTaskTraceVo fallbackTrace = buildFallbackTraceVo(taskNo, protocol, fallbackCommandMap.get(taskNo)); |
| | | if (fallbackTrace != null) { |
| | | result.put(taskNo, fallbackTrace); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private FallbackMoveCommand parseFallbackMoveCommand(BasStationOpt opt) { |
| | | if (opt == null || opt.getTaskNo() == null || opt.getTaskNo() <= 0) { |
| | | return null; |
| | | } |
| | | try { |
| | | StationCommand command = JSON.parseObject(opt.getCommand(), StationCommand.class); |
| | | if (command == null || command.getCommandType() != StationCommandType.MOVE) { |
| | | return null; |
| | | } |
| | | List<Integer> navigatePath = distinctPositiveStationIds(command.getNavigatePath()); |
| | | if (navigatePath.isEmpty()) { |
| | | return null; |
| | | } |
| | | FallbackMoveCommand item = new FallbackMoveCommand(); |
| | | item.taskNo = opt.getTaskNo(); |
| | | item.traceVersion = command.getTraceVersion(); |
| | | item.segmentNo = command.getSegmentNo(); |
| | | item.segmentCount = command.getSegmentCount(); |
| | | item.stationId = command.getStationId(); |
| | | item.targetStaNo = command.getTargetStaNo(); |
| | | item.navigatePath = navigatePath; |
| | | item.sendTime = opt.getSendTime(); |
| | | return item; |
| | | } catch (Exception ignore) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | private StationTaskTraceVo buildFallbackTraceVo(Integer taskNo, |
| | | StationProtocol protocol, |
| | | List<FallbackMoveCommand> commandList) { |
| | | if (taskNo == null || protocol == null || commandList == null || commandList.isEmpty()) { |
| | | return null; |
| | | } |
| | | |
| | | Integer latestTraceVersion = null; |
| | | long latestTimestamp = 0L; |
| | | for (FallbackMoveCommand item : commandList) { |
| | | if (item == null) { |
| | | continue; |
| | | } |
| | | if (item.traceVersion != null && (latestTraceVersion == null || item.traceVersion > latestTraceVersion)) { |
| | | latestTraceVersion = item.traceVersion; |
| | | } |
| | | long ts = item.sendTime == null ? 0L : item.sendTime.getTime(); |
| | | if (ts > latestTimestamp) { |
| | | latestTimestamp = ts; |
| | | } |
| | | } |
| | | |
| | | List<FallbackMoveCommand> sameTraceCommandList = new ArrayList<>(); |
| | | for (FallbackMoveCommand item : commandList) { |
| | | if (item == null) { |
| | | continue; |
| | | } |
| | | if (latestTraceVersion == null || latestTraceVersion.equals(item.traceVersion)) { |
| | | sameTraceCommandList.add(item); |
| | | } |
| | | } |
| | | sameTraceCommandList.sort((a, b) -> { |
| | | int av = a.segmentNo == null ? Integer.MAX_VALUE : a.segmentNo; |
| | | int bv = b.segmentNo == null ? Integer.MAX_VALUE : b.segmentNo; |
| | | if (av != bv) { |
| | | return Integer.compare(av, bv); |
| | | } |
| | | long at = a.sendTime == null ? 0L : a.sendTime.getTime(); |
| | | long bt = b.sendTime == null ? 0L : b.sendTime.getTime(); |
| | | return Long.compare(at, bt); |
| | | }); |
| | | |
| | | List<Integer> issuedPath = new ArrayList<>(); |
| | | List<StationTaskTraceSegmentVo> segmentList = new ArrayList<>(); |
| | | FallbackMoveCommand latestCommand = null; |
| | | for (FallbackMoveCommand item : sameTraceCommandList) { |
| | | if (item == null || item.navigatePath == null || item.navigatePath.isEmpty()) { |
| | | continue; |
| | | } |
| | | appendMergedPath(issuedPath, item.navigatePath); |
| | | segmentList.add(toTraceSegment(item)); |
| | | if (latestCommand == null || compareFallbackCommand(item, latestCommand) > 0) { |
| | | latestCommand = item; |
| | | } |
| | | } |
| | | if (issuedPath.isEmpty()) { |
| | | return null; |
| | | } |
| | | |
| | | Integer currentStationId = protocol.getStationId(); |
| | | List<Integer> passedStationIds = new ArrayList<>(); |
| | | List<Integer> pendingStationIds = new ArrayList<>(); |
| | | if (currentStationId != null) { |
| | | int currentIndex = issuedPath.indexOf(currentStationId); |
| | | if (currentIndex >= 0) { |
| | | passedStationIds = copyIntegerSubList(issuedPath, 0, currentIndex); |
| | | pendingStationIds = copyIntegerSubList(issuedPath, currentIndex + 1, issuedPath.size()); |
| | | } else { |
| | | pendingStationIds = new ArrayList<>(issuedPath); |
| | | } |
| | | } else { |
| | | pendingStationIds = new ArrayList<>(issuedPath); |
| | | } |
| | | |
| | | StationTaskTraceVo vo = new StationTaskTraceVo(); |
| | | vo.setTaskNo(taskNo); |
| | | vo.setThreadImpl("DB_FALLBACK"); |
| | | vo.setStatus(StationTaskTraceRegistry.STATUS_RUNNING); |
| | | vo.setTraceVersion(latestTraceVersion == null ? 1 : latestTraceVersion); |
| | | vo.setStartStationId(issuedPath.isEmpty() ? null : issuedPath.get(0)); |
| | | vo.setCurrentStationId(currentStationId); |
| | | vo.setFinalTargetStationId(protocol.getTargetStaNo() != null ? protocol.getTargetStaNo() |
| | | : latestCommand == null ? null : latestCommand.targetStaNo); |
| | | vo.setBlockedStationId(null); |
| | | vo.setFullPathStationIds(new ArrayList<>(issuedPath)); |
| | | vo.setIssuedStationIds(new ArrayList<>(issuedPath)); |
| | | vo.setPassedStationIds(passedStationIds); |
| | | vo.setPendingStationIds(pendingStationIds); |
| | | vo.setLatestIssuedSegmentPath(latestCommand == null ? Collections.emptyList() : new ArrayList<>(latestCommand.navigatePath)); |
| | | vo.setSegmentList(segmentList); |
| | | vo.setIssuedSegmentCount(segmentList.size()); |
| | | vo.setTotalSegmentCount(latestCommand == null || latestCommand.segmentCount == null |
| | | ? segmentList.size() |
| | | : latestCommand.segmentCount); |
| | | vo.setUpdatedAt(latestTimestamp > 0L ? latestTimestamp : System.currentTimeMillis()); |
| | | vo.setEvents(Collections.emptyList()); |
| | | return vo; |
| | | } |
| | | |
| | | private int compareFallbackCommand(FallbackMoveCommand a, FallbackMoveCommand b) { |
| | | int av = a == null || a.segmentNo == null ? Integer.MIN_VALUE : a.segmentNo; |
| | | int bv = b == null || b.segmentNo == null ? Integer.MIN_VALUE : b.segmentNo; |
| | | if (av != bv) { |
| | | return Integer.compare(av, bv); |
| | | } |
| | | long at = a == null || a.sendTime == null ? 0L : a.sendTime.getTime(); |
| | | long bt = b == null || b.sendTime == null ? 0L : b.sendTime.getTime(); |
| | | return Long.compare(at, bt); |
| | | } |
| | | |
| | | private StationTaskTraceSegmentVo toTraceSegment(FallbackMoveCommand item) { |
| | | StationTaskTraceSegmentVo segmentVo = new StationTaskTraceSegmentVo(); |
| | | if (item == null) { |
| | | return segmentVo; |
| | | } |
| | | segmentVo.setSegmentNo(item.segmentNo); |
| | | segmentVo.setSegmentCount(item.segmentCount); |
| | | segmentVo.setStationId(item.stationId); |
| | | segmentVo.setTargetStationId(item.targetStaNo); |
| | | segmentVo.setSegmentPath(item.navigatePath == null ? Collections.emptyList() : new ArrayList<>(item.navigatePath)); |
| | | segmentVo.setIssued(Boolean.TRUE); |
| | | return segmentVo; |
| | | } |
| | | |
| | | private void appendMergedPath(List<Integer> target, List<Integer> source) { |
| | | if (target == null || source == null || source.isEmpty()) { |
| | | return; |
| | | } |
| | | if (target.isEmpty()) { |
| | | target.addAll(source); |
| | | return; |
| | | } |
| | | int overlap = 0; |
| | | int maxOverlap = Math.min(target.size(), source.size()); |
| | | for (int size = maxOverlap; size >= 1; size--) { |
| | | boolean matched = true; |
| | | for (int i = 0; i < size; i++) { |
| | | Integer left = target.get(target.size() - size + i); |
| | | Integer right = source.get(i); |
| | | if (left == null || !left.equals(right)) { |
| | | matched = false; |
| | | break; |
| | | } |
| | | } |
| | | if (matched) { |
| | | overlap = size; |
| | | break; |
| | | } |
| | | } |
| | | for (int i = overlap; i < source.size(); i++) { |
| | | Integer stationId = source.get(i); |
| | | if (stationId != null) { |
| | | target.add(stationId); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private List<Integer> copyIntegerSubList(List<Integer> source, int fromIndex, int toIndex) { |
| | | if (source == null || source.isEmpty()) { |
| | | return new ArrayList<>(); |
| | | } |
| | | int from = Math.max(0, fromIndex); |
| | | int to = Math.min(source.size(), Math.max(from, toIndex)); |
| | | List<Integer> result = new ArrayList<>(); |
| | | for (int i = from; i < to; i++) { |
| | | Integer value = source.get(i); |
| | | if (value != null) { |
| | | result.add(value); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private List<Integer> distinctPositiveStationIds(List<Integer> stationIdList) { |
| | |
| | | private double sequentialRisk; |
| | | } |
| | | |
| | | private static class FallbackMoveCommand { |
| | | private Integer taskNo; |
| | | private Integer traceVersion; |
| | | private Integer segmentNo; |
| | | private Integer segmentCount; |
| | | private Integer stationId; |
| | | private Integer targetStaNo; |
| | | private List<Integer> navigatePath = Collections.emptyList(); |
| | | private Date sendTime; |
| | | } |
| | | |
| | | private static class PathGlobalPolicy { |
| | | private double lenWeightFactor = 1.0d; |
| | | private double congWeightFactor = 1.0d; |