Browse Source

feat:
传输设备信息

18339543638 2 years ago
parent
commit
b6cd7bc8e8

+ 8 - 2
nb-service/iot-service/src/main/java/com/nb/aliyun/service/AliyunConsumerGroupService.java

@@ -4,6 +4,8 @@ import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.nb.aliyun.service.bean.AliIotMessagePojo;
 import com.nb.aliyun.service.process.DeviceMsgHandler;
+import com.nb.web.api.entity.BusDeviceEntity;
+import com.nb.web.api.feign.IDeviceClient;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -27,10 +29,12 @@ public class AliyunConsumerGroupService implements CommandLineRunner {
 
     private final DeviceMsgHandler deviceMsgHandler;
 
+    private final IDeviceClient deviceService;
 
-    public AliyunConsumerGroupService(AliyunIotSubscribeClient client, DeviceMsgHandler deviceMsgHandler) {
+    public AliyunConsumerGroupService(AliyunIotSubscribeClient client, DeviceMsgHandler deviceMsgHandler,IDeviceClient deviceService) {
         this.client = client;
         this.deviceMsgHandler = deviceMsgHandler;
+        this.deviceService=deviceService;
     }
 
     /**
@@ -101,7 +105,9 @@ public class AliyunConsumerGroupService implements CommandLineRunner {
             String messageId = message.getStringProperty(AliIotConstant.MESSAGE_ID);
             JSONObject content = JSONUtil.parseObj(new String(message.getBody(byte[].class)));
             log.info("阿里云物联网发送的数据:"+content.toString());
-            AliIotMessagePojo msg = AliIotMessagePojo.of(topic, messageId, content);
+            String deviceName = content.getStr(AliIotConstant.DEVICE_NAME);
+            BusDeviceEntity device = deviceService.getByDeviceId(deviceName);
+            AliIotMessagePojo msg = AliIotMessagePojo.of(topic, messageId, content,device);
             deviceMsgHandler.handleMessage(msg,true);
         } catch (Exception e) {
             log.error("阿里云数据【{}】处理失败 ", JSONUtil.toJsonStr(message), e);

+ 3 - 0
nb-service/iot-service/src/main/java/com/nb/aliyun/service/bean/AliIotMessagePojo.java

@@ -1,6 +1,7 @@
 package com.nb.aliyun.service.bean;
 
 import cn.hutool.json.JSONObject;
+import com.nb.web.api.entity.BusDeviceEntity;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
@@ -20,4 +21,6 @@ public class AliIotMessagePojo implements Serializable {
     private String topic;
     private String messageId;
     private JSONObject content;
+
+    private BusDeviceEntity device;
 }

+ 1 - 3
nb-service/iot-service/src/main/java/com/nb/aliyun/service/process/DeviceMsgHandler.java

@@ -43,9 +43,7 @@ public class DeviceMsgHandler {
      */
     public void handleMessage(AliIotMessagePojo message,boolean aliIot) {
         try {
-            JSONObject content = message.getContent();
-            String deviceName = content.getStr(AliIotConstant.DEVICE_NAME);
-            BusDeviceEntity device = deviceService.getByDeviceId(deviceName);
+            BusDeviceEntity device = message.getDevice();
             //分发数据
             if(aliIot){
                 distributeIotMsg(message, ObjectUtil.isNull(device)?null:device.getTenantId());

+ 2 - 1
nb-service/web-service/src/main/java/com/nb/web/service/bus/service/LocalBusDeviceService.java

@@ -213,7 +213,8 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
     @Override
     public BusDeviceEntity getByDeviceId(String deviceId) {
         BusDeviceEntity device = getOne(new QueryWrapper<BusDeviceEntity>().lambda()
-                .eq(BusDeviceEntity::getDeviceId,deviceId));
+                .eq(BusDeviceEntity::getDeviceId,deviceId)
+                .last("limit 1"));
         return device;
     }