2024-01-16 04:22:41 +00:00
|
|
|
#include <RemoteVariable.hpp>
|
|
|
|
|
|
|
|
RemoteVariable::RemoteVariable() {
|
|
|
|
this->topic = nullptr;
|
|
|
|
this->value = nullptr;
|
|
|
|
}
|
|
|
|
RemoteVariable::~RemoteVariable() {
|
|
|
|
delete this->topic;
|
|
|
|
delete this->value;
|
|
|
|
}
|
2024-02-09 10:48:43 +00:00
|
|
|
void RemoteVariable::begin(size_t size, const char* topic, ESPMegaIoT* iot, bool useValueRequest, const char* valueRequestTopic) {
|
2024-01-16 04:22:41 +00:00
|
|
|
this->iot = iot;
|
|
|
|
this->size = size;
|
2024-02-09 10:48:43 +00:00
|
|
|
this->topic = topic;
|
|
|
|
this->useValueRequest = useValueRequest;
|
|
|
|
this->valueRequestTopic = valueRequestTopic;
|
2024-01-16 04:22:41 +00:00
|
|
|
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);
|
2024-02-09 10:48:43 +00:00
|
|
|
this->subscribe();
|
2024-01-16 04:22:41 +00:00
|
|
|
}
|
2024-01-16 05:48:06 +00:00
|
|
|
|
2024-02-09 10:48:43 +00:00
|
|
|
void RemoteVariable::begin(size_t size, const char* topic, ESPMegaIoT* iot) {
|
2024-01-16 05:48:06 +00:00
|
|
|
this->begin(size, topic, iot, false, nullptr);
|
|
|
|
}
|
|
|
|
|
2024-01-16 04:22:41 +00:00
|
|
|
void RemoteVariable::subscribe() {
|
2024-02-09 10:48:43 +00:00
|
|
|
ESP_LOGD("RemoteVariable", "Subscribing to %s", this->topic);
|
2024-01-16 04:22:41 +00:00
|
|
|
this->iot->subscribe(this->topic);
|
2024-01-16 05:48:06 +00:00
|
|
|
if(this->useValueRequest) {
|
2024-02-09 10:48:43 +00:00
|
|
|
ESP_LOGD("RemoteVariable", "Subscribing to %s", this->valueRequestTopic);
|
2024-02-05 16:47:53 +00:00
|
|
|
this->requestValue();
|
2024-01-16 05:48:06 +00:00
|
|
|
}
|
2024-01-16 04:22:41 +00:00
|
|
|
}
|
|
|
|
char* RemoteVariable::getValue() {
|
2024-02-09 10:48:43 +00:00
|
|
|
return this->value;
|
2024-01-16 04:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteVariable::mqtt_callback(char* topic, char* payload) {
|
|
|
|
if (strcmp(topic, this->topic) == 0) {
|
2024-02-09 10:48:43 +00:00
|
|
|
ESP_LOGD("RemoteVariable", "Received MQTT message from %s", topic);
|
2024-01-16 04:22:41 +00:00
|
|
|
strcpy(this->value, payload);
|
|
|
|
}
|
2024-01-16 05:48:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteVariable::requestValue() {
|
|
|
|
if(!this->useValueRequest)
|
|
|
|
return;
|
2024-02-09 10:48:43 +00:00
|
|
|
ESP_LOGD("RemoteVariable", "Sending request to %s", this->valueRequestTopic);
|
2024-01-16 05:48:06 +00:00
|
|
|
this->iot->publish(this->valueRequestTopic, "request");
|
|
|
|
}
|
|
|
|
|
2024-02-09 10:48:43 +00:00
|
|
|
void RemoteVariable::enableSetValue(const char* setValueTopic) {
|
2024-01-16 05:48:06 +00:00
|
|
|
this->useSetValue = true;
|
|
|
|
this->setValueTopic = setValueTopic;
|
|
|
|
}
|
|
|
|
|
2024-02-09 10:48:43 +00:00
|
|
|
void RemoteVariable::setValue(const char* value) {
|
2024-01-16 05:48:06 +00:00
|
|
|
if(!this->useSetValue)
|
|
|
|
return;
|
|
|
|
this->iot->publish(this->setValueTopic, value);
|
2024-01-16 04:22:41 +00:00
|
|
|
}
|