| | |
| | | |
| | | public class StationV5SegmentExecutor { |
| | | |
| | | private static final String CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = "stationCommandSegmentAdvanceRatio"; |
| | | private static final double DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO = 0.3d; |
| | | private static final int NEXT_SEGMENT_ADVANCE_NODE_COUNT = 1; |
| | | private static final long CURRENT_STATION_TIMEOUT_MS = 1000L * 60L; |
| | | |
| | | private final DeviceConfig deviceConfig; |
| | |
| | | int segCursor = 0; |
| | | Integer lastCurrentStationId = null; |
| | | boolean firstRun = true; |
| | | double segmentAdvanceRatio = loadSegmentAdvanceRatio(); |
| | | while (true) { |
| | | try { |
| | | Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + original.getTaskNo()); |
| | |
| | | |
| | | StationCommand currentSegmentCommand = localPlan.getSegmentCommands().get(segCursor); |
| | | int currentSegEndIndex = safeIndex(currentSegmentCommand.getSegmentEndIndex()); |
| | | int currentSegStartIndex = safeIndex(currentSegmentCommand.getSegmentStartIndex()); |
| | | int segLen = Math.max(1, currentSegEndIndex - currentSegStartIndex + 1); |
| | | int remainingSegment = Math.max(0, currentSegEndIndex - currentIndex); |
| | | int thresholdSegment = (int) Math.ceil(segLen * segmentAdvanceRatio); |
| | | if (remainingSegment <= thresholdSegment && segCursor < localPlan.getSegmentCommands().size() - 1) { |
| | | if (remainingSegment <= NEXT_SEGMENT_ADVANCE_NODE_COUNT |
| | | && segCursor < localPlan.getSegmentCommands().size() - 1) { |
| | | StationCommand nextCommand = localPlan.getSegmentCommands().get(segCursor + 1); |
| | | if (sendSegmentWithRetry(nextCommand)) { |
| | | segCursor++; |
| | |
| | | |
| | | private boolean sendSegmentWithRetry(StationCommand command) { |
| | | while (true) { |
| | | if (isTaskMoveReset(command == null ? null : command.getTaskNo())) { |
| | | return false; |
| | | } |
| | | CommandResponse commandResponse = commandSender.apply(command); |
| | | if (commandResponse == null) { |
| | | sleepQuietly(200L); |
| | |
| | | } |
| | | } |
| | | |
| | | private double loadSegmentAdvanceRatio() { |
| | | try { |
| | | ConfigService configService = SpringUtils.getBean(ConfigService.class); |
| | | if (configService == null) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | Config config = configService.getOne(new QueryWrapper<Config>() |
| | | .eq("code", CFG_STATION_COMMAND_SEGMENT_ADVANCE_RATIO)); |
| | | if (config == null || Cools.isEmpty(config.getValue())) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | return normalizeSegmentAdvanceRatio(config.getValue()); |
| | | } catch (Exception ignore) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | private boolean isTaskMoveReset(Integer taskNo) { |
| | | if (taskNo == null || redisUtil == null) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | private double normalizeSegmentAdvanceRatio(String valueText) { |
| | | if (valueText == null) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | String text = valueText.trim(); |
| | | if (text.isEmpty()) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | if (text.endsWith("%")) { |
| | | text = text.substring(0, text.length() - 1).trim(); |
| | | } |
| | | try { |
| | | double ratio = Double.parseDouble(text); |
| | | if (ratio > 1d && ratio <= 100d) { |
| | | ratio = ratio / 100d; |
| | | } |
| | | if (ratio < 0d) { |
| | | return 0d; |
| | | } |
| | | if (ratio > 1d) { |
| | | return 1d; |
| | | } |
| | | return ratio; |
| | | } catch (Exception ignore) { |
| | | return DEFAULT_STATION_COMMAND_SEGMENT_ADVANCE_RATIO; |
| | | } |
| | | Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo); |
| | | return cancel != null; |
| | | } |
| | | |
| | | private StationProtocol findCurrentStationByTask(Integer taskNo) { |