| | |
| | | AgvModel agvModel = agvModelService.selectByType(AgvModelType.CTU_BOX_TRANSPORT_AGV.toString()); // can be optimized |
| | | Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModel.getDiameter(), MapDataConstant.MAX_DISTANCE_BETWEEN_ADJACENT_AGV_FACTOR); |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | Resource resource = new ClassPathResource("agv.py"); |
| | | |
| | | File tempScript = null; |
| | | |
| | | try { |
| | | InputStream is = resource.getInputStream(); |
| | | tempScript = File.createTempFile("agv", ".py"); |
| | | tempScript.deleteOnExit(); |
| | | InputStream is = resource.getInputStream(); |
| | | tempScript = File.createTempFile("agv", ".py"); |
| | | tempScript.deleteOnExit(); |
| | | |
| | | Files.copy(is, tempScript.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| | | tempScript.setExecutable(true); |
| | | Files.copy(is, tempScript.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| | | tempScript.setExecutable(true); |
| | | |
| | | ProcessBuilder processBuilder = new ProcessBuilder( |
| | | "python" // 或者 "python3" 取决于系统配置 |
| | | , tempScript.getAbsolutePath() |
| | | , String.valueOf(avoidDistance) |
| | | , redisProperties.getHost() |
| | | , redisProperties.getPassword() |
| | | , String.valueOf(redisProperties.getPort()) |
| | | , String.valueOf(redisProperties.getIndex()) |
| | | ); |
| | | ProcessBuilder processBuilder = new ProcessBuilder( |
| | | "python" // 或者 "python3" 取决于系统配置 |
| | | , tempScript.getAbsolutePath() |
| | | , String.valueOf(avoidDistance) |
| | | , redisProperties.getHost() |
| | | , redisProperties.getPassword() |
| | | , String.valueOf(redisProperties.getPort()) |
| | | , String.valueOf(redisProperties.getIndex()) |
| | | ); |
| | | |
| | | processBuilder.redirectErrorStream(true); |
| | | processBuilder.redirectErrorStream(true); |
| | | |
| | | // File file = resource.getFile(); |
| | | // ProcessBuilder processBuilder = new ProcessBuilder( |
| | | // "python" |
| | | // , file.getAbsolutePath() |
| | | // , String.valueOf(avoidDistance) |
| | | // , redisProperties.getHost() |
| | | // , redisProperties.getPassword() |
| | | // , String.valueOf(redisProperties.getPort()) |
| | | // , String.valueOf(redisProperties.getIndex()) |
| | | // ); |
| | | // processBuilder.redirectErrorStream(true); |
| | | Process process = processBuilder.start(); |
| | | |
| | | Process process = processBuilder.start(); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| | | String line; |
| | | StringBuilder builder = new StringBuilder(); |
| | | while ((line = reader.readLine()) != null) { |
| | | builder.append(line); |
| | | } |
| | | |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| | | String line; |
| | | StringBuilder builder = new StringBuilder(); |
| | | while ((line = reader.readLine()) != null) { |
| | | builder.append(line); |
| | | int exitCode = process.waitFor(); |
| | | if (exitCode != 0) { |
| | | log.error("Python script exited with error code: {}", exitCode); |
| | | log.error("python error:{}", builder.toString()); |
| | | return; |
| | | } |
| | | reader.close(); |
| | | |
| | | if (builder.length() <= 0) { |
| | | return; |
| | | } |
| | | |
| | | String result = builder.toString(); |
| | | |
| | | if (!Cools.isEmpty(result)) { |
| | | if (!"1".equals(result)) { |
| | | log.error("Failed to call python"); |
| | | } |
| | | |
| | | int exitCode = process.waitFor(); |
| | | if (exitCode != 0) { |
| | | log.error("Python script exited with error code: {}", exitCode); |
| | | log.error("python error:{}", builder.toString()); |
| | | return; |
| | | } |
| | | reader.close(); |
| | | |
| | | if (builder.length() <= 0) { |
| | | return; |
| | | } |
| | | |
| | | String result = builder.toString(); |
| | | |
| | | if (!Cools.isEmpty(result)) { |
| | | if (!"1".equals(result)) { |
| | | log.error("Failed to call python"); |
| | | } |
| | | } |
| | | } catch (IOException | InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | // log.error("python finish {}", System.currentTimeMillis() - startTime); |
| | | |