From 2bdbfd1ac24d4e9a87daf6fb8b99ec32264d5752 Mon Sep 17 00:00:00 2001 From: luxiaotao1123 <t1341870251@163.com> Date: 星期六, 21 十二月 2024 11:25:27 +0800 Subject: [PATCH] # --- zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AvoidWaveCalculator.java | 107 ++++++++++++++++++++++++++++++++++------------------- 1 files changed, 68 insertions(+), 39 deletions(-) diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AvoidWaveCalculator.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AvoidWaveCalculator.java index 411d0da..2a6d863 100644 --- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AvoidWaveCalculator.java +++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AvoidWaveCalculator.java @@ -3,6 +3,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zy.acs.common.utils.Utils; import com.zy.acs.framework.common.Cools; +import com.zy.acs.manager.common.config.RedisProperties; import com.zy.acs.manager.common.utils.MapDataUtils; import com.zy.acs.manager.core.constant.MapDataConstant; import com.zy.acs.manager.core.service.astart.DynamicNodeType; @@ -21,13 +22,15 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.time.StopWatch; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; -import java.io.BufferedReader; -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; @@ -59,6 +62,8 @@ private CodeService codeService; @Autowired private MapService mapService; + @Autowired + private RedisProperties redisProperties; @PostConstruct @SuppressWarnings("all") @@ -80,6 +85,26 @@ }); } + private File pythonFile = null; + + private File loadPythonFile() { + File scriptFile = null; + try { + Resource resource = new ClassPathResource("agv.py"); + + InputStream is = resource.getInputStream(); + scriptFile = File.createTempFile("agv", ".py"); + scriptFile.deleteOnExit(); + + Files.copy(is, scriptFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + scriptFile.setExecutable(true); + } catch (Exception e) { + throw new RuntimeException(e); + } + return scriptFile; + } + public void calcWaveScope() { Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; boolean lockAcquired = false; @@ -93,53 +118,57 @@ return; } + // python 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(); + if (null == pythonFile) { + pythonFile = loadPythonFile(); + } - ProcessBuilder processBuilder = new ProcessBuilder("python" - , "D:\\tmp\\pyagv\\agv4.py" + ProcessBuilder processBuilder = new ProcessBuilder( + "python" // 鎴栬�� "python3" 鍙栧喅浜庣郴缁熼厤缃� + , pythonFile.getAbsolutePath() , String.valueOf(avoidDistance) + , redisProperties.getHost() + , redisProperties.getPassword() + , String.valueOf(redisProperties.getPort()) + , String.valueOf(redisProperties.getIndex()) ); + processBuilder.redirectErrorStream(true); - try { - Process process = processBuilder.start(); + Process process = processBuilder.start(); - // 璇诲彇Python鑴氭湰鐨勮緭鍑� - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - String line; - StringBuilder builder = new StringBuilder(); - while ((line = reader.readLine()) != null) { - builder.append(line); - } - - // 绛夊緟Python鑴氭湰鎵ц瀹屾垚 - int exitCode = process.waitFor(); - if (exitCode != 0) { - System.out.println("Python script exited with error code: " + exitCode); - return; - } - reader.close(); - - if (builder.length() <= 0) { - return; - } - - String result = builder.toString(); - - if (!Cools.isEmpty(result)) { - if (!"1".equals(result)) { - log.error("Fail python"); - } - } - } catch (IOException | InterruptedException e) { - e.printStackTrace(); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + StringBuilder builder = new StringBuilder(); + while ((line = reader.readLine()) != null) { + builder.append(line); } - log.error("python finish {}", System.currentTimeMillis() - startTime); + 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"); + } + } +// log.error("python finish {}", System.currentTimeMillis() - startTime); + + // java // String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev); // String[][] waveMatrix = mapDataDispatcher.initWaveMatrix(lev); // -- Gitblit v1.9.1