Browse Source

属性入库增加物模型过滤

xiwa 2 years ago
parent
commit
a7fd715409

+ 1 - 1
iot-common/src/main/java/cc/iotkit/common/Constants.java

@@ -25,7 +25,7 @@ public interface Constants {
 
 
     String CACHE_SPACE = "space_cache";
     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";
     String CACHE_USER_INFO = "user_info_cache";
 
 

+ 33 - 2
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.Constants;
 import cc.iotkit.common.utils.JsonUtil;
 import cc.iotkit.common.utils.JsonUtil;
 import cc.iotkit.data.IDeviceInfoData;
 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.device.message.ThingModelMessage;
+import cc.iotkit.model.product.ThingModel;
 import cc.iotkit.mq.ConsumerHandler;
 import cc.iotkit.mq.ConsumerHandler;
 import cc.iotkit.mq.MqConsumer;
 import cc.iotkit.mq.MqConsumer;
 import cc.iotkit.temporal.IDevicePropertyData;
 import cc.iotkit.temporal.IDevicePropertyData;
@@ -22,7 +25,9 @@ import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PostConstruct;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 
 /**
 /**
  * 设备属性消息消费入库
  * 设备属性消息消费入库
@@ -38,6 +43,9 @@ public class DevicePropertyConsumer implements ConsumerHandler<ThingModelMessage
     @Autowired
     @Autowired
     @Qualifier("deviceInfoDataCache")
     @Qualifier("deviceInfoDataCache")
     private IDeviceInfoData deviceInfoData;
     private IDeviceInfoData deviceInfoData;
+    @Autowired
+    @Qualifier("thingModelDataCache")
+    private IThingModelData thingModelData;
 
 
     @PostConstruct
     @PostConstruct
     public void init() {
     public void init() {
@@ -52,13 +60,36 @@ public class DevicePropertyConsumer implements ConsumerHandler<ThingModelMessage
 
 
         Map<String, Object> properties = (Map<String, Object>) msg.getData();
         Map<String, Object> properties = (Map<String, Object>) msg.getData();
         String deviceId = msg.getDeviceId();
         String deviceId = msg.getDeviceId();
+        DeviceInfo deviceInfo = deviceInfoData.findByDeviceId(deviceId);
+        if (deviceInfo == null) {
+            return;
+        }
+
+        //物模型校验,过滤非物模型属性
+        ThingModel thingModel = thingModelData.findById(deviceInfo.getProductKey());
+        if (thingModel == null) {
+            return;
+        }
+
+        //物模型属性
+        Map<String, String> thingModelProperties = thingModel.getModel().
+                getProperties().stream().collect(Collectors.toMap(
+                ThingModel.Property::getIdentifier, ThingModel.Property::getName));
+
+        Map<String, Object> addProperties = new HashMap<>();
+        //删除非属性字段
+        properties.forEach((key,val)->{
+            if (thingModelProperties.containsKey(key)) {
+                addProperties.put(key,val);
+            }
+        });
 
 
         //更新设备当前属性
         //更新设备当前属性
-        updateDeviceCurrentProperties(deviceId, properties);
+        updateDeviceCurrentProperties(deviceId, addProperties);
 
 
         //保存属性记录
         //保存属性记录
         try {
         try {
-            devicePropertyData.addProperties(deviceId, properties, msg.getOccurred());
+            devicePropertyData.addProperties(deviceId, addProperties, msg.getOccurred());
         } catch (Throwable e) {
         } catch (Throwable e) {
             log.warn("save property data error", e);
             log.warn("save property data error", e);
         }
         }

+ 14 - 0
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) {
+    }
+
+}

+ 1 - 0
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_PRODUCT, config,
                 Constants.CACHE_OAUTH_CLIENT, config,
                 Constants.CACHE_OAUTH_CLIENT, config,
                 Constants.CACHE_CATEGORY, config,
                 Constants.CACHE_CATEGORY, config,
+                Constants.CACHE_THING_MODEL, config,
                 //统计缓存5分钟
                 //统计缓存5分钟
                 Constants.CACHE_DEVICE_STATS, config.entryTtl(Duration.ofMinutes(5))
                 Constants.CACHE_DEVICE_STATS, config.entryTtl(Duration.ofMinutes(5))
         );
         );

+ 62 - 0
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<ThingModel> findAll() {
+        return thingModelData.findAll();
+    }
+
+    @Override
+    public Paging<ThingModel> findAll(int page, int size) {
+        return thingModelData.findAll(page, size);
+    }
+
+}

+ 0 - 2
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, String> {
 public interface IThingModelData extends ICommonData<ThingModel, String> {
 
 
-    ThingModel findByProductKey(String productKey);
-
 }
 }

+ 2 - 5
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) {
         if (deviceInfo == null) {
             return 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)) {
         if (!indexSet.contains(index)) {
             IndexCoordinates indexCoordinates = IndexCoordinates.of(index);
             IndexCoordinates indexCoordinates = IndexCoordinates.of(index);
             if (!template.indexOps(indexCoordinates).exists()) {
             if (!template.indexOps(indexCoordinates).exists()) {

+ 0 - 2
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, String> {
 public interface ThingModelRepository extends JpaRepository<TbThingModel, String> {
 
 
-    TbThingModel findByProductKey(String productKey);
-
 }
 }

+ 0 - 5
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
     @Autowired
     private ThingModelRepository thingModelRepository;
     private ThingModelRepository thingModelRepository;
 
 
-    @Override
-    public ThingModel findByProductKey(String productKey) {
-        return ThingModelMapper.toDtoFix(thingModelRepository.findByProductKey(productKey));
-    }
-
     @Override
     @Override
     public ThingModel findById(String s) {
     public ThingModel findById(String s) {
         return ThingModelMapper.toDtoFix(thingModelRepository.findById(s).orElse(null));
         return ThingModelMapper.toDtoFix(thingModelRepository.findById(s).orElse(null));

+ 4 - 2
iot-standalone/src/main/java/cc/iotkit/manager/controller/ProductController.java

@@ -47,8 +47,10 @@ public class ProductController {
     @Qualifier("productDataCache")
     @Qualifier("productDataCache")
     private IProductData productData;
     private IProductData productData;
     @Autowired
     @Autowired
+    @Qualifier("thingModelDataCache")
     private IThingModelData thingModelData;
     private IThingModelData thingModelData;
     @Autowired
     @Autowired
+    @Qualifier("categoryDataCache")
     private ICategoryData categoryData;
     private ICategoryData categoryData;
     @Autowired
     @Autowired
     private DataOwnerService dataOwnerService;
     private DataOwnerService dataOwnerService;
@@ -97,7 +99,7 @@ public class ProductController {
     @PostMapping("/thingModel/save")
     @PostMapping("/thingModel/save")
     public void saveThingModel(String productKey, String model) {
     public void saveThingModel(String productKey, String model) {
         checkProductOwner(productKey);
         checkProductOwner(productKey);
-        ThingModel oldData = thingModelData.findByProductKey(productKey);
+        ThingModel oldData = thingModelData.findById(productKey);
         ThingModel thingModel = new ThingModel(productKey, productKey, JsonUtil.parse(model, ThingModel.Model.class));
         ThingModel thingModel = new ThingModel(productKey, productKey, JsonUtil.parse(model, ThingModel.Model.class));
         if (oldData == null) {
         if (oldData == null) {
             //定义时序数据库物模型数据结构
             //定义时序数据库物模型数据结构
@@ -112,7 +114,7 @@ public class ProductController {
     @PostMapping("/thingModel/{productKey}/delete")
     @PostMapping("/thingModel/{productKey}/delete")
     public void deleteThingModel(String productKey) {
     public void deleteThingModel(String productKey) {
         checkProductOwner(productKey);
         checkProductOwner(productKey);
-        ThingModel thingModel = thingModelData.findByProductKey(productKey);
+        ThingModel thingModel = thingModelData.findById(productKey);
         //删除时序数据库物模型数据结构
         //删除时序数据库物模型数据结构
         dbStructureData.defineThingModel(thingModel);
         dbStructureData.defineThingModel(thingModel);
         thingModelData.deleteById(productKey);
         thingModelData.deleteById(productKey);

+ 1 - 1
iot-standalone/src/main/java/cc/iotkit/manager/service/ThingModelService.java

@@ -25,7 +25,7 @@ public class ThingModelService {
     private IThingModelData thingModelData;
     private IThingModelData thingModelData;
 
 
     public void parseParams(ThingService<Object> service) {
     public void parseParams(ThingService<Object> service) {
-        ThingModel thingModel = thingModelData.findByProductKey(service.getProductKey());
+        ThingModel thingModel = thingModelData.findById(service.getProductKey());
         ThingModel.Model model = thingModel.getModel();
         ThingModel.Model model = thingModel.getModel();
 
 
         String type = service.getType();
         String type = service.getType();