From 006979df31b7201aae346bdecd292eac0e81ccc3 Mon Sep 17 00:00:00 2001 From: yarnom Date: Tue, 29 Jul 2025 14:56:08 +0800 Subject: [PATCH] feat: update forward server --- .../config/RtcmPortConfigManager.java | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 sec-beidou-rtcm/src/main/java/com/imdroid/sideslope/config/RtcmPortConfigManager.java diff --git a/sec-beidou-rtcm/src/main/java/com/imdroid/sideslope/config/RtcmPortConfigManager.java b/sec-beidou-rtcm/src/main/java/com/imdroid/sideslope/config/RtcmPortConfigManager.java new file mode 100644 index 00000000..d77106e0 --- /dev/null +++ b/sec-beidou-rtcm/src/main/java/com/imdroid/sideslope/config/RtcmPortConfigManager.java @@ -0,0 +1,123 @@ +package com.imdroid.sideslope.config; + +import com.imdroid.sideslope.server.tcp.DeviceTcpPortManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +@Component +public class RtcmPortConfigManager { + private static final Logger logger = LoggerFactory.getLogger(RtcmPortConfigManager.class); + + @Value("${rtcm.config.file:/root/beidou/config/rtcm_port}") + private String configFilePath; + + @Autowired + private DeviceTcpPortManager deviceTcpPortManager; + + // 当前配置的设备和端口映射 + private final Map currentConfig = new ConcurrentHashMap<>(); + + @PostConstruct + public void init() { + // 确保配置目录存在 + File configFile = new File(configFilePath); + if (!configFile.getParentFile().exists()) { + configFile.getParentFile().mkdirs(); + } + // 初始加载配置 + loadConfig(); + } + + @Scheduled(fixedRate = 60000) // 每60秒执行一次 + public void scheduledLoadConfig() { + loadConfig(); + } + + private void loadConfig() { + Map newConfig = new HashMap<>(); + File configFile = new File(configFilePath); + + if (!configFile.exists()) { + logger.info("Config file not found: {}, will create when needed", configFilePath); + return; + } + + try (BufferedReader reader = new BufferedReader(new FileReader(configFile))) { + String line; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.isEmpty() || line.startsWith("#")) { + continue; // 跳过空行和注释 + } + + String[] parts = line.split("\\s+"); + if (parts.length >= 2) { + try { + String deviceId = parts[0]; + int port = Integer.parseInt(parts[1]); + newConfig.put(deviceId, port); + } catch (NumberFormatException e) { + logger.warn("Invalid port number in config file: {}", line); + } + } + } + } catch (IOException e) { + logger.error("Error reading config file: {}", configFilePath, e); + return; + } + + // 找出需要移除的设备 + Set devicesToRemove = new HashSet<>(currentConfig.keySet()); + devicesToRemove.removeAll(newConfig.keySet()); + + // 找出新增或更新的设备 + Map devicesToUpdate = new HashMap<>(newConfig); + + // 移除不再需要的设备 + for (String deviceId : devicesToRemove) { + deviceTcpPortManager.removeDevice(deviceId); + logger.info("Removed device {} from TCP port manager", deviceId); + } + + // 更新或添加新的设备配置 + for (Map.Entry entry : devicesToUpdate.entrySet()) { + String deviceId = entry.getKey(); + int port = entry.getValue(); + + // 如果端口发生变化,需要重新创建 + Integer currentPort = currentConfig.get(deviceId); + if (currentPort == null || !currentPort.equals(port)) { + if (currentPort != null) { + deviceTcpPortManager.removeDevice(deviceId); + } + deviceTcpPortManager.addDevice(deviceId, port); + logger.info("Updated device {} to use port {}", deviceId, port); + } + } + + // 更新当前配置 + currentConfig.clear(); + currentConfig.putAll(newConfig); + logger.info("Config updated, current devices: {}", currentConfig); + } + + public boolean isDeviceEnabled(String deviceId) { + return currentConfig.containsKey(deviceId); + } + + public Integer getDevicePort(String deviceId) { + return currentConfig.get(deviceId); + } +} \ No newline at end of file