| | |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import javax.annotation.PreDestroy; |
| | | import java.io.BufferedReader; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.io.*; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.StandardCopyOption; |
| | | import java.util.List; |
| | | import java.util.concurrent.ExecutorService; |
| | | import java.util.concurrent.Executors; |
| | |
| | | |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | Resource codeMatrixResource = new ClassPathResource("codeMatrix.txt"); |
| | | Resource cdaMatrixResource = new ClassPathResource("cdaMatrix.txt"); |
| | | |
| | | File codeMatrixFile = codeMatrixResource.getFile(); |
| | | File cdaMatrixFile = cdaMatrixResource.getFile(); |
| | | |
| | | String codeMatrixPath = codeMatrixFile.getAbsolutePath(); |
| | | String cdaMatrixPath = cdaMatrixFile.getAbsolutePath(); |
| | | |
| | | Resource resource = new ClassPathResource("agv.py"); |
| | | File file = resource.getFile(); |
| | | ProcessBuilder processBuilder = new ProcessBuilder( |
| | | "python" |
| | | , file.getAbsolutePath() |
| | | , String.valueOf(avoidDistance) |
| | | , codeMatrixPath |
| | | , cdaMatrixPath |
| | | , redisProperties.getHost() |
| | | , redisProperties.getPassword() |
| | | , String.valueOf(redisProperties.getPort()) |
| | | , String.valueOf(redisProperties.getIndex()) |
| | | ); |
| | | processBuilder.redirectErrorStream(true); |
| | | |
| | | File tempScript = null; |
| | | |
| | | try { |
| | | InputStream is = resource.getInputStream(); |
| | | tempScript = File.createTempFile("agv", ".py"); |
| | | tempScript.deleteOnExit(); |
| | | |
| | | 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.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(); |
| | | |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| | |
| | | |
| | | int exitCode = process.waitFor(); |
| | | if (exitCode != 0) { |
| | | System.out.println("Python script exited with error code: " + exitCode); |
| | | log.error("Python script exited with error code: {}", exitCode); |
| | | log.error("python error:{}", builder.toString()); |
| | | return; |
| | | } |
| | | reader.close(); |