|
@@ -1,6 +1,7 @@
|
|
|
package com.ruoyi.data.service.impl;
|
|
package com.ruoyi.data.service.impl;
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONArray;
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.core.lang.UUID;
|
|
import cn.hutool.core.lang.UUID;
|
|
|
import cn.hutool.json.JSONObject;
|
|
import cn.hutool.json.JSONObject;
|
|
@@ -24,6 +25,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
import org.eclipse.paho.client.mqttv3.MqttCallback;
|
|
|
|
|
+import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -121,13 +123,25 @@ public class MqttServiceImpl implements MqttService {
|
|
|
}
|
|
}
|
|
|
for(MqttObj obj:mqttObjList){
|
|
for(MqttObj obj:mqttObjList){
|
|
|
if(obj.getStatus() == 1) {
|
|
if(obj.getStatus() == 1) {
|
|
|
- JSONObject topicObj = obj.getTopicQos(protocolType);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ JSONArray topicObjs = (JSONArray) JSONUtil.parse(obj.getServerTopic());
|
|
|
MQTTConnect mqttConnect = new MQTTConnect();
|
|
MQTTConnect mqttConnect = new MQTTConnect();
|
|
|
try {
|
|
try {
|
|
|
mqttConnect.createMqttClient(obj.getServerAddress(), obj.getUuid(), obj.getAccount(), obj.getPassword(), new Callback());
|
|
mqttConnect.createMqttClient(obj.getServerAddress(), obj.getUuid(), obj.getAccount(), obj.getPassword(), new Callback());
|
|
|
- if (topicObj != null) {
|
|
|
|
|
- String topic = topicObj.get("name").toString().replace("#", "");
|
|
|
|
|
- mqttConnect.pub(topic, mqttMsg.toString(), Integer.valueOf((String) topicObj.get("qos")));
|
|
|
|
|
|
|
+ if (topicObjs.size()>0) {
|
|
|
|
|
+ String finalProtocolType = protocolType;
|
|
|
|
|
+ topicObjs.forEach(i->{
|
|
|
|
|
+ JSONObject jsonObject1 = (JSONObject)i;
|
|
|
|
|
+ String topic = jsonObject1.getStr("name");
|
|
|
|
|
+ topic = StrUtil.replace(topic,"$protocolType$", finalProtocolType);
|
|
|
|
|
+ topic = StrUtil.replace(topic,"$sensorId$", String.valueOf(sensorId));
|
|
|
|
|
+ try {
|
|
|
|
|
+ mqttConnect.pub(topic, mqttMsg.toString(), (Integer) jsonObject1.get("qos"));
|
|
|
|
|
+ } catch (MqttException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
} else {
|
|
} else {
|
|
|
String topic = "sensor/"+protocolType+"/" + sensorId;
|
|
String topic = "sensor/"+protocolType+"/" + sensorId;
|
|
|
mqttConnect.pub(topic, mqttMsg.toString(), 0);
|
|
mqttConnect.pub(topic, mqttMsg.toString(), 0);
|