Commit 60817a6a5ba8aaa60ba63c280461dbd846cbe127
1 parent
15857674
perf:【iot】IotDataSinkTcpConfig 常量枚举
Showing
2 changed files
with
42 additions
and
14 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/config/IotDataSinkTcpConfig.java
| ... | ... | @@ -11,6 +11,35 @@ import lombok.Data; |
| 11 | 11 | public class IotDataSinkTcpConfig extends IotAbstractDataSinkConfig { |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | + * 默认连接超时时间(毫秒) | |
| 15 | + */ | |
| 16 | + public static final int DEFAULT_CONNECT_TIMEOUT_MS = 5000; | |
| 17 | + /** | |
| 18 | + * 默认读取超时时间(毫秒) | |
| 19 | + */ | |
| 20 | + public static final int DEFAULT_READ_TIMEOUT_MS = 10000; | |
| 21 | + /** | |
| 22 | + * 默认是否启用 SSL | |
| 23 | + */ | |
| 24 | + public static final boolean DEFAULT_SSL = false; | |
| 25 | + /** | |
| 26 | + * 默认数据格式 | |
| 27 | + */ | |
| 28 | + public static final String DEFAULT_DATA_FORMAT = "JSON"; | |
| 29 | + /** | |
| 30 | + * 默认心跳间隔时间(毫秒) | |
| 31 | + */ | |
| 32 | + public static final long DEFAULT_HEARTBEAT_INTERVAL_MS = 30000L; | |
| 33 | + /** | |
| 34 | + * 默认重连间隔时间(毫秒) | |
| 35 | + */ | |
| 36 | + public static final long DEFAULT_RECONNECT_INTERVAL_MS = 5000L; | |
| 37 | + /** | |
| 38 | + * 默认最大重连次数 | |
| 39 | + */ | |
| 40 | + public static final int DEFAULT_MAX_RECONNECT_ATTEMPTS = 3; | |
| 41 | + | |
| 42 | + /** | |
| 14 | 43 | * TCP 服务器地址 |
| 15 | 44 | */ |
| 16 | 45 | private String host; |
| ... | ... | @@ -23,17 +52,17 @@ public class IotDataSinkTcpConfig extends IotAbstractDataSinkConfig { |
| 23 | 52 | /** |
| 24 | 53 | * 连接超时时间(毫秒) |
| 25 | 54 | */ |
| 26 | - private Integer connectTimeoutMs = 5000; | |
| 55 | + private Integer connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MS; | |
| 27 | 56 | |
| 28 | 57 | /** |
| 29 | 58 | * 读取超时时间(毫秒) |
| 30 | 59 | */ |
| 31 | - private Integer readTimeoutMs = 10000; | |
| 60 | + private Integer readTimeoutMs = DEFAULT_READ_TIMEOUT_MS; | |
| 32 | 61 | |
| 33 | 62 | /** |
| 34 | 63 | * 是否启用 SSL |
| 35 | 64 | */ |
| 36 | - private Boolean ssl = false; | |
| 65 | + private Boolean ssl = DEFAULT_SSL; | |
| 37 | 66 | |
| 38 | 67 | /** |
| 39 | 68 | * SSL 证书路径(当 ssl=true 时需要) |
| ... | ... | @@ -43,21 +72,21 @@ public class IotDataSinkTcpConfig extends IotAbstractDataSinkConfig { |
| 43 | 72 | /** |
| 44 | 73 | * 数据格式:JSON 或 BINARY |
| 45 | 74 | */ |
| 46 | - private String dataFormat = "JSON"; | |
| 75 | + private String dataFormat = DEFAULT_DATA_FORMAT; | |
| 47 | 76 | |
| 48 | 77 | /** |
| 49 | 78 | * 心跳间隔时间(毫秒),0 表示不启用心跳 |
| 50 | 79 | */ |
| 51 | - private Long heartbeatIntervalMs = 30000L; | |
| 80 | + private Long heartbeatIntervalMs = DEFAULT_HEARTBEAT_INTERVAL_MS; | |
| 52 | 81 | |
| 53 | 82 | /** |
| 54 | 83 | * 重连间隔时间(毫秒) |
| 55 | 84 | */ |
| 56 | - private Long reconnectIntervalMs = 5000L; | |
| 85 | + private Long reconnectIntervalMs = DEFAULT_RECONNECT_INTERVAL_MS; | |
| 57 | 86 | |
| 58 | 87 | /** |
| 59 | 88 | * 最大重连次数 |
| 60 | 89 | */ |
| 61 | - private Integer maxReconnectAttempts = 3; | |
| 90 | + private Integer maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS; | |
| 62 | 91 | |
| 63 | 92 | } |
| 64 | 93 | \ 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
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.service.rule.data.action.tcp; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 4 | 4 | import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage; |
| 5 | +import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkTcpConfig; | |
| 5 | 6 | import lombok.extern.slf4j.Slf4j; |
| 6 | 7 | |
| 7 | 8 | import javax.net.ssl.SSLSocketFactory; |
| ... | ... | @@ -38,16 +39,15 @@ public class IotTcpClient { |
| 38 | 39 | private BufferedReader reader; |
| 39 | 40 | private final AtomicBoolean connected = new AtomicBoolean(false); |
| 40 | 41 | |
| 41 | - // TODO @puhui999:default 值,IotDataSinkTcpConfig.java 枚举起来哈; | |
| 42 | 42 | public IotTcpClient(String host, Integer port, Integer connectTimeoutMs, Integer readTimeoutMs, |
| 43 | 43 | Boolean ssl, String sslCertPath, String dataFormat) { |
| 44 | 44 | this.host = host; |
| 45 | 45 | this.port = port; |
| 46 | - this.connectTimeoutMs = connectTimeoutMs != null ? connectTimeoutMs : 5000; | |
| 47 | - this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : 10000; | |
| 48 | - this.ssl = ssl != null ? ssl : false; | |
| 46 | + this.connectTimeoutMs = connectTimeoutMs != null ? connectTimeoutMs : IotDataSinkTcpConfig.DEFAULT_CONNECT_TIMEOUT_MS; | |
| 47 | + this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : IotDataSinkTcpConfig.DEFAULT_READ_TIMEOUT_MS; | |
| 48 | + this.ssl = ssl != null ? ssl : IotDataSinkTcpConfig.DEFAULT_SSL; | |
| 49 | 49 | this.sslCertPath = sslCertPath; |
| 50 | - this.dataFormat = dataFormat != null ? dataFormat : "JSON"; | |
| 50 | + this.dataFormat = dataFormat != null ? dataFormat : IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT; | |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| ... | ... | @@ -99,9 +99,8 @@ public class IotTcpClient { |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | try { |
| 102 | - // TODO @puhui999:枚举值 | |
| 103 | 102 | String messageData; |
| 104 | - if ("JSON".equalsIgnoreCase(dataFormat)) { | |
| 103 | + if (IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT.equalsIgnoreCase(dataFormat)) { | |
| 105 | 104 | // JSON 格式 |
| 106 | 105 | messageData = JsonUtils.toJsonString(message); |
| 107 | 106 | } else { | ... | ... |