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

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

View File

@ -1,11 +1,13 @@
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.ThingModelMessage;
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessageDO;
import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 处理 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.redis.deviceData.DeviceDataRedisDAO;
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.thingmodel.IotDataSpecsDataTypeEnum;
import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
@ -84,6 +85,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
@Resource
private IotDevicePropertyDataMapper devicePropertyDataMapper;
@Resource
private TdThingModelMessageMapper tdThingModelMessageMapper;
@Override
public void defineDevicePropertyData(Long productId) {
// 1.1 查询产品和物模型
@ -108,7 +112,10 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
return;
}
newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP));
// 2.1.1 创建产品超级表
devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields);
// 2.1.2 创建物模型日志超级表
tdThingModelMessageMapper.createSuperTable(product.getProductKey());
return;
}
// 2.2 情况二如果是修改的时候需要更新表

View File

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

View File

@ -16,11 +16,6 @@ public interface IotThingModelMessageService {
*/
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()
.setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus()));
// 1.2 创建物模型日志设备表
createThingModelMessageDeviceTable(device.getProductKey(), device.getDeviceKey());
tdThingModelMessageMapper.createTableWithTag(device.getProductKey(), device.getDeviceKey());
}
// 2. 获取设备属性并进行物模型校验过滤非物模型属性
@ -108,32 +108,34 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
}
@Override
@TenantIgnore
public void createSuperTable(Long productId) {
// 1. 查询产品
IotProductDO product = productService.getProduct(productId);
// 2. 获取超级表的名称和数据库名称
// 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(),
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()
);
// 3. 创建超级表
tdEngineDDLMapper.createSuperTable(new TdTableDO(databaseName, superTableName, schemaFields, tagsFields));
}
// @Override
// @TenantIgnore
// public void createSuperTable(Long productId) {
// // 1. 查询产品
// IotProductDO product = productService.getProduct(productId);
// // 2. 创建日志超级表
// tdThingModelMessageMapper.createSuperTable(product.getProductKey());
//
// // 2. 获取超级表的名称和数据库名称
// // 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(),
//// 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()
//// );
//// // 3. 创建超级表
//// tdEngineDDLMapper.createSuperTable(new TdTableDO(databaseName, superTableName, schemaFields, tagsFields));
// }
private List<IotThingModelDO> getValidFunctionList(String productKey) {
return filterList(iotThingModelService.getProductThingModelListByProductKey(productKey),
@ -237,13 +239,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
*
*/
private void createThingModelMessageDeviceTable(String productKey, String deviceKey){
// 2. 创建物模型日志设备数据表
// tdThingModelMessageMapper.createTableWithTag(ThingModelMessageDO.builder().build()
// .setSuperTableName(productKey)
// .setTableName(deviceKey)
// .setDeviceKey(deviceKey));
tdThingModelMessageMapper.createTableWithTag(productKey, deviceKey);
}
/**

View File

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

View File

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