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 diff --git a/src/EspMQTTClient.cpp b/src/EspMQTTClient.cpp index 3bcd5b0..2d87ea0 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. @@ -750,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 } } } 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.)