From 84179802c975eec7ae4be0d29b8c5dba448dcf11 Mon Sep 17 00:00:00 2001 From: jay <75509151@qq.com> Date: Tue, 1 Aug 2023 16:30:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=BE=E5=A4=87=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E7=BC=93=E5=AD=98-=E6=B7=BB=E5=8A=A0=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/message/DevicePropertyCache.java | 35 +++++++++++++++++++ .../es/service/DevicePropertyDataImpl.java | 5 ++- .../td/service/DevicePropertyDataImpl.java | 12 ++++--- .../comps/service/DevicePropertyConsumer.java | 10 ++++-- 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/device/message/DevicePropertyCache.java diff --git a/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/device/message/DevicePropertyCache.java b/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/device/message/DevicePropertyCache.java new file mode 100644 index 00000000..641c0a73 --- /dev/null +++ b/iot-common/iot-common-dao/iot-common-model/src/main/java/cc/iotkit/model/device/message/DevicePropertyCache.java @@ -0,0 +1,35 @@ +/* + * +---------------------------------------------------------------------- + * | Copyright (c) 奇特物联 2021-2022 All rights reserved. + * +---------------------------------------------------------------------- + * | Licensed 未经许可不能去掉「奇特物联」相关版权 + * +---------------------------------------------------------------------- + * | Author: xw2sy@163.com + * +---------------------------------------------------------------------- + */ +package cc.iotkit.model.device.message; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class DevicePropertyCache { + + + // 属性值 + private Object value; + + // 属性值时间: 设备上报时间 + private Long occurred; + + + public Map toMap() { + return Map.of("value", value, "occurred", occurred); + } + +} diff --git a/iot-common/iot-common-dao/iot-temporal-serviceImpl-es/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java b/iot-common/iot-common-dao/iot-temporal-serviceImpl-es/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java index b9087401..988beb81 100644 --- a/iot-common/iot-common-dao/iot-temporal-serviceImpl-es/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java +++ b/iot-common/iot-common-dao/iot-temporal-serviceImpl-es/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java @@ -13,6 +13,7 @@ import cc.iotkit.common.utils.MapstructUtils; import cc.iotkit.data.manager.IDeviceInfoData; import cc.iotkit.model.device.DeviceInfo; import cc.iotkit.model.device.message.DeviceProperty; +import cc.iotkit.model.device.message.DevicePropertyCache; import cc.iotkit.temporal.IDevicePropertyData; import cc.iotkit.temporal.es.document.DocDeviceProperty; import org.elasticsearch.index.query.QueryBuilders; @@ -63,9 +64,11 @@ public class DevicePropertyDataImpl implements IDevicePropertyData { @Override public void addProperties(String deviceId, Map properties, long time) { properties.forEach((key, val) -> { + DevicePropertyCache propertyCache = (DevicePropertyCache) val; String index = getIndex(deviceId, key); + long occurred = Objects.nonNull( propertyCache.getOccurred() )? propertyCache.getOccurred() : time; template.save( - new DocDeviceProperty(UUID.randomUUID().toString(), deviceId, key, val, time), + new DocDeviceProperty(UUID.randomUUID().toString(), deviceId, key, propertyCache.getValue(), occurred), IndexCoordinates.of(index) ); }); diff --git a/iot-common/iot-common-dao/iot-temproal-serviceImpl-td/src/main/java/cc/iotkit/temporal/td/service/DevicePropertyDataImpl.java b/iot-common/iot-common-dao/iot-temproal-serviceImpl-td/src/main/java/cc/iotkit/temporal/td/service/DevicePropertyDataImpl.java index 3068de45..38bb0130 100644 --- a/iot-common/iot-common-dao/iot-temproal-serviceImpl-td/src/main/java/cc/iotkit/temporal/td/service/DevicePropertyDataImpl.java +++ b/iot-common/iot-common-dao/iot-temproal-serviceImpl-td/src/main/java/cc/iotkit/temporal/td/service/DevicePropertyDataImpl.java @@ -12,6 +12,7 @@ package cc.iotkit.temporal.td.service; import cc.iotkit.data.manager.IDeviceInfoData; import cc.iotkit.model.device.DeviceInfo; import cc.iotkit.model.device.message.DeviceProperty; +import cc.iotkit.model.device.message.DevicePropertyCache; import cc.iotkit.temporal.IDevicePropertyData; import cc.iotkit.temporal.td.config.Constants; import cc.iotkit.temporal.td.dao.TdTemplate; @@ -22,9 +23,7 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; @Slf4j @@ -65,10 +64,15 @@ public class DevicePropertyDataImpl implements IDevicePropertyData { if (device == null) { return; } + Map propertiesMap = new HashMap<>(); + properties.forEach((key, val) -> { + DevicePropertyCache propertyCache = (DevicePropertyCache) val; + propertiesMap.put(key, propertyCache.getValue()); + }); //获取设备旧属性 Map oldProperties = deviceInfoData.getProperties(deviceId); //用新属性覆盖 - oldProperties.putAll(properties); + oldProperties.putAll(propertiesMap); StringBuilder sbFieldNames = new StringBuilder(); StringBuilder sbFieldPlaces = new StringBuilder(); diff --git a/iot-components/iot-component-server/src/main/java/cc/iotkit/comps/service/DevicePropertyConsumer.java b/iot-components/iot-component-server/src/main/java/cc/iotkit/comps/service/DevicePropertyConsumer.java index 52f068b3..13c6c651 100755 --- a/iot-components/iot-component-server/src/main/java/cc/iotkit/comps/service/DevicePropertyConsumer.java +++ b/iot-components/iot-component-server/src/main/java/cc/iotkit/comps/service/DevicePropertyConsumer.java @@ -15,6 +15,7 @@ import cc.iotkit.common.utils.StringUtils; import cc.iotkit.data.manager.IDeviceInfoData; import cc.iotkit.data.manager.IThingModelData; import cc.iotkit.model.device.DeviceInfo; +import cc.iotkit.model.device.message.DevicePropertyCache; import cc.iotkit.model.device.message.ThingModelMessage; import cc.iotkit.model.product.ThingModel; import cc.iotkit.mq.ConsumerHandler; @@ -78,10 +79,14 @@ public class DevicePropertyConsumer implements ConsumerHandler addProperties = new HashMap<>(); + Long occurred = msg.getOccurred(); //删除非属性字段 properties.forEach((key,val)->{ if (thingModelProperties.containsKey(key)) { - addProperties.put(key,val); + DevicePropertyCache propertyCache = new DevicePropertyCache(); + propertyCache.setValue(val); + propertyCache.setOccurred(occurred); + addProperties.put(key,propertyCache); handleLocate(deviceInfo,val,thingModelProperties.get(key)); } }); @@ -91,12 +96,13 @@ public class DevicePropertyConsumer implements ConsumerHandler