|
|
@@ -1,6 +1,7 @@
|
|
|
package com.zd.laboratory.mqtt.config;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.zd.algorithm.api.fire.RemoteFireDeviceService;
|
|
|
import com.zd.common.core.redis.RedisService;
|
|
|
@@ -35,7 +36,8 @@ import org.springframework.integration.mqtt.support.MqttHeaders;
|
|
|
import org.springframework.messaging.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -281,13 +283,26 @@ public class MqttConfig {
|
|
|
public void handleMessage(Message<?> message) throws MessagingException {
|
|
|
MessageHeaders messageHeaders = message.getHeaders();
|
|
|
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.contains("788D4C6C6187ABC")) {
|
|
|
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);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(messageStr);
|
|
|
jsonObject.put("deviceNo", topicEnd);
|