[功能添加]:物模型日志表创建

This commit is contained in:
alwayssuper 2024-12-31 17:00:25 +08:00
parent 9c3aa5d95f
commit eaee4642d6
8 changed files with 62 additions and 66 deletions

View File

@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
// TODO @芋艿纠结下字段 // TODO @芋艿纠结下字段
@Deprecated
/** /**
* TD 物模型消息日志的数据库 * TD 物模型消息日志的数据库
*/ */
@ -17,26 +18,17 @@ public class ThingModelMessageDO {
// TODO @haohaosuperTableName tableName 是不是合并因为每个 mapper 操作的时候有且只会使用到其中一个
/**
* 超级表名称
*/
private String superTableName;
/**
* 表名称
*/
private String tableName;
/** /**
* 消息 ID * 消息 ID
*/ */
private String id; private String id;
/** /**
* 扩展功能的参数 * 系统扩展参数
*
* 例如设备状态系统时间固件版本等系统级信息
*/ */
private Object sys; private Object system;
/** /**
* 请求方法 * 请求方法
@ -55,6 +47,12 @@ public class ThingModelMessageDO {
*/ */
private Long time; private Long time;
/**
* 设备信息
*/
private String productKey;
/** /**
* 设备 key * 设备 key
*/ */

View File

@ -1,11 +1,13 @@
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.ThingModelMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessageDO; import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessageDO;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS; import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore; import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/** /**
* 处理 TD 中物模型消息日志的操作 * 处理 TD 中物模型消息日志的操作
@ -20,13 +22,13 @@ public interface TdThingModelMessageMapper {
* *
*/ */
void createSuperTable(ThingModelMessageDO superTable); void createSuperTable(@Param("productKey") String productKey);
/** /**
* 创建子表 * 创建子表
* *
*/ */
void createTableWithTag(ThingModelMessageDO table); void createTableWithTag(@Param("productKey") String productKey,@Param("deviceKey") String deviceKey);
} }

View File

@ -17,6 +17,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper; import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper;
import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO; import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper; import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
import cn.iocoder.yudao.module.iot.dal.tdengine.TdThingModelMessageMapper;
import cn.iocoder.yudao.module.iot.enums.IotConstants; import cn.iocoder.yudao.module.iot.enums.IotConstants;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum; import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum; import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
@ -84,6 +85,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
@Resource @Resource
private IotDevicePropertyDataMapper devicePropertyDataMapper; private IotDevicePropertyDataMapper devicePropertyDataMapper;
@Resource
private TdThingModelMessageMapper tdThingModelMessageMapper;
@Override @Override
public void defineDevicePropertyData(Long productId) { public void defineDevicePropertyData(Long productId) {
// 1.1 查询产品和物模型 // 1.1 查询产品和物模型
@ -108,7 +112,10 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
return; return;
} }
newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP)); newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP));
// 2.1.1 创建产品超级表
devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields); devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields);
// 2.1.2 创建物模型日志超级表
tdThingModelMessageMapper.createSuperTable(product.getProductKey());
return; return;
} }
// 2.2 情况二如果是修改的时候需要更新表 // 2.2 情况二如果是修改的时候需要更新表

View File

@ -34,9 +34,6 @@ public class IotProductServiceImpl implements IotProductService {
@Resource @Resource
private IotProductMapper productMapper; private IotProductMapper productMapper;
@Resource
@Lazy // 延迟加载解决循环依赖
private IotThingModelMessageService thingModelMessageService;
@Resource @Resource
@Lazy // 延迟加载解决循环依赖 @Lazy // 延迟加载解决循环依赖
private IotDevicePropertyDataService devicePropertyDataService; private IotDevicePropertyDataService devicePropertyDataService;
@ -125,8 +122,7 @@ public class IotProductServiceImpl implements IotProductService {
if (Objects.equals(status, IotProductStatusEnum.PUBLISHED.getStatus())) { if (Objects.equals(status, IotProductStatusEnum.PUBLISHED.getStatus())) {
// 3.1 创建产品超级表数据模型 // 3.1 创建产品超级表数据模型
devicePropertyDataService.defineDevicePropertyData(id); devicePropertyDataService.defineDevicePropertyData(id);
// 3.2 创建物模型日志超级表数据模型 TODO 待定message 要不要分
thingModelMessageService.createSuperTable(id);
} }
productMapper.updateById(updateObj); productMapper.updateById(updateObj);
} }

View File

@ -16,11 +16,6 @@ public interface IotThingModelMessageService {
*/ */
void saveThingModelMessage(IotDeviceDO device, ThingModelMessage thingModelMessage); void saveThingModelMessage(IotDeviceDO device, ThingModelMessage thingModelMessage);
/**
* 创建物模型消息日志超级表
*
* @param productId 产品编号
*/
void createSuperTable(Long productId);
} }

View File

@ -79,7 +79,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO() iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO()
.setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus())); .setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus()));
// 1.2 创建物模型日志设备表 // 1.2 创建物模型日志设备表
createThingModelMessageDeviceTable(device.getProductKey(), device.getDeviceKey()); tdThingModelMessageMapper.createTableWithTag(device.getProductKey(), device.getDeviceKey());
} }
// 2. 获取设备属性并进行物模型校验过滤非物模型属性 // 2. 获取设备属性并进行物模型校验过滤非物模型属性
@ -108,32 +108,34 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType())); thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
} }
@Override // @Override
@TenantIgnore // @TenantIgnore
public void createSuperTable(Long productId) { // public void createSuperTable(Long productId) {
// 1. 查询产品 // // 1. 查询产品
IotProductDO product = productService.getProduct(productId); // IotProductDO product = productService.getProduct(productId);
// // 2. 创建日志超级表
// 2. 获取超级表的名称和数据库名称 // tdThingModelMessageMapper.createSuperTable(product.getProductKey());
// TODO @alwayssuper最好 databaseNamesuperTableName 的处理放到 tdThinkModelMessageMapper 可以考虑弄个 default 方法 //
String databaseName = IotTdDatabaseUtils.getDatabaseName(url); // // 2. 获取超级表的名称和数据库名称
String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(product.getProductKey()); // // TODO @alwayssuper最好 databaseNamesuperTableName 的处理放到 tdThinkModelMessageMapper 可以考虑弄个 default 方法
//// String databaseName = IotTdDatabaseUtils.getDatabaseName(url);
// 解析物模型获取字段列表 //// String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(product.getProductKey());
List<TdFieldDO> schemaFields = List.of( ////
TdFieldDO.builder().fieldName("time").dataType("TIMESTAMP").build(), //// // 解析物模型获取字段列表
TdFieldDO.builder().fieldName("id").dataType("NCHAR").dataLength(64).build(), //// List<TdFieldDO> schemaFields = List.of(
TdFieldDO.builder().fieldName("sys").dataType("NCHAR").dataLength(2048).build(), //// TdFieldDO.builder().fieldName("time").dataType("TIMESTAMP").build(),
TdFieldDO.builder().fieldName("method").dataType("NCHAR").dataLength(256).build(), //// TdFieldDO.builder().fieldName("id").dataType("NCHAR").dataLength(64).build(),
TdFieldDO.builder().fieldName("params").dataType("NCHAR").dataLength(2048).build() //// TdFieldDO.builder().fieldName("sys").dataType("NCHAR").dataLength(2048).build(),
); //// TdFieldDO.builder().fieldName("method").dataType("NCHAR").dataLength(256).build(),
// 设置超级表的标签 //// TdFieldDO.builder().fieldName("params").dataType("NCHAR").dataLength(2048).build()
List<TdFieldDO> tagsFields = List.of( //// );
TdFieldDO.builder().fieldName("device_key").dataType("NCHAR").dataLength(64).build() //// // 设置超级表的标签
); //// List<TdFieldDO> tagsFields = List.of(
// 3. 创建超级表 //// TdFieldDO.builder().fieldName("device_key").dataType("NCHAR").dataLength(64).build()
tdEngineDDLMapper.createSuperTable(new TdTableDO(databaseName, superTableName, schemaFields, tagsFields)); //// );
} //// // 3. 创建超级表
//// tdEngineDDLMapper.createSuperTable(new TdTableDO(databaseName, superTableName, schemaFields, tagsFields));
// }
private List<IotThingModelDO> getValidFunctionList(String productKey) { private List<IotThingModelDO> getValidFunctionList(String productKey) {
return filterList(iotThingModelService.getProductThingModelListByProductKey(productKey), return filterList(iotThingModelService.getProductThingModelListByProductKey(productKey),
@ -237,13 +239,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
* *
*/ */
private void createThingModelMessageDeviceTable(String productKey, String deviceKey){ private void createThingModelMessageDeviceTable(String productKey, String deviceKey){
tdThingModelMessageMapper.createTableWithTag(productKey, deviceKey);
// 2. 创建物模型日志设备数据表
// tdThingModelMessageMapper.createTableWithTag(ThingModelMessageDO.builder().build()
// .setSuperTableName(productKey)
// .setTableName(deviceKey)
// .setDeviceKey(deviceKey));
} }
/** /**

View File

@ -42,7 +42,7 @@
<!-- 根据标签获取最新数据 --> <!-- 根据标签获取最新数据 -->
<select id="selectLastDataListByTags" parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TagsSelectDO" <select id="selectLastDataListByTags" parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TagsSelectDO"
resultType="Map"> resultType="java.util.Map">
SELECT LAST(*) SELECT LAST(*)
FROM ${dataBaseName}.${stableName} FROM ${dataBaseName}.${stableName}
GROUP BY ${tagsName} GROUP BY ${tagsName}

View File

@ -6,12 +6,13 @@
<!-- 创建物模型消息日志超级表 --> <!-- 创建物模型消息日志超级表 -->
<update id="createSuperTable"> <update id="createSuperTable">
CREATE STABLE thing_model_message_${superTableName}( CREATE STABLE thing_model_message_${productKey}(
ts TIMESTAMP, ts TIMESTAMP,
id NCHAR(64), id NCHAR(64),
sys NCHAR(2048), sys NCHAR(2048),
method NCHAR(255), method NCHAR(255),
params NCHAR(2048) params NCHAR(2048),
device_name NCHAR(64)
)TAGS ( )TAGS (
device_key NCHAR(50) device_key NCHAR(50)
) )
@ -19,13 +20,14 @@
<!-- 创建物模型消息日志子表带有deviceKey的TAG --> <!-- 创建物模型消息日志子表带有deviceKey的TAG -->
<update id="createTableWithTag"> <update id="createTableWithTag">
CREATE STABLE ${tableName} CREATE STABLE ${deviceKey}
USING thing_model_message_${superTableName}( USING thing_model_message_${productKey}(
ts, ts,
id , id ,
sys , sys ,
method , method ,
params params ,
device_name
)TAGS( )TAGS(
#{device_key} #{device_key}
) )