From 8ad2eaf682c916f76bbf780861fb5fc0716f34f2 Mon Sep 17 00:00:00 2001 From: lilin Date: Fri, 9 Jan 2026 15:37:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=BD=A8=E8=BF=B9=E6=9B=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/TsFilesServiceImpl.java | 89 +++++++++++++++++-- java/src/main/resources/config/trj_config.txt | 6 ++ .../resources/config/trj_config_ins_img.txt | 6 ++ 3 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 java/src/main/resources/config/trj_config.txt create mode 100644 java/src/main/resources/config/trj_config_ins_img.txt diff --git a/java/src/main/java/com/yfd/platform/modules/experimentalData/service/impl/TsFilesServiceImpl.java b/java/src/main/java/com/yfd/platform/modules/experimentalData/service/impl/TsFilesServiceImpl.java index 70007a3..921e70e 100644 --- a/java/src/main/java/com/yfd/platform/modules/experimentalData/service/impl/TsFilesServiceImpl.java +++ b/java/src/main/java/com/yfd/platform/modules/experimentalData/service/impl/TsFilesServiceImpl.java @@ -5434,20 +5434,35 @@ public class TsFilesServiceImpl extends ServiceImpl impl //判断文件后缀是.txt还是.csv String fileName = tsFiles.getFileName(); - TsFiles tsFilesData = tsFilesMapper.selectById(configId); +// TsFiles tsFilesData = tsFilesMapper.selectById(configId); + + // 根据文件后缀确定配置文件路径 + String configResourcePath; + if (fileName.toLowerCase().endsWith(".csv")) { + configResourcePath = "config/trj_config.txt"; + } else if (fileName.toLowerCase().endsWith(".txt")) { + configResourcePath = "config/trj_config_ins_img.txt"; + } else { + throw new IllegalArgumentException("不支持的文件格式: " + fileName + ",仅支持.csv和.txt文件"); + } + + if (currentTaskFuture != null && !currentTaskFuture.isDone()) { currentTaskFuture.cancel(true); } currentTaskFuture = executorService.submit(() -> { try { - // 1. 获取配置文件路径 - StorageSourceConfig config = getStorageSourceConfig("filePath", "local", tsTask.getLocalStorageId()); - Path basePath = Paths.get(config.getValue() + tsFilesData.getWorkPath()); +// // 1. 获取配置文件路径 +// StorageSourceConfig config = getStorageSourceConfig("filePath", "local", tsTask.getLocalStorageId()); +// Path basePath = Paths.get(config.getValue() + tsFilesData.getWorkPath()); +// +// // 2. 读取配置文件(假设配置文件名为 trj_config.txt) +// Path configPath = basePath.resolve(tsFilesData.getFileName()); +// Map columnMapping = parseConfigFile(configPath, token); - // 2. 读取配置文件(假设配置文件名为 trj_config.txt) - Path configPath = basePath.resolve(tsFilesData.getFileName()); - Map columnMapping = parseConfigFile(configPath, token); + // 1. 从资源文件读取配置文件 + Map columnMapping = parseConfigFromResource(configResourcePath, token); String timeColumn = columnMapping.get("time"); String lonColumn = columnMapping.get("lon"); @@ -5458,6 +5473,8 @@ public class TsFilesServiceImpl extends ServiceImpl impl timeColumn, lonColumn, latColumn, hgtColumn); // 3. 获取数据文件路径 + StorageSourceConfig config = getStorageSourceConfig("filePath", "local", tsTask.getLocalStorageId()); + Path basePath = Paths.get(config.getValue() + tsFiles.getWorkPath()); Path dataPath = basePath.resolve(tsFiles.getFileName()); // 4. 处理数据文件 @@ -5477,6 +5494,64 @@ public class TsFilesServiceImpl extends ServiceImpl impl } } + /** + * 从资源文件读取配置文件并解析为列名映射 + */ + public Map parseConfigFromResource(String resourcePath, String token) throws IOException { + Map columnMapping = new HashMap<>(); + + // 使用ClassLoader读取资源文件 + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + try (InputStream inputStream = classLoader.getResourceAsStream(resourcePath); + BufferedReader reader = inputStream != null ? + new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)) : null) { + + if (reader == null) { + throw new IOException("找不到配置文件: " + resourcePath); + } + + String line; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.isEmpty() || line.startsWith("#")) { + continue; + } + + // 分割键值对,支持=和:分隔符 + String[] parts = line.split("[=:]", 2); + if (parts.length < 2) { + LOGGER.warn("忽略无效行: {}", line); + continue; + } + + String key = parts[0].trim().toLowerCase(); + String value = parts[1].trim(); + + // 清理引号 + if (value.startsWith("\"") && value.endsWith("\"")) { + value = value.substring(1, value.length() - 1); + } + + // 只处理需要的键 + if (key.equals("time") || key.equals("lon") || + key.equals("lat") || key.equals("hgt")) { + columnMapping.put(key, value); + } + } + } + + // 验证是否获取到所有必需的列 + if (columnMapping.size() < 4) { + JSONObject jsonResponse = new JSONObject(); + jsonResponse.put("message", "配置文件选择错误,请重新选择!"); + + ServerSendEventServer.sendMessageById(token, jsonResponse.toString()); + throw new IOException("配置文件缺少必需列"); + } + + return columnMapping; + } + // // 封装发送数据的逻辑 // private void sendData(String token, String[] values, int lineCount) { diff --git a/java/src/main/resources/config/trj_config.txt b/java/src/main/resources/config/trj_config.txt new file mode 100644 index 0000000..1058610 --- /dev/null +++ b/java/src/main/resources/config/trj_config.txt @@ -0,0 +1,6 @@ +time: UTC +lon: LonGps +lat: LatGps +hgt: HgtGps + + diff --git a/java/src/main/resources/config/trj_config_ins_img.txt b/java/src/main/resources/config/trj_config_ins_img.txt new file mode 100644 index 0000000..e7a2a7c --- /dev/null +++ b/java/src/main/resources/config/trj_config_ins_img.txt @@ -0,0 +1,6 @@ +time: GPU_TEMP +lon: LON +lat: LAT +hgt: ALT + +