Commit eaee4642d628ecfb146ac5a1e1826a3c9cbaad6a

Authored by alwayssuper
1 parent 9c3aa5d9

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

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/tdengine/ThingModelMessageDO.java
@@ -6,6 +6,7 @@ import lombok.Data; @@ -6,6 +6,7 @@ import lombok.Data;
6 import lombok.NoArgsConstructor; 6 import lombok.NoArgsConstructor;
7 7
8 // TODO @芋艿:纠结下字段 8 // TODO @芋艿:纠结下字段
  9 +@Deprecated
9 /** 10 /**
10 * TD 物模型消息日志的数据库 11 * TD 物模型消息日志的数据库
11 */ 12 */
@@ -17,26 +18,17 @@ public class ThingModelMessageDO { @@ -17,26 +18,17 @@ public class ThingModelMessageDO {
17 18
18 19
19 20
20 - // TODO @haohao:superTableName 和 tableName 是不是合并。因为每个 mapper 操作的时候,有且只会使用到其中一个。  
21 - /**  
22 - * 超级表名称  
23 - */  
24 - private String superTableName;  
25 -  
26 - /**  
27 - * 表名称  
28 - */  
29 - private String tableName;  
30 -  
31 /** 21 /**
32 * 消息 ID 22 * 消息 ID
33 */ 23 */
34 private String id; 24 private String id;
35 25
36 /** 26 /**
37 - * 扩展功能的参数 27 + * 系统扩展参数
  28 + *
  29 + * 例如:设备状态、系统时间、固件版本等系统级信息
38 */ 30 */
39 - private Object sys; 31 + private Object system;
40 32
41 /** 33 /**
42 * 请求方法 34 * 请求方法
@@ -56,6 +48,12 @@ public class ThingModelMessageDO { @@ -56,6 +48,12 @@ public class ThingModelMessageDO {
56 private Long time; 48 private Long time;
57 49
58 /** 50 /**
  51 + * 设备信息
  52 + */
  53 + private String productKey;
  54 +
  55 +
  56 + /**
59 * 设备 key 57 * 设备 key
60 */ 58 */
61 private String deviceKey; 59 private String deviceKey;
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/TdThingModelMessageMapper.java
1 package cn.iocoder.yudao.module.iot.dal.tdengine; 1 package cn.iocoder.yudao.module.iot.dal.tdengine;
2 2
3 import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore; 3 import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
  4 +import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessage;
4 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessageDO; 5 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.ThingModelMessageDO;
5 import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS; 6 import cn.iocoder.yudao.module.iot.framework.tdengine.core.annotation.TDengineDS;
6 import com.baomidou.dynamic.datasource.annotation.DS; 7 import com.baomidou.dynamic.datasource.annotation.DS;
7 import com.baomidou.mybatisplus.annotation.InterceptorIgnore; 8 import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
8 import org.apache.ibatis.annotations.Mapper; 9 import org.apache.ibatis.annotations.Mapper;
  10 +import org.apache.ibatis.annotations.Param;
9 11
10 /** 12 /**
11 * 处理 TD 中物模型消息日志的操作 13 * 处理 TD 中物模型消息日志的操作
@@ -20,13 +22,13 @@ public interface TdThingModelMessageMapper { @@ -20,13 +22,13 @@ public interface TdThingModelMessageMapper {
20 * 22 *
21 */ 23 */
22 24
23 - void createSuperTable(ThingModelMessageDO superTable); 25 + void createSuperTable(@Param("productKey") String productKey);
24 26
25 /** 27 /**
26 * 创建子表 28 * 创建子表
27 * 29 *
28 */ 30 */
29 31
30 - void createTableWithTag(ThingModelMessageDO table); 32 + void createTableWithTag(@Param("productKey") String productKey,@Param("deviceKey") String deviceKey);
31 33
32 } 34 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDevicePropertyDataServiceImpl.java
@@ -17,6 +17,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO; @@ -17,6 +17,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.thingmodel.IotThingModelDO;
17 import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper; 17 import cn.iocoder.yudao.module.iot.dal.tdengine.IotDevicePropertyDataMapper;
18 import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO; 18 import cn.iocoder.yudao.module.iot.dal.redis.deviceData.DeviceDataRedisDAO;
19 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper; 19 import cn.iocoder.yudao.module.iot.dal.tdengine.TdEngineDMLMapper;
  20 +import cn.iocoder.yudao.module.iot.dal.tdengine.TdThingModelMessageMapper;
20 import cn.iocoder.yudao.module.iot.enums.IotConstants; 21 import cn.iocoder.yudao.module.iot.enums.IotConstants;
21 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum; 22 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotDataSpecsDataTypeEnum;
22 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum; 23 import cn.iocoder.yudao.module.iot.enums.thingmodel.IotThingModelTypeEnum;
@@ -84,6 +85,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe @@ -84,6 +85,9 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
84 @Resource 85 @Resource
85 private IotDevicePropertyDataMapper devicePropertyDataMapper; 86 private IotDevicePropertyDataMapper devicePropertyDataMapper;
86 87
  88 + @Resource
  89 + private TdThingModelMessageMapper tdThingModelMessageMapper;
  90 +
87 @Override 91 @Override
88 public void defineDevicePropertyData(Long productId) { 92 public void defineDevicePropertyData(Long productId) {
89 // 1.1 查询产品和物模型 93 // 1.1 查询产品和物模型
@@ -108,7 +112,10 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe @@ -108,7 +112,10 @@ public class IotDevicePropertyDataServiceImpl implements IotDevicePropertyDataSe
108 return; 112 return;
109 } 113 }
110 newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP)); 114 newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP));
  115 + // 2.1.1 创建产品超级表
111 devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields); 116 devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields);
  117 + // 2.1.2 创建物模型日志超级表
  118 + tdThingModelMessageMapper.createSuperTable(product.getProductKey());
112 return; 119 return;
113 } 120 }
114 // 2.2 情况二:如果是修改的时候,需要更新表 121 // 2.2 情况二:如果是修改的时候,需要更新表
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/product/IotProductServiceImpl.java
@@ -36,9 +36,6 @@ public class IotProductServiceImpl implements IotProductService { @@ -36,9 +36,6 @@ public class IotProductServiceImpl implements IotProductService {
36 36
37 @Resource 37 @Resource
38 @Lazy // 延迟加载,解决循环依赖 38 @Lazy // 延迟加载,解决循环依赖
39 - private IotThingModelMessageService thingModelMessageService;  
40 - @Resource  
41 - @Lazy // 延迟加载,解决循环依赖  
42 private IotDevicePropertyDataService devicePropertyDataService; 39 private IotDevicePropertyDataService devicePropertyDataService;
43 40
44 @Override 41 @Override
@@ -125,8 +122,7 @@ public class IotProductServiceImpl implements IotProductService { @@ -125,8 +122,7 @@ public class IotProductServiceImpl implements IotProductService {
125 if (Objects.equals(status, IotProductStatusEnum.PUBLISHED.getStatus())) { 122 if (Objects.equals(status, IotProductStatusEnum.PUBLISHED.getStatus())) {
126 // 3.1 创建产品超级表数据模型 123 // 3.1 创建产品超级表数据模型
127 devicePropertyDataService.defineDevicePropertyData(id); 124 devicePropertyDataService.defineDevicePropertyData(id);
128 - // 3.2 创建物模型日志超级表数据模型 TODO 待定:message 要不要分;  
129 - thingModelMessageService.createSuperTable(id); 125 +
130 } 126 }
131 productMapper.updateById(updateObj); 127 productMapper.updateById(updateObj);
132 } 128 }
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThingModelMessageService.java
@@ -16,11 +16,6 @@ public interface IotThingModelMessageService { @@ -16,11 +16,6 @@ public interface IotThingModelMessageService {
16 */ 16 */
17 void saveThingModelMessage(IotDeviceDO device, ThingModelMessage thingModelMessage); 17 void saveThingModelMessage(IotDeviceDO device, ThingModelMessage thingModelMessage);
18 18
19 - /**  
20 - * 创建物模型消息日志超级表  
21 - *  
22 - * @param productId 产品编号  
23 - */  
24 - void createSuperTable(Long productId); 19 +
25 20
26 } 21 }
27 \ No newline at end of file 22 \ No newline at end of file
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/tdengine/IotThingModelMessageServiceImpl.java
@@ -79,7 +79,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ @@ -79,7 +79,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
79 iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO() 79 iotDeviceService.updateDeviceStatus(new IotDeviceStatusUpdateReqVO()
80 .setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus())); 80 .setId(device.getId()).setStatus(IotDeviceStatusEnum.ONLINE.getStatus()));
81 // 1.2 创建物模型日志设备表 81 // 1.2 创建物模型日志设备表
82 - createThingModelMessageDeviceTable(device.getProductKey(), device.getDeviceKey()); 82 + tdThingModelMessageMapper.createTableWithTag(device.getProductKey(), device.getDeviceKey());
83 } 83 }
84 84
85 // 2. 获取设备属性并进行物模型校验,过滤非物模型属性 85 // 2. 获取设备属性并进行物模型校验,过滤非物模型属性
@@ -108,32 +108,34 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ @@ -108,32 +108,34 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
108 thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType())); 108 thingModel -> IotThingModelTypeEnum.PROPERTY.getType().equals(thingModel.getType()));
109 } 109 }
110 110
111 - @Override  
112 - @TenantIgnore  
113 - public void createSuperTable(Long productId) {  
114 - // 1. 查询产品  
115 - IotProductDO product = productService.getProduct(productId);  
116 -  
117 - // 2. 获取超级表的名称和数据库名称  
118 - // TODO @alwayssuper:最好 databaseName、superTableName 的处理,放到 tdThinkModelMessageMapper 里。可以考虑,弄个 default 方法  
119 - String databaseName = IotTdDatabaseUtils.getDatabaseName(url);  
120 - String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(product.getProductKey());  
121 -  
122 - // 解析物模型,获取字段列表  
123 - List<TdFieldDO> schemaFields = List.of(  
124 - TdFieldDO.builder().fieldName("time").dataType("TIMESTAMP").build(),  
125 - TdFieldDO.builder().fieldName("id").dataType("NCHAR").dataLength(64).build(),  
126 - TdFieldDO.builder().fieldName("sys").dataType("NCHAR").dataLength(2048).build(),  
127 - TdFieldDO.builder().fieldName("method").dataType("NCHAR").dataLength(256).build(),  
128 - TdFieldDO.builder().fieldName("params").dataType("NCHAR").dataLength(2048).build()  
129 - );  
130 - // 设置超级表的标签  
131 - List<TdFieldDO> tagsFields = List.of(  
132 - TdFieldDO.builder().fieldName("device_key").dataType("NCHAR").dataLength(64).build()  
133 - );  
134 - // 3. 创建超级表  
135 - tdEngineDDLMapper.createSuperTable(new TdTableDO(databaseName, superTableName, schemaFields, tagsFields));  
136 - } 111 +// @Override
  112 +// @TenantIgnore
  113 +// public void createSuperTable(Long productId) {
  114 +// // 1. 查询产品
  115 +// IotProductDO product = productService.getProduct(productId);
  116 +// // 2. 创建日志超级表
  117 +// tdThingModelMessageMapper.createSuperTable(product.getProductKey());
  118 +//
  119 +// // 2. 获取超级表的名称和数据库名称
  120 +// // TODO @alwayssuper:最好 databaseName、superTableName 的处理,放到 tdThinkModelMessageMapper 里。可以考虑,弄个 default 方法
  121 +//// String databaseName = IotTdDatabaseUtils.getDatabaseName(url);
  122 +//// String superTableName = IotTdDatabaseUtils.getThingModelMessageSuperTableName(product.getProductKey());
  123 +////
  124 +//// // 解析物模型,获取字段列表
  125 +//// List<TdFieldDO> schemaFields = List.of(
  126 +//// TdFieldDO.builder().fieldName("time").dataType("TIMESTAMP").build(),
  127 +//// TdFieldDO.builder().fieldName("id").dataType("NCHAR").dataLength(64).build(),
  128 +//// TdFieldDO.builder().fieldName("sys").dataType("NCHAR").dataLength(2048).build(),
  129 +//// TdFieldDO.builder().fieldName("method").dataType("NCHAR").dataLength(256).build(),
  130 +//// TdFieldDO.builder().fieldName("params").dataType("NCHAR").dataLength(2048).build()
  131 +//// );
  132 +//// // 设置超级表的标签
  133 +//// List<TdFieldDO> tagsFields = List.of(
  134 +//// TdFieldDO.builder().fieldName("device_key").dataType("NCHAR").dataLength(64).build()
  135 +//// );
  136 +//// // 3. 创建超级表
  137 +//// tdEngineDDLMapper.createSuperTable(new TdTableDO(databaseName, superTableName, schemaFields, tagsFields));
  138 +// }
137 139
138 private List<IotThingModelDO> getValidFunctionList(String productKey) { 140 private List<IotThingModelDO> getValidFunctionList(String productKey) {
139 return filterList(iotThingModelService.getProductThingModelListByProductKey(productKey), 141 return filterList(iotThingModelService.getProductThingModelListByProductKey(productKey),
@@ -237,13 +239,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ @@ -237,13 +239,7 @@ public class IotThingModelMessageServiceImpl implements IotThingModelMessageServ
237 * 239 *
238 */ 240 */
239 private void createThingModelMessageDeviceTable(String productKey, String deviceKey){ 241 private void createThingModelMessageDeviceTable(String productKey, String deviceKey){
240 -  
241 -  
242 - // 2. 创建物模型日志设备数据表  
243 -// tdThingModelMessageMapper.createTableWithTag(ThingModelMessageDO.builder().build()  
244 -// .setSuperTableName(productKey)  
245 -// .setTableName(deviceKey)  
246 -// .setDeviceKey(deviceKey)); 242 + tdThingModelMessageMapper.createTableWithTag(productKey, deviceKey);
247 } 243 }
248 244
249 /** 245 /**
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/tdengine/TdEngineDMLMapper.xml
@@ -42,7 +42,7 @@ @@ -42,7 +42,7 @@
42 42
43 <!-- 根据标签获取最新数据 --> 43 <!-- 根据标签获取最新数据 -->
44 <select id="selectLastDataListByTags" parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TagsSelectDO" 44 <select id="selectLastDataListByTags" parameterType="cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.TagsSelectDO"
45 - resultType="Map"> 45 + resultType="java.util.Map">
46 SELECT LAST(*) 46 SELECT LAST(*)
47 FROM ${dataBaseName}.${stableName} 47 FROM ${dataBaseName}.${stableName}
48 GROUP BY ${tagsName} 48 GROUP BY ${tagsName}
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/tdengine/TdThinkModelMessageMapper.xml
@@ -6,12 +6,13 @@ @@ -6,12 +6,13 @@
6 6
7 <!-- 创建物模型消息日志超级表 --> 7 <!-- 创建物模型消息日志超级表 -->
8 <update id="createSuperTable"> 8 <update id="createSuperTable">
9 - CREATE STABLE thing_model_message_${superTableName}( 9 + CREATE STABLE thing_model_message_${productKey}(
10 ts TIMESTAMP, 10 ts TIMESTAMP,
11 id NCHAR(64), 11 id NCHAR(64),
12 sys NCHAR(2048), 12 sys NCHAR(2048),
13 method NCHAR(255), 13 method NCHAR(255),
14 - params NCHAR(2048) 14 + params NCHAR(2048),
  15 + device_name NCHAR(64)
15 )TAGS ( 16 )TAGS (
16 device_key NCHAR(50) 17 device_key NCHAR(50)
17 ) 18 )
@@ -19,13 +20,14 @@ @@ -19,13 +20,14 @@
19 20
20 <!-- 创建物模型消息日志子表,带有deviceKey的TAG --> 21 <!-- 创建物模型消息日志子表,带有deviceKey的TAG -->
21 <update id="createTableWithTag"> 22 <update id="createTableWithTag">
22 - CREATE STABLE ${tableName}  
23 - USING thing_model_message_${superTableName}( 23 + CREATE STABLE ${deviceKey}
  24 + USING thing_model_message_${productKey}(
24 ts, 25 ts,
25 id , 26 id ,
26 sys , 27 sys ,
27 method , 28 method ,
28 - params 29 + params ,
  30 + device_name
29 )TAGS( 31 )TAGS(
30 #{device_key} 32 #{device_key}
31 ) 33 )