From 6ff25f9d2356cd7bf0a6c15c19a93f22f807d4f6 Mon Sep 17 00:00:00 2001 From: Den K Date: Sun, 19 Feb 2023 22:43:20 +0200 Subject: [PATCH 1/3] Added Caller interface and support. Provided example --- examples/MethodCallback/CallerInterface.h | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/MethodCallback/CallerInterface.h diff --git a/examples/MethodCallback/CallerInterface.h b/examples/MethodCallback/CallerInterface.h new file mode 100644 index 0000000..e72b22f --- /dev/null +++ b/examples/MethodCallback/CallerInterface.h @@ -0,0 +1,39 @@ +#pragma once +#include "Arduino.h" +#include "EspMQTTClient.h" + +// check working example here https://github.com/dino-rider/IotDevice_test + +class IotDevice; + +class Peripheral: public EspMQTTCaller +{ +protected: + EspMQTTClient *client; + String topic; +public: + Peripheral(EspMQTTClient *_client,String _topic); + void publish(String message, bool retain); + void subscribe(); + void setTopic(String _topic) {topic=_topic;}; + String getTopic(){return topic}; + virtual void cMessageReceivedCallback(const String &topicStr, const String &message); +}; + +void Peripheral::publish(const String message, bool retain) +{ + client->publish(topic, message, retain); +} + +void Peripheral::subscribe() +{ + client->subscribe(topic, static_cast(this), 0); +} + +Peripheral::Peripheral(EspMQTTClient *_client, String _topic): client(_client) +{} + +void Peripheral::cMessageReceivedCallback(const String &topicStr, const String &message) +{ + Serial.println(message); +} \ No newline at end of file From 14c29e318482d52b2addfd8df4408fa2df33888e Mon Sep 17 00:00:00 2001 From: Den K Date: Sun, 19 Feb 2023 22:49:39 +0200 Subject: [PATCH 2/3] added Caller interface, and support. --- src/EspMQTTClient.cpp | 12 +++++++++++- src/EspMQTTClient.h | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/EspMQTTClient.cpp b/src/EspMQTTClient.cpp index 3bcd5b0..b770a83 100644 --- a/src/EspMQTTClient.cpp +++ b/src/EspMQTTClient.cpp @@ -469,7 +469,7 @@ bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallback messa found = _topicSubscriptionList[i].topic.equals(topic); if(!found) - _topicSubscriptionList.push_back({ topic, messageReceivedCallback, NULL }); + _topicSubscriptionList.push_back({ topic, messageReceivedCallback, NULL, NULL }); } if (_enableDebugMessages) @@ -493,6 +493,16 @@ bool EspMQTTClient::subscribe(const String &topic, MessageReceivedCallbackWithTo return false; } +bool EspMQTTClient::subscribe(const String &topic, EspMQTTCaller *caller, uint8_t qos) +{ + if(subscribe(topic, (MessageReceivedCallback)NULL, qos)) + { + _topicSubscriptionList[_topicSubscriptionList.size()-1].caller = caller; + return true; + } + return false; +} + bool EspMQTTClient::unsubscribe(const String &topic) { // Do not try to unsubscribe if MQTT is not connected. diff --git a/src/EspMQTTClient.h b/src/EspMQTTClient.h index 5511bf5..f90475c 100644 --- a/src/EspMQTTClient.h +++ b/src/EspMQTTClient.h @@ -29,6 +29,17 @@ #endif +class EspMQTTClient; // forward declaration + +// Caller interface to inherit and call back object methods + +class EspMQTTCaller +{ +public: + virtual void cVoidCallback(){}; + virtual void cMessageReceivedCallback(const String &topicStr, const String &message){}; +}; + void onConnectionEstablished(); // MUST be implemented in your sketch. Called once everythings is connected (Wifi, mqtt). typedef std::function ConnectionEstablishedCallback; @@ -71,6 +82,8 @@ class EspMQTTClient String topic; MessageReceivedCallback callback; MessageReceivedCallbackWithTopic callbackWithTopic; + EspMQTTCaller *caller; + }; std::vector _topicSubscriptionList; @@ -154,6 +167,7 @@ class EspMQTTClient bool publish(const String &topic, const String &payload, bool retain = false); bool subscribe(const String &topic, MessageReceivedCallback messageReceivedCallback, uint8_t qos = 0); bool subscribe(const String &topic, MessageReceivedCallbackWithTopic messageReceivedCallback, uint8_t qos = 0); + bool subscribe(const String &topic, EspMQTTCaller *caller , uint8_t qos = 0); bool unsubscribe(const String &topic); //Unsubscribes from the topic, if it exists, and removes it from the CallbackList. void setKeepAlive(uint16_t keepAliveSeconds); // Change the keepalive interval (15 seconds by default) inline void setMqttClientName(const char* name) { _mqttClientName = name; }; // Allow to set client name manually (must be done in setup(), else it will not work.) From bcb9063248c4e006584fd6c80a9885479ccf1eac Mon Sep 17 00:00:00 2001 From: Den K Date: Sun, 19 Feb 2023 23:07:01 +0200 Subject: [PATCH 3/3] added the actual callback --- src/EspMQTTClient.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/EspMQTTClient.cpp b/src/EspMQTTClient.cpp index b770a83..2d87ea0 100644 --- a/src/EspMQTTClient.cpp +++ b/src/EspMQTTClient.cpp @@ -760,6 +760,8 @@ void EspMQTTClient::mqttMessageReceivedCallback(char* topic, uint8_t* payload, u _topicSubscriptionList[i].callback(payloadStr); // Call the callback if(_topicSubscriptionList[i].callbackWithTopic != NULL) _topicSubscriptionList[i].callbackWithTopic(topicStr, payloadStr); // Call the callback + if(_topicSubscriptionList[i].caller != NULL) + _topicSubscriptionList[i].caller->cMessageReceivedCallback(topicStr, payloadStr); // Call the callback } } }