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

This commit is contained in:
YunaiV 2024-12-19 21:23:07 +08:00
parent 95067fd6c6
commit b2434b7b41
8 changed files with 40 additions and 29 deletions

View File

@ -40,6 +40,6 @@ public interface IotConstants {
* <p> * <p>
* 格式为 thing_model_message_{productKey}_{deviceName} * 格式为 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";
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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