From a7fd71540908c2c8495a2ddd3c9041ebba84c4da Mon Sep 17 00:00:00 2001 From: xiwa Date: Sun, 6 Nov 2022 14:51:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B1=9E=E6=80=A7=E5=85=A5=E5=BA=93=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=89=A9=E6=A8=A1=E5=9E=8B=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cc/iotkit/common/Constants.java | 2 +- .../comps/service/DevicePropertyConsumer.java | 35 ++++++++++- .../data/cache/ThingModelCacheEvict.java | 14 +++++ .../cc/iotkit/data/config/CacheConfig.java | 1 + .../data/service/ThingModelDataCache.java | 62 +++++++++++++++++++ .../java/cc/iotkit/data/IThingModelData.java | 2 - .../es/service/DevicePropertyDataImpl.java | 7 +-- .../iotkit/data/dao/ThingModelRepository.java | 2 - .../data/service/ThingModelDataImpl.java | 5 -- .../manager/controller/ProductController.java | 6 +- .../manager/service/ThingModelService.java | 2 +- 11 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 iot-data/iot-data-cache/src/main/java/cc/iotkit/data/cache/ThingModelCacheEvict.java create mode 100644 iot-data/iot-data-cache/src/main/java/cc/iotkit/data/service/ThingModelDataCache.java diff --git a/iot-common/src/main/java/cc/iotkit/common/Constants.java b/iot-common/src/main/java/cc/iotkit/common/Constants.java index b123c877..3e990142 100755 --- a/iot-common/src/main/java/cc/iotkit/common/Constants.java +++ b/iot-common/src/main/java/cc/iotkit/common/Constants.java @@ -25,7 +25,7 @@ public interface Constants { String CACHE_SPACE = "space_cache"; - String THING_MODEL_CACHE = "thing_model_cache"; + String CACHE_THING_MODEL = "thing_model_cache"; String CACHE_USER_INFO = "user_info_cache"; 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 3bdcf5ae..3bb457b5 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 @@ -12,7 +12,10 @@ package cc.iotkit.comps.service; import cc.iotkit.common.Constants; import cc.iotkit.common.utils.JsonUtil; import cc.iotkit.data.IDeviceInfoData; +import cc.iotkit.data.IThingModelData; +import cc.iotkit.model.device.DeviceInfo; import cc.iotkit.model.device.message.ThingModelMessage; +import cc.iotkit.model.product.ThingModel; import cc.iotkit.mq.ConsumerHandler; import cc.iotkit.mq.MqConsumer; import cc.iotkit.temporal.IDevicePropertyData; @@ -22,7 +25,9 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; +import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; /** * 设备属性消息消费入库 @@ -38,6 +43,9 @@ public class DevicePropertyConsumer implements ConsumerHandler properties = (Map) msg.getData(); String deviceId = msg.getDeviceId(); + DeviceInfo deviceInfo = deviceInfoData.findByDeviceId(deviceId); + if (deviceInfo == null) { + return; + } + + //物模型校验,过滤非物模型属性 + ThingModel thingModel = thingModelData.findById(deviceInfo.getProductKey()); + if (thingModel == null) { + return; + } + + //物模型属性 + Map thingModelProperties = thingModel.getModel(). + getProperties().stream().collect(Collectors.toMap( + ThingModel.Property::getIdentifier, ThingModel.Property::getName)); + + Map addProperties = new HashMap<>(); + //删除非属性字段 + properties.forEach((key,val)->{ + if (thingModelProperties.containsKey(key)) { + addProperties.put(key,val); + } + }); //更新设备当前属性 - updateDeviceCurrentProperties(deviceId, properties); + updateDeviceCurrentProperties(deviceId, addProperties); //保存属性记录 try { - devicePropertyData.addProperties(deviceId, properties, msg.getOccurred()); + devicePropertyData.addProperties(deviceId, addProperties, msg.getOccurred()); } catch (Throwable e) { log.warn("save property data error", e); } diff --git a/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/cache/ThingModelCacheEvict.java b/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/cache/ThingModelCacheEvict.java new file mode 100644 index 00000000..8c900ed3 --- /dev/null +++ b/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/cache/ThingModelCacheEvict.java @@ -0,0 +1,14 @@ +package cc.iotkit.data.cache; + +import cc.iotkit.common.Constants; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.stereotype.Component; + +@Component +public class ThingModelCacheEvict { + + @CacheEvict(value = Constants.CACHE_THING_MODEL, key = "#root.method.name+#s") + public void findById(String s) { + } + +} diff --git a/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/config/CacheConfig.java b/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/config/CacheConfig.java index 78124ad8..77b7c360 100755 --- a/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/config/CacheConfig.java +++ b/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/config/CacheConfig.java @@ -44,6 +44,7 @@ public class CacheConfig { Constants.CACHE_PRODUCT, config, Constants.CACHE_OAUTH_CLIENT, config, Constants.CACHE_CATEGORY, config, + Constants.CACHE_THING_MODEL, config, //统计缓存5分钟 Constants.CACHE_DEVICE_STATS, config.entryTtl(Duration.ofMinutes(5)) ); diff --git a/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/service/ThingModelDataCache.java b/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/service/ThingModelDataCache.java new file mode 100644 index 00000000..efd5ef8d --- /dev/null +++ b/iot-data/iot-data-cache/src/main/java/cc/iotkit/data/service/ThingModelDataCache.java @@ -0,0 +1,62 @@ +package cc.iotkit.data.service; + +import cc.iotkit.common.Constants; +import cc.iotkit.data.IThingModelData; +import cc.iotkit.data.cache.ThingModelCacheEvict; +import cc.iotkit.model.Paging; +import cc.iotkit.model.product.ThingModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@Qualifier("thingModelDataCache") +public class ThingModelDataCache implements IThingModelData { + + @Autowired + private IThingModelData thingModelData; + @Autowired + private ThingModelCacheEvict thingModelCacheEvict; + + @Override + @Cacheable(value = Constants.CACHE_THING_MODEL, key = "#root.method.name+#s", unless = "#result == null") + public ThingModel findById(String s) { + return thingModelData.findById(s); + } + + @Override + public ThingModel save(ThingModel data) { + data = thingModelData.save(data); + thingModelCacheEvict.findById(data.getId()); + return data; + } + + @Override + public ThingModel add(ThingModel data) { + return thingModelData.add(data); + } + + @Override + public void deleteById(String s) { + thingModelData.deleteById(s); + } + + @Override + public long count() { + return thingModelData.count(); + } + + @Override + public List findAll() { + return thingModelData.findAll(); + } + + @Override + public Paging findAll(int page, int size) { + return thingModelData.findAll(page, size); + } + +} diff --git a/iot-data/iot-data-service/src/main/java/cc/iotkit/data/IThingModelData.java b/iot-data/iot-data-service/src/main/java/cc/iotkit/data/IThingModelData.java index 26e3891c..0e866fca 100755 --- a/iot-data/iot-data-service/src/main/java/cc/iotkit/data/IThingModelData.java +++ b/iot-data/iot-data-service/src/main/java/cc/iotkit/data/IThingModelData.java @@ -4,6 +4,4 @@ import cc.iotkit.model.product.ThingModel; public interface IThingModelData extends ICommonData { - ThingModel findByProductKey(String productKey); - } diff --git a/iot-data/iot-es-temporal-service/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java b/iot-data/iot-es-temporal-service/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java index a33f07e1..ded0e9f4 100755 --- a/iot-data/iot-es-temporal-service/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java +++ b/iot-data/iot-es-temporal-service/src/main/java/cc/iotkit/temporal/es/service/DevicePropertyDataImpl.java @@ -77,11 +77,8 @@ public class DevicePropertyDataImpl implements IDevicePropertyData { if (deviceInfo == null) { return null; } - String pk = deviceInfo.getProductKey().toLowerCase(); - String index = String.format("device_property_%s_%s", pk, name); - if (null == index || StringUtils.isBlank(index)) { - return null; - } + String pk = deviceInfo.getProductKey(); + String index = String.format("device_property_%s_%s", pk, name).toLowerCase(); if (!indexSet.contains(index)) { IndexCoordinates indexCoordinates = IndexCoordinates.of(index); if (!template.indexOps(indexCoordinates).exists()) { diff --git a/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/dao/ThingModelRepository.java b/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/dao/ThingModelRepository.java index 4e9aa6fc..9a808dd8 100755 --- a/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/dao/ThingModelRepository.java +++ b/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/dao/ThingModelRepository.java @@ -14,6 +14,4 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface ThingModelRepository extends JpaRepository { - TbThingModel findByProductKey(String productKey); - } diff --git a/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java b/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java index 2c938adf..15b11a89 100755 --- a/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java +++ b/iot-data/iot-rdb-data-service/src/main/java/cc/iotkit/data/service/ThingModelDataImpl.java @@ -29,11 +29,6 @@ public class ThingModelDataImpl implements IThingModelData { @Autowired private ThingModelRepository thingModelRepository; - @Override - public ThingModel findByProductKey(String productKey) { - return ThingModelMapper.toDtoFix(thingModelRepository.findByProductKey(productKey)); - } - @Override public ThingModel findById(String s) { return ThingModelMapper.toDtoFix(thingModelRepository.findById(s).orElse(null)); diff --git a/iot-standalone/src/main/java/cc/iotkit/manager/controller/ProductController.java b/iot-standalone/src/main/java/cc/iotkit/manager/controller/ProductController.java index 2382b336..82f97526 100755 --- a/iot-standalone/src/main/java/cc/iotkit/manager/controller/ProductController.java +++ b/iot-standalone/src/main/java/cc/iotkit/manager/controller/ProductController.java @@ -47,8 +47,10 @@ public class ProductController { @Qualifier("productDataCache") private IProductData productData; @Autowired + @Qualifier("thingModelDataCache") private IThingModelData thingModelData; @Autowired + @Qualifier("categoryDataCache") private ICategoryData categoryData; @Autowired private DataOwnerService dataOwnerService; @@ -97,7 +99,7 @@ public class ProductController { @PostMapping("/thingModel/save") public void saveThingModel(String productKey, String model) { checkProductOwner(productKey); - ThingModel oldData = thingModelData.findByProductKey(productKey); + ThingModel oldData = thingModelData.findById(productKey); ThingModel thingModel = new ThingModel(productKey, productKey, JsonUtil.parse(model, ThingModel.Model.class)); if (oldData == null) { //定义时序数据库物模型数据结构 @@ -112,7 +114,7 @@ public class ProductController { @PostMapping("/thingModel/{productKey}/delete") public void deleteThingModel(String productKey) { checkProductOwner(productKey); - ThingModel thingModel = thingModelData.findByProductKey(productKey); + ThingModel thingModel = thingModelData.findById(productKey); //删除时序数据库物模型数据结构 dbStructureData.defineThingModel(thingModel); thingModelData.deleteById(productKey); diff --git a/iot-standalone/src/main/java/cc/iotkit/manager/service/ThingModelService.java b/iot-standalone/src/main/java/cc/iotkit/manager/service/ThingModelService.java index fe47ca0d..9efd9242 100755 --- a/iot-standalone/src/main/java/cc/iotkit/manager/service/ThingModelService.java +++ b/iot-standalone/src/main/java/cc/iotkit/manager/service/ThingModelService.java @@ -25,7 +25,7 @@ public class ThingModelService { private IThingModelData thingModelData; public void parseParams(ThingService service) { - ThingModel thingModel = thingModelData.findByProductKey(service.getProductKey()); + ThingModel thingModel = thingModelData.findById(service.getProductKey()); ThingModel.Model model = thingModel.getModel(); String type = service.getType();