Commit 9fbced1192a453d6557085b3959acead2ee9c71c
1 parent
60817a6a
perf:【iot】IotDataSinkWebSocketConfig 常量枚举
Showing
4 changed files
with
63 additions
and
22 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/config/IotDataSinkWebSocketConfig.java
| @@ -14,6 +14,51 @@ import lombok.Data; | @@ -14,6 +14,51 @@ import lombok.Data; | ||
| 14 | public class IotDataSinkWebSocketConfig extends IotAbstractDataSinkConfig { | 14 | public class IotDataSinkWebSocketConfig extends IotAbstractDataSinkConfig { |
| 15 | 15 | ||
| 16 | /** | 16 | /** |
| 17 | + * 默认连接超时时间(毫秒) | ||
| 18 | + */ | ||
| 19 | + public static final int DEFAULT_CONNECT_TIMEOUT_MS = 5000; | ||
| 20 | + /** | ||
| 21 | + * 默认发送超时时间(毫秒) | ||
| 22 | + */ | ||
| 23 | + public static final int DEFAULT_SEND_TIMEOUT_MS = 10000; | ||
| 24 | + /** | ||
| 25 | + * 默认心跳间隔时间(毫秒) | ||
| 26 | + */ | ||
| 27 | + public static final long DEFAULT_HEARTBEAT_INTERVAL_MS = 30000L; | ||
| 28 | + /** | ||
| 29 | + * 默认心跳消息内容 | ||
| 30 | + */ | ||
| 31 | + public static final String DEFAULT_HEARTBEAT_MESSAGE = "{\"type\":\"heartbeat\"}"; | ||
| 32 | + /** | ||
| 33 | + * 默认是否启用 SSL 证书验证 | ||
| 34 | + */ | ||
| 35 | + public static final boolean DEFAULT_VERIFY_SSL_CERT = true; | ||
| 36 | + /** | ||
| 37 | + * 默认数据格式 | ||
| 38 | + */ | ||
| 39 | + public static final String DEFAULT_DATA_FORMAT = "JSON"; | ||
| 40 | + /** | ||
| 41 | + * 默认重连间隔时间(毫秒) | ||
| 42 | + */ | ||
| 43 | + public static final long DEFAULT_RECONNECT_INTERVAL_MS = 5000L; | ||
| 44 | + /** | ||
| 45 | + * 默认最大重连次数 | ||
| 46 | + */ | ||
| 47 | + public static final int DEFAULT_MAX_RECONNECT_ATTEMPTS = 3; | ||
| 48 | + /** | ||
| 49 | + * 默认是否启用压缩 | ||
| 50 | + */ | ||
| 51 | + public static final boolean DEFAULT_ENABLE_COMPRESSION = false; | ||
| 52 | + /** | ||
| 53 | + * 默认消息发送重试次数 | ||
| 54 | + */ | ||
| 55 | + public static final int DEFAULT_SEND_RETRY_COUNT = 1; | ||
| 56 | + /** | ||
| 57 | + * 默认消息发送重试间隔(毫秒) | ||
| 58 | + */ | ||
| 59 | + public static final long DEFAULT_SEND_RETRY_INTERVAL_MS = 1000L; | ||
| 60 | + | ||
| 61 | + /** | ||
| 17 | * WebSocket 服务器地址 | 62 | * WebSocket 服务器地址 |
| 18 | * 例如:ws://localhost:8080/ws 或 wss://example.com/ws | 63 | * 例如:ws://localhost:8080/ws 或 wss://example.com/ws |
| 19 | */ | 64 | */ |
| @@ -22,22 +67,22 @@ public class IotDataSinkWebSocketConfig extends IotAbstractDataSinkConfig { | @@ -22,22 +67,22 @@ public class IotDataSinkWebSocketConfig extends IotAbstractDataSinkConfig { | ||
| 22 | /** | 67 | /** |
| 23 | * 连接超时时间(毫秒) | 68 | * 连接超时时间(毫秒) |
| 24 | */ | 69 | */ |
| 25 | - private Integer connectTimeoutMs = 5000; | 70 | + private Integer connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MS; |
| 26 | 71 | ||
| 27 | /** | 72 | /** |
| 28 | * 发送超时时间(毫秒) | 73 | * 发送超时时间(毫秒) |
| 29 | */ | 74 | */ |
| 30 | - private Integer sendTimeoutMs = 10000; | 75 | + private Integer sendTimeoutMs = DEFAULT_SEND_TIMEOUT_MS; |
| 31 | 76 | ||
| 32 | /** | 77 | /** |
| 33 | * 心跳间隔时间(毫秒),0 表示不启用心跳 | 78 | * 心跳间隔时间(毫秒),0 表示不启用心跳 |
| 34 | */ | 79 | */ |
| 35 | - private Long heartbeatIntervalMs = 30000L; | 80 | + private Long heartbeatIntervalMs = DEFAULT_HEARTBEAT_INTERVAL_MS; |
| 36 | 81 | ||
| 37 | /** | 82 | /** |
| 38 | * 心跳消息内容(JSON 格式) | 83 | * 心跳消息内容(JSON 格式) |
| 39 | */ | 84 | */ |
| 40 | - private String heartbeatMessage = "{\"type\":\"heartbeat\"}"; | 85 | + private String heartbeatMessage = DEFAULT_HEARTBEAT_MESSAGE; |
| 41 | 86 | ||
| 42 | /** | 87 | /** |
| 43 | * 子协议列表(逗号分隔) | 88 | * 子协议列表(逗号分隔) |
| @@ -52,36 +97,36 @@ public class IotDataSinkWebSocketConfig extends IotAbstractDataSinkConfig { | @@ -52,36 +97,36 @@ public class IotDataSinkWebSocketConfig extends IotAbstractDataSinkConfig { | ||
| 52 | /** | 97 | /** |
| 53 | * 是否启用 SSL 证书验证(仅对 wss:// 生效) | 98 | * 是否启用 SSL 证书验证(仅对 wss:// 生效) |
| 54 | */ | 99 | */ |
| 55 | - private Boolean verifySslCert = true; | 100 | + private Boolean verifySslCert = DEFAULT_VERIFY_SSL_CERT; |
| 56 | 101 | ||
| 57 | /** | 102 | /** |
| 58 | * 数据格式:JSON 或 TEXT | 103 | * 数据格式:JSON 或 TEXT |
| 59 | */ | 104 | */ |
| 60 | - private String dataFormat = "JSON"; | 105 | + private String dataFormat = DEFAULT_DATA_FORMAT; |
| 61 | 106 | ||
| 62 | /** | 107 | /** |
| 63 | * 重连间隔时间(毫秒) | 108 | * 重连间隔时间(毫秒) |
| 64 | */ | 109 | */ |
| 65 | - private Long reconnectIntervalMs = 5000L; | 110 | + private Long reconnectIntervalMs = DEFAULT_RECONNECT_INTERVAL_MS; |
| 66 | 111 | ||
| 67 | /** | 112 | /** |
| 68 | * 最大重连次数 | 113 | * 最大重连次数 |
| 69 | */ | 114 | */ |
| 70 | - private Integer maxReconnectAttempts = 3; | 115 | + private Integer maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS; |
| 71 | 116 | ||
| 72 | /** | 117 | /** |
| 73 | * 是否启用压缩 | 118 | * 是否启用压缩 |
| 74 | */ | 119 | */ |
| 75 | - private Boolean enableCompression = false; | 120 | + private Boolean enableCompression = DEFAULT_ENABLE_COMPRESSION; |
| 76 | 121 | ||
| 77 | /** | 122 | /** |
| 78 | * 消息发送重试次数 | 123 | * 消息发送重试次数 |
| 79 | */ | 124 | */ |
| 80 | - private Integer sendRetryCount = 1; | 125 | + private Integer sendRetryCount = DEFAULT_SEND_RETRY_COUNT; |
| 81 | 126 | ||
| 82 | /** | 127 | /** |
| 83 | * 消息发送重试间隔(毫秒) | 128 | * 消息发送重试间隔(毫秒) |
| 84 | */ | 129 | */ |
| 85 | - private Long sendRetryIntervalMs = 1000L; | 130 | + private Long sendRetryIntervalMs = DEFAULT_SEND_RETRY_INTERVAL_MS; |
| 86 | 131 | ||
| 87 | } | 132 | } |
| 88 | \ No newline at end of file | 133 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/enums/rule/IotDataSinkTypeEnum.java
| @@ -16,8 +16,8 @@ import java.util.Arrays; | @@ -16,8 +16,8 @@ import java.util.Arrays; | ||
| 16 | public enum IotDataSinkTypeEnum implements ArrayValuable<Integer> { | 16 | public enum IotDataSinkTypeEnum implements ArrayValuable<Integer> { |
| 17 | 17 | ||
| 18 | HTTP(1, "HTTP"), | 18 | HTTP(1, "HTTP"), |
| 19 | - TCP(2, "TCP"), // TODO @puhui999:待实现; | ||
| 20 | - WEBSOCKET(3, "WebSocket"), // TODO @puhui999:待实现; | 19 | + TCP(2, "TCP"), |
| 20 | + WEBSOCKET(3, "WebSocket"), | ||
| 21 | 21 | ||
| 22 | MQTT(10, "MQTT"), // TODO 待实现; | 22 | MQTT(10, "MQTT"), // TODO 待实现; |
| 23 | 23 |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/IotTcpDataRuleAction.java
| @@ -7,8 +7,6 @@ import cn.iocoder.yudao.module.iot.service.rule.data.action.tcp.IotTcpClient; | @@ -7,8 +7,6 @@ import cn.iocoder.yudao.module.iot.service.rule.data.action.tcp.IotTcpClient; | ||
| 7 | import lombok.extern.slf4j.Slf4j; | 7 | import lombok.extern.slf4j.Slf4j; |
| 8 | import org.springframework.stereotype.Component; | 8 | import org.springframework.stereotype.Component; |
| 9 | 9 | ||
| 10 | -import java.time.Duration; | ||
| 11 | - | ||
| 12 | /** | 10 | /** |
| 13 | * TCP 的 {@link IotDataRuleAction} 实现类 | 11 | * TCP 的 {@link IotDataRuleAction} 实现类 |
| 14 | * <p> | 12 | * <p> |
| @@ -23,9 +21,6 @@ import java.time.Duration; | @@ -23,9 +21,6 @@ import java.time.Duration; | ||
| 23 | public class IotTcpDataRuleAction extends | 21 | public class IotTcpDataRuleAction extends |
| 24 | IotDataRuleCacheableAction<IotDataSinkTcpConfig, IotTcpClient> { | 22 | IotDataRuleCacheableAction<IotDataSinkTcpConfig, IotTcpClient> { |
| 25 | 23 | ||
| 26 | - private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(5); | ||
| 27 | - private static final Duration SEND_TIMEOUT = Duration.ofSeconds(10); | ||
| 28 | - | ||
| 29 | @Override | 24 | @Override |
| 30 | public Integer getType() { | 25 | public Integer getType() { |
| 31 | return IotDataSinkTypeEnum.TCP.getType(); | 26 | return IotDataSinkTypeEnum.TCP.getType(); |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/websocket/IotWebSocketClient.java
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.service.rule.data.action.websocket; | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.service.rule.data.action.websocket; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 4 | import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; | 4 | import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; |
| 5 | +import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkWebSocketConfig; | ||
| 5 | import lombok.extern.slf4j.Slf4j; | 6 | import lombok.extern.slf4j.Slf4j; |
| 6 | 7 | ||
| 7 | import java.net.URI; | 8 | import java.net.URI; |
| @@ -36,9 +37,9 @@ public class IotWebSocketClient implements WebSocket.Listener { | @@ -36,9 +37,9 @@ public class IotWebSocketClient implements WebSocket.Listener { | ||
| 36 | 37 | ||
| 37 | public IotWebSocketClient(String serverUrl, Integer connectTimeoutMs, Integer sendTimeoutMs, String dataFormat) { | 38 | public IotWebSocketClient(String serverUrl, Integer connectTimeoutMs, Integer sendTimeoutMs, String dataFormat) { |
| 38 | this.serverUrl = serverUrl; | 39 | this.serverUrl = serverUrl; |
| 39 | - this.connectTimeoutMs = connectTimeoutMs != null ? connectTimeoutMs : 5000; | ||
| 40 | - this.sendTimeoutMs = sendTimeoutMs != null ? sendTimeoutMs : 10000; | ||
| 41 | - this.dataFormat = dataFormat != null ? dataFormat : "JSON"; | 40 | + this.connectTimeoutMs = connectTimeoutMs != null ? connectTimeoutMs : IotDataSinkWebSocketConfig.DEFAULT_CONNECT_TIMEOUT_MS; |
| 41 | + this.sendTimeoutMs = sendTimeoutMs != null ? sendTimeoutMs : IotDataSinkWebSocketConfig.DEFAULT_SEND_TIMEOUT_MS; | ||
| 42 | + this.dataFormat = dataFormat != null ? dataFormat : IotDataSinkWebSocketConfig.DEFAULT_DATA_FORMAT; | ||
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | /** | 45 | /** |
| @@ -113,7 +114,7 @@ public class IotWebSocketClient implements WebSocket.Listener { | @@ -113,7 +114,7 @@ public class IotWebSocketClient implements WebSocket.Listener { | ||
| 113 | 114 | ||
| 114 | try { | 115 | try { |
| 115 | String messageData; | 116 | String messageData; |
| 116 | - if ("JSON".equalsIgnoreCase(dataFormat)) { | 117 | + if (IotDataSinkWebSocketConfig.DEFAULT_DATA_FORMAT.equalsIgnoreCase(dataFormat)) { |
| 117 | messageData = JsonUtils.toJsonString(message); | 118 | messageData = JsonUtils.toJsonString(message); |
| 118 | } else { | 119 | } else { |
| 119 | messageData = message.toString(); | 120 | messageData = message.toString(); |