Преглед на файлове

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

YunaiV преди 6 месеца
родител
ревизия
7745035fa4

+ 5 - 3
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;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
 import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
 import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
@@ -30,8 +31,8 @@ public interface IotDevicePropertyDataMapper {
     default void alterProductPropertySTable(String productKey,
                                             List<TDengineTableField> oldFields,
                                             List<TDengineTableField> newFields) {
-        oldFields.removeIf(field -> TDengineTableField.FIELD_TS.equals(field.getField())
-                || TDengineTableField.FIELD_DEVICE_KEY.equals(field.getField()));
+        oldFields.removeIf(field -> StrUtil.equalsAny(field.getField(),
+                TDengineTableField.FIELD_TS, "device_key", "report_time"));
         List<TDengineTableField> addFields = newFields.stream().filter( // 新增的字段
                         newField -> oldFields.stream().noneMatch(oldField -> oldField.getField().equals(newField.getField())))
                 .collect(Collectors.toList());
@@ -79,7 +80,8 @@ public interface IotDevicePropertyDataMapper {
                                              @Param("field") TDengineTableField field);
 
     void insert(@Param("device") IotDeviceDO device,
-                @Param("properties") Map<String, Object> properties);
+                @Param("properties") Map<String, Object> properties,
+                @Param("reportTime") Long reportTime);
 
     // TODO @芋艿:待实现
     /**

+ 0 - 5
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 {
      */
     public static final String FIELD_TS = "ts";
 
-    /**
-     * 字段名 - 我们系统定义的 device_key 字段,非 TDengine 默认字段
-     */
-    public static final String FIELD_DEVICE_KEY = "device_key";
-
     public static final String TYPE_TINYINT = "TINYINT";
     public static final String TYPE_INT = "INT";
     public static final String TYPE_FLOAT = "FLOAT";

+ 3 - 2
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/data/IotDevicePropertyServiceImpl.java

@@ -1,6 +1,7 @@
 package cn.iocoder.yudao.module.iot.service.device.data;
 
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.LocalDateTimeUtil;
 import cn.hutool.core.map.MapUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
@@ -97,7 +98,6 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
                 log.info("[defineDevicePropertyData][productId({}) 没有需要定义的属性]", productId);
                 return;
             }
-            newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP));
             devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields);
             return;
         }
@@ -147,7 +147,8 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
         }
 
         // 3.1 保存设备属性【数据】
-        devicePropertyDataMapper.insert(device, properties);
+        devicePropertyDataMapper.insert(device, properties,
+                LocalDateTimeUtil.toEpochMilli(message.getReportTime())); // TODO @芋艿:后续要看看,查询的时候,能不能用 LocalDateTime
 
         // 3.2 保存设备属性【日志】
         deviceDataRedisDAO.set(message.getDeviceKey(), convertMap(properties.entrySet(), Map.Entry::getKey,

+ 18 - 14
yudao-module-iot/yudao-module-iot-biz/src/main/resources/mapper/device/IotDevicePropertyDataMapper.xml

@@ -9,13 +9,16 @@
     </select>
 
     <update id="createProductPropertySTable">
-        CREATE STABLE product_property_${productKey}
-        <foreach item="field" collection="fields" separator="," open="(" close=")">
+        CREATE STABLE product_property_${productKey} (
+            ts TIMESTAMP,
+            report_time TIMESTAMP,
+        <foreach item="field" collection="fields" separator=",">
             ${field.field} ${field.type}
             <if test="field.length != null and field.length > 0">
                 (${field.length})
             </if>
         </foreach>
+        )
         TAGS (
             device_key NCHAR(50)
         )
@@ -42,19 +45,20 @@
         DROP COLUMN ${field.field}
     </update>
 
-    <insert id="insertDevicePropertyData">
-        INSERT INTO device_property_${deviceKey} 
-        USING product_property_${productKey} 
-        TAGS ('${deviceKey}')
-        (ts
-        <foreach item="item" collection="columns" separator=",">
-            ,${item.fieldName}
+    <!-- TODO 芋艿,report_time 需要增加下 -->
+    <insert id="insert">
+        INSERT INTO device_property_${device.deviceKey}
+        USING product_property_${device.productKey}
+        TAGS ('${device.deviceKey}')
+        (ts, report_time,
+        <foreach item="key" collection="properties.keys" separator=",">
+            ${key}
         </foreach>
-        ) 
-        VALUES 
-        (NOW
-        <foreach item="item" collection="columns" separator=",">
-            ,#{item.fieldValue}
+        )
+        VALUES
+        (NOW, #{reportTime},
+        <foreach item="value" collection="properties.values" separator=",">
+            #{value}
         </foreach>
         )
     </insert>