Pārlūkot izejas kodu

【代码评审】IoT:模拟设备发送的 review

YunaiV 8 mēneši atpakaļ
vecāks
revīzija
b2434b7b41

+ 1 - 1
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/IotConstants.java

@@ -40,6 +40,6 @@ public interface IotConstants {
      * <p>
      * 格式为 thing_model_message_{productKey}_{deviceName}
      */
-    String THINK_MODEL_MESSAGE_TABLE_NAME_FORMAT = "thing_model_message_%s_%s";
+    String THING_MODEL_MESSAGE_TABLE_NAME_FORMAT = "thing_model_message_%s_%s";
 
 }

+ 7 - 7
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/ThinkModelMessageDO.java → yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/ThingModelMessageDO.java

@@ -4,9 +4,8 @@ import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
-import org.apache.xmlbeans.impl.xb.xsdschema.Public;
-
 
+// TODO @芋艿:纠结下字段
 /**
  * TD 物模型消息日志的数据库
  */
@@ -14,7 +13,8 @@ import org.apache.xmlbeans.impl.xb.xsdschema.Public;
 @Builder
 @NoArgsConstructor
 @AllArgsConstructor
-public class ThinkModelMessageDO {
+public class ThingModelMessageDO {
+
     /**
      * 数据库名称
      */
@@ -32,7 +32,7 @@ public class ThinkModelMessageDO {
     private String tableName;
 
     /**
-     * 消息ID
+     * 消息 ID
      */
     private String id;
 
@@ -42,7 +42,9 @@ public class ThinkModelMessageDO {
     private Object sys;
 
     /**
-     * 请求方法 例如:thing.event.property.post
+     * 请求方法
+     *
+     * 例如:thing.event.property.post
      */
     private String method;
 
@@ -56,11 +58,9 @@ public class ThinkModelMessageDO {
      */
     private Long time;
 
-
     /**
      * 设备 key
      */
     private String deviceKey;
 
-
 }

+ 4 - 3
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/TdThinkModelMessageMapper.java

@@ -1,7 +1,7 @@
 package cn.iocoder.yudao.module.iot.dal.tdengine;
 
 import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
-import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThinkModelMessageDO;
+import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessageDO;
 import com.baomidou.dynamic.datasource.annotation.DS;
 import org.apache.ibatis.annotations.Mapper;
 
@@ -17,12 +17,13 @@ public interface TdThinkModelMessageMapper {
      *
      */
     @TenantIgnore
-    void createSuperTable(ThinkModelMessageDO superTable);
+    void createSuperTable(ThingModelMessageDO superTable);
 
     /**
      * 创建子表
      *
      */
     @TenantIgnore
-    void createTableWithTag(ThinkModelMessageDO table);
+    void createTableWithTag(ThingModelMessageDO table);
+
 }

+ 2 - 0
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/mq/producer/simulatesend/SimulateSendProducer.java

@@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Component;
 
+// TODO @alwayssuper:是不是还没用起来哈?Producer 最好属于某个模块;
 /**
  * SimulateSend 模拟设备上报的 Producer
  *
@@ -14,6 +15,7 @@ import org.springframework.stereotype.Component;
 @Slf4j
 @Component
 public class SimulateSendProducer {
+
     @Resource
     private ApplicationContext applicationContext;
 

+ 1 - 1
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java

@@ -122,7 +122,7 @@ public class IotProductServiceImpl implements IotProductService {
             thingModelFunctionService.createSuperTableDataModel(id);
             // 3.2 创建物模型日志超级表数据模型
             thingModelMessageService.createSuperTable(id);
-        }
+        }x
         productMapper.updateById(updateObj);
     }
 

+ 10 - 7
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThingModelMessageServiceImpl.java

@@ -57,12 +57,14 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
     private IotProductThingModelService iotProductThingModelService;
     @Resource
     private IotDeviceService iotDeviceService;
+    @Resource
+    private IotProductService productService;
+
     @Resource
     private TdEngineDDLMapper tdEngineDDLMapper;
     @Resource
     private TdEngineDMLMapper tdEngineDMLMapper;
-    @Resource
-    private IotProductService productService;
+
     @Resource
     private DeviceDataRedisDAO deviceDataRedisDAO;
 
@@ -119,11 +121,12 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
         IotProductDO product = productService.getProduct(productId);
 
         // 2. 获取超级表的名称和数据库名称
+        // TODO @alwayssuper:最好 databaseName、superTableName 的处理,放到 tdThinkModelMessageMapper 里。可以考虑,弄个 default 方法
         String databaseName = iotTdDatabaseUtils.getDatabaseName();
-        String superTableName = IotTdDatabaseUtils.getThinkModelMessageSuperTableName(product.getProductKey());
+        String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(product.getProductKey());
 
         // 3. 创建超级表
-        tdThinkModelMessageMapper.createSuperTable(ThinkModelMessageDO.builder().build()
+        tdThinkModelMessageMapper.createSuperTable(ThingModelMessageDO.builder().build()
                 .setDataBaseName(databaseName)
                 .setSuperTableName(superTableName));
     }
@@ -238,16 +241,16 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
 
         // 1. 获取超级表的名称、数据库名称、设备日志表名称
         String databaseName = iotTdDatabaseUtils.getDatabaseName();
-        String superTableName = IotTdDatabaseUtils.getThinkModelMessageSuperTableName(productKey);
+        String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(productKey);
+        // TODO @alwayssuper:最好 databaseName、superTableName、thinkModelMessageDeviceTableName 的处理,放到 tdThinkModelMessageMapper 里。可以考虑,弄个 default 方法
         String thinkModelMessageDeviceTableName = IotTdDatabaseUtils.getThinkModelMessageDeviceTableName(productKey, deviceName);
 
         // 2. 创建物模型日志设备数据表
-        tdThinkModelMessageMapper.createTableWithTag(ThinkModelMessageDO.builder().build()
+        tdThinkModelMessageMapper.createTableWithTag(ThingModelMessageDO.builder().build()
                 .setDataBaseName(databaseName)
                 .setSuperTableName(superTableName)
                 .setTableName(thinkModelMessageDeviceTableName)
                 .setDeviceKey(deviceKey));
-
     }
 
     /**

+ 8 - 3
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/util/IotTdDatabaseUtils.java

@@ -4,8 +4,10 @@ import cn.iocoder.yudao.module.iot.enums.IotConstants;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
+// TODO @芋艿:可能要思索下,有没更好的处理方式
+// TODO @芋艿:怎么改成无状态
 /**
- * TD数据库工具类
+ * TD 数据库工具类
  *
  * @author AlwaysSuper
  */
@@ -19,6 +21,7 @@ public class IotTdDatabaseUtils {
      * 获取数据库名称
      */
     public String getDatabaseName() {
+//       TODO @alwayssuper:StrUtil.subAfter("/")
         int index = url.lastIndexOf("/");
         return index != -1 ? url.substring(index + 1) : url;
     }
@@ -31,6 +34,7 @@ public class IotTdDatabaseUtils {
      * @return 产品超级表表名
      */
     public static String getProductSuperTableName(Integer deviceType, String productKey) {
+        // TODO @alwayssuper:枚举字段,不要 1、2、3;不符合预期,抛出异常
         return switch (deviceType) {
             case 1 -> String.format(IotConstants.GATEWAY_SUB_STABLE_NAME_FORMAT, productKey).toLowerCase();
             case 2 -> String.format(IotConstants.GATEWAY_STABLE_NAME_FORMAT, productKey).toLowerCase();
@@ -45,7 +49,8 @@ public class IotTdDatabaseUtils {
      * @return 物模型日志超级表表名
      *
      */
-    public static String getThinkModelMessageSuperTableName(String productKey) {
+    public static String getThingModelMessageSuperTableName(String productKey) {
+        // TODO @alwayssuper:是不是应该 + 拼接就好,不用 format
         return String.format("thing_model_message_", productKey).toLowerCase();
     }
 
@@ -57,7 +62,7 @@ public class IotTdDatabaseUtils {
      * @return 物模型日志设备表名
      */
     public static String getThinkModelMessageDeviceTableName(String productKey, String deviceName) {
-        return String.format(IotConstants.THINK_MODEL_MESSAGE_TABLE_NAME_FORMAT, productKey.toLowerCase(), deviceName.toLowerCase());
+        return String.format(IotConstants.THING_MODEL_MESSAGE_TABLE_NAME_FORMAT, productKey.toLowerCase(), deviceName.toLowerCase());
     }
 
 }

+ 7 - 7
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/tdengine/TdThinkModelMessageMapper.xml

@@ -4,17 +4,17 @@
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="cn.iocoder.yudao.module.iot.dal.tdengine.TdThinkModelMessageMapper">
 
-
     <!-- 创建物模型消息日志超级表 -->
+    <!-- TODO @芋艿:捉摸下字段,特别是 sys、ts 这种缩写 -->
     <update id="createSuperTable">
         CREATE STABLE ${dataBaseName}.${superTableName}(
-        ts TIMESTAMP,
-        id VARCHAR(255),
-        sys VARCHAR(2048),
-        method VARCHAR(255),
-        params VARCHAR(2048)
+            ts TIMESTAMP,
+            id VARCHAR(255),
+            sys VARCHAR(2048),
+            method VARCHAR(255),
+            params VARCHAR(2048)
         )TAGS (
-        deviceKey VARCHAR(255)
+            deviceKey VARCHAR(255)
         )
     </update>