|
|
@@ -1,5 +1,7 @@
|
|
|
package com.yunfeiyun.agmp.iots.core.mqtt.network;
|
|
|
|
|
|
+import com.yunfeiyun.agmp.common.constant.ErrorCode;
|
|
|
+import com.yunfeiyun.agmp.common.exception.BizException;
|
|
|
import com.yunfeiyun.agmp.common.utils.ip.IpUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.spring.SpringUtils;
|
|
|
import com.yunfeiyun.agmp.iots.core.mqtt.modal.MqttTopicValue;
|
|
|
@@ -34,7 +36,6 @@ public class MqttCore {
|
|
|
private String connectionId;
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 内部维护的发布者
|
|
|
*/
|
|
|
@@ -48,8 +49,8 @@ public class MqttCore {
|
|
|
/**
|
|
|
* topic -> device
|
|
|
*/
|
|
|
- Map<String,String> topicToDevId=new HashMap<>();
|
|
|
- Map<String,MqttTopicValue> topicValueMap = new HashMap<>();
|
|
|
+ Map<String, String> topicToDevId = new HashMap<>();
|
|
|
+ Map<String, MqttTopicValue> topicValueMap = new HashMap<>();
|
|
|
|
|
|
public String getConnectionId() {
|
|
|
return connectionId;
|
|
|
@@ -74,7 +75,12 @@ public class MqttCore {
|
|
|
* @return
|
|
|
*/
|
|
|
public Device getDevice(String topic) {
|
|
|
- return SpringUtils.getBean(topicValueMap.get(topic).getServiceName());
|
|
|
+ MqttTopicValue topicValue = topicValueMap.get(topic);
|
|
|
+ if (topicValue == null) {
|
|
|
+ log.info("【解析数据:根据topic获取Device: topic:{}】", topicValue);
|
|
|
+ throw new BizException(ErrorCode.FAILURE.getCode(), topic + "未找到Device");
|
|
|
+ }
|
|
|
+ return SpringUtils.getBean(topicValue.getServiceName());
|
|
|
}
|
|
|
|
|
|
public String getServiceType() {
|
|
|
@@ -148,7 +154,7 @@ public class MqttCore {
|
|
|
private void buildClient() throws MqttException {
|
|
|
//return new MqttClient("tcp://47.96.123.180:1883", UUID.randomUUID().toString().replace("-",""));
|
|
|
String url = "tcp://" + mqttConfig.getIp() + ":" + mqttConfig.getPort();
|
|
|
- this.mqttClient = new MqttClient(url, connectionId+ IpUtils.getHostIp(), new MemoryPersistence());
|
|
|
+ this.mqttClient = new MqttClient(url, IpUtils.getHostIp() + ":SAAS:" + connectionId, new MemoryPersistence());
|
|
|
log.info("【初始化】构建 MQTT clientId {}", this.mqttClient.getClientId());
|
|
|
}
|
|
|
|
|
|
@@ -206,6 +212,7 @@ public class MqttCore {
|
|
|
|
|
|
/**
|
|
|
* 关闭链接
|
|
|
+ *
|
|
|
* @throws MqttException
|
|
|
*/
|
|
|
public void close() throws MqttException {
|
|
|
@@ -213,7 +220,7 @@ public class MqttCore {
|
|
|
mqttClient.disconnect();
|
|
|
this.mqttClient.close();
|
|
|
log.info("【关闭MQTT连接成功】 connectionId: {}", connectionId);
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
log.info("【关闭MQTT连接失败:已关闭】 connectionId: {}", connectionId);
|
|
|
}
|
|
|
|
|
|
@@ -221,15 +228,16 @@ public class MqttCore {
|
|
|
|
|
|
/**
|
|
|
* 订阅。原始类不对外暴漏
|
|
|
+ *
|
|
|
* @param topics
|
|
|
* @throws MqttException
|
|
|
*/
|
|
|
- public void subscribe(String[] topics,MqttConfig cfgYf) throws MqttException {
|
|
|
+ public void subscribe(String[] topics, MqttConfig cfgYf) throws MqttException {
|
|
|
// 否则向mqttConfig中追加订阅
|
|
|
- mqttConfig.setSubTopic(Stream.concat(Stream.of(topics),Stream.of(mqttConfig.getSubTopic())).toArray(String[]::new));
|
|
|
- if(getClient().isConnected()){
|
|
|
+ mqttConfig.setSubTopic(Stream.concat(Stream.of(topics), Stream.of(mqttConfig.getSubTopic())).toArray(String[]::new));
|
|
|
+ if (getClient().isConnected()) {
|
|
|
// 如果此mqtt已经建立了连接,则正常订阅
|
|
|
- for(String topic :topics){
|
|
|
+ for (String topic : topics) {
|
|
|
getClient().subscribe(topic);
|
|
|
log.info("[MQTT] {} 连接已建立 追加订阅主题 {}", cfgYf.getDeviceType(), topic);
|
|
|
}
|
|
|
@@ -238,24 +246,26 @@ public class MqttCore {
|
|
|
|
|
|
/**
|
|
|
* 取消订阅,原始类不对外暴漏
|
|
|
+ *
|
|
|
* @param topics
|
|
|
* @throws MqttException
|
|
|
*/
|
|
|
public void unsubscribe(String[] topics) throws MqttException {
|
|
|
getClient().unsubscribe(topics);
|
|
|
- for(String topic:topics){
|
|
|
+ for (String topic : topics) {
|
|
|
topicToDevId.remove(topic);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将topic映射到设备id
|
|
|
+ *
|
|
|
* @param mqttTopicValues
|
|
|
*/
|
|
|
- public void bindTopicToDeviceId(List<MqttTopicValue> mqttTopicValues){
|
|
|
- for(MqttTopicValue mqttTopicValue:mqttTopicValues){
|
|
|
- topicToDevId.put(mqttTopicValue.getTopic(),mqttTopicValue.getDevId());
|
|
|
- topicValueMap.put(mqttTopicValue.getTopic(),mqttTopicValue);
|
|
|
+ public void bindTopicToDeviceId(List<MqttTopicValue> mqttTopicValues) {
|
|
|
+ for (MqttTopicValue mqttTopicValue : mqttTopicValues) {
|
|
|
+ topicToDevId.put(mqttTopicValue.getTopic(), mqttTopicValue.getDevId());
|
|
|
+ topicValueMap.put(mqttTopicValue.getTopic(), mqttTopicValue);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -267,10 +277,11 @@ public class MqttCore {
|
|
|
|
|
|
/**
|
|
|
* 根据topic 获取设备id
|
|
|
+ *
|
|
|
* @param topic
|
|
|
* @return
|
|
|
*/
|
|
|
- public String getDevIdByTopic(String topic){
|
|
|
+ public String getDevIdByTopic(String topic) {
|
|
|
return topicToDevId.get(topic);
|
|
|
}
|
|
|
}
|