|
@@ -2,6 +2,8 @@ package com.cn.fdapfe.biz.service.impl;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.time.Instant;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
@@ -15,10 +17,14 @@ import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.cn.fdapfe.biz.domain.Formulas;
|
|
|
import com.cn.fdapfe.biz.mapper.FormulasMapper;
|
|
|
+import com.cn.fdapfe.common.core.domain.AjaxResult;
|
|
|
import com.cn.fdapfe.common.utils.DateUtils;
|
|
|
import com.cn.fdapfe.common.utils.SecurityUtils;
|
|
|
import com.cn.fdapfe.common.utils.StringUtils;
|
|
|
+import com.cn.fdapfe.common.utils.file.FileCopyUtils;
|
|
|
+import com.cn.fdapfe.common.utils.file.FileToMultipartFile;
|
|
|
import com.cn.fdapfe.common.utils.http.HttpUtils;
|
|
|
+import com.cn.fdapfe.web.controller.common.CommonController;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -27,6 +33,7 @@ import org.springframework.stereotype.Service;
|
|
|
import com.cn.fdapfe.biz.mapper.FdAlgorithmMapper;
|
|
|
import com.cn.fdapfe.biz.domain.FdAlgorithm;
|
|
|
import com.cn.fdapfe.biz.service.IFdAlgorithmService;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.script.ScriptEngine;
|
|
@@ -49,6 +56,8 @@ public class FdAlgorithmServiceImpl implements IFdAlgorithmService
|
|
|
private static final long CHECK_INTERVAL = 1000; // 检查间隔1秒(毫秒)
|
|
|
@Resource
|
|
|
private FormulasMapper formulasMapper;
|
|
|
+ @Resource
|
|
|
+ private CommonController commonController;
|
|
|
|
|
|
/**
|
|
|
* 查询故障诊断算法功能验证
|
|
@@ -93,70 +102,57 @@ public class FdAlgorithmServiceImpl implements IFdAlgorithmService
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public int insertFdAlgorithm(FdAlgorithm po) throws IOException, InterruptedException {
|
|
|
+ public int insertFdAlgorithm(FdAlgorithm po) throws Exception {
|
|
|
po.setCreateTime(DateUtils.getNowDate());
|
|
|
po.setCreateBy(SecurityUtils.getUsername());
|
|
|
-
|
|
|
+ ArrayList<String> todoFileUrl = new ArrayList<>();
|
|
|
+ ArrayList<String>jsonDataArray = new ArrayList<>();
|
|
|
// 记录开始执行外部程序的时间
|
|
|
Date startTime = new Date();
|
|
|
po.setStartTime(startTime);
|
|
|
-
|
|
|
- // 启动外部程序
|
|
|
- ExeProcessManager.ExeResult topOneResult = ExeProcessManager.startExe(
|
|
|
- po.getOutputPath() + File.separator + "topOne.exe",
|
|
|
- false
|
|
|
- );
|
|
|
- ExeProcessManager.ExeResult topTwoResult = ExeProcessManager.startExe(
|
|
|
- po.getOutputPath() + File.separator + "topTwo.exe",
|
|
|
- false
|
|
|
- );
|
|
|
- // 记录执行结束时间(两个进程都已执行完毕)
|
|
|
- Date endTime = new Date();
|
|
|
- po.setEndTime(endTime);
|
|
|
-
|
|
|
- // 循环获取并解析输出文件(保留原有逻辑)
|
|
|
- Object json = checkAndParseJson(po.getOutputPath() + File.separator + "output", "return_indicators.json");
|
|
|
- System.err.println(json);
|
|
|
- po.setReturnData(json.toString());
|
|
|
-
|
|
|
- return mapper.insert(po);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 因为文件生成有一定延迟,循环获取生成的文件
|
|
|
- * @param directoryPath
|
|
|
- * @param fileName
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static Object checkAndParseJson(String directoryPath, String fileName) {
|
|
|
- File directory = new File(directoryPath);
|
|
|
- if (!directory.exists() || !directory.isDirectory()) {
|
|
|
- System.err.println("错误:指定目录不存在或不是有效目录 - " + directoryPath);
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- File jsonFile = new File(directory, fileName);
|
|
|
- while (!jsonFile.exists()) {
|
|
|
- System.out.println("等待文件出现:" + fileName);
|
|
|
- try {
|
|
|
- Thread.sleep(CHECK_INTERVAL);
|
|
|
- } catch (InterruptedException e) {
|
|
|
- Thread.currentThread().interrupt();
|
|
|
- System.err.println("检查过程被中断");
|
|
|
- return null;
|
|
|
+ // 将传递的参数解析为数组进行单独取值操作
|
|
|
+ ObjectMapper dataMapper = new ObjectMapper();
|
|
|
+ LinkedHashMap[] array = dataMapper.readValue(po.getPostApiData(), LinkedHashMap[].class);
|
|
|
+ // 循环数组,取出每个参数组
|
|
|
+ for (LinkedHashMap item : array) {
|
|
|
+ // 将上传的文件重命名并复制到当前模型输入目录中
|
|
|
+ FileCopyUtils.copyFile2source(item.get("inputFile").toString(), item.get("inputFilePath").toString());
|
|
|
+ // 将算法配置信息重新写入到算法的input.txt中
|
|
|
+ FileCopyUtils.writeToTxt(item.get("inputPath").toString(), item.get("modelData").toString());
|
|
|
+ // 启动诊断算法对进行诊断
|
|
|
+ ExeProcessManager.startExe(item.get("exePath").toString(), true);
|
|
|
+ // 获取输出目录
|
|
|
+ String outputPath = item.get("outputPath").toString();
|
|
|
+ File outputDir = new File(outputPath);
|
|
|
+ // 检查输出目录是否存在
|
|
|
+ if (outputDir.exists() && outputDir.isDirectory()) {
|
|
|
+ // 获取目录中的所有文件
|
|
|
+ File[] files = outputDir.listFiles();
|
|
|
+ if (files != null) {
|
|
|
+ for (File file : files) {
|
|
|
+ if (file.isFile()) {
|
|
|
+ FileToMultipartFile multipartFile = new FileToMultipartFile(file.getAbsolutePath(), "file");
|
|
|
+ AjaxResult ajaxResult = commonController.customUploadFile(multipartFile, "D:/modelData");
|
|
|
+ // 如果是JSON文件,解析其内容作为返回数据
|
|
|
+ if (ajaxResult.get("fileSuffix").equals("json") || ajaxResult.get("fileSuffix").equals("txt")) {
|
|
|
+ String jsonPath = file.getAbsolutePath();
|
|
|
+ String jsonData = new String(Files.readAllBytes(Paths.get(jsonPath)));
|
|
|
+ jsonDataArray.add(jsonData);
|
|
|
+ }else if(ajaxResult.get("fileSuffix").equals("png") || ajaxResult.get("fileSuffix").equals("jpg")){
|
|
|
+ todoFileUrl.add(ajaxResult.get("todofilePath").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- jsonFile = new File(directory, fileName); // 重新获取文件对象(防止目录变化)
|
|
|
- }
|
|
|
-
|
|
|
- System.out.println("找到文件:" + jsonFile.getAbsolutePath());
|
|
|
- try {
|
|
|
- return new ObjectMapper().readValue(jsonFile, Object.class);
|
|
|
- } catch (IOException e) {
|
|
|
- System.err.println("解析错误:" + e.getMessage());
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
}
|
|
|
+ // 将算法生成的图片结果集转换成JSONString进行入库前准备,图片结果集
|
|
|
+ po.setReturnDataImage(JSON.toJSONString(todoFileUrl));
|
|
|
+ //json 文件结果集
|
|
|
+ po.setReturnData(JSON.toJSONString(jsonDataArray));
|
|
|
+ return mapper.insert(po);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 修改故障诊断算法功能验证
|
|
|
*
|
|
@@ -168,7 +164,7 @@ public class FdAlgorithmServiceImpl implements IFdAlgorithmService
|
|
|
FdAlgorithm id = mapper.selectById(po.getId());
|
|
|
po.setUpdateTime(DateUtils.getNowDate());
|
|
|
po.setUpdateBy(SecurityUtils.getUsername());
|
|
|
- return id == null? insertFdAlgorithm(po): mapper.updateById(po);
|
|
|
+ return id == null? mapper.insert(po): mapper.updateById(po);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -200,7 +196,7 @@ public class FdAlgorithmServiceImpl implements IFdAlgorithmService
|
|
|
String returnData = po.getReturnData();
|
|
|
JSONArray dataArray = convertToStandardJson(returnData);
|
|
|
|
|
|
- // 创建 JavaScript 脚本引擎
|
|
|
+ // 创建 JavaScript 脚本
|
|
|
ScriptEngineManager manager = new ScriptEngineManager();
|
|
|
ScriptEngine engine = manager.getEngineByName("JavaScript");
|
|
|
Map<String, List<Object>> resultMap = new HashMap<>();
|