diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/config/SocketConnectionConfig.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/config/SocketConnectionConfig.java new file mode 100644 index 00000000..f053029e --- /dev/null +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/config/SocketConnectionConfig.java @@ -0,0 +1,169 @@ +package com.njcn.gather.detection.util.socket.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * Socket连接配置管理类 + * 定义哪些requestId需要建立通道连接,以及IP/PORT配置 + * + * @Author: hongawen + * @Date: 2024/12/10 + */ +@Component +@ConfigurationProperties(prefix = "socket") +public class SocketConnectionConfig { + + /** + * 程控源设备配置 + */ + private SourceConfig source = new SourceConfig(); + + /** + * 被检设备配置 + */ + private DeviceConfig device = new DeviceConfig(); + + @Data + public static class SourceConfig { + /** + * 程控源IP地址 + */ + private String ip; + + /** + * 程控源端口号 + */ + private Integer port; + } + + @Data + public static class DeviceConfig { + /** + * 被检设备IP地址 + */ + private String ip; + + /** + * 被检设备端口号 + */ + private Integer port; + } + + /** + * 获取程控源配置 + */ + public SourceConfig getSource() { + return source; + } + + /** + * 获取被检设备配置 + */ + public DeviceConfig getDevice() { + return device; + } + + /** + * 需要建立程控源通道的requestId集合 + * 这些requestId在发送消息时,如果程控源通道不存在,会自动建立连接 + */ + private static final Set SOURCE_CONNECTION_REQUEST_IDS = new HashSet<>(Arrays.asList( + // 源通讯检测 + "yjc_ytxjy" + // 可以根据实际业务需求添加更多requestId + )); + + /** + * 需要建立被检设备通道的requestId集合 + * 这些requestId在发送消息时,如果被检设备通道不存在,会自动建立连接 + */ + private static final Set DEVICE_CONNECTION_REQUEST_IDS = new HashSet<>(Arrays.asList( + // 连接建立 + "yjc_sbtxjy", + // ftp文件传送指令 + "FTP_SEND$01" + // 可以根据实际业务需求添加更多requestId + )); + + /** + * 检查指定的requestId是否需要建立程控源连接 + * + * @param requestId 请求ID + * @return boolean true:需要建立连接, false:不需要建立连接 + */ + public static boolean needsSourceConnection(String requestId) { + return SOURCE_CONNECTION_REQUEST_IDS.contains(requestId); + } + + /** + * 检查指定的requestId是否需要建立被检设备连接 + * + * @param requestId 请求ID + * @return boolean true:需要建立连接, false:不需要建立连接 + */ + public static boolean needsDeviceConnection(String requestId) { + return DEVICE_CONNECTION_REQUEST_IDS.contains(requestId); + } + + /** + * 添加需要建立程控源连接的requestId + * 支持运行时动态添加 + * + * @param requestId 请求ID + */ + public static void addSourceConnectionRequestId(String requestId) { + SOURCE_CONNECTION_REQUEST_IDS.add(requestId); + } + + /** + * 添加需要建立被检设备连接的requestId + * 支持运行时动态添加 + * + * @param requestId 请求ID + */ + public static void addDeviceConnectionRequestId(String requestId) { + DEVICE_CONNECTION_REQUEST_IDS.add(requestId); + } + + /** + * 移除程控源连接requestId + * + * @param requestId 请求ID + */ + public static void removeSourceConnectionRequestId(String requestId) { + SOURCE_CONNECTION_REQUEST_IDS.remove(requestId); + } + + /** + * 移除被检设备连接requestId + * + * @param requestId 请求ID + */ + public static void removeDeviceConnectionRequestId(String requestId) { + DEVICE_CONNECTION_REQUEST_IDS.remove(requestId); + } + + /** + * 获取所有需要建立程控源连接的requestId集合(只读) + * + * @return Set requestId集合 + */ + public static Set getSourceConnectionRequestIds() { + return new HashSet<>(SOURCE_CONNECTION_REQUEST_IDS); + } + + /** + * 获取所有需要建立被检设备连接的requestId集合(只读) + * + * @return Set requestId集合 + */ + public static Set getDeviceConnectionRequestIds() { + return new HashSet<>(DEVICE_CONNECTION_REQUEST_IDS); + } +} \ No newline at end of file