Remote Variable

This commit is contained in:
Siwat Sirichai 2024-01-16 11:22:41 +07:00
parent a2fe4a3d67
commit dbdb8eefbe
5 changed files with 49 additions and 1492 deletions

View file

@ -0,0 +1,32 @@
#include <RemoteVariable.hpp>
RemoteVariable::RemoteVariable() {
this->topic = nullptr;
this->value = nullptr;
}
RemoteVariable::~RemoteVariable() {
delete this->topic;
delete this->value;
}
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot) {
this->iot = iot;
this->size = size;
topic = (char*)calloc(size, sizeof(char));
value = (char*)calloc(size, sizeof(char));
auto bindedMqttCallback = std::bind(&RemoteVariable::mqtt_callback, this, std::placeholders::_1, std::placeholders::_2);
this->iot->registerMqttCallback(bindedMqttCallback);
auto bindedSubscribe = std::bind(&RemoteVariable::subscribe, this);
this->iot->registerSubscribeCallback(bindedSubscribe);
}
void RemoteVariable::subscribe() {
this->iot->subscribe(this->topic);
}
char* RemoteVariable::getValue() {
return this->getValue();
}
void RemoteVariable::mqtt_callback(char* topic, char* payload) {
if (strcmp(topic, this->topic) == 0) {
strcpy(this->value, payload);
}
}