root %!s(int64=2) %!d(string=hai) anos
pai
achega
b3293ac054

+ 82 - 0
api-common/src/main/java/api/common/util/OsUtil.java

@@ -0,0 +1,82 @@
+package api.common.util;
+
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+
+import java.net.InetAddress;
+
+/**
+ * 对本地系统进行操作
+ */
+@Slf4j
+public class OsUtil {
+
+    @SneakyThrows
+    public static String getHostName() {
+        final String hostName = InetAddress.getLocalHost().getHostName();
+        log.info("本地系统主机名为:{}", hostName);
+        return hostName;
+    }
+
+    @SneakyThrows
+    public static String getIp() {
+        return InetAddress.getLocalHost().getHostAddress();
+    }
+
+
+    @SneakyThrows
+    public static void exec(String command) {
+        Runtime.getRuntime().exec(command);
+    }
+
+    public static void kill( String command) {
+        exec("echo kill $(ps -ef | grep '" + command + "' | awk '{ print $2 }')");
+    }
+
+//    @SneakyThrows
+//    public static String execute(String command) {
+//        String result;
+//        log.info("开始执行 linux 命令:" + command);
+//        Runtime run = Runtime.getRuntime();
+//        Process process = run.exec(command);
+//        InputStream in = new BufferedInputStream(process.getInputStream());
+//        StringBuilder out = new StringBuilder();
+//        byte[] b = new byte[8192];
+//        for (int n; (n = in.read(b)) != -1; ) {
+//            out.append(new String(b, 0, n));
+//        }
+//        in.close();
+//        process.destroy();
+//        result = out.toString();
+//        log.info("执行结果为:" + result);
+//        return result;
+//    }
+//
+//    /**
+//     * 指定工作目录执行目录
+//     *
+//     * @param command   命令
+//     * @param directory 工作目录
+//     * @return 执行结果
+//     */
+//    @SneakyThrows
+//    public static String execute(String command, String directory) {
+//        String result;
+//        log.info("执行本地操作系统命令:{}", command);
+//        Runtime run = Runtime.getRuntime();
+//        Process process = run.exec(command, null, new File(directory));
+//        InputStream in = new BufferedInputStream(process.getInputStream());
+//        StringBuilder out = new StringBuilder();
+//        byte[] b = new byte[8192];
+//        for (int n; (n = in.read(b)) != -1; ) {
+//            out.append(new String(b, 0, n));
+//        }
+//        in.close();
+//        process.destroy();
+//        result = out.toString();
+//        log.info("执行结果为:{}", result);
+//        return result;
+//    }
+
+
+}

+ 28 - 14
simulation-resource-video/src/main/java/com/css/simulation/resource/video/service/VideoService.java

@@ -3,6 +3,7 @@ package com.css.simulation.resource.video.service;
 import api.common.pojo.po.scene.VehicleTypePO;
 import api.common.util.FileUtil;
 import api.common.util.LinuxUtil;
+import api.common.util.OsUtil;
 import api.common.util.StringUtil;
 import com.css.simulation.resource.video.configuration.redis.CustomRedisClient;
 import com.css.simulation.resource.video.entity.VehicleEntity;
@@ -10,7 +11,6 @@ import com.css.simulation.resource.video.mapper.ConfigMapper;
 import com.css.simulation.resource.video.mapper.SimulationAutomaticProjectMapper;
 import com.css.simulation.resource.video.mapper.VehicleMapper;
 import com.css.simulation.resource.video.util.MinioUtil;
-import com.css.simulation.resource.video.util.OsUtil;
 import io.minio.MinioClient;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
@@ -105,39 +105,53 @@ public class VideoService {
             MinioUtil.downloadToFile(minioClient, bucketName, csv2PathOfMinio, csv2PathOfLinux);
 
             log.info("生成 xosc 文件。");
-            String xoscPath = generateXosc(rootDirectoryPathOfLinux, xodrPathOfLinux, osgbPathOfLinux, projectId, projectType);
+            String xoscPath = generateXosc(rootDirectoryPathOfLinux, xodrPathOfLinux, osgbPathOfLinux, projectId,
+                    projectType);
             log.info("启动虚拟窗口并通过 esmini 截图。");
             String pictureDirectoryPath = rootDirectoryPathOfLinux + "picture";
             FileUtil.createDirectory(pictureDirectoryPath);
-            String esminiCommandTemp = esminiCommand + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double.parseDouble(maxSimulationTime), 2);
             // 获取屏幕号创建新的虚拟屏幕
             final long newScreenNum = customRedisClient.increment("screen-number", 1);
             log.info("获取新的屏幕号:" + newScreenNum);
-            final String xvfbCommand2 = xvfbCommand.replaceAll("screenNum", newScreenNum + "");
-            LinuxUtil.execute(xvfbCommand2);
-            LinuxUtil.execute(esminiCommandTemp);
+            final String xvfbCommand2 = xvfbCommand.replaceAll("screen-num", newScreenNum + "");
+            final String esminiCommand2 = esminiCommand.replaceAll("screen-num", newScreenNum + "")
+                    + " " + xoscPath + " " + pictureDirectoryPath + "/screenshot " + StringUtil.doubleToString(Double
+                    .parseDouble(maxSimulationTime), 2);
+            OsUtil.exec( xvfbCommand2);
+            OsUtil.exec( esminiCommand2);
+
+//            LinuxUtil.execute(xvfbCommand2);
+//            LinuxUtil.execute(esminiCommand2);
             log.info("删除 esmini 进程。");
-            LinuxUtil.kill(esminiCommandTemp);
+            OsUtil.kill( esminiCommand2);
+//            LinuxUtil.kill(esminiCommand2);
+            StringBuilder removeCommand = new StringBuilder();
+            removeCommand.append("rm -f ");
             int num = 14;
             for (int i = 0; i < num; i++) {
-                String remove = "rm -f " + pictureDirectoryPath + "/screenshot_0000" + i + ".tga";
-                LinuxUtil.execute(remove);
+                removeCommand.append(pictureDirectoryPath).append("/screenshot_0000").append(i).append(".tga ");
             }
+            OsUtil.kill( removeCommand.toString());
             log.info("生成视频。");
             String videoTargetPathOfLinux = rootDirectoryPathOfLinux + "video/" + videoName;
             FileUtil.createParentDirectory(videoTargetPathOfLinux);
             String videoTargetPathOfMinio = rootDirectoryPathOfMinio + videoName;
-            LinuxUtil.execute("ffmpeg -f image2 -framerate 30 -start_number " + num
-                    + " -i " + pictureDirectoryPath + "/screenshot_%05d.tga"
-                    + " -c:v libx264 -vf format=yuv420p -crf 20 " + videoTargetPathOfLinux);
+            OsUtil.exec( "ffmpeg -f image2 -framerate 30 -start_number " + num + " -i "
+                    + pictureDirectoryPath + "/screenshot_%05d.tga" + " -c:v libx264 -vf format=yuv420p -crf 20 "
+                    + videoTargetPathOfLinux);
+//            LinuxUtil.execute("ffmpeg -f image2 -framerate 30 -start_number " + num
+//                    + " -i " + pictureDirectoryPath + "/screenshot_%05d.tga"
+//                    + " -c:v libx264 -vf format=yuv420p -crf 20 " + videoTargetPathOfLinux);
             //删除生成的临时文件
             String removeAll = "rm -rf " + pictureDirectoryPath;
             log.info("删除全部图片==" + removeAll);
-            LinuxUtil.execute(removeAll);
+            OsUtil.exec( removeAll);
+//            LinuxUtil.execute(removeAll);
             //6 将视频上传到 minio
             MinioUtil.uploadFromFile(minioClient, videoTargetPathOfLinux, bucketName, videoTargetPathOfMinio);
             log.info("上传成功:" + videoTargetPathOfMinio);
-            LinuxUtil.kill(xvfbCommand2);
+            OsUtil.kill(xvfbCommand2);
+//            LinuxUtil.kill(xvfbCommand2);
             //* -------------------------------- 删除临时文件 --------------------------------
 //        FileUtil.rm(xodrPathOfLinux);
 //        FileUtil.rm(osgbPathOfLinux);

+ 0 - 80
simulation-resource-video/src/main/java/com/css/simulation/resource/video/util/OsUtil.java

@@ -1,80 +0,0 @@
-package com.css.simulation.resource.video.util;
-
-import lombok.SneakyThrows;
-import lombok.extern.slf4j.Slf4j;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.InputStream;
-import java.net.InetAddress;
-
-/**
- * 对本地系统进行操作
- */
-@Slf4j
-public class OsUtil {
-
-    @SneakyThrows
-    public static String getHostName() {
-        final String hostName = InetAddress.getLocalHost().getHostName();
-        log.info("本地系统主机名为:{}", hostName);
-        return hostName;
-    }
-
-    @SneakyThrows
-    public static String getIp() {
-        return InetAddress.getLocalHost().getHostAddress();
-    }
-
-    @SneakyThrows
-    public static String execute(String command) {
-        String result;
-        log.info("开始执行 linux 命令:" + command);
-        Runtime run = Runtime.getRuntime();
-        Process process = run.exec(command);
-        InputStream in = new BufferedInputStream(process.getInputStream());
-        StringBuilder out = new StringBuilder();
-        byte[] b = new byte[8192];
-        for (int n; (n = in.read(b)) != -1; ) {
-            out.append(new String(b, 0, n));
-        }
-        in.close();
-        process.destroy();
-        result = out.toString();
-        log.info("执行结果为:" + result);
-        return result;
-    }
-
-    /**
-     * 指定工作目录执行目录
-     *
-     * @param command   命令
-     * @param directory 工作目录
-     * @return 执行结果
-     */
-    @SneakyThrows
-    public static String execute(String command, String directory) {
-        String result;
-        log.info("执行本地操作系统命令:{}", command);
-        Runtime run = Runtime.getRuntime();
-        Process process = run.exec(command, null, new File(directory));
-        InputStream in = new BufferedInputStream(process.getInputStream());
-        StringBuilder out = new StringBuilder();
-        byte[] b = new byte[8192];
-        for (int n; (n = in.read(b)) != -1; ) {
-            out.append(new String(b, 0, n));
-        }
-        in.close();
-        process.destroy();
-        result = out.toString();
-        log.info("执行结果为:{}", result);
-        return result;
-    }
-
-
-    public static String kill(String command) {
-        return execute("echo kill $(ps -ef | grep '" + command + "' | awk '{ print $2 }')");
-    }
-
-
-}