【功能优化】IoT:设备属性日志表,增加 report_time 上报时间
This commit is contained in:
parent
043d82e5b6
commit
7745035fa4
|
@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.iot.dal.tdengine;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
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.framework.tenant.core.aop.TenantIgnore;
|
||||||
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
|
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
|
||||||
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
|
import cn.iocoder.yudao.module.iot.dal.dataobject.tdengine.SelectVisualDO;
|
||||||
|
@ -30,8 +31,8 @@ public interface IotDevicePropertyDataMapper {
|
||||||
default void alterProductPropertySTable(String productKey,
|
default void alterProductPropertySTable(String productKey,
|
||||||
List<TDengineTableField> oldFields,
|
List<TDengineTableField> oldFields,
|
||||||
List<TDengineTableField> newFields) {
|
List<TDengineTableField> newFields) {
|
||||||
oldFields.removeIf(field -> TDengineTableField.FIELD_TS.equals(field.getField())
|
oldFields.removeIf(field -> StrUtil.equalsAny(field.getField(),
|
||||||
|| TDengineTableField.FIELD_DEVICE_KEY.equals(field.getField()));
|
TDengineTableField.FIELD_TS, "device_key", "report_time"));
|
||||||
List<TDengineTableField> addFields = newFields.stream().filter( // 新增的字段
|
List<TDengineTableField> addFields = newFields.stream().filter( // 新增的字段
|
||||||
newField -> oldFields.stream().noneMatch(oldField -> oldField.getField().equals(newField.getField())))
|
newField -> oldFields.stream().noneMatch(oldField -> oldField.getField().equals(newField.getField())))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
@ -79,7 +80,8 @@ public interface IotDevicePropertyDataMapper {
|
||||||
@Param("field") TDengineTableField field);
|
@Param("field") TDengineTableField field);
|
||||||
|
|
||||||
void insert(@Param("device") IotDeviceDO device,
|
void insert(@Param("device") IotDeviceDO device,
|
||||||
@Param("properties") Map<String, Object> properties);
|
@Param("properties") Map<String, Object> properties,
|
||||||
|
@Param("reportTime") Long reportTime);
|
||||||
|
|
||||||
// TODO @芋艿:待实现
|
// TODO @芋艿:待实现
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,11 +17,6 @@ public class TDengineTableField {
|
||||||
*/
|
*/
|
||||||
public static final String FIELD_TS = "ts";
|
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_TINYINT = "TINYINT";
|
||||||
public static final String TYPE_INT = "INT";
|
public static final String TYPE_INT = "INT";
|
||||||
public static final String TYPE_FLOAT = "FLOAT";
|
public static final String TYPE_FLOAT = "FLOAT";
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package cn.iocoder.yudao.module.iot.service.device.data;
|
package cn.iocoder.yudao.module.iot.service.device.data;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
|
@ -97,7 +98,6 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
|
||||||
log.info("[defineDevicePropertyData][productId({}) 没有需要定义的属性]", productId);
|
log.info("[defineDevicePropertyData][productId({}) 没有需要定义的属性]", productId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
newFields.add(0, new TDengineTableField(TDengineTableField.FIELD_TS, TDengineTableField.TYPE_TIMESTAMP));
|
|
||||||
devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields);
|
devicePropertyDataMapper.createProductPropertySTable(product.getProductKey(), newFields);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -147,7 +147,8 @@ public class IotDevicePropertyServiceImpl implements IotDevicePropertyService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3.1 保存设备属性【数据】
|
// 3.1 保存设备属性【数据】
|
||||||
devicePropertyDataMapper.insert(device, properties);
|
devicePropertyDataMapper.insert(device, properties,
|
||||||
|
LocalDateTimeUtil.toEpochMilli(message.getReportTime())); // TODO @芋艿:后续要看看,查询的时候,能不能用 LocalDateTime
|
||||||
|
|
||||||
// 3.2 保存设备属性【日志】
|
// 3.2 保存设备属性【日志】
|
||||||
deviceDataRedisDAO.set(message.getDeviceKey(), convertMap(properties.entrySet(), Map.Entry::getKey,
|
deviceDataRedisDAO.set(message.getDeviceKey(), convertMap(properties.entrySet(), Map.Entry::getKey,
|
||||||
|
|
|
@ -9,13 +9,16 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<update id="createProductPropertySTable">
|
<update id="createProductPropertySTable">
|
||||||
CREATE STABLE product_property_${productKey}
|
CREATE STABLE product_property_${productKey} (
|
||||||
<foreach item="field" collection="fields" separator="," open="(" close=")">
|
ts TIMESTAMP,
|
||||||
|
report_time TIMESTAMP,
|
||||||
|
<foreach item="field" collection="fields" separator=",">
|
||||||
${field.field} ${field.type}
|
${field.field} ${field.type}
|
||||||
<if test="field.length != null and field.length > 0">
|
<if test="field.length != null and field.length > 0">
|
||||||
(${field.length})
|
(${field.length})
|
||||||
</if>
|
</if>
|
||||||
</foreach>
|
</foreach>
|
||||||
|
)
|
||||||
TAGS (
|
TAGS (
|
||||||
device_key NCHAR(50)
|
device_key NCHAR(50)
|
||||||
)
|
)
|
||||||
|
@ -42,19 +45,20 @@
|
||||||
DROP COLUMN ${field.field}
|
DROP COLUMN ${field.field}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
<insert id="insertDevicePropertyData">
|
<!-- TODO 芋艿,report_time 需要增加下 -->
|
||||||
INSERT INTO device_property_${deviceKey}
|
<insert id="insert">
|
||||||
USING product_property_${productKey}
|
INSERT INTO device_property_${device.deviceKey}
|
||||||
TAGS ('${deviceKey}')
|
USING product_property_${device.productKey}
|
||||||
(ts
|
TAGS ('${device.deviceKey}')
|
||||||
<foreach item="item" collection="columns" separator=",">
|
(ts, report_time,
|
||||||
,${item.fieldName}
|
<foreach item="key" collection="properties.keys" separator=",">
|
||||||
|
${key}
|
||||||
</foreach>
|
</foreach>
|
||||||
)
|
)
|
||||||
VALUES
|
VALUES
|
||||||
(NOW
|
(NOW, #{reportTime},
|
||||||
<foreach item="item" collection="columns" separator=",">
|
<foreach item="value" collection="properties.values" separator=",">
|
||||||
,#{item.fieldValue}
|
#{value}
|
||||||
</foreach>
|
</foreach>
|
||||||
)
|
)
|
||||||
</insert>
|
</insert>
|
||||||
|
|
Loading…
Reference in New Issue