remoteVar Callbacks
This commit is contained in:
parent
becdf35a27
commit
57dfa30cf0
|
@ -95,6 +95,9 @@ void RemoteVariable::mqtt_callback(char* topic, char* payload) {
|
|||
if (strcmp(topic, this->topic) == 0) {
|
||||
ESP_LOGD("RemoteVariable", "Received MQTT message from %s", topic);
|
||||
strcpy(this->value, payload);
|
||||
for (auto& callback : this->valueChangeCallback) {
|
||||
callback.second(this->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,3 +134,11 @@ void RemoteVariable::setValue(const char* value) {
|
|||
return;
|
||||
this->iot->publish(this->setValueTopic, value);
|
||||
}
|
||||
|
||||
uint8_t RemoteVariable::registerCallback(std::function<void(char*)> callback) {
|
||||
this->valueChangeCallback[this->valueChangeCallbackCount] = callback;
|
||||
return valueChangeCallbackCount++;
|
||||
}
|
||||
void RemoteVariable::unregisterCallback(uint8_t handler) {
|
||||
this->valueChangeCallback.erase(handler);
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
#include <ESPMegaIoT.hpp>
|
||||
#include <map>
|
||||
/**
|
||||
* @brief A class that create a variable that exists on other devices and can be accessed remotely
|
||||
*
|
||||
|
@ -19,6 +20,9 @@ class RemoteVariable
|
|||
void subscribe();
|
||||
void requestValue();
|
||||
char* getValue();
|
||||
uint8_t registerCallback(std::function<void(char*)>);
|
||||
void unregisterCallback(uint8_t handler);
|
||||
|
||||
private:
|
||||
void mqtt_callback(char* topic, char* payload);
|
||||
ESPMegaIoT* iot;
|
||||
|
@ -29,4 +33,6 @@ class RemoteVariable
|
|||
const char* valueRequestTopic;
|
||||
bool useSetValue;
|
||||
const char* setValueTopic;
|
||||
uint8_t valueChangeCallbackCount;
|
||||
std::map<uint8_t,std::function<void(char*)>> valueChangeCallback;
|
||||
};
|
Loading…
Reference in New Issue