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) {
|
if (strcmp(topic, this->topic) == 0) {
|
||||||
ESP_LOGD("RemoteVariable", "Received MQTT message from %s", topic);
|
ESP_LOGD("RemoteVariable", "Received MQTT message from %s", topic);
|
||||||
strcpy(this->value, payload);
|
strcpy(this->value, payload);
|
||||||
|
for (auto& callback : this->valueChangeCallback) {
|
||||||
|
callback.second(this->value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,4 +133,12 @@ void RemoteVariable::setValue(const char* value) {
|
||||||
if(!this->useSetValue)
|
if(!this->useSetValue)
|
||||||
return;
|
return;
|
||||||
this->iot->publish(this->setValueTopic, value);
|
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
|
#pragma once
|
||||||
#include <ESPMegaIoT.hpp>
|
#include <ESPMegaIoT.hpp>
|
||||||
|
#include <map>
|
||||||
/**
|
/**
|
||||||
* @brief A class that create a variable that exists on other devices and can be accessed remotely
|
* @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 subscribe();
|
||||||
void requestValue();
|
void requestValue();
|
||||||
char* getValue();
|
char* getValue();
|
||||||
|
uint8_t registerCallback(std::function<void(char*)>);
|
||||||
|
void unregisterCallback(uint8_t handler);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void mqtt_callback(char* topic, char* payload);
|
void mqtt_callback(char* topic, char* payload);
|
||||||
ESPMegaIoT* iot;
|
ESPMegaIoT* iot;
|
||||||
|
@ -29,4 +33,6 @@ class RemoteVariable
|
||||||
const char* valueRequestTopic;
|
const char* valueRequestTopic;
|
||||||
bool useSetValue;
|
bool useSetValue;
|
||||||
const char* setValueTopic;
|
const char* setValueTopic;
|
||||||
|
uint8_t valueChangeCallbackCount;
|
||||||
|
std::map<uint8_t,std::function<void(char*)>> valueChangeCallback;
|
||||||
};
|
};
|
Loading…
Reference in New Issue