Commit 7745035fa4d43bced70a8ea96b35711ac624ea01

Authored by YunaiV
1 parent 043d82e5

【功能优化】IoT:设备属性日志表,增加 report_time 上报时间

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/tdengine/IotDevicePropertyDataMapper.java
... ... @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.dal.tdengine;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
4 4 import cn.hutool.core.util.ObjectUtil;
  5 +import cn.hutool.core.util.StrUtil;
5 6 import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
6 7 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
7 8 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
... ... @@ -30,8 +31,8 @@ public interface IotDevicePropertyDataMapper {
30 31 default void alterProductPropertySTable(String productKey,
31 32 List<TDengineTableField> oldFields,
32 33 List<TDengineTableField> newFields) {
33   - oldFields.removeIf(field -> TDengineTableField.FIELD_TS.equals(field.getField())
34   - || TDengineTableField.FIELD_DEVICE_KEY.equals(field.getField()));
  34 + oldFields.removeIf(field -> StrUtil.equalsAny(field.getField(),
  35 + TDengineTableField.FIELD_TS, "device_key", "report_time"));
35 36 List<TDengineTableField> addFields = newFields.stream().filter( // 新增的字段
36 37 newField -> oldFields.stream().noneMatch(oldField -> oldField.getField().equals(newField.getField())))
37 38 .collect(Collectors.toList());
... ... @@ -79,7 +80,8 @@ public interface IotDevicePropertyDataMapper {
79 80 @Param("field") TDengineTableField field);
80 81  
81 82 void insert(@Param("device") IotDeviceDO device,
82   - @Param("properties") Map<String, Object> properties);
  83 + @Param("properties") Map<String, Object> properties,
  84 + @Param("reportTime") Long reportTime);
83 85  
84 86 // TODO @芋艿:待实现
85 87 /**
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/tdengine/core/TDengineTableField.java
... ... @@ -17,11 +17,6 @@ public class TDengineTableField {
17 17 */
18 18 public static final String FIELD_TS = "ts";
19 19  
20   - /**
21   - * 字段名 - 我们系统定义的 device_key 字段,非 TDengine 默认字段
22   - */
23   - public static final String FIELD_DEVICE_KEY = "device_key";
24   -
25 20 public static final String TYPE_TINYINT = "TINYINT";
26 21 public static final String TYPE_INT = "INT";
27 22 public static final String TYPE_FLOAT = "FLOAT";
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDevicePropertyServiceImpl.java
1 1 package cn.iocoder.yudao.module.iot.service.device.data;
2 2  
3 3 import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.date.LocalDateTimeUtil;
4 5 import cn.hutool.core.map.MapUtil;
5 6 import cn.hutool.core.util.StrUtil;
6 7 import cn.hutool.json.JSONObject;
... ... @@ -97,7 +98,6 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
97 98 log.info("[defineDevicePropertyData][productId({}) 没有需要定义的属性]", productId);
98 99 return;
99 100 }
100   - newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP));
101 101 devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields);
102 102 return;
103 103 }
... ... @@ -147,7 +147,8 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
147 147 }
148 148  
149 149 // 3.1 保存设备属性【数据】
150   - devicePropertyDataMapper.insert(device, properties);
  150 + devicePropertyDataMapper.insert(device, properties,
  151 + LocalDateTimeUtil.toEpochMilli(message.getReportTime())); // TODO @芋艿:后续要看看,查询的时候,能不能用 LocalDateTime
151 152  
152 153 // 3.2 保存设备属性【日志】
153 154 deviceDataRedisDAO.set(message.getDeviceKey(), convertMap(properties.entrySet(), Map.Entry::getKey,
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDevicePropertyDataMapper.xml
... ... @@ -9,13 +9,16 @@
9 9 </select>
10 10  
11 11 <update id="createProductPropertySTable">
12   - CREATE STABLE product_property_${productKey}
13   - <foreach item="field" collection="fields" separator="," open="(" close=")">
  12 + CREATE STABLE product_property_${productKey} (
  13 + ts TIMESTAMP,
  14 + report_time TIMESTAMP,
  15 + <foreach item="field" collection="fields" separator=",">
14 16 ${field.field} ${field.type}
15 17 <if test="field.length != null and field.length > 0">
16 18 (${field.length})
17 19 </if>
18 20 </foreach>
  21 + )
19 22 TAGS (
20 23 device_key NCHAR(50)
21 24 )
... ... @@ -42,19 +45,20 @@
42 45 DROP COLUMN ${field.field}
43 46 </update>
44 47  
45   - <insert id="insertDevicePropertyData">
46   - INSERT INTO device_property_${deviceKey}
47   - USING product_property_${productKey}
48   - TAGS ('${deviceKey}')
49   - (ts
50   - <foreach item="item" collection="columns" separator=",">
51   - ,${item.fieldName}
  48 + <!-- TODO 芋艿,report_time 需要增加下 -->
  49 + <insert id="insert">
  50 + INSERT INTO device_property_${device.deviceKey}
  51 + USING product_property_${device.productKey}
  52 + TAGS ('${device.deviceKey}')
  53 + (ts, report_time,
  54 + <foreach item="key" collection="properties.keys" separator=",">
  55 + ${key}
52 56 </foreach>
53   - )
54   - VALUES
55   - (NOW
56   - <foreach item="item" collection="columns" separator=",">
57   - ,#{item.fieldValue}
  57 + )
  58 + VALUES
  59 + (NOW, #{reportTime},
  60 + <foreach item="value" collection="properties.values" separator=",">
  61 + #{value}
58 62 </foreach>
59 63 )
60 64 </insert>
... ...