From d5dd896b45c8c35ef9d5a801d92ba8b34e3351c5 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 29 Mar 2021 20:35:55 +0200 Subject: [PATCH] MqttClient::unsubscribe implemented --- library.json | 2 +- library.properties | 2 +- src/TinyMqtt.cpp | 45 +++++++++++++++++++++---------- src/TinyMqtt.h | 3 ++- tests/local-tests/local-tests.ino | 4 +-- 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/library.json b/library.json index decfde2..a2cc462 100644 --- a/library.json +++ b/library.json @@ -6,7 +6,7 @@ "type": "git", "url": "https://github.com/hsaturn/TinyMqtt.git" }, - "version": "0.7.0", + "version": "0.7.1", "exclude": "", "examples": "examples/*/*.ino", "frameworks": "arduino", diff --git a/library.properties b/library.properties index 4275563..1a093b4 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TinyMqtt -version=0.7.0 +version=0.7.1 author=Francois BIOT, HSaturn, maintainer=Francois BIOT, HSaturn, sentence=A tiny broker and client library for MQTT messaging. diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 9c37ac1..3a95f39 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -41,7 +41,6 @@ MqttClient::~MqttClient() { close(); delete client; - Serial << "Client deleted" << endl; } void MqttClient::close(bool bSendDisconnect) @@ -269,24 +268,42 @@ MqttError MqttClient::subscribe(Topic topic, uint8_t qos) subscriptions.insert(topic); - if (parent==nullptr) // remote broker ? + if (parent==nullptr) // remote broker { - debug("remote subscribe"); - MqttMessage msg(MqttMessage::Type::Subscribe, 2); - - // TODO manage packet identifier - msg.add(0); - msg.add(0); - - msg.add(topic); - msg.add(qos); - ret = msg.sendTo(this); - - // TODO we should wait (state machine) for SUBACK + return sendTopic(topic, MqttMessage::Type::Subscribe, qos); } return ret; } +MqttError MqttClient::unsubscribe(Topic topic) +{ + auto it=subscriptions.find(topic); + if (it != subscriptions.end()) + { + subscriptions.erase(it); + if (parent==nullptr) // remote broker + { + return sendTopic(topic, MqttMessage::Type::UnSubscribe, 0); + } + } + return MqttOk; +} + +MqttError MqttClient::sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos) +{ + MqttMessage msg(type, 2); + + // TODO manage packet identifier + msg.add(0); + msg.add(0); + + msg.add(topic); + msg.add(qos); + + // TODO instead we should wait (state machine) for SUBACK / UNSUBACK ? + return msg.sendTo(this); +} + long MqttClient::counter=0; void MqttClient::processMessage() diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index d40e959..3246e63 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -149,7 +149,7 @@ class MqttClient MqttError publish(const Topic& t) { return publish(t, nullptr, 0);}; MqttError subscribe(Topic topic, uint8_t qos=0); - MqttError unsubscribe(Topic& topic); + MqttError unsubscribe(Topic topic); // connected to local broker // TODO seems to be useless @@ -178,6 +178,7 @@ class MqttClient static long counter; // Number of messages sent private: + MqttError sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos); void resubscribe(); friend class MqttBroker; diff --git a/tests/local-tests/local-tests.ino b/tests/local-tests/local-tests.ino index a8c6dad..6dc0233 100644 --- a/tests/local-tests/local-tests.ino +++ b/tests/local-tests/local-tests.ino @@ -96,8 +96,9 @@ test(local_unsubscribe) MqttClient publisher(&broker); publisher.publish("a/b"); - // subscriber.unsubscribe("a/b"); TODO not yet implemented + subscriber.unsubscribe("a/b"); + publisher.publish("a/b"); publisher.publish("a/b"); assertTrue(published[""]["a/b"] == 1); // Only one publish has been received @@ -120,7 +121,6 @@ test(local_nocallback_when_destroyed) assertEqual(published.size(), (size_t)0); // Only one publish has been received } - //---------------------------------------------------------------------------- // setup() and loop() void setup() {