|
@@ -1,10 +1,14 @@
|
|
|
package com.cn.fdapfe.biz.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.cn.fdapfe.biz.domain.Data;
|
|
|
import com.cn.fdapfe.biz.domain.FaultPhysicalModel;
|
|
|
import com.cn.fdapfe.biz.mapper.FaultPhysicalModelMapper;
|
|
|
import com.cn.fdapfe.biz.service.IDataService;
|
|
|
+import com.cn.fdapfe.common.core.domain.AjaxResult;
|
|
|
+import com.cn.fdapfe.common.utils.FileToJsonConverter;
|
|
|
+import com.cn.fdapfe.common.utils.file.FileToMultipartFile;
|
|
|
import com.cn.fdapfe.common.utils.http.HttpClientUtils;
|
|
|
import com.cn.fdapfe.web.controller.common.CommonController;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
@@ -20,6 +24,8 @@ import com.cn.fdapfe.biz.service.IModelDataGenService;
|
|
|
import com.cn.fdapfe.common.utils.DateUtils;
|
|
|
import com.cn.fdapfe.common.utils.SecurityUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
@@ -48,10 +54,9 @@ public class ModelDataGenServiceImpl implements IModelDataGenService {
|
|
|
private IDataService dataService;
|
|
|
@Resource
|
|
|
private FaultPhysicalModelMapper faultPhysicalModelMapper;
|
|
|
-
|
|
|
-
|
|
|
@Resource
|
|
|
private CommonController commonController;
|
|
|
+
|
|
|
private static final Pattern FILE_PATH_PATTERN = Pattern.compile(
|
|
|
// Windows路径(支持斜杠或反斜杠)
|
|
|
"^([a-zA-Z]:(?:[/\\\\](?:[^/\\\\:*?\"<>|\r\n]+))*[/\\\\]?[^/\\\\:*?\"<>|\r\n]*)$|" +
|
|
@@ -106,32 +111,95 @@ public class ModelDataGenServiceImpl implements IModelDataGenService {
|
|
|
@Override
|
|
|
public int add(ModelDataGen po) throws IOException {
|
|
|
FaultPhysicalModel model = faultPhysicalModelMapper.selectById(po.getModelId());
|
|
|
+
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
LinkedHashMap[] array = mapper.readValue(po.getPostApiData(), LinkedHashMap[].class);
|
|
|
for (LinkedHashMap item : array) {
|
|
|
- Map map = processMap(item);
|
|
|
- Map returnMap = HttpClientUtils.sendPostRequest(model.getModelPath(), map);
|
|
|
+ Data data = new Data();
|
|
|
+ if(model.getModelAttribution().equals("0")){
|
|
|
+ try {
|
|
|
+ //启动模型开始生成
|
|
|
+ startExe(item.get("exePath").toString());
|
|
|
+ //判断是否进行加噪
|
|
|
+ if(item.get("isNoise").toString().equals("true")){
|
|
|
+ //如果加噪,根据模型id查询
|
|
|
+ FaultPhysicalModel noiseModel = faultPhysicalModelMapper.selectById(item.get("noiseModel").toString());
|
|
|
+ HashMap<String, Object> noiseMap = new HashMap<>();
|
|
|
+ noiseMap.put("noiseType",item.get("noiseType").toString());
|
|
|
+ noiseMap.put("noiseFilePath",item.get("outputPath").toString());
|
|
|
+ //调用噪声算法进行加噪
|
|
|
+ sendPostRequest(noiseModel.getModelPath(),JSON.toJSONString(noiseMap));
|
|
|
+ //如果加噪声了数据类型就是噪声数据
|
|
|
+ data.setType("3");
|
|
|
+ }
|
|
|
+ //没有加噪声了数据类型就是物理模型数据数据
|
|
|
+ data.setType("0");
|
|
|
+ //将文件进行备份存储
|
|
|
+ FileToMultipartFile file = new FileToMultipartFile(item.get("outputPath").toString(), "file");
|
|
|
+ AjaxResult ajaxResult = commonController.customUploadFile(file, "D:/modelData");
|
|
|
+ data.setUrl(ajaxResult.get("url").toString());
|
|
|
+ data.setName(po.getDataGenName());
|
|
|
+ data.setFileName(ajaxResult.get("originalFilename").toString());
|
|
|
+ data.setFileSize(ajaxResult.get("fileSize").toString());
|
|
|
+ data.setFileSizeBytes(ajaxResult.get("fileSizeBytes").toString());
|
|
|
+ data.setFileSuffix(ajaxResult.get("fileSuffix").toString());
|
|
|
+ dataService.insertData(data);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }else if(model.getModelAttribution().equals("2")){
|
|
|
+ //直接调用噪声,手动上传的为实际数据
|
|
|
+ data.setType("1");
|
|
|
+ String key = processMap(item);
|
|
|
+ HashMap<String, Object> noiseMap = new HashMap<>();
|
|
|
+ noiseMap.put("noiseType",item.get("noiseType").toString());
|
|
|
+ noiseMap.put(key,item.get(key).toString());
|
|
|
+ FaultPhysicalModel noiseModel = faultPhysicalModelMapper.selectById(po.getModelId());
|
|
|
+ //调用噪声算法进行加噪
|
|
|
+ sendPostRequest(noiseModel.getModelPath(),JSON.toJSONString(noiseMap));
|
|
|
+ //将文件进行备份存储
|
|
|
+ FileToMultipartFile file = new FileToMultipartFile(item.get("outputPath").toString(), "file");
|
|
|
+ try {
|
|
|
+ AjaxResult ajaxResult = commonController.customUploadFile(file, "D:/modelData");
|
|
|
+ data.setUrl(ajaxResult.get("url").toString());
|
|
|
+ data.setName(po.getDataGenName());
|
|
|
+ data.setFileName(ajaxResult.get("originalFilename").toString());
|
|
|
+ data.setFileSize(ajaxResult.get("fileSize").toString());
|
|
|
+ data.setFileSizeBytes(ajaxResult.get("fileSizeBytes").toString());
|
|
|
+ data.setFileSuffix(ajaxResult.get("fileSuffix").toString());
|
|
|
+ dataService.insertData(data);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return 1;
|
|
|
}
|
|
|
+ public static void startExe(String exePath) throws IOException, InterruptedException {
|
|
|
+ ProcessBuilder processBuilder = new ProcessBuilder(exePath);
|
|
|
+ Process process = processBuilder.start();
|
|
|
+ int exitCode = process.waitFor();
|
|
|
+ logger.info("程序退出码: {}", exitCode);
|
|
|
+ }
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ String filePath = "C:/Users/15138/Desktop/fea_all.mat"; // 替换为实际的文件路径
|
|
|
+ JSONArray jsonArray = FileToJsonConverter.convertFileToJsonArray(filePath);
|
|
|
+ System.out.println(jsonArray.toString());
|
|
|
+ }
|
|
|
|
|
|
- private Map processMap(LinkedHashMap<String, Object> map) {
|
|
|
+ private String processMap(LinkedHashMap<String, Object> map) {
|
|
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
String key = entry.getKey();
|
|
|
Object value = entry.getValue();
|
|
|
+
|
|
|
// 检查值是否为字符串且符合文件路径格式
|
|
|
- if (value instanceof String) {
|
|
|
- String filePath = (String) value;
|
|
|
- boolean validFilePath = isValidFilePath(filePath);
|
|
|
- if (validFilePath) {
|
|
|
- // 转换为File对象并更新map
|
|
|
- File file = new File(filePath);
|
|
|
- map.put(key, file);
|
|
|
- System.out.println("字段 '" + key + "' 已转换为File对象: " + file);
|
|
|
- }
|
|
|
+ if (value instanceof String && isValidFilePath((String) value)) {
|
|
|
+ return key; // 找到路径参数,返回对应的key
|
|
|
}
|
|
|
}
|
|
|
- return map;
|
|
|
+
|
|
|
+ // 没有找到符合条件的路径参数
|
|
|
+ throw new IllegalArgumentException("Map中未找到有效的文件路径参数");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -166,12 +234,7 @@ public class ModelDataGenServiceImpl implements IModelDataGenService {
|
|
|
return ids.length == 1 ? mapper.deleteById(ids[0]) : mapper.deleteBatchIds(Arrays.asList(ids));
|
|
|
}
|
|
|
|
|
|
- public static void startExe(String exePath) throws IOException, InterruptedException {
|
|
|
- ProcessBuilder processBuilder = new ProcessBuilder(exePath);
|
|
|
- Process process = processBuilder.start();
|
|
|
- int exitCode = process.waitFor();
|
|
|
- logger.info("程序退出码: {}", exitCode);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
public static void writeToTxt(String filePath, String content) throws IOException {
|
|
|
try (FileWriter writer = new FileWriter(filePath, false)) {
|