Просмотр исходного кода

采集器数据byte数组处理成字符串

hanzhiwei лет назад: 2
Родитель
Сommit
0060ffabdf

+ 19 - 4
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/mqtt/config/MqttConfig.java

@@ -1,6 +1,7 @@
 package com.zd.laboratory.mqtt.config;
 package com.zd.laboratory.mqtt.config;
 
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollUtil;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
 import com.zd.algorithm.api.fire.RemoteFireDeviceService;
 import com.zd.algorithm.api.fire.RemoteFireDeviceService;
 import com.zd.common.core.redis.RedisService;
 import com.zd.common.core.redis.RedisService;
@@ -35,7 +36,8 @@ import org.springframework.integration.mqtt.support.MqttHeaders;
 import org.springframework.messaging.*;
 import org.springframework.messaging.*;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
-import java.nio.charset.StandardCharsets;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.util.*;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
@@ -281,13 +283,26 @@ public class MqttConfig {
             public void handleMessage(Message<?> message) throws MessagingException {
             public void handleMessage(Message<?> message) throws MessagingException {
                 MessageHeaders messageHeaders = message.getHeaders();
                 MessageHeaders messageHeaders = message.getHeaders();
                 String receivedTopic = (String) messageHeaders.get(MqttHeaders.RECEIVED_TOPIC);
                 String receivedTopic = (String) messageHeaders.get(MqttHeaders.RECEIVED_TOPIC);
-                //logger.info("[通道] - [{}]", receivedTopic);
-                //logger.info("[消息] - [{}]", message.getPayload());
-                String messageStr = message.getPayload().toString();
+//                logger.info("[通道] - [{}],[{}]", receivedTopic,message.getPayload());
+//                String messageStr = message.getPayload().toString();
+                String messageStr = JSON.toJSONString(message.getPayload());
                 if (receivedTopic.startsWith(devicePrefix)) {
                 if (receivedTopic.startsWith(devicePrefix)) {
                     if (receivedTopic.contains("788D4C6C6187ABC")) {
                     if (receivedTopic.contains("788D4C6C6187ABC")) {
                         logger.info("[原始消息] - [{}]", message.getPayload());
                         logger.info("[原始消息] - [{}]", message.getPayload());
                     }
                     }
+                    if ((message.getPayload() instanceof byte[])) {
+                        messageStr = "";
+                        try {
+                            ByteArrayInputStream bis  = new ByteArrayInputStream((byte[]) message.getPayload());
+                            byte[] buffer = new byte[1024];
+                            int len = -1;
+                            while((len = bis.read(buffer)) != -1){
+                                messageStr+= new String(buffer);
+                            }
+                        } catch (IOException e) {
+                           logger.error("传感器数据转换异常!");
+                        }
+                    }
                     String topicEnd = receivedTopic.substring(receivedTopic.lastIndexOf("/") + 1);
                     String topicEnd = receivedTopic.substring(receivedTopic.lastIndexOf("/") + 1);
                     JSONObject jsonObject = JSONObject.parseObject(messageStr);
                     JSONObject jsonObject = JSONObject.parseObject(messageStr);
                     jsonObject.put("deviceNo", topicEnd);
                     jsonObject.put("deviceNo", topicEnd);