Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Caller interface to callback object methods. #127

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/MethodCallback/CallerInterface.h
Original file line number Diff line number Diff line change
@@ -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<EspMQTTCaller*>(this), 0);
}

Peripheral::Peripheral(EspMQTTClient *_client, String _topic): client(_client)
{}

void Peripheral::cMessageReceivedCallback(const String &topicStr, const String &message)
{
Serial.println(message);
}
14 changes: 13 additions & 1 deletion src/EspMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}
}
14 changes: 14 additions & 0 deletions src/EspMQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@

#endif

class EspMQTTClient; // forward declaration

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed considering that the class definition is 4 rows below?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, probably I just left it there from before. Works fine without it.


// 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<void()> ConnectionEstablishedCallback;
Expand Down Expand Up @@ -71,6 +82,8 @@ class EspMQTTClient
String topic;
MessageReceivedCallback callback;
MessageReceivedCallbackWithTopic callbackWithTopic;
EspMQTTCaller *caller;

};
std::vector<TopicSubscriptionRecord> _topicSubscriptionList;

Expand Down Expand Up @@ -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.)
Expand Down