Commit b2434b7b41b52aefff1de1412f0ebea0064524f1
1 parent
95067fd6
【代码评审】IoT:模拟设备发送的 review
Showing
8 changed files
with
40 additions
and
29 deletions
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 { |
| 40 | 40 | * <p> |
| 41 | 41 | * 格式为 thing_model_message_{productKey}_{deviceName} |
| 42 | 42 | */ |
| 43 | - String THINK_MODEL_MESSAGE_TABLE_NAME_FORMAT = "thing_model_message_%s_%s"; | |
| 43 | + String THING_MODEL_MESSAGE_TABLE_NAME_FORMAT = "thing_model_message_%s_%s"; | |
| 44 | 44 | |
| 45 | 45 | } |
| 46 | 46 | \ No newline at end of file | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/ThinkModelMessageDO.java renamed to 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; |
| 4 | 4 | import lombok.Builder; |
| 5 | 5 | import lombok.Data; |
| 6 | 6 | import lombok.NoArgsConstructor; |
| 7 | -import org.apache.xmlbeans.impl.xb.xsdschema.Public; | |
| 8 | - | |
| 9 | 7 | |
| 8 | +// TODO @芋艿:纠结下字段 | |
| 10 | 9 | /** |
| 11 | 10 | * TD 物模型消息日志的数据库 |
| 12 | 11 | */ |
| ... | ... | @@ -14,7 +13,8 @@ import org.apache.xmlbeans.impl.xb.xsdschema.Public; |
| 14 | 13 | @Builder |
| 15 | 14 | @NoArgsConstructor |
| 16 | 15 | @AllArgsConstructor |
| 17 | -public class ThinkModelMessageDO { | |
| 16 | +public class ThingModelMessageDO { | |
| 17 | + | |
| 18 | 18 | /** |
| 19 | 19 | * 数据库名称 |
| 20 | 20 | */ |
| ... | ... | @@ -32,7 +32,7 @@ public class ThinkModelMessageDO { |
| 32 | 32 | private String tableName; |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | - * 消息ID | |
| 35 | + * 消息 ID | |
| 36 | 36 | */ |
| 37 | 37 | private String id; |
| 38 | 38 | |
| ... | ... | @@ -42,7 +42,9 @@ public class ThinkModelMessageDO { |
| 42 | 42 | private Object sys; |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | - * 请求方法 例如:thing.event.property.post | |
| 45 | + * 请求方法 | |
| 46 | + * | |
| 47 | + * 例如:thing.event.property.post | |
| 46 | 48 | */ |
| 47 | 49 | private String method; |
| 48 | 50 | |
| ... | ... | @@ -56,11 +58,9 @@ public class ThinkModelMessageDO { |
| 56 | 58 | */ |
| 57 | 59 | private Long time; |
| 58 | 60 | |
| 59 | - | |
| 60 | 61 | /** |
| 61 | 62 | * 设备 key |
| 62 | 63 | */ |
| 63 | 64 | private String deviceKey; |
| 64 | 65 | |
| 65 | - | |
| 66 | 66 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/TdThinkModelMessageMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.dal.tdengine; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; |
| 4 | -import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThinkModelMessageDO; | |
| 4 | +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessageDO; | |
| 5 | 5 | import com.baomidou.dynamic.datasource.annotation.DS; |
| 6 | 6 | import org.apache.ibatis.annotations.Mapper; |
| 7 | 7 | |
| ... | ... | @@ -17,12 +17,13 @@ public interface TdThinkModelMessageMapper { |
| 17 | 17 | * |
| 18 | 18 | */ |
| 19 | 19 | @TenantIgnore |
| 20 | - void createSuperTable(ThinkModelMessageDO superTable); | |
| 20 | + void createSuperTable(ThingModelMessageDO superTable); | |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * 创建子表 |
| 24 | 24 | * |
| 25 | 25 | */ |
| 26 | 26 | @TenantIgnore |
| 27 | - void createTableWithTag(ThinkModelMessageDO table); | |
| 27 | + void createTableWithTag(ThingModelMessageDO table); | |
| 28 | + | |
| 28 | 29 | } | ... | ... |
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; |
| 5 | 5 | import org.springframework.context.ApplicationContext; |
| 6 | 6 | import org.springframework.stereotype.Component; |
| 7 | 7 | |
| 8 | +// TODO @alwayssuper:是不是还没用起来哈?Producer 最好属于某个模块; | |
| 8 | 9 | /** |
| 9 | 10 | * SimulateSend 模拟设备上报的 Producer |
| 10 | 11 | * |
| ... | ... | @@ -14,6 +15,7 @@ import org.springframework.stereotype.Component; |
| 14 | 15 | @Slf4j |
| 15 | 16 | @Component |
| 16 | 17 | public class SimulateSendProducer { |
| 18 | + | |
| 17 | 19 | @Resource |
| 18 | 20 | private ApplicationContext applicationContext; |
| 19 | 21 | ... | ... |
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 { |
| 122 | 122 | thingModelFunctionService.createSuperTableDataModel(id); |
| 123 | 123 | // 3.2 创建物模型日志超级表数据模型 |
| 124 | 124 | thingModelMessageService.createSuperTable(id); |
| 125 | - } | |
| 125 | + }x | |
| 126 | 126 | productMapper.updateById(updateObj); |
| 127 | 127 | } |
| 128 | 128 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThingModelMessageServiceImpl.java
| ... | ... | @@ -58,11 +58,13 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ |
| 58 | 58 | @Resource |
| 59 | 59 | private IotDeviceService iotDeviceService; |
| 60 | 60 | @Resource |
| 61 | + private IotProductService productService; | |
| 62 | + | |
| 63 | + @Resource | |
| 61 | 64 | private TdEngineDDLMapper tdEngineDDLMapper; |
| 62 | 65 | @Resource |
| 63 | 66 | private TdEngineDMLMapper tdEngineDMLMapper; |
| 64 | - @Resource | |
| 65 | - private IotProductService productService; | |
| 67 | + | |
| 66 | 68 | @Resource |
| 67 | 69 | private DeviceDataRedisDAO deviceDataRedisDAO; |
| 68 | 70 | |
| ... | ... | @@ -119,11 +121,12 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ |
| 119 | 121 | IotProductDO product = productService.getProduct(productId); |
| 120 | 122 | |
| 121 | 123 | // 2. 获取超级表的名称和数据库名称 |
| 124 | + // TODO @alwayssuper:最好 databaseName、superTableName 的处理,放到 tdThinkModelMessageMapper 里。可以考虑,弄个 default 方法 | |
| 122 | 125 | String databaseName = iotTdDatabaseUtils.getDatabaseName(); |
| 123 | - String superTableName = IotTdDatabaseUtils.getThinkModelMessageSuperTableName(product.getProductKey()); | |
| 126 | + String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(product.getProductKey()); | |
| 124 | 127 | |
| 125 | 128 | // 3. 创建超级表 |
| 126 | - tdThinkModelMessageMapper.createSuperTable(ThinkModelMessageDO.builder().build() | |
| 129 | + tdThinkModelMessageMapper.createSuperTable(ThingModelMessageDO.builder().build() | |
| 127 | 130 | .setDataBaseName(databaseName) |
| 128 | 131 | .setSuperTableName(superTableName)); |
| 129 | 132 | } |
| ... | ... | @@ -238,16 +241,16 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ |
| 238 | 241 | |
| 239 | 242 | // 1. 获取超级表的名称、数据库名称、设备日志表名称 |
| 240 | 243 | String databaseName = iotTdDatabaseUtils.getDatabaseName(); |
| 241 | - String superTableName = IotTdDatabaseUtils.getThinkModelMessageSuperTableName(productKey); | |
| 244 | + String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(productKey); | |
| 245 | + // TODO @alwayssuper:最好 databaseName、superTableName、thinkModelMessageDeviceTableName 的处理,放到 tdThinkModelMessageMapper 里。可以考虑,弄个 default 方法 | |
| 242 | 246 | String thinkModelMessageDeviceTableName = IotTdDatabaseUtils.getThinkModelMessageDeviceTableName(productKey, deviceName); |
| 243 | 247 | |
| 244 | 248 | // 2. 创建物模型日志设备数据表 |
| 245 | - tdThinkModelMessageMapper.createTableWithTag(ThinkModelMessageDO.builder().build() | |
| 249 | + tdThinkModelMessageMapper.createTableWithTag(ThingModelMessageDO.builder().build() | |
| 246 | 250 | .setDataBaseName(databaseName) |
| 247 | 251 | .setSuperTableName(superTableName) |
| 248 | 252 | .setTableName(thinkModelMessageDeviceTableName) |
| 249 | 253 | .setDeviceKey(deviceKey)); |
| 250 | - | |
| 251 | 254 | } |
| 252 | 255 | |
| 253 | 256 | /** | ... | ... |
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; |
| 4 | 4 | import org.springframework.beans.factory.annotation.Value; |
| 5 | 5 | import org.springframework.stereotype.Component; |
| 6 | 6 | |
| 7 | +// TODO @芋艿:可能要思索下,有没更好的处理方式 | |
| 8 | +// TODO @芋艿:怎么改成无状态 | |
| 7 | 9 | /** |
| 8 | - * TD数据库工具类 | |
| 10 | + * TD 数据库工具类 | |
| 9 | 11 | * |
| 10 | 12 | * @author AlwaysSuper |
| 11 | 13 | */ |
| ... | ... | @@ -19,6 +21,7 @@ public class IotTdDatabaseUtils { |
| 19 | 21 | * 获取数据库名称 |
| 20 | 22 | */ |
| 21 | 23 | public String getDatabaseName() { |
| 24 | +// TODO @alwayssuper:StrUtil.subAfter("/") | |
| 22 | 25 | int index = url.lastIndexOf("/"); |
| 23 | 26 | return index != -1 ? url.substring(index + 1) : url; |
| 24 | 27 | } |
| ... | ... | @@ -31,6 +34,7 @@ public class IotTdDatabaseUtils { |
| 31 | 34 | * @return 产品超级表表名 |
| 32 | 35 | */ |
| 33 | 36 | public static String getProductSuperTableName(Integer deviceType, String productKey) { |
| 37 | + // TODO @alwayssuper:枚举字段,不要 1、2、3;不符合预期,抛出异常 | |
| 34 | 38 | return switch (deviceType) { |
| 35 | 39 | case 1 -> String.format(IotConstants.GATEWAY_SUB_STABLE_NAME_FORMAT, productKey).toLowerCase(); |
| 36 | 40 | case 2 -> String.format(IotConstants.GATEWAY_STABLE_NAME_FORMAT, productKey).toLowerCase(); |
| ... | ... | @@ -45,7 +49,8 @@ public class IotTdDatabaseUtils { |
| 45 | 49 | * @return 物模型日志超级表表名 |
| 46 | 50 | * |
| 47 | 51 | */ |
| 48 | - public static String getThinkModelMessageSuperTableName(String productKey) { | |
| 52 | + public static String getThingModelMessageSuperTableName(String productKey) { | |
| 53 | + // TODO @alwayssuper:是不是应该 + 拼接就好,不用 format | |
| 49 | 54 | return String.format("thing_model_message_", productKey).toLowerCase(); |
| 50 | 55 | } |
| 51 | 56 | |
| ... | ... | @@ -57,7 +62,7 @@ public class IotTdDatabaseUtils { |
| 57 | 62 | * @return 物模型日志设备表名 |
| 58 | 63 | */ |
| 59 | 64 | public static String getThinkModelMessageDeviceTableName(String productKey, String deviceName) { |
| 60 | - return String.format(IotConstants.THINK_MODEL_MESSAGE_TABLE_NAME_FORMAT, productKey.toLowerCase(), deviceName.toLowerCase()); | |
| 65 | + return String.format(IotConstants.THING_MODEL_MESSAGE_TABLE_NAME_FORMAT, productKey.toLowerCase(), deviceName.toLowerCase()); | |
| 61 | 66 | } |
| 62 | 67 | |
| 63 | 68 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/tdengine/TdThinkModelMessageMapper.xml
| ... | ... | @@ -4,17 +4,17 @@ |
| 4 | 4 | "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 5 | 5 | <mapper namespace="cn.iocoder.yudao.module.iot.dal.tdengine.TdThinkModelMessageMapper"> |
| 6 | 6 | |
| 7 | - | |
| 8 | 7 | <!-- 创建物模型消息日志超级表 --> |
| 8 | + <!-- TODO @芋艿:捉摸下字段,特别是 sys、ts 这种缩写 --> | |
| 9 | 9 | <update id="createSuperTable"> |
| 10 | 10 | CREATE STABLE ${dataBaseName}.${superTableName}( |
| 11 | - ts TIMESTAMP, | |
| 12 | - id VARCHAR(255), | |
| 13 | - sys VARCHAR(2048), | |
| 14 | - method VARCHAR(255), | |
| 15 | - params VARCHAR(2048) | |
| 11 | + ts TIMESTAMP, | |
| 12 | + id VARCHAR(255), | |
| 13 | + sys VARCHAR(2048), | |
| 14 | + method VARCHAR(255), | |
| 15 | + params VARCHAR(2048) | |
| 16 | 16 | )TAGS ( |
| 17 | - deviceKey VARCHAR(255) | |
| 17 | + deviceKey VARCHAR(255) | |
| 18 | 18 | ) |
| 19 | 19 | </update> |
| 20 | 20 | ... | ... |