|
@@ -0,0 +1,504 @@
|
|
|
+package com.acquisition.service.DataAcquisition;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.file.*;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class FileProcessorConfigService {
|
|
|
+
|
|
|
+ private final String collectionServiceDir;
|
|
|
+
|
|
|
+ // 构造方法中初始化路径
|
|
|
+ public FileProcessorConfigService() {
|
|
|
+ this.collectionServiceDir = System.getProperty("user.home") + "\\Desktop\\CollectionService";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理数据文件
|
|
|
+ *
|
|
|
+ * @throws Exception 如果处理文件时出现错误
|
|
|
+ */
|
|
|
+
|
|
|
+ public void processFiles() throws Exception {
|
|
|
+ // 读取配置Excel文件
|
|
|
+ List<List<String>> configData = configreadConfigExcel(collectionServiceDir + "\\demo\\config.xlsx");
|
|
|
+
|
|
|
+ // 从第1行开始遍历配置数据,i从0开始
|
|
|
+ for (int i = 1; i < configData.size(); i++) { // i从0开始
|
|
|
+ List<String> rowData = configData.get(i); // 当前行数据
|
|
|
+ String folderNamecj = rowData.get(6); // 第一行第6列数据
|
|
|
+ String folderNamezf = rowData.get(14); // 第一行第14列数据
|
|
|
+ String folderNameredis = rowData.get(20); // 第一行第20列数据
|
|
|
+ String folderNamefs = rowData.get(22); // 第一行第22列数据
|
|
|
+ String folderNamejs = rowData.get(25); // 第一行第25列数据
|
|
|
+
|
|
|
+ String dataFolderPathcj = collectionServiceDir + "\\data" + (i) + "\\" + folderNamecj; // data1, data2, ...
|
|
|
+ String dataFolderPathzf = collectionServiceDir + "\\data" + (i) + "\\" + folderNamezf;
|
|
|
+ String dataFolderPathredis = collectionServiceDir + "\\data" + (i) + "\\" + folderNameredis;
|
|
|
+ String dataFolderPathfs = collectionServiceDir + "\\data" + (i) + "\\" + folderNamefs;
|
|
|
+ String dataFolderPathjs = collectionServiceDir + "\\data" + (i) + "\\" + folderNamejs;
|
|
|
+
|
|
|
+ // 获取该行对应的配置总表路径
|
|
|
+ String configExcelPath = collectionServiceDir + "\\data" + (i) + "\\配置总表.xlsx";
|
|
|
+
|
|
|
+ // 1. 读取配置总表.xlsx中的数据并生成sqlite3文件
|
|
|
+ List<List<String>> configExcelData = readConfigExcel(configExcelPath);
|
|
|
+
|
|
|
+ // 获取并传递需要的列数据给generateKeyTableFile方法
|
|
|
+ // 处理采集第2列到第10列的数据
|
|
|
+ if (!folderNamecj.equals("无")) {
|
|
|
+ cjmodifyJsonConfigFile(dataFolderPathcj, "wormholed.json", folderNamecj, rowData.get(28)); // 修改json文件,使用第28列数据作为插件
|
|
|
+ JsonpluginFile(dataFolderPathcj, rowData.get(28) + ".json", rowData.get(7), rowData.get(9), rowData.get(13), rowData.get(11));
|
|
|
+ generateKeyTableFile(dataFolderPathcj, configExcelData, 2, 10); // 读取第2列到第10列数据
|
|
|
+ }
|
|
|
+ //处理转发第11列到第19列的数据
|
|
|
+ if (!folderNamezf.equals("无")) {
|
|
|
+ modifyJsonConfigFile(dataFolderPathzf, "wormholed.json", folderNamezf); // 修改json文件
|
|
|
+ zfJsonpluginFile(dataFolderPathzf, null, rowData.get(15), rowData.get(16), rowData.get(17));
|
|
|
+ generateKeyTableFile(dataFolderPathzf, configExcelData, 11, 19); // 读取第11列到第19列数据
|
|
|
+ }
|
|
|
+ //处理redis第20列到第24列的数据
|
|
|
+ if (!folderNameredis.equals("无")) {
|
|
|
+ modifyJsonConfigFile(dataFolderPathredis, "wormholed.json", folderNameredis); // 修改json文件,修改pwd为第14列
|
|
|
+ redisJsonpluginFile(dataFolderPathredis, rowData.get(19), rowData.get(27));
|
|
|
+ generateKeyTableFile(dataFolderPathredis, configExcelData, 20, 24); // 读取20列到第24列的数据
|
|
|
+ }
|
|
|
+ //处理发送第25列到第29列的数据
|
|
|
+ if (!folderNamefs.equals("无")) {
|
|
|
+ modifyJsonConfigFile(dataFolderPathfs, "wormholed.json", folderNamefs); // 修改json文件,修改pwd为第14列
|
|
|
+ fsJsonpluginFile(dataFolderPathfs, rowData.get(23), rowData.get(24));
|
|
|
+ generateKeyTableFile(dataFolderPathfs, configExcelData, 25, 29); // 读取第25列到第29列的数据
|
|
|
+ }
|
|
|
+ //处理接收第30列到第34列的数据
|
|
|
+ if (!folderNamejs.equals("无")) {
|
|
|
+ modifyJsonConfigFile(dataFolderPathjs, "wormholed.json", folderNamejs); // 修改json文件,修改pwd为第14列
|
|
|
+ jsJsonpluginFile(dataFolderPathjs, rowData.get(26));
|
|
|
+ generateKeyTableFile(dataFolderPathjs, configExcelData, 30, 34); // 读取第30列到第34列的数据
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //读取Excel文件中的数据
|
|
|
+ public List<List<String>> configreadConfigExcel(String excelPath) throws Exception {
|
|
|
+ List<List<String>> data = new ArrayList<>();
|
|
|
+
|
|
|
+ try (FileInputStream fis = new FileInputStream(excelPath);
|
|
|
+ Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
+ Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
|
|
|
+ int maxColumn = getMaxColumnCount(sheet); // 获取最大列数
|
|
|
+
|
|
|
+ for (Row row : sheet) {
|
|
|
+ List<String> rowData = new ArrayList<>();
|
|
|
+ if (row != null) { // 添加非空检查
|
|
|
+ for (int i = 0; i < maxColumn; i++) {
|
|
|
+ Cell cell = row.getCell(i);
|
|
|
+ String cellValue = getCellValueAsString(cell);
|
|
|
+ rowData.add(cellValue);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (int i = 0; i < maxColumn; i++) {
|
|
|
+ rowData.add("无");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ data.add(rowData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取最大列数
|
|
|
+ private int getMaxColumnCount(Sheet sheet) {
|
|
|
+ int maxColumn = 0;
|
|
|
+ for (Row row : sheet) {
|
|
|
+ if (row != null) {
|
|
|
+ maxColumn = Math.max(maxColumn, row.getLastCellNum());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return maxColumn;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String getCellValueAsString(Cell cell) {
|
|
|
+ if (cell == null || cell.getCellType() == CellType.BLANK) {
|
|
|
+ return "无";
|
|
|
+ }
|
|
|
+
|
|
|
+ switch (cell.getCellType()) {
|
|
|
+ case STRING:
|
|
|
+ return cell.getStringCellValue().trim();
|
|
|
+ case NUMERIC:
|
|
|
+ if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
+ return cell.getDateCellValue().toString().trim();
|
|
|
+ } else {
|
|
|
+ return String.valueOf(cell.getNumericCellValue()).trim();
|
|
|
+ }
|
|
|
+ case BOOLEAN:
|
|
|
+ return String.valueOf(cell.getBooleanCellValue()).trim();
|
|
|
+ case FORMULA:
|
|
|
+ return cell.getCellFormula().trim();
|
|
|
+ default:
|
|
|
+ return cell.toString().trim();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //读取Excel文件
|
|
|
+ public List<List<String>> readConfigExcel(String excelPath) throws Exception {
|
|
|
+ List<List<String>> data = new ArrayList<>();
|
|
|
+
|
|
|
+ // 通过 FileInputStream 读取 Excel 文件
|
|
|
+ try (FileInputStream fis = new FileInputStream(excelPath);
|
|
|
+ Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
+
|
|
|
+ // 获取第一个工作表
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+
|
|
|
+ // 遍历每一行
|
|
|
+ for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { // 采用getPhysicalNumberOfRows来提高效率
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+
|
|
|
+ if (row == null) continue; // 跳过空行
|
|
|
+
|
|
|
+ List<String> rowData = new ArrayList<>();
|
|
|
+
|
|
|
+ // 遍历每个单元格
|
|
|
+ for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) { // 采用getPhysicalNumberOfCells来提高效率
|
|
|
+ Cell cell = row.getCell(j);
|
|
|
+ if (cell == null) {
|
|
|
+ rowData.add(""); // 如果单元格为空,添加空字符串
|
|
|
+ } else {
|
|
|
+ switch (cell.getCellType()) {
|
|
|
+ case STRING:
|
|
|
+ rowData.add(cell.getStringCellValue().trim());
|
|
|
+ break;
|
|
|
+ case NUMERIC:
|
|
|
+ rowData.add(String.valueOf(cell.getNumericCellValue()).trim());
|
|
|
+ break;
|
|
|
+ case BOOLEAN:
|
|
|
+ rowData.add(String.valueOf(cell.getBooleanCellValue()).trim());
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ rowData.add(""); // 对于其他类型的单元格返回空字符串
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将读取到的行数据添加到数据列表中
|
|
|
+ data.add(rowData);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new Exception("Error reading Excel file: " + e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 生成keytable.sqlite3文件
|
|
|
+ private void generateKeyTableFile(String dataFolderPath, List<List<String>> configExcelData, int startCol, int endCol) throws Exception {
|
|
|
+ // 提取指定列范围的数据
|
|
|
+ List<List<String>> dataToWrite = new ArrayList<>();
|
|
|
+
|
|
|
+ // 从configExcelData中获取第startCol到endCol列的数据
|
|
|
+ for (List<String> row : configExcelData) {
|
|
|
+ // 只取指定范围的列
|
|
|
+ List<String> rowData = row.subList(startCol, endCol + 1); // 提取列范围的数据
|
|
|
+ dataToWrite.add(rowData);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据提取的数据生成sqlite3文件
|
|
|
+ String keytableDir = dataFolderPath + "\\keytable";
|
|
|
+ File keytableDirFile = new File(keytableDir);
|
|
|
+ if (!keytableDirFile.exists()) {
|
|
|
+ keytableDirFile.mkdir(); // 创建 keytable 目录
|
|
|
+ }
|
|
|
+
|
|
|
+ String sqliteFilePath = keytableDir + "\\keytable.sqlite3";
|
|
|
+ // 写入SQLite数据库
|
|
|
+ writeToSQLite(dataToWrite, sqliteFilePath);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 写入SQLite数据库
|
|
|
+ private void writeToSQLite(List<List<String>> data, String sqliteFilePath) {
|
|
|
+ try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + sqliteFilePath)) {
|
|
|
+ // 获取列名(假设第一行是列名)
|
|
|
+ List<String> columnNames = data.get(0); // 第一行作为列名
|
|
|
+
|
|
|
+ // 创建表格
|
|
|
+ String createTableSQL = "CREATE TABLE IF NOT EXISTS keytable (";
|
|
|
+
|
|
|
+ // 检查每列的数据类型
|
|
|
+ for (int i = 0; i < columnNames.size(); i++) {
|
|
|
+ String columnType = "TEXT"; // 默认列类型为 TEXT
|
|
|
+
|
|
|
+ // 遍历当前列的所有数据来检查是否包含数字
|
|
|
+ for (int rowIdx = 1; rowIdx < data.size(); rowIdx++) { // 从第二行开始检查数据
|
|
|
+ String cellValue = data.get(rowIdx).get(i).trim();
|
|
|
+ // 如果列中任何数据是数字,将列的数据类型设置为 INTEGER
|
|
|
+ if (cellValue.matches("-?\\d+(\\.\\d+)?")) { // 允许整数或浮动数字
|
|
|
+ columnType = "INTEGER";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ createTableSQL += columnNames.get(i) + " " + columnType; // 使用原始列名和类型
|
|
|
+ if (i < columnNames.size() - 1) {
|
|
|
+ createTableSQL += ", ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ createTableSQL += ");";
|
|
|
+
|
|
|
+ // 执行创建表格语句
|
|
|
+ try (Statement stmt = conn.createStatement()) {
|
|
|
+ stmt.execute(createTableSQL);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 插入数据
|
|
|
+ StringBuilder insertSQL = new StringBuilder("INSERT INTO keytable VALUES (");
|
|
|
+ for (int i = 0; i < columnNames.size() - 1; i++) {
|
|
|
+ insertSQL.append("?, ");
|
|
|
+ }
|
|
|
+ insertSQL.append("?);"); // 最后一列不需要逗号
|
|
|
+
|
|
|
+ try (PreparedStatement pstmt = conn.prepareStatement(insertSQL.toString())) {
|
|
|
+ for (int rowIdx = 1; rowIdx < data.size(); rowIdx++) { // 从第二行开始处理数据
|
|
|
+ List<String> row = data.get(rowIdx);
|
|
|
+ for (int colIdx = 0; colIdx < row.size(); colIdx++) {
|
|
|
+ String value = row.get(colIdx).trim();
|
|
|
+
|
|
|
+ // 判断值是否为数字并设定相应的数据类型
|
|
|
+ if (value.matches("-?\\d+(\\.\\d+)?")) {
|
|
|
+ // 如果是浮动数字,转换为整数
|
|
|
+ try {
|
|
|
+ double numericValue = Double.parseDouble(value);
|
|
|
+ // 如果是浮动数字,四舍五入并转为整数
|
|
|
+ if (numericValue % 1 != 0) {
|
|
|
+ pstmt.setInt(colIdx + 1, (int) Math.round(numericValue)); // 插入整数
|
|
|
+ } else {
|
|
|
+ pstmt.setInt(colIdx + 1, (int) numericValue); // 插入整数
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ pstmt.setString(colIdx + 1, value); // 如果无法解析为数字,插入为字符串
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ pstmt.setString(colIdx + 1, value); // 插入文本
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pstmt.addBatch(); // 添加批处理
|
|
|
+ }
|
|
|
+ pstmt.executeBatch(); // 执行批量插入
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 修改采集wormholed.json文件
|
|
|
+ public void cjmodifyJsonConfigFile(String folderPath, String jsonFileName, String pwdData, String pluginData) throws IOException {
|
|
|
+ String jsonFilePath = folderPath + "/config/" + jsonFileName;
|
|
|
+ String content = new String(Files.readAllBytes(Paths.get(jsonFilePath)));
|
|
|
+
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+
|
|
|
+ // 使用 Jackson 将 JSON 解析为 LinkedHashMap,保留顺序
|
|
|
+ LinkedHashMap<String, Object> orderedJsonMap = mapper.readValue(content, LinkedHashMap.class);
|
|
|
+
|
|
|
+
|
|
|
+ // 修改指定部分数据
|
|
|
+ // 修改 engine 部分
|
|
|
+ Object engineObj = orderedJsonMap.get("engine");
|
|
|
+ if (engineObj instanceof LinkedHashMap) {
|
|
|
+ LinkedHashMap<String, Object> engine = (LinkedHashMap<String, Object>) engineObj;
|
|
|
+ engine.put("pwd", "/home/GYEE/service/" + pwdData);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 修改 ci 部分
|
|
|
+ Object ciObj = orderedJsonMap.get("ci");
|
|
|
+ if (ciObj instanceof LinkedHashMap) {
|
|
|
+ LinkedHashMap<String, Object> ci = (LinkedHashMap<String, Object>) ciObj;
|
|
|
+ ci.put("plugin", pluginData + ".so");
|
|
|
+ ci.put("config", pluginData + ".json");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用 Jackson 将 LinkedHashMap 写入文件,保持格式化和顺序
|
|
|
+ mapper.writeValue(Paths.get(jsonFilePath).toFile(), orderedJsonMap);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 采集plugin.json文件目录
|
|
|
+ public void JsonpluginFile(String folderPath, String jsonFileName, String ipData, String portData, String gyfpip, String gyfpport) throws IOException {
|
|
|
+ //采集104或者modbusJSON地址
|
|
|
+ String jsonFilePath = folderPath + "/plugin/" + jsonFileName;
|
|
|
+ //采集gyfp文件地址
|
|
|
+ String gyfpFilePath = folderPath + "/plugin/" + "libplugingyfp.json";
|
|
|
+
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+
|
|
|
+ // 读取并修改采集第一个 JSON 文件
|
|
|
+ modifyJsonFile(mapper, jsonFilePath, ipData, portData, null);
|
|
|
+ // 读取并修改采集第二个 JSON 文件
|
|
|
+ modifyJsonFile(mapper, gyfpFilePath, gyfpip, gyfpport, null);
|
|
|
+
|
|
|
+ // 根据 jsonFileName 删除对应的文件
|
|
|
+ deleteFileByFileName(folderPath, jsonFileName);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转发plugin.json文件目录
|
|
|
+ public void zfJsonpluginFile(String folderPath, String ipData, String portData, String gyfpip, String gyfpport) throws IOException {
|
|
|
+
|
|
|
+ //转发gyfp文件地址
|
|
|
+ String zfFilePath = folderPath + "/plugin/" + "libplugingyfp.json";
|
|
|
+ //转发gyfp2文件地址
|
|
|
+ String zf2FilePath = folderPath + "/plugin/" + "libplugingyfp2.json";
|
|
|
+
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+
|
|
|
+ // 读取并修改转发gyfp JSON 文件
|
|
|
+ modifyJsonFile(mapper, zfFilePath, ipData, portData, null);
|
|
|
+ // 读取并修改转发gyfp2 JSON 文件
|
|
|
+ modifyJsonFile(mapper, zf2FilePath, gyfpip, gyfpport, null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // redis plugin.json文件目录
|
|
|
+ public void redisJsonpluginFile(String folderPath, String portData, String dbid) throws IOException {
|
|
|
+
|
|
|
+ //redis gyfp2文件地址
|
|
|
+ String redisFilePath = folderPath + "/plugin/" + "libplugingyfp2.json";
|
|
|
+ //redis-redis文件地址
|
|
|
+ String redis2FilePath = folderPath + "/plugin/" + "libpluginredis.json";
|
|
|
+
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+
|
|
|
+ // 读取并修改redis gyfp JSON 文件
|
|
|
+ modifyJsonFile(mapper, redisFilePath, null, portData, null);
|
|
|
+ // 读取并修改redis redis JSON 文件
|
|
|
+ modifyJsonFile(mapper, redis2FilePath, null, null, dbid);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送plugin.json文件目录
|
|
|
+ public void fsJsonpluginFile(String folderPath, String ipData, String portData) throws IOException {
|
|
|
+
|
|
|
+ //fs gyfp2文件地址
|
|
|
+ String fsFilePath = folderPath + "/plugin/" + "libplugingyfp2.json";
|
|
|
+
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+
|
|
|
+ // 读取并修改fs gyfp2 JSON 文件
|
|
|
+ modifyJsonFile(mapper, fsFilePath, ipData, portData, null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 接收plugin.json文件目录
|
|
|
+ public void jsJsonpluginFile(String folderPath, String portData) throws IOException {
|
|
|
+
|
|
|
+ //js gyfp2文件地址
|
|
|
+ String fsFilePath = folderPath + "/plugin/" + "libplugingyfp2.json";
|
|
|
+
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+
|
|
|
+ // 读取并修改fs gyfp2 JSON 文件
|
|
|
+ modifyJsonFile(mapper, fsFilePath, null, portData, null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //修改plugin文件夹json文件
|
|
|
+ private void modifyJsonFile(ObjectMapper mapper, String filePath, String ipData, String portData, String dbid) throws IOException {
|
|
|
+ String content = new String(Files.readAllBytes(Paths.get(filePath)));
|
|
|
+ // 使用 Jackson 将 JSON 解析为 LinkedHashMap,保留顺序
|
|
|
+ LinkedHashMap<String, Object> orderedJsonMap = mapper.readValue(content, LinkedHashMap.class);
|
|
|
+
|
|
|
+ // 修改指定部分数据
|
|
|
+ // 修改 ip 部分
|
|
|
+ if (orderedJsonMap.containsKey("ip") && ipData != null && !ipData.equals("无")) {
|
|
|
+ orderedJsonMap.put("ip", ipData);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 修改 dbid 部分
|
|
|
+ if (orderedJsonMap.containsKey("dbid") && dbid != null && !dbid.equals("无")) {
|
|
|
+ orderedJsonMap.put("dbid", dbid);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 修改 port 部分
|
|
|
+ if (orderedJsonMap.containsKey("port") && portData != null && !portData.equals("无")) {
|
|
|
+ try {
|
|
|
+ double doublePort = Double.parseDouble(portData);
|
|
|
+ orderedJsonMap.put("port", (int) doublePort);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ System.err.println("转换port为int类型失败,使用默认值0 " + filePath + " " + e.getMessage());
|
|
|
+ orderedJsonMap.put("port", 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ // 使用 Jackson 将 LinkedHashMap 写入文件,保持格式化和顺序
|
|
|
+ mapper.writeValue(Paths.get(filePath).toFile(), orderedJsonMap);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //采集有两种-执行删除plugin中json文件
|
|
|
+ private void deleteFileByFileName(String folderPath, String jsonFileName) {
|
|
|
+ Path filePathToDelete = null;
|
|
|
+ if ("libplugin104.json".equals(jsonFileName)) {
|
|
|
+ filePathToDelete = Paths.get(folderPath, "plugin", "libpluginmodbus.json");
|
|
|
+ } else {
|
|
|
+ filePathToDelete = Paths.get(folderPath, "plugin", "libplugin104.json");
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Files.delete(filePathToDelete);
|
|
|
+ System.out.println("成功删除文件:" + filePathToDelete);
|
|
|
+ } catch (NoSuchFileException e) {
|
|
|
+ System.err.println("文件不存在,删除失败:" + filePathToDelete + " " + e.getMessage());
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println("删除文件失败:" + filePathToDelete + " " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //修改除采集外其它四个目录wormholed文件
|
|
|
+ public void modifyJsonConfigFile(String folderPath, String jsonFileName, String pwdData) throws IOException {
|
|
|
+ String jsonFilePath = folderPath + "/config/" + jsonFileName;
|
|
|
+ String content = new String(Files.readAllBytes(Paths.get(jsonFilePath)));
|
|
|
+
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
+
|
|
|
+ // 使用 Jackson 将 JSON 解析为 LinkedHashMap,保留顺序
|
|
|
+ LinkedHashMap<String, Object> orderedJsonMap = mapper.readValue(content, LinkedHashMap.class);
|
|
|
+
|
|
|
+ // 修改 engine 部分
|
|
|
+ Object engineObj = orderedJsonMap.get("engine");
|
|
|
+ if (engineObj instanceof LinkedHashMap) {
|
|
|
+ LinkedHashMap<String, Object> engine = (LinkedHashMap<String, Object>) engineObj;
|
|
|
+ engine.put("pwd", "/home/GYEE/service/" + pwdData);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用 Jackson 将 LinkedHashMap 写入文件,保持格式化和顺序
|
|
|
+ mapper.writeValue(Paths.get(jsonFilePath).toFile(), orderedJsonMap);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|