Bladeren bron

perf:【iot】IotDataSinkTcpConfig 常量枚举

puhui999 5 maanden geleden
bovenliggende
commit
60817a6a5b

+ 36 - 7
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/config/IotDataSinkTcpConfig.java

@@ -10,6 +10,35 @@ import lombok.Data;
 @Data
 public class IotDataSinkTcpConfig extends IotAbstractDataSinkConfig {
 
+    /**
+     * 默认连接超时时间(毫秒)
+     */
+    public static final int DEFAULT_CONNECT_TIMEOUT_MS = 5000;
+    /**
+     * 默认读取超时时间(毫秒)
+     */
+    public static final int DEFAULT_READ_TIMEOUT_MS = 10000;
+    /**
+     * 默认是否启用 SSL
+     */
+    public static final boolean DEFAULT_SSL = false;
+    /**
+     * 默认数据格式
+     */
+    public static final String DEFAULT_DATA_FORMAT = "JSON";
+    /**
+     * 默认心跳间隔时间(毫秒)
+     */
+    public static final long DEFAULT_HEARTBEAT_INTERVAL_MS = 30000L;
+    /**
+     * 默认重连间隔时间(毫秒)
+     */
+    public static final long DEFAULT_RECONNECT_INTERVAL_MS = 5000L;
+    /**
+     * 默认最大重连次数
+     */
+    public static final int DEFAULT_MAX_RECONNECT_ATTEMPTS = 3;
+
     /**
      * TCP 服务器地址
      */
@@ -23,17 +52,17 @@ public class IotDataSinkTcpConfig extends IotAbstractDataSinkConfig {
     /**
      * 连接超时时间(毫秒)
      */
-    private Integer connectTimeoutMs = 5000;
+    private Integer connectTimeoutMs = DEFAULT_CONNECT_TIMEOUT_MS;
 
     /**
      * 读取超时时间(毫秒)
      */
-    private Integer readTimeoutMs = 10000;
+    private Integer readTimeoutMs = DEFAULT_READ_TIMEOUT_MS;
 
     /**
      * 是否启用 SSL
      */
-    private Boolean ssl = false;
+    private Boolean ssl = DEFAULT_SSL;
 
     /**
      * SSL 证书路径(当 ssl=true 时需要)
@@ -43,21 +72,21 @@ public class IotDataSinkTcpConfig extends IotAbstractDataSinkConfig {
     /**
      * 数据格式:JSON 或 BINARY
      */
-    private String dataFormat = "JSON";
+    private String dataFormat = DEFAULT_DATA_FORMAT;
 
     /**
      * 心跳间隔时间(毫秒),0 表示不启用心跳
      */
-    private Long heartbeatIntervalMs = 30000L;
+    private Long heartbeatIntervalMs = DEFAULT_HEARTBEAT_INTERVAL_MS;
 
     /**
      * 重连间隔时间(毫秒)
      */
-    private Long reconnectIntervalMs = 5000L;
+    private Long reconnectIntervalMs = DEFAULT_RECONNECT_INTERVAL_MS;
 
     /**
      * 最大重连次数
      */
-    private Integer maxReconnectAttempts = 3;
+    private Integer maxReconnectAttempts = DEFAULT_MAX_RECONNECT_ATTEMPTS;
 
 }

+ 6 - 7
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;
 
 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
 import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
+import cn.iocoder.yudao.module.iot.dal.dataobject.rule.config.IotDataSinkTcpConfig;
 import lombok.extern.slf4j.Slf4j;
 
 import javax.net.ssl.SSLSocketFactory;
@@ -38,16 +39,15 @@ public class IotTcpClient {
     private BufferedReader reader;
     private final AtomicBoolean connected = new AtomicBoolean(false);
 
-    // TODO @puhui999:default 值,IotDataSinkTcpConfig.java 枚举起来哈;
     public IotTcpClient(String host, Integer port, Integer connectTimeoutMs, Integer readTimeoutMs,
                         Boolean ssl, String sslCertPath, String dataFormat) {
         this.host = host;
         this.port = port;
-        this.connectTimeoutMs = connectTimeoutMs != null ? connectTimeoutMs : 5000;
-        this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : 10000;
-        this.ssl = ssl != null ? ssl : false;
+        this.connectTimeoutMs = connectTimeoutMs != null ? connectTimeoutMs : IotDataSinkTcpConfig.DEFAULT_CONNECT_TIMEOUT_MS;
+        this.readTimeoutMs = readTimeoutMs != null ? readTimeoutMs : IotDataSinkTcpConfig.DEFAULT_READ_TIMEOUT_MS;
+        this.ssl = ssl != null ? ssl : IotDataSinkTcpConfig.DEFAULT_SSL;
         this.sslCertPath = sslCertPath;
-        this.dataFormat = dataFormat != null ? dataFormat : "JSON";
+        this.dataFormat = dataFormat != null ? dataFormat : IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT;
     }
 
     /**
@@ -99,9 +99,8 @@ public class IotTcpClient {
         }
 
         try {
-            // TODO @puhui999:枚举值
             String messageData;
-            if ("JSON".equalsIgnoreCase(dataFormat)) {
+            if (IotDataSinkTcpConfig.DEFAULT_DATA_FORMAT.equalsIgnoreCase(dataFormat)) {
                 // JSON 格式
                 messageData = JsonUtils.toJsonString(message);
             } else {