李春阳 1 year ago
parent
commit
6933eb344c

+ 108 - 8
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneEvaluationRuleService.java

@@ -2,18 +2,13 @@ package com.css.simulation.resource.server.app.service;
 
 import api.common.pojo.common.ResponseBodyVO;
 import api.common.pojo.constants.DictConstants;
-import api.common.pojo.param.scene.SceneEvaluationDetailParam;
-import api.common.pojo.param.scene.SceneEvaluationForListParam;
-import api.common.pojo.param.scene.SceneEvaluationRuleParam;
-import api.common.pojo.param.scene.SceneEvaluationScriptParam;
+import api.common.pojo.param.MinioParameter;
+import api.common.pojo.param.scene.*;
 import api.common.pojo.po.scene.SceneComplexityPO;
 import api.common.pojo.po.scene.SceneEvaluationOperatePO;
 import api.common.pojo.po.scene.SceneEvaluationRulePO;
 import api.common.pojo.po.scene.SceneRiskPO;
-import api.common.util.LogUtil;
-import api.common.util.PythonUtil;
-import api.common.util.StringUtil;
-import api.common.util.TimeUtil;
+import api.common.util.*;
 import com.alibaba.druid.util.StringUtils;
 import com.css.simulation.resource.server.infra.db.mysql.mapper.SceneComplexityMapper;
 import com.css.simulation.resource.server.infra.db.mysql.mapper.SceneEvaluationRuleMapper;
@@ -21,13 +16,16 @@ import com.css.simulation.resource.server.infra.db.mysql.mapper.ScenePackageMapp
 import com.css.simulation.resource.server.infra.db.mysql.mapper.SceneRiskMapper;
 import com.css.simulation.resource.server.infra.feign.service.FileDownService;
 import com.css.simulation.resource.server.infra.util.AuthUtil;
+import feign.Response;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.*;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
@@ -38,6 +36,13 @@ import java.util.stream.Collectors;
 @Service
 public class SceneEvaluationRuleService {
 
+
+//    @Value("${server.linux-path.temp}")
+    private String linuxTempPath = "";
+//
+//    @Value("${server.scene-evaluation.script}")
+    private String sceneEvaluationScript = "";
+
     @Resource
     private ScenePackageMapper scenePackageMapper;
 
@@ -203,4 +208,99 @@ public class SceneEvaluationRuleService {
         return SceneEvaluationOperatePO.builder().sceneComplexityPOMap(complexityPOMap).sceneRiskPOMap(riskPOMap).build();
     }
 
+
+    /**
+     * 场景上传计算复杂度
+     *
+     * @param param
+     */
+    public void computeSceneReference(SceneImportParam param, String sceneId, String taskId, String computeType, String evaluationPath) {
+        if (StringUtils.isEmpty(param.getSceneEvaluationRuleId())) {
+            return;
+        }
+        String ruleId = param.getSceneEvaluationRuleId();
+        // 获取场景评价规则
+        SceneEvaluationRulePO sceneEvaluationRulePO = sceneEvaluationRuleMapper.querySceneEvaluationPyById(ruleId);
+        if (sceneEvaluationRulePO == null) {
+            log.error(ruleId + " 的场景评价规则已删除");
+            return;
+        }
+        // 1 判断有没有用户目录,没有则复制
+        String evaluationDirectoryOfUser = linuxTempPath + "scene/evaluation/" + taskId + "/";
+        String scriptsPath = evaluationDirectoryOfUser + "scripts/";
+        if (!new File(evaluationDirectoryOfUser).exists()) {
+            // 创建临时目录
+            FileUtil.createDirectory(evaluationDirectoryOfUser);
+            // 2 将场景评价规则脚本保存到 script 目录
+            FileUtil.createDirectory(scriptsPath);
+            final ArrayList<String> scriptFilePath = CollectionUtil.createArrayList(
+                    "/data_preprocessing.py",
+                    "/elevation.py",
+                    "/scenario_criticality_algorithm.py",
+                    "/scenario_evaluation_main.py",
+                    "/scenario_evaluation_utils52.py"
+
+            );
+            for (String pyFilePath : scriptFilePath) {
+                Response download = fileDownService.download(MinioParameter.builder().objectName(sceneEvaluationScript + pyFilePath).build());
+                String pyStr = download.body().toString();
+                download.close();
+                FileUtil.writeStringToLocalFile(pyStr, scriptsPath + pyFilePath);
+            }
+        }
+        // 下载场景评价脚本到脚本目录
+        if (sceneEvaluationRulePO.getScriptPath() == null) {
+            return;
+        }
+        String pyMainPath = scriptsPath + "/" + sceneEvaluationRulePO.getRuleId() + ".py";
+        if (!new File(sceneEvaluationRulePO.getRuleId()).exists()) {
+            Response download = fileDownService.download(MinioParameter.builder().objectName(sceneEvaluationRulePO.getScriptPath()).build());
+            String pyStr = download.body().toString();
+            download.close();
+            FileUtil.writeStringToLocalFile(pyStr, pyMainPath);
+        }
+
+        if (StringUtils.equals(computeType, "1")) {
+            // 计算复杂度,根据场景 id 获取场景信息,下载 osc odr
+
+        } else if (StringUtils.equals(computeType, "2")) {
+            // 计算危险度 从 minio path 下载 csv (ego 和 sensors)
+
+        } else {
+            return;
+        }
+
+
+        String sceneEvaluationCommand = "python3 " + pyMainPath + " " + (evaluationDirectoryOfUser + sceneId);
+        String sceneEvaluationResult;
+//        ScoreEntity evaluationScore = null;
+        log.info("开始执行场景评价命令:" + sceneEvaluationCommand);
+        Runtime r = Runtime.getRuntime();
+        Process p = null;
+        try {
+            p = r.exec(sceneEvaluationCommand, null, new File(evaluationDirectoryOfUser + sceneId));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
+        StringBuilder sb = new StringBuilder();
+        String inline;
+        while (true) {
+            try {
+                if (null == (inline = br.readLine())) break;
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+            sb.append(inline).append("\n");
+        }
+        sceneEvaluationResult = sb.toString();
+        log.info("场景" + sceneId + " 的场景评价结束,结果为:" + sceneEvaluationResult);
+        String replace = StringUtil.replace(sceneEvaluationResult, "'", "\"");
+//        try {
+//            evaluationScore = JsonUtil.jsonToBean(replace, ScoreEntity.class);
+//        } catch (Exception e) { // 打分失败
+//            log.debug("场景" + sceneId + " 的场景评价失败:", e);
+//        }
+    }
+
 }

+ 6 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/server/app/service/SceneReferenceLibService.java

@@ -329,6 +329,12 @@ public class SceneReferenceLibService {
 
                             }
                             successNum += 1;
+                            // 计算复杂度
+                            if (!StringUtils.isEmpty(params.getSceneEvaluationRuleId())) {
+
+
+                            }
+
                         } catch (Exception e) {
                             errorMessage = e.getMessage();
                             log.error("parse scene reference lib file failed.", e);