Commit c5b4968e5502325ea6e8627f59b8c17a8ccaeb44
1 parent
997bfe8f
feat: 【IoT 物联网】数据流转目的 TCP 执行器
Showing
6 changed files
with
594 additions
and
0 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/config/IotAbstractDataSinkConfig.java
| @@ -17,6 +17,8 @@ import lombok.Data; | @@ -17,6 +17,8 @@ import lombok.Data; | ||
| 17 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true) | 17 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type", visible = true) |
| 18 | @JsonSubTypes({ | 18 | @JsonSubTypes({ |
| 19 | @JsonSubTypes.Type(value = IotDataSinkHttpConfig.class, name = "1"), | 19 | @JsonSubTypes.Type(value = IotDataSinkHttpConfig.class, name = "1"), |
| 20 | + @JsonSubTypes.Type(value = IotDataSinkTcpConfig.class, name = "2"), | ||
| 21 | + @JsonSubTypes.Type(value = IotDataSinkWebSocketConfig.class, name = "3"), | ||
| 20 | @JsonSubTypes.Type(value = IotDataSinkMqttConfig.class, name = "10"), | 22 | @JsonSubTypes.Type(value = IotDataSinkMqttConfig.class, name = "10"), |
| 21 | @JsonSubTypes.Type(value = IotDataSinkRedisConfig.class, name = "21"), | 23 | @JsonSubTypes.Type(value = IotDataSinkRedisConfig.class, name = "21"), |
| 22 | @JsonSubTypes.Type(value = IotDataSinkRocketMQConfig.class, name = "30"), | 24 | @JsonSubTypes.Type(value = IotDataSinkRocketMQConfig.class, name = "30"), |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/config/IotDataSinkTcpConfig.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.dal.dataobject.rule.config; | ||
| 2 | + | ||
| 3 | +import lombok.Data; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * IoT TCP 配置 {@link IotAbstractDataSinkConfig} 实现类 | ||
| 7 | + * | ||
| 8 | + * @author HUIHUI | ||
| 9 | + */ | ||
| 10 | +@Data | ||
| 11 | +public class IotDataSinkTcpConfig extends IotAbstractDataSinkConfig { | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * TCP 服务器地址 | ||
| 15 | + */ | ||
| 16 | + private String host; | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * TCP 服务器端口 | ||
| 20 | + */ | ||
| 21 | + private Integer port; | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * 连接超时时间(毫秒) | ||
| 25 | + */ | ||
| 26 | + private Integer connectTimeoutMs = 5000; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * 读取超时时间(毫秒) | ||
| 30 | + */ | ||
| 31 | + private Integer readTimeoutMs = 10000; | ||
| 32 | + | ||
| 33 | + /** | ||
| 34 | + * 是否启用 SSL | ||
| 35 | + */ | ||
| 36 | + private Boolean ssl = false; | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * SSL 证书路径(当 ssl=true 时需要) | ||
| 40 | + */ | ||
| 41 | + private String sslCertPath; | ||
| 42 | + | ||
| 43 | + /** | ||
| 44 | + * 数据格式:JSON 或 BINARY | ||
| 45 | + */ | ||
| 46 | + private String dataFormat = "JSON"; | ||
| 47 | + | ||
| 48 | + /** | ||
| 49 | + * 心跳间隔时间(毫秒),0 表示不启用心跳 | ||
| 50 | + */ | ||
| 51 | + private Long heartbeatIntervalMs = 30000L; | ||
| 52 | + | ||
| 53 | + /** | ||
| 54 | + * 重连间隔时间(毫秒) | ||
| 55 | + */ | ||
| 56 | + private Long reconnectIntervalMs = 5000L; | ||
| 57 | + | ||
| 58 | + /** | ||
| 59 | + * 最大重连次数 | ||
| 60 | + */ | ||
| 61 | + private Integer maxReconnectAttempts = 3; | ||
| 62 | + | ||
| 63 | +} | ||
| 0 | \ No newline at end of file | 64 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/config/IotDataSinkWebSocketConfig.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.dal.dataobject.rule.config; | ||
| 2 | + | ||
| 3 | +import lombok.Data; | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * IoT WebSocket 配置 {@link IotAbstractDataSinkConfig} 实现类 | ||
| 7 | + * <p> | ||
| 8 | + * 配置设备消息通过 WebSocket 协议发送到外部 WebSocket 服务器 | ||
| 9 | + * 支持 WebSocket (ws://) 和 WebSocket Secure (wss://) 连接 | ||
| 10 | + * | ||
| 11 | + * @author HUIHUI | ||
| 12 | + */ | ||
| 13 | +@Data | ||
| 14 | +public class IotDataSinkWebSocketConfig extends IotAbstractDataSinkConfig { | ||
| 15 | + | ||
| 16 | + /** | ||
| 17 | + * WebSocket 服务器地址 | ||
| 18 | + * 例如:ws://localhost:8080/ws 或 wss://example.com/ws | ||
| 19 | + */ | ||
| 20 | + private String serverUrl; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 连接超时时间(毫秒) | ||
| 24 | + */ | ||
| 25 | + private Integer connectTimeoutMs = 5000; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 发送超时时间(毫秒) | ||
| 29 | + */ | ||
| 30 | + private Integer sendTimeoutMs = 10000; | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 心跳间隔时间(毫秒),0 表示不启用心跳 | ||
| 34 | + */ | ||
| 35 | + private Long heartbeatIntervalMs = 30000L; | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 心跳消息内容(JSON 格式) | ||
| 39 | + */ | ||
| 40 | + private String heartbeatMessage = "{\"type\":\"heartbeat\"}"; | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * 子协议列表(逗号分隔) | ||
| 44 | + */ | ||
| 45 | + private String subprotocols; | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 自定义请求头(JSON 格式) | ||
| 49 | + */ | ||
| 50 | + private String customHeaders; | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * 是否启用 SSL 证书验证(仅对 wss:// 生效) | ||
| 54 | + */ | ||
| 55 | + private Boolean verifySslCert = true; | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * 数据格式:JSON 或 TEXT | ||
| 59 | + */ | ||
| 60 | + private String dataFormat = "JSON"; | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * 重连间隔时间(毫秒) | ||
| 64 | + */ | ||
| 65 | + private Long reconnectIntervalMs = 5000L; | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * 最大重连次数 | ||
| 69 | + */ | ||
| 70 | + private Integer maxReconnectAttempts = 3; | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * 是否启用压缩 | ||
| 74 | + */ | ||
| 75 | + private Boolean enableCompression = false; | ||
| 76 | + | ||
| 77 | + /** | ||
| 78 | + * 消息发送重试次数 | ||
| 79 | + */ | ||
| 80 | + private Integer sendRetryCount = 1; | ||
| 81 | + | ||
| 82 | + /** | ||
| 83 | + * 消息发送重试间隔(毫秒) | ||
| 84 | + */ | ||
| 85 | + private Long sendRetryIntervalMs = 1000L; | ||
| 86 | + | ||
| 87 | +} | ||
| 0 | \ No newline at end of file | 88 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/IotTcpDataRuleAction.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.service.rule.data.action; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; | ||
| 4 | +import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkTcpConfig; | ||
| 5 | +import cn.iocoder.yudao.module.iot.enums.rule.IotDataSinkTypeEnum; | ||
| 6 | +import cn.iocoder.yudao.module.iot.service.rule.data.action.tcp.IotTcpClient; | ||
| 7 | +import lombok.extern.slf4j.Slf4j; | ||
| 8 | +import org.springframework.stereotype.Component; | ||
| 9 | + | ||
| 10 | +import java.time.Duration; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * TCP 的 {@link IotDataRuleAction} 实现类 | ||
| 14 | + * <p> | ||
| 15 | + * 负责将设备消息发送到外部 TCP 服务器 | ||
| 16 | + * 支持普通 TCP 和 SSL TCP 连接,支持 JSON 和 BINARY 数据格式 | ||
| 17 | + * 使用连接池管理 TCP 连接,提高性能和资源利用率 | ||
| 18 | + * | ||
| 19 | + * @author HUIHUI | ||
| 20 | + */ | ||
| 21 | +@Component | ||
| 22 | +@Slf4j | ||
| 23 | +public class IotTcpDataRuleAction extends | ||
| 24 | + IotDataRuleCacheableAction<IotDataSinkTcpConfig, IotTcpClient> { | ||
| 25 | + | ||
| 26 | + private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(5); | ||
| 27 | + private static final Duration SEND_TIMEOUT = Duration.ofSeconds(10); | ||
| 28 | + | ||
| 29 | + @Override | ||
| 30 | + public Integer getType() { | ||
| 31 | + return IotDataSinkTypeEnum.TCP.getType(); | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + @Override | ||
| 35 | + protected IotTcpClient initProducer(IotDataSinkTcpConfig config) throws Exception { | ||
| 36 | + // 1. 参数校验 | ||
| 37 | + if (config.getHost() == null || config.getHost().trim().isEmpty()) { | ||
| 38 | + throw new IllegalArgumentException("TCP 服务器地址不能为空"); | ||
| 39 | + } | ||
| 40 | + if (config.getPort() == null || config.getPort() <= 0 || config.getPort() > 65535) { | ||
| 41 | + throw new IllegalArgumentException("TCP 服务器端口无效"); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + // 2. 创建 TCP 客户端 | ||
| 45 | + IotTcpClient tcpClient = new IotTcpClient( | ||
| 46 | + config.getHost(), | ||
| 47 | + config.getPort(), | ||
| 48 | + config.getConnectTimeoutMs(), | ||
| 49 | + config.getReadTimeoutMs(), | ||
| 50 | + config.getSsl(), | ||
| 51 | + config.getSslCertPath(), | ||
| 52 | + config.getDataFormat() | ||
| 53 | + ); | ||
| 54 | + | ||
| 55 | + // 3. 连接服务器 | ||
| 56 | + tcpClient.connect(); | ||
| 57 | + | ||
| 58 | + log.info("[initProducer][TCP 客户端创建并连接成功,服务器: {}:{},SSL: {},数据格式: {}]", | ||
| 59 | + config.getHost(), config.getPort(), config.getSsl(), config.getDataFormat()); | ||
| 60 | + | ||
| 61 | + return tcpClient; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + protected void closeProducer(IotTcpClient producer) throws Exception { | ||
| 66 | + if (producer != null) { | ||
| 67 | + producer.close(); | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 72 | + protected void execute(IotDeviceMessage message, IotDataSinkTcpConfig config) throws Exception { | ||
| 73 | + try { | ||
| 74 | + // 1. 获取或创建 TCP 客户端 | ||
| 75 | + IotTcpClient tcpClient = getProducer(config); | ||
| 76 | + | ||
| 77 | + // 2. 检查连接状态,如果断开则重新连接 | ||
| 78 | + if (!tcpClient.isConnected()) { | ||
| 79 | + log.warn("[execute][TCP 连接已断开,尝试重新连接,服务器: {}:{}]", config.getHost(), config.getPort()); | ||
| 80 | + tcpClient.connect(); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + // 3. 发送消息并等待结果 | ||
| 84 | + tcpClient.sendMessage(message); | ||
| 85 | + | ||
| 86 | + // 4. 记录发送成功日志 | ||
| 87 | + log.info("[execute][message({}) config({}) 发送成功,TCP 服务器: {}:{}]", | ||
| 88 | + message, config, config.getHost(), config.getPort()); | ||
| 89 | + | ||
| 90 | + } catch (Exception e) { | ||
| 91 | + log.error("[execute][message({}) config({}) 发送失败,TCP 服务器: {}:{}]", | ||
| 92 | + message, config, config.getHost(), config.getPort(), e); | ||
| 93 | + throw e; | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | +} | ||
| 0 | \ No newline at end of file | 98 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/tcp/IotTcpClient.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.service.rule.data.action.tcp; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 4 | +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; | ||
| 5 | +import lombok.extern.slf4j.Slf4j; | ||
| 6 | + | ||
| 7 | +import javax.net.ssl.SSLSocketFactory; | ||
| 8 | +import java.io.BufferedReader; | ||
| 9 | +import java.io.IOException; | ||
| 10 | +import java.io.InputStreamReader; | ||
| 11 | +import java.io.OutputStream; | ||
| 12 | +import java.net.InetSocketAddress; | ||
| 13 | +import java.net.Socket; | ||
| 14 | +import java.nio.charset.StandardCharsets; | ||
| 15 | +import java.util.concurrent.atomic.AtomicBoolean; | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * IoT TCP 客户端 | ||
| 19 | + * <p> | ||
| 20 | + * 负责与外部 TCP 服务器建立连接并发送设备消息 | ||
| 21 | + * 支持 JSON 和 BINARY 两种数据格式,支持 SSL 加密连接 | ||
| 22 | + * | ||
| 23 | + * @author HUIHUI | ||
| 24 | + */ | ||
| 25 | +@Slf4j | ||
| 26 | +public class IotTcpClient { | ||
| 27 | + | ||
| 28 | + private final String host; | ||
| 29 | + private final Integer port; | ||
| 30 | + private final Integer connectTimeoutMs; | ||
| 31 | + private final Integer readTimeoutMs; | ||
| 32 | + private final Boolean ssl; | ||
| 33 | + private final String sslCertPath; | ||
| 34 | + private final String dataFormat; | ||
| 35 | + | ||
| 36 | + private Socket socket; | ||
| 37 | + private OutputStream outputStream; | ||
| 38 | + private BufferedReader reader; | ||
| 39 | + private final AtomicBoolean connected = new AtomicBoolean(false); | ||
| 40 | + | ||
| 41 | + public IotTcpClient(String host, Integer port, Integer connectTimeoutMs, Integer readTimeoutMs, | ||
| 42 | + Boolean ssl, String sslCertPath, String dataFormat) { | ||
| 43 | + this.host = host; | ||
| 44 | + this.port = port; | ||
| 45 | + this.connectTimeoutMs = connectTimeoutMs != null ? connectTimeoutMs : 5000; | ||
| 46 | + this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : 10000; | ||
| 47 | + this.ssl = ssl != null ? ssl : false; | ||
| 48 | + this.sslCertPath = sslCertPath; | ||
| 49 | + this.dataFormat = dataFormat != null ? dataFormat : "JSON"; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * 连接到 TCP 服务器 | ||
| 54 | + */ | ||
| 55 | + public void connect() throws Exception { | ||
| 56 | + if (connected.get()) { | ||
| 57 | + log.warn("[connect][TCP 客户端已经连接,无需重复连接]"); | ||
| 58 | + return; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + try { | ||
| 62 | + if (ssl) { | ||
| 63 | + // SSL 连接 | ||
| 64 | + SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); | ||
| 65 | + socket = sslSocketFactory.createSocket(); | ||
| 66 | + } else { | ||
| 67 | + // 普通连接 | ||
| 68 | + socket = new Socket(); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + // 连接服务器 | ||
| 72 | + socket.connect(new InetSocketAddress(host, port), connectTimeoutMs); | ||
| 73 | + socket.setSoTimeout(readTimeoutMs); | ||
| 74 | + | ||
| 75 | + // 获取输入输出流 | ||
| 76 | + outputStream = socket.getOutputStream(); | ||
| 77 | + reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); | ||
| 78 | + | ||
| 79 | + connected.set(true); | ||
| 80 | + log.info("[connect][TCP 客户端连接成功,服务器地址: {}:{}]", host, port); | ||
| 81 | + | ||
| 82 | + } catch (Exception e) { | ||
| 83 | + close(); | ||
| 84 | + log.error("[connect][TCP 客户端连接失败,服务器地址: {}:{}]", host, port, e); | ||
| 85 | + throw e; | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + /** | ||
| 90 | + * 发送设备消息 | ||
| 91 | + * | ||
| 92 | + * @param message 设备消息 | ||
| 93 | + * @throws Exception 发送异常 | ||
| 94 | + */ | ||
| 95 | + public void sendMessage(IotDeviceMessage message) throws Exception { | ||
| 96 | + if (!connected.get()) { | ||
| 97 | + throw new IllegalStateException("TCP 客户端未连接"); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + try { | ||
| 101 | + String messageData; | ||
| 102 | + if ("JSON".equalsIgnoreCase(dataFormat)) { | ||
| 103 | + // JSON 格式 | ||
| 104 | + messageData = JsonUtils.toJsonString(message); | ||
| 105 | + } else { | ||
| 106 | + // BINARY 格式(这里简化为字符串,实际可能需要自定义二进制协议) | ||
| 107 | + messageData = message.toString(); | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + // 发送消息 | ||
| 111 | + outputStream.write(messageData.getBytes(StandardCharsets.UTF_8)); | ||
| 112 | + outputStream.write('\n'); // 添加换行符作为消息分隔符 | ||
| 113 | + outputStream.flush(); | ||
| 114 | + | ||
| 115 | + log.debug("[sendMessage][发送消息成功,设备 ID: {},消息长度: {}]", | ||
| 116 | + message.getDeviceId(), messageData.length()); | ||
| 117 | + | ||
| 118 | + } catch (Exception e) { | ||
| 119 | + log.error("[sendMessage][发送消息失败,设备 ID: {}]", message.getDeviceId(), e); | ||
| 120 | + throw e; | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /** | ||
| 125 | + * 关闭连接 | ||
| 126 | + */ | ||
| 127 | + public void close() { | ||
| 128 | + if (!connected.get()) { | ||
| 129 | + return; | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + try { | ||
| 133 | + // 关闭资源 | ||
| 134 | + if (reader != null) { | ||
| 135 | + try { | ||
| 136 | + reader.close(); | ||
| 137 | + } catch (IOException e) { | ||
| 138 | + log.warn("[close][关闭输入流失败]", e); | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + if (outputStream != null) { | ||
| 142 | + try { | ||
| 143 | + outputStream.close(); | ||
| 144 | + } catch (IOException e) { | ||
| 145 | + log.warn("[close][关闭输出流失败]", e); | ||
| 146 | + } | ||
| 147 | + } | ||
| 148 | + if (socket != null) { | ||
| 149 | + try { | ||
| 150 | + socket.close(); | ||
| 151 | + } catch (IOException e) { | ||
| 152 | + log.warn("[close][关闭 Socket 失败]", e); | ||
| 153 | + } | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + connected.set(false); | ||
| 157 | + log.info("[close][TCP 客户端连接已关闭,服务器地址: {}:{}]", host, port); | ||
| 158 | + | ||
| 159 | + } catch (Exception e) { | ||
| 160 | + log.error("[close][关闭 TCP 客户端连接异常]", e); | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + /** | ||
| 165 | + * 检查连接状态 | ||
| 166 | + * | ||
| 167 | + * @return 是否已连接 | ||
| 168 | + */ | ||
| 169 | + public boolean isConnected() { | ||
| 170 | + return connected.get() && socket != null && !socket.isClosed(); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + @Override | ||
| 174 | + public String toString() { | ||
| 175 | + return "IotTcpClient{" + | ||
| 176 | + "host='" + host + '\'' + | ||
| 177 | + ", port=" + port + | ||
| 178 | + ", ssl=" + ssl + | ||
| 179 | + ", dataFormat='" + dataFormat + '\'' + | ||
| 180 | + ", connected=" + connected.get() + | ||
| 181 | + '}'; | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | +} | ||
| 0 | \ No newline at end of file | 185 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/data/action/IotTcpDataRuleActionTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.iot.service.rule.data.action; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 4 | +import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; | ||
| 5 | +import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkTcpConfig; | ||
| 6 | +import cn.iocoder.yudao.module.iot.service.rule.data.action.tcp.IotTcpClient; | ||
| 7 | +import org.junit.jupiter.api.BeforeEach; | ||
| 8 | +import org.junit.jupiter.api.Test; | ||
| 9 | +import org.mockito.Mock; | ||
| 10 | +import org.mockito.MockitoAnnotations; | ||
| 11 | + | ||
| 12 | +import static org.junit.jupiter.api.Assertions.*; | ||
| 13 | +import static org.mockito.Mockito.*; | ||
| 14 | + | ||
| 15 | +/** | ||
| 16 | + * {@link IotTcpDataRuleAction} 的单元测试 | ||
| 17 | + * | ||
| 18 | + * @author HUIHUI | ||
| 19 | + */ | ||
| 20 | +class IotTcpDataRuleActionTest { | ||
| 21 | + | ||
| 22 | + private IotTcpDataRuleAction tcpDataRuleAction; | ||
| 23 | + | ||
| 24 | + @Mock | ||
| 25 | + private IotTcpClient mockTcpClient; | ||
| 26 | + | ||
| 27 | + @BeforeEach | ||
| 28 | + void setUp() { | ||
| 29 | + MockitoAnnotations.openMocks(this); | ||
| 30 | + tcpDataRuleAction = new IotTcpDataRuleAction(); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + @Test | ||
| 34 | + void testGetType() { | ||
| 35 | + // 准备参数 | ||
| 36 | + Integer expectedType = 2; // 数据接收类型枚举中 TCP 类型的值 | ||
| 37 | + | ||
| 38 | + // 调用方法 | ||
| 39 | + Integer actualType = tcpDataRuleAction.getType(); | ||
| 40 | + | ||
| 41 | + // 断言结果 | ||
| 42 | + assertEquals(expectedType, actualType); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + @Test | ||
| 46 | + void testInitProducer_Success() throws Exception { | ||
| 47 | + // 准备参数 | ||
| 48 | + IotDataSinkTcpConfig config = new IotDataSinkTcpConfig(); | ||
| 49 | + config.setHost("localhost"); | ||
| 50 | + config.setPort(8080); | ||
| 51 | + config.setDataFormat("JSON"); | ||
| 52 | + config.setSsl(false); | ||
| 53 | + | ||
| 54 | + // 调用方法 & 断言结果 | ||
| 55 | + // 此测试需要实际的 TCP 连接,在单元测试中可能不可用 | ||
| 56 | + // 目前我们只验证配置是否有效 | ||
| 57 | + assertNotNull(config.getHost()); | ||
| 58 | + assertTrue(config.getPort() > 0 && config.getPort() <= 65535); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @Test | ||
| 62 | + void testInitProducer_InvalidHost() { | ||
| 63 | + // 准备参数 | ||
| 64 | + IotDataSinkTcpConfig config = new IotDataSinkTcpConfig(); | ||
| 65 | + config.setHost(""); | ||
| 66 | + config.setPort(8080); | ||
| 67 | + | ||
| 68 | + // 调用方法 & 断言结果 | ||
| 69 | + IotTcpDataRuleAction action = new IotTcpDataRuleAction(); | ||
| 70 | + | ||
| 71 | + // 测试验证逻辑(通常在 initProducer 方法中) | ||
| 72 | + assertThrows(IllegalArgumentException.class, () -> { | ||
| 73 | + if (config.getHost() == null || config.getHost().trim().isEmpty()) { | ||
| 74 | + throw new IllegalArgumentException("TCP 服务器地址不能为空"); | ||
| 75 | + } | ||
| 76 | + }); | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + @Test | ||
| 80 | + void testInitProducer_InvalidPort() { | ||
| 81 | + // 准备参数 | ||
| 82 | + IotDataSinkTcpConfig config = new IotDataSinkTcpConfig(); | ||
| 83 | + config.setHost("localhost"); | ||
| 84 | + config.setPort(-1); | ||
| 85 | + | ||
| 86 | + // 调用方法 & 断言结果 | ||
| 87 | + assertThrows(IllegalArgumentException.class, () -> { | ||
| 88 | + if (config.getPort() == null || config.getPort() <= 0 || config.getPort() > 65535) { | ||
| 89 | + throw new IllegalArgumentException("TCP 服务器端口无效"); | ||
| 90 | + } | ||
| 91 | + }); | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + @Test | ||
| 95 | + void testCloseProducer() throws Exception { | ||
| 96 | + // 准备参数 | ||
| 97 | + IotTcpClient client = mock(IotTcpClient.class); | ||
| 98 | + | ||
| 99 | + // 调用方法 | ||
| 100 | + tcpDataRuleAction.closeProducer(client); | ||
| 101 | + | ||
| 102 | + // 断言结果 | ||
| 103 | + verify(client, times(1)).close(); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + @Test | ||
| 107 | + void testExecute_WithValidConfig() { | ||
| 108 | + // 准备参数 | ||
| 109 | + IotDeviceMessage message = IotDeviceMessage.requestOf("thing.property.report", | ||
| 110 | + "{\"temperature\": 25.5, \"humidity\": 60}"); | ||
| 111 | + | ||
| 112 | + IotDataSinkTcpConfig config = new IotDataSinkTcpConfig(); | ||
| 113 | + config.setHost("localhost"); | ||
| 114 | + config.setPort(8080); | ||
| 115 | + config.setDataFormat("JSON"); | ||
| 116 | + | ||
| 117 | + // 调用方法 & 断言结果 | ||
| 118 | + // 通常这需要实际的 TCP 连接 | ||
| 119 | + // 在单元测试中,我们只验证输入参数 | ||
| 120 | + assertNotNull(message); | ||
| 121 | + assertNotNull(config); | ||
| 122 | + assertNotNull(config.getHost()); | ||
| 123 | + assertTrue(config.getPort() > 0); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + @Test | ||
| 127 | + void testConfig_DefaultValues() { | ||
| 128 | + // 准备参数 | ||
| 129 | + IotDataSinkTcpConfig config = new IotDataSinkTcpConfig(); | ||
| 130 | + | ||
| 131 | + // 调用方法 & 断言结果 | ||
| 132 | + // 验证默认值 | ||
| 133 | + assertEquals("JSON", config.getDataFormat()); | ||
| 134 | + assertEquals(5000, config.getConnectTimeoutMs()); | ||
| 135 | + assertEquals(10000, config.getReadTimeoutMs()); | ||
| 136 | + assertEquals(false, config.getSsl()); | ||
| 137 | + assertEquals(30000L, config.getHeartbeatIntervalMs()); | ||
| 138 | + assertEquals(5000L, config.getReconnectIntervalMs()); | ||
| 139 | + assertEquals(3, config.getMaxReconnectAttempts()); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + @Test | ||
| 143 | + void testMessageSerialization() { | ||
| 144 | + // 准备参数 | ||
| 145 | + IotDeviceMessage message = IotDeviceMessage.builder() | ||
| 146 | + .deviceId(123L) | ||
| 147 | + .method("thing.property.report") | ||
| 148 | + .params("{\"temperature\": 25.5}") | ||
| 149 | + .build(); | ||
| 150 | + | ||
| 151 | + // 调用方法 | ||
| 152 | + String json = JsonUtils.toJsonString(message); | ||
| 153 | + | ||
| 154 | + // 断言结果 | ||
| 155 | + assertNotNull(json); | ||
| 156 | + assertTrue(json.contains("\"deviceId\":123")); | ||
| 157 | + assertTrue(json.contains("\"method\":\"thing.property.report\"")); | ||
| 158 | + assertTrue(json.contains("\"temperature\":25.5")); | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | +} | ||
| 0 | \ No newline at end of file | 162 | \ No newline at end of file |