Skip to content

Commit

Permalink
Remove Thing Description from MQTT Broker on destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
Heiko Bornholdt committed Aug 27, 2020
1 parent 6ff9eac commit 1030f16
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public CompletableFuture<Void> expose(ExposedThing thing) {
public CompletableFuture<Void> destroy(ExposedThing thing) {
log.info("MqttServer stop exposing '{}' as unique '/{}/*'", thing.getId(), thing.getId());

unexposeTD(thing);

// dispose all created subscriptions
Collection<Disposable> thingSubscriptions = subscriptions.removeAll(thing.getId());
for (Disposable subscription: thingSubscriptions) {
Expand Down Expand Up @@ -236,6 +238,26 @@ private void exposeTD(ExposedThing thing) {
}
}

/**
* To "delete" a retained message from the broker, we need to publish an empty message under the
* same topic.
*
* @param thing
*/
private void unexposeTD(ExposedThing thing) {
String topic = thing.getId();
log.debug("Remove published '{}' Thing Description at topic '{}'", thing.getId(), topic);

try {
MqttMessage mqttMessage = new MqttMessage();
mqttMessage.setRetained(true);
settingsClientPair.second().publish(topic, mqttMessage);
}
catch (MqttException e) {
log.warn("Unable to remove published thing description at topic '{}': {}", topic, e.getMessage());
}
}

private void listenOnMqttMessages() {
// connect incoming messages to Thing
settingsClientPair.second().setCallback(new MqttCallback() {
Expand Down

0 comments on commit 1030f16

Please sign in to comment.