| | |
| | | import com.zy.core.network.DeviceConnectPool; |
| | | import com.zy.core.network.ZyStationConnectDriver; |
| | | import com.zy.core.network.entity.ZyStationStatusEntity; |
| | | import com.zy.core.thread.support.RecentStationArrivalTracker; |
| | | import com.zy.core.utils.DeviceLogRedisKeyBuilder; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | private int deviceLogCollectTime = 200; |
| | | private long deviceDataLogTime = System.currentTimeMillis(); |
| | | private ExecutorService executor = Executors.newFixedThreadPool(9999); |
| | | private final RecentStationArrivalTracker recentArrivalTracker = new RecentStationArrivalTracker(); |
| | | |
| | | public ZyStationV3Thread(DeviceConfig deviceConfig, RedisUtil redisUtil) { |
| | | this.deviceConfig = deviceConfig; |
| | |
| | | stationProtocol.setRunBlock(statusEntity.isRunBlock()); |
| | | stationProtocol.setEnableIn(statusEntity.isEnableIn()); |
| | | stationProtocol.setWeight(statusEntity.getWeight()); |
| | | recentArrivalTracker.observe(statusEntity.getStationId(), statusEntity.getTaskNo(), statusEntity.isLoading()); |
| | | } |
| | | |
| | | if (!Cools.isEmpty(stationProtocol.getSystemWarning())) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public boolean hasRecentArrival(Integer stationId, Integer taskNo) { |
| | | return recentArrivalTracker.hasRecentArrival(stationId, taskNo); |
| | | } |
| | | |
| | | @Override |
| | | public StationCommand getCommand(StationCommandType commandType, Integer taskNo, Integer stationId, Integer targetStationId, Integer palletSize) { |
| | | return getCommand(commandType, taskNo, stationId, targetStationId, palletSize, null); |
| | | } |
| | | |
| | | @Override |
| | | public StationCommand getCommand(StationCommandType commandType, |
| | | Integer taskNo, |
| | | Integer stationId, |
| | | Integer targetStationId, |
| | | Integer palletSize, |
| | | Double pathLenFactor) { |
| | | StationCommand stationCommand = new StationCommand(); |
| | | stationCommand.setTaskNo(taskNo); |
| | | stationCommand.setStationId(stationId); |
| | |
| | | |
| | | if (commandType == StationCommandType.MOVE) { |
| | | if (!stationId.equals(targetStationId)) { |
| | | List<Integer> path = calcPathStationIds(stationId, targetStationId); |
| | | List<Integer> path = calcPathStationIds(taskNo, stationId, targetStationId, pathLenFactor); |
| | | if (path == null || path.isEmpty()) { |
| | | log.warn("输送线命令生成失败,路径为空,taskNo={}, stationId={}, targetStationId={}", |
| | | taskNo, stationId, targetStationId); |
| | | return null; |
| | | } |
| | | stationCommand.setNavigatePath(path); |
| | | } |
| | | } |
| | |
| | | return zyStationConnectDriver.readOriginCommand(address, length); |
| | | } |
| | | |
| | | private List<Integer> calcPathStationIds(Integer startStationId, Integer targetStationId) { |
| | | private List<Integer> calcPathStationIds(Integer taskNo, |
| | | Integer startStationId, |
| | | Integer targetStationId, |
| | | Double pathLenFactor) { |
| | | NavigateUtils navigateUtils = SpringUtils.getBean(NavigateUtils.class); |
| | | if (navigateUtils == null) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<NavigateNode> nodes = navigateUtils.calcByStationId(startStationId, targetStationId); |
| | | List<NavigateNode> nodes = navigateUtils.calcByStationId(startStationId, targetStationId, taskNo, pathLenFactor); |
| | | List<Integer> ids = new ArrayList<>(); |
| | | for (NavigateNode n : nodes) { |
| | | JSONObject v = JSONObject.parseObject(n.getNodeValue()); |
| | |
| | | segCmd.setCommandType(original.getCommandType()); |
| | | segCmd.setPalletSize(original.getPalletSize()); |
| | | segCmd.setNavigatePath(new ArrayList<>(path.subList(0, currentEndIdx + 1))); |
| | | sendCommand(segCmd); |
| | | if (!sendSegmentWithRetry(segCmd, original.getTaskNo())) { |
| | | return; |
| | | } |
| | | |
| | | long runTime = System.currentTimeMillis(); |
| | | boolean firstRun = true; |
| | |
| | | nextCmd.setPalletSize(original.getPalletSize()); |
| | | nextCmd.setNavigatePath(new ArrayList<>(path.subList(currentStartIdx, currentEndIdx + 1))); |
| | | nextCmd.setOriginalNavigatePath(path); |
| | | while (true) { |
| | | CommandResponse commandResponse = sendCommand(nextCmd); |
| | | if (commandResponse == null) { |
| | | Thread.sleep(200); |
| | | continue; |
| | | } |
| | | |
| | | if (commandResponse.getResult()) { |
| | | break; |
| | | } |
| | | |
| | | Thread.sleep(200); |
| | | if (!sendSegmentWithRetry(nextCmd, original.getTaskNo())) { |
| | | break; |
| | | } |
| | | } |
| | | Thread.sleep(500); |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private boolean sendSegmentWithRetry(StationCommand command, Integer taskNo) { |
| | | while (true) { |
| | | if (isTaskMoveReset(taskNo)) { |
| | | return false; |
| | | } |
| | | CommandResponse commandResponse = sendCommand(command); |
| | | if (commandResponse == null) { |
| | | sleepQuietly(200L); |
| | | continue; |
| | | } |
| | | if (commandResponse.getResult()) { |
| | | return true; |
| | | } |
| | | sleepQuietly(200L); |
| | | } |
| | | } |
| | | |
| | | private boolean isTaskMoveReset(Integer taskNo) { |
| | | if (taskNo == null || redisUtil == null) { |
| | | return false; |
| | | } |
| | | Object cancel = redisUtil.get(RedisKeyType.DEVICE_STATION_MOVE_RESET.key + taskNo); |
| | | return cancel != null; |
| | | } |
| | | |
| | | private void sleepQuietly(long millis) { |
| | | try { |
| | | Thread.sleep(millis); |
| | | } catch (Exception ignore) { |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public boolean clearPath(Integer taskNo) { |
| | | return false; |
| | | } |
| | | } |