Bladeren bron

开发平台,MQTT设备数据上报调试接口增加平台产品、设备Id和设备属性存在的校验

james 1 maand geleden
bovenliggende
commit
edcc127f9d

+ 2 - 2
data-transfer/data-transfer-biz/src/main/java/com/usky/transfer/MybatisGenerator.java

@@ -42,7 +42,7 @@ public class MybatisGenerator {
         //2、数据源配置
         //修改数据源
         DataSourceConfig dsc = new DataSourceConfig();
-        dsc.setUrl("jdbc:mysql://172.16.120.165:3306/usky-cloud?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
+        dsc.setUrl("jdbc:mysql://192.168.10.165:3306/usky-cloud?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
         dsc.setDriverName("com.mysql.jdbc.Driver");
         dsc.setUsername("root");
         dsc.setPassword("yt123456");
@@ -70,7 +70,7 @@ public class MybatisGenerator {
         // strategy.setTablePrefix("t_"); // 表名前缀
         strategy.setEntityLombokModel(true); //使用lombok
         //修改自己想要生成的表
-        strategy.setInclude("dmp_device_command");  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
+        strategy.setInclude("dmp_product_attribute");  // 逆向工程使用的表   如果要生成多个,这里可以传入String[]
         mpg.setStrategy(strategy);
 
         // 关闭默认 xml 生成,调整生成 至 根目录

+ 137 - 0
data-transfer/data-transfer-biz/src/main/java/com/usky/transfer/domain/DmpProductAttribute.java

@@ -0,0 +1,137 @@
+package com.usky.transfer.domain;
+
+import java.math.BigDecimal;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import java.time.LocalDateTime;
+import java.io.Serializable;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * <p>
+ * 产品属性表
+ * </p>
+ *
+ * @author ya
+ * @since 2025-02-10
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+public class DmpProductAttribute implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 主键id
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    /**
+     * 产品ID
+     */
+    private Integer productId;
+
+    /**
+     * 属性名称
+     */
+    private String attributeName;
+
+    /**
+     * 属性标识
+     */
+    private String attributeCode;
+
+    /**
+     * 对应端口/属性ID
+     */
+    private Integer attributePort;
+
+    /**
+     * 属性类型;1:必选,2:可选
+     */
+    private Integer attributeType;
+
+    /**
+     * 数据类型(1、数值型 2、字符型 3、bool型)
+     */
+    private Integer dataType;
+
+    /**
+     * 绑定状态;1:已绑定,2:未绑定
+     */
+    private Integer bindStatus;
+
+    /**
+     * 长度
+     */
+    private Integer attributeLength;
+
+    /**
+     * 单位
+     */
+    private String attributeUnit;
+
+    /**
+     * 最大值
+     */
+    private BigDecimal maximum;
+
+    /**
+     * 最小值
+     */
+    private BigDecimal minimum;
+
+    /**
+     * 时间格式
+     */
+    private String timeFormat;
+
+    /**
+     * 布尔值false
+     */
+    private String boolFalse;
+
+    /**
+     * 布尔值true
+     */
+    private String boolTrue;
+
+    /**
+     * 删除标识;0:未删除,1:已删除
+     */
+    private Integer deleteFlag;
+
+    /**
+     * 描述
+     */
+    private String attributeDescribe;
+
+    /**
+     * 创建人
+     */
+    private String createdBy;
+
+    /**
+     * 创建时间
+     */
+    private LocalDateTime createdTime;
+
+    /**
+     * 更新人
+     */
+    private String updatedBy;
+
+    /**
+     * 更新时间
+     */
+    private LocalDateTime updatedTime;
+
+    /**
+     * 租户号
+     */
+    private Integer tenantId;
+
+
+}

+ 16 - 0
data-transfer/data-transfer-biz/src/main/java/com/usky/transfer/mapper/DmpProductAttributeMapper.java

@@ -0,0 +1,16 @@
+package com.usky.transfer.mapper;
+
+import com.usky.transfer.domain.DmpProductAttribute;
+import com.usky.common.mybatis.core.CrudMapper;
+
+/**
+ * <p>
+ * 产品属性表 Mapper 接口
+ * </p>
+ *
+ * @author ya
+ * @since 2025-02-10
+ */
+public interface DmpProductAttributeMapper extends CrudMapper<DmpProductAttribute> {
+
+}

+ 16 - 0
data-transfer/data-transfer-biz/src/main/java/com/usky/transfer/service/DmpProductAttributeService.java

@@ -0,0 +1,16 @@
+package com.usky.transfer.service;
+
+import com.usky.transfer.domain.DmpProductAttribute;
+import com.usky.common.mybatis.core.CrudService;
+
+/**
+ * <p>
+ * 产品属性表 服务类
+ * </p>
+ *
+ * @author ya
+ * @since 2025-02-10
+ */
+public interface DmpProductAttributeService extends CrudService<DmpProductAttribute> {
+
+}

+ 20 - 0
data-transfer/data-transfer-biz/src/main/java/com/usky/transfer/service/impl/DmpProductAttributeServiceImpl.java

@@ -0,0 +1,20 @@
+package com.usky.transfer.service.impl;
+
+import com.usky.transfer.domain.DmpProductAttribute;
+import com.usky.transfer.mapper.DmpProductAttributeMapper;
+import com.usky.transfer.service.DmpProductAttributeService;
+import com.usky.common.mybatis.core.AbstractCrudService;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ * 产品属性表 服务实现类
+ * </p>
+ *
+ * @author ya
+ * @since 2025-02-10
+ */
+@Service
+public class DmpProductAttributeServiceImpl extends AbstractCrudService<DmpProductAttributeMapper, DmpProductAttribute> implements DmpProductAttributeService {
+
+}

+ 50 - 0
data-transfer/data-transfer-biz/src/main/java/com/usky/transfer/service/impl/QueryInfluxdbDataServiceImpl.java

@@ -1,5 +1,6 @@
 package com.usky.transfer.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.nacos.shaded.com.google.protobuf.Internal;
@@ -7,6 +8,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.usky.common.core.exception.BusinessException;
 import com.usky.common.core.util.UUIDUtils;
 import com.usky.common.security.utils.SecurityUtils;
 import com.usky.transfer.domain.*;
@@ -49,6 +51,8 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
     private DmpDeviceStatusService dmpDeviceStatusService;
     @Autowired
     private DmpDeviceCommandService dmpDeviceCommandService;
+    @Autowired
+    private DmpProductAttributeService dmpProductAttributeService;
 
     @Resource
     private MyProducer myProducer;
@@ -252,9 +256,55 @@ public class QueryInfluxdbDataServiceImpl extends AbstractCrudService<QueryInflu
         Map<String,Object> rec_map = new HashMap<>();
 
         String topic = mqttDeviceDataVO.getTopic();
+        String[] topics = topic.split("/");
+        String productCode1 = topics[1];
+        String deviceId1 = topics[2];
         String payload = mqttDeviceDataVO.getDeviceData();
 
         JSONObject deviceDataJson = JSONObject.parseObject(payload);
+        String productCode2 = deviceDataJson.get("productCode").toString();
+        Object tag = JSONObject.toJSONString(deviceDataJson.get("tags"));
+        JSONObject tagJson = JSON.parseObject(tag.toString());
+        String deviceId2 = tagJson.get("device_id").toString();
+        Object metric = JSONObject.toJSONString(deviceDataJson.get("metrics"));
+        JSONObject metricJson = JSON.parseObject(metric.toString());
+
+        if(!productCode1.equals(productCode2) || !deviceId1.equals(deviceId2)){
+            throw new BusinessException("Topic和请求体中的产品编码、设备Id不一致,请修改");
+        }
+
+        LambdaQueryWrapper<DmpProduct> lambdaQuery = Wrappers.lambdaQuery();
+        lambdaQuery.eq(DmpProduct::getProductCode,productCode1)
+                .eq(DmpProduct::getDeleteFlag,0);
+        List<DmpProduct> dmpProductList = dmpProductService.list(lambdaQuery);
+        if(dmpProductList.size() <= 0){
+            throw new BusinessException("产品编码不存在,请修改");
+        }
+        LambdaQueryWrapper<DmpDevice> lambdaQuery2 = Wrappers.lambdaQuery();
+        lambdaQuery2.eq(DmpDevice::getDeviceId,deviceId1)
+                .eq(DmpDevice::getProductCode,productCode1)
+                .eq(DmpDevice::getServiceStatus,2)
+                .eq(DmpDevice::getDeleteFlag,0);
+        List<DmpDevice> dmpDeviceList = dmpDeviceService.list(lambdaQuery2);
+        if(dmpDeviceList.size() <= 0){
+            throw new BusinessException("设备Id不存在或在本产品下不存在,请修改");
+        }
+
+        List<String> metricList = new ArrayList<>();
+        if(Objects.nonNull(metricJson)){
+            for(String entry: metricJson.keySet()){
+                metricList.add(entry.toLowerCase());
+            }
+        }
+        int count1 = metricList.size();
+        LambdaQueryWrapper<DmpProductAttribute> lambdaQuery3 = Wrappers.lambdaQuery();
+        lambdaQuery3.eq(DmpProductAttribute::getProductId,dmpProductList.get(0).getId())
+                .in(DmpProductAttribute::getAttributeCode,metricList);
+        int count2 = dmpProductAttributeService.count(lambdaQuery3);
+        if(count1 > count2){
+            throw new BusinessException("设备属性编码不存在,请检查");
+        }
+
 
         mqttGateway.sendToMqtt(topic, deviceDataJson.toJSONString());
 

+ 31 - 0
data-transfer/data-transfer-biz/src/main/resources/mapper/transfer/DmpProductAttributeMapper.xml

@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.usky.transfer.mapper.DmpProductAttributeMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.usky.transfer.domain.DmpProductAttribute">
+        <id column="id" property="id" />
+        <result column="product_id" property="productId" />
+        <result column="attribute_name" property="attributeName" />
+        <result column="attribute_code" property="attributeCode" />
+        <result column="attribute_port" property="attributePort" />
+        <result column="attribute_type" property="attributeType" />
+        <result column="data_type" property="dataType" />
+        <result column="bind_status" property="bindStatus" />
+        <result column="attribute_length" property="attributeLength" />
+        <result column="attribute_unit" property="attributeUnit" />
+        <result column="maximum" property="maximum" />
+        <result column="minimum" property="minimum" />
+        <result column="time_format" property="timeFormat" />
+        <result column="bool_false" property="boolFalse" />
+        <result column="bool_true" property="boolTrue" />
+        <result column="delete_flag" property="deleteFlag" />
+        <result column="attribute_describe" property="attributeDescribe" />
+        <result column="created_by" property="createdBy" />
+        <result column="created_time" property="createdTime" />
+        <result column="updated_by" property="updatedBy" />
+        <result column="updated_time" property="updatedTime" />
+        <result column="tenant_id" property="tenantId" />
+    </resultMap>
+
+</mapper>