feat: 设备属性缓存-添加属性修改时间

V0.5.x
jay 2023-08-01 16:30:22 +08:00
parent 79ba8beeb3
commit 84179802c9
4 changed files with 55 additions and 7 deletions

View File

@ -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<String, Object> toMap() {
return Map.of("value", value, "occurred", occurred);
}
}

View File

@ -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<String, Object> 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)
);
});

View File

@ -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<String, Object> propertiesMap = new HashMap<>();
properties.forEach((key, val) -> {
DevicePropertyCache propertyCache = (DevicePropertyCache) val;
propertiesMap.put(key, propertyCache.getValue());
});
//获取设备旧属性
Map<String, Object> oldProperties = deviceInfoData.getProperties(deviceId);
//用新属性覆盖
oldProperties.putAll(properties);
oldProperties.putAll(propertiesMap);
StringBuilder sbFieldNames = new StringBuilder();
StringBuilder sbFieldPlaces = new StringBuilder();

View File

@ -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<ThingModelMessage
ThingModel.Property::getIdentifier, ThingModel.Property::getDataType));
Map<String, Object> 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<ThingModelMessage
//保存属性记录
try {
devicePropertyData.addProperties(deviceId, addProperties, msg.getOccurred());
devicePropertyData.addProperties(deviceId, addProperties, occurred);
} catch (Throwable e) {
log.warn("save property data error", e);
}
}
private void handleLocate(DeviceInfo deviceInfo,Object data,ThingModel.DataType dataType){
if("position".equals(dataType.getType())){//如果是定位属性需要做一些处理
Object specs = dataType.getSpecs();