From ebd2f4397a92c6a5096de1b86d59154363344720 Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期二, 13 五月 2025 08:48:15 +0800
Subject: [PATCH] #

---
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AvoidWaveCalculator.java |  292 ++++++++++++++++++++++++++--------------------------------
 1 files changed, 131 insertions(+), 161 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 2a6d863..4a11335 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
@@ -20,15 +20,18 @@
 import com.zy.acs.manager.manager.service.AgvService;
 import com.zy.acs.manager.manager.service.CodeService;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang.time.StopWatch;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.DependsOn;
 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.*;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
 import java.util.List;
@@ -42,6 +45,7 @@
  */
 @Slf4j
 @Component
+@DependsOn("mapDataDispatcher")
 public class AvoidWaveCalculator {
 
     private static final ReentrantLock lock = new ReentrantLock(Boolean.TRUE);
@@ -49,6 +53,8 @@
     private static final int LOCK_TIMEOUT = 5;
 
     private ExecutorService singleThreadExecutor;
+
+    private File pythonFile = null;
 
     @Autowired
     private MapDataDispatcher mapDataDispatcher;
@@ -70,22 +76,132 @@
     public void execute() {
         this.singleThreadExecutor = Executors.newSingleThreadExecutor();
         this.singleThreadExecutor.execute(() -> {
-
-            try { Thread.sleep(200); } catch (InterruptedException ignore) {}
+            try { Thread.sleep(500); } catch (InterruptedException ignore) {}
 
             this.calcDynamicNodeWhenBoot();
-
-//            while (!Thread.currentThread().isInterrupted()) {
-//
-//                this.calcWaveScope();
-//
-//                try { Thread.sleep(500); } catch (InterruptedException ignore) {}
-//            }
-
         });
     }
 
-    private File pythonFile = null;
+
+    public boolean calcWaveScope() {
+        Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV;
+        boolean lockAcquired = false;
+
+        long startTime = System.currentTimeMillis();
+
+        try {
+            if (!(lockAcquired = lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS))) {
+                log.warn("AvoidWaveCalculator execute fail, cause can not acquire lock ...");
+                return false;
+            }
+
+//            return this.calcWaveScopeByPython(lev);
+            return this.calcWaveScopeByJava(lev);
+
+        } catch (Exception e) {
+
+            log.error(this.getClass().getSimpleName(), e);
+            return false;
+        } finally {
+
+            if (lockAcquired) {
+                lock.unlock();
+            }
+
+            long during = System.currentTimeMillis() - startTime;
+            if (during > 50) {
+                log.info("婊ゆ尝鍑芥暟鑺辫垂鏃堕棿涓猴細{}姣......", during);
+            }
+        }
+    }
+
+    private boolean calcWaveScopeByPython(Integer lev) throws Exception {
+        // 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);
+
+        if (null == pythonFile) {
+            pythonFile = loadPythonFile();
+        }
+
+        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);
+
+        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);
+        }
+
+        int exitCode = process.waitFor();
+        if (exitCode != 0) {
+            log.error("Python script exited with error code: {}", exitCode);
+            log.error("python error:{}", builder.toString());
+            return false;
+        }
+        reader.close();
+
+        if (builder.length() <= 0) {
+            return false;
+        }
+
+        String result = builder.toString();
+
+        if (Cools.isEmpty(result)) {
+            return false;
+        }
+        if (!"1".equals(result)) {
+            log.error("Failed to call python");
+            return false;
+        }
+
+        return true;
+    }
+
+    private boolean calcWaveScopeByJava(Integer lev) throws Exception {
+
+        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);
+
+        // java
+        String[][] codeMatrix = mapDataDispatcher.getCodeMatrix(lev);
+        String[][] waveMatrix = mapDataDispatcher.initWaveMatrix(lev);
+
+        // lock path
+        DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev);
+        for (int i = 0; i < dynamicMatrix.length; i++) {
+            for (int j = 0; j < dynamicMatrix[i].length; j++) {
+                DynamicNode dynamicNode = dynamicMatrix[i][j];
+                String vehicle = dynamicNode.getVehicle();
+                if (!DynamicNodeType.ACCESS.val.equals(vehicle) && !DynamicNodeType.BLOCK.val.equals(vehicle)) {
+
+                    List<NavigateNode> includeList = mapService.getWaveScopeByCode(lev, codeMatrix[i][j], avoidDistance);
+
+                    for (NavigateNode navigateNode : includeList) {
+                        String waveNode = waveMatrix[navigateNode.getX()][navigateNode.getY()]; // overlay
+                        waveMatrix[navigateNode.getX()][navigateNode.getY()] = MapDataUtils.generateWaveNode(waveNode, vehicle);
+                    }
+                }
+            }
+        }
+
+//            mapDataDispatcher.printMatrix(waveMatrix);
+        mapDataDispatcher.setWaveMatrix(lev, waveMatrix);
+
+        return true;
+    }
 
     private File loadPythonFile() {
         File scriptFile = null;
@@ -98,157 +214,11 @@
 
             Files.copy(is, scriptFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
 
-            scriptFile.setExecutable(true);
+            boolean executable = scriptFile.setExecutable(true);
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
         return scriptFile;
-    }
-
-    public void calcWaveScope() {
-        Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV;
-        boolean lockAcquired = false;
-
-        StopWatch stopWatch = new StopWatch();
-        stopWatch.start();
-
-        try {
-            if (!(lockAcquired = lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS))) {
-                log.warn("AvoidWaveCalculator execute fail, cause can not acquire lock ...");
-                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);
-
-            if (null == pythonFile) {
-                pythonFile = loadPythonFile();
-            }
-
-            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);
-
-            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);
-            }
-
-            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);
-//
-//            // lock path
-//            DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev);
-//            for (int i = 0; i < dynamicMatrix.length; i++) {
-//                for (int j = 0; j < dynamicMatrix[i].length; j++) {
-//                    DynamicNode dynamicNode = dynamicMatrix[i][j];
-//                    String vehicle = dynamicNode.getVehicle();
-//                    if (!DynamicNodeType.ACCESS.val.equals(vehicle) && !DynamicNodeType.BLOCK.val.equals(vehicle)) {
-//                        AgvModel agvModel = agvModelService.getById(agvService.selectByUuid(vehicle).getAgvModel());    // can be optimized
-//
-//                        Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModel.getDiameter(), MapDataConstant.MAX_DISTANCE_BETWEEN_ADJACENT_AGV_FACTOR);
-//                        List<NavigateNode> includeList = mapService.getWaveScopeByCode(lev, codeMatrix[i][j], avoidDistance);
-//
-//                        for (NavigateNode navigateNode : includeList) {
-//                            String waveNode = waveMatrix[navigateNode.getX()][navigateNode.getY()]; // overlay
-//                            waveMatrix[navigateNode.getX()][navigateNode.getY()] = MapDataUtils.generateWaveNode(waveNode, vehicle);
-//                        }
-//                    }
-//                }
-//            }
-//
-////            mapDataDispatcher.printMatrix(waveMatrix);
-//            mapDataDispatcher.setWaveMatrix(lev, waveMatrix);
-
-        } catch (Exception e) {
-
-            log.error(this.getClass().getSimpleName(), e);
-        } finally {
-
-            if (lockAcquired) {
-                lock.unlock();
-            }
-
-            stopWatch.stop();
-            if (stopWatch.getTime() > 100) {
-                log.info("婊ゆ尝鍑芥暟鑺辫垂鏃堕棿涓猴細{}姣......", stopWatch.getTime());
-            }
-
-        }
-    }
-
-    public void syncWaveBySingleVeh(String agvNo, String codeData) {
-        if (Cools.isEmpty(agvNo, codeData)) {
-            return;
-        }
-
-        boolean lockAcquired = false;
-        Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV;
-
-        try {
-            if (!(lockAcquired = lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS))) {
-                log.warn("AvoidWaveCalculator syncWaveBySingleVeh fail, cause can not acquire lock ...");
-                return;
-            }
-
-            Agv agv = agvService.selectByUuid(agvNo);
-            AgvModel agvModel = agvModelService.getById(agv.getAgvModel());
-            Double avoidDistance = MapDataUtils.getVehicleWaveSafeDistance(agvModel.getDiameter(), MapDataConstant.MAX_DISTANCE_BETWEEN_ADJACENT_AGV_FACTOR);
-
-            String[][] waveMatrix = mapDataDispatcher.getWaveMatrix(lev);
-
-            List<NavigateNode> includeList = mapService.getWaveScopeByCode(lev, codeData, avoidDistance);
-
-            for (NavigateNode navigateNode : includeList) {
-                String waveNode = waveMatrix[navigateNode.getX()][navigateNode.getY()];
-                waveMatrix[navigateNode.getX()][navigateNode.getY()] = MapDataUtils.generateWaveNode(waveNode, agv.getUuid());
-            }
-
-            mapDataDispatcher.setWaveMatrix(lev, waveMatrix);
-
-        } catch (Exception e) {
-            log.error("AvoidWaveCalculator.syncWaveBySingleVeh fail", e);
-        } finally {
-
-            if (lockAcquired) {
-                lock.unlock();
-            }
-        }
     }
 
     public void calcDynamicNodeWhenBoot() {
@@ -283,7 +253,7 @@
         DynamicNode dynamicNode = dynamicMatrix[codeMatrixIdx[0]][codeMatrixIdx[1]];
         String vehicle = dynamicNode.getVehicle();
         if (vehicle.equals(DynamicNodeType.ACCESS.val)) {
-            mapDataDispatcher.modifyDynamicMatrix(null, Utils.singletonList(code.getData()), agv.getUuid());
+            mapDataDispatcher.modifyDynamicMatrix(null, Utils.singletonList(codeMatrixIdx), agv.getUuid());
         }
     }
 

--
Gitblit v1.9.1