|
|
@@ -25,10 +25,7 @@ import org.jetlinks.core.device.DeviceOperator;
|
|
|
import org.jetlinks.core.device.DeviceRegistry;
|
|
|
import org.jetlinks.core.device.MqttAuthenticationRequest;
|
|
|
import org.jetlinks.core.event.EventBus;
|
|
|
-import org.jetlinks.core.message.CommonDeviceMessage;
|
|
|
-import org.jetlinks.core.message.CommonDeviceMessageReply;
|
|
|
-import org.jetlinks.core.message.DeviceMessage;
|
|
|
-import org.jetlinks.core.message.Message;
|
|
|
+import org.jetlinks.core.message.*;
|
|
|
import org.jetlinks.core.message.codec.DefaultTransport;
|
|
|
import org.jetlinks.core.message.codec.FromDeviceMessageContext;
|
|
|
import org.jetlinks.core.message.codec.MqttMessage;
|
|
|
@@ -125,8 +122,8 @@ class MqttServerDeviceGateway implements DeviceGateway, MonitorSupportDeviceGate
|
|
|
.publishOn(Schedulers.parallel())
|
|
|
.flatMap(this::handleConnection)
|
|
|
.flatMap(tuple3 -> handleAuthResponse(tuple3.getT1(), tuple3.getT2(), tuple3.getT3()))
|
|
|
- .doOnNext(tp-> handleSubscriptionTopic(tp.getT1(),tp.getT2()))
|
|
|
- .doOnNext(tp-> handleUnSubscriptionTopic(tp.getT1(),tp.getT2()))
|
|
|
+ .flatMap(this::handleSubscriptionTopic)
|
|
|
+ .flatMap(this::handleUnSubscriptionTopic)
|
|
|
.flatMap(tp -> handleAcceptedMqttConnection(tp.getT1(), tp.getT2(), tp.getT3()), Integer.MAX_VALUE)
|
|
|
.onErrorContinue((err, obj) -> log.error("处理MQTT连接失败", err))
|
|
|
.subscriberContext(ReactiveLogger.start("network", mqttServer.getId()))
|
|
|
@@ -217,24 +214,34 @@ class MqttServerDeviceGateway implements DeviceGateway, MonitorSupportDeviceGate
|
|
|
}));
|
|
|
}
|
|
|
//处理已经建立连接的MQTT连接的主题订阅
|
|
|
- private Mono<Void> handleSubscriptionTopic(MqttConnection connection, DeviceOperator operator) {
|
|
|
- return connection
|
|
|
- .handleSubscribe(true)
|
|
|
+ private Mono<Tuple3<MqttConnection, DeviceOperator, MqttConnectionSession>> handleSubscriptionTopic(Tuple3<MqttConnection, DeviceOperator, MqttConnectionSession> tuple3) {
|
|
|
+ return tuple3.getT1()
|
|
|
+ .handleSubscribe(false)
|
|
|
.doOnNext(topic->{
|
|
|
MqttSubscribeMessage message = topic.getMessage();
|
|
|
Set<String> topics = message.topicSubscriptions().stream().map(MqttTopicSubscription::topicName).collect(Collectors.toSet());
|
|
|
- operator.addTopics(topics);
|
|
|
- }).then();
|
|
|
+ tuple3.getT2().addTopics(topics);
|
|
|
+ })
|
|
|
+ .flatMap(ignore->
|
|
|
+ eventBus.publish(String.format("/dashboard/device/%s/changed/topics",
|
|
|
+ tuple3.getT2().getDeviceId()),new TimeSyncMessage()))
|
|
|
+ .collectList()
|
|
|
+ .map(ignore->tuple3);
|
|
|
}
|
|
|
|
|
|
//取消MQTT连接的主题订阅
|
|
|
- private Mono<Void> handleUnSubscriptionTopic(MqttConnection connection, DeviceOperator operator) {
|
|
|
- return connection
|
|
|
- .handleUnSubscribe(true)
|
|
|
+ private Mono<Tuple3<MqttConnection, DeviceOperator, MqttConnectionSession>> handleUnSubscriptionTopic(Tuple3<MqttConnection, DeviceOperator, MqttConnectionSession> tuple3) {
|
|
|
+ return tuple3.getT1()
|
|
|
+ .handleUnSubscribe(false)
|
|
|
.doOnNext(topic->{
|
|
|
MqttUnsubscribeMessage message = topic.getMessage();
|
|
|
- operator.removeTopics(message.topics());
|
|
|
- }).then();
|
|
|
+ tuple3.getT2().removeTopics(message.topics());
|
|
|
+ })
|
|
|
+ .flatMap(ignore->
|
|
|
+ eventBus.publish(String.format("/dashboard/device/%s/changed/topics",
|
|
|
+ tuple3.getT2().getDeviceId()),new TimeSyncMessage()))
|
|
|
+ .collectList()
|
|
|
+ .map(ignore->tuple3);
|
|
|
}
|
|
|
|
|
|
//处理已经建立连接的MQTT连接
|