remote variable tested and moved into production
This commit is contained in:
parent
df2543ad58
commit
3467c90e52
6 changed files with 49 additions and 27 deletions
|
@ -8,34 +8,39 @@ RemoteVariable::~RemoteVariable() {
|
|||
delete this->topic;
|
||||
delete this->value;
|
||||
}
|
||||
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot, bool useValueRequest, char* valueRequestTopic) {
|
||||
void RemoteVariable::begin(size_t size, const char* topic, ESPMegaIoT* iot, bool useValueRequest, const char* valueRequestTopic) {
|
||||
this->iot = iot;
|
||||
this->size = size;
|
||||
topic = (char*)calloc(size, sizeof(char));
|
||||
this->topic = topic;
|
||||
this->useValueRequest = useValueRequest;
|
||||
this->valueRequestTopic = valueRequestTopic;
|
||||
value = (char*)calloc(size, sizeof(char));
|
||||
this->valueRequestTopic = nullptr;
|
||||
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);
|
||||
this->subscribe();
|
||||
}
|
||||
|
||||
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot) {
|
||||
void RemoteVariable::begin(size_t size, const char* topic, ESPMegaIoT* iot) {
|
||||
this->begin(size, topic, iot, false, nullptr);
|
||||
}
|
||||
|
||||
void RemoteVariable::subscribe() {
|
||||
ESP_LOGD("RemoteVariable", "Subscribing to %s", this->topic);
|
||||
this->iot->subscribe(this->topic);
|
||||
if(this->useValueRequest) {
|
||||
ESP_LOGD("RemoteVariable", "Subscribing to %s", this->valueRequestTopic);
|
||||
this->requestValue();
|
||||
}
|
||||
}
|
||||
char* RemoteVariable::getValue() {
|
||||
return this->getValue();
|
||||
return this->value;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -43,15 +48,16 @@ void RemoteVariable::mqtt_callback(char* topic, char* payload) {
|
|||
void RemoteVariable::requestValue() {
|
||||
if(!this->useValueRequest)
|
||||
return;
|
||||
ESP_LOGD("RemoteVariable", "Sending request to %s", this->valueRequestTopic);
|
||||
this->iot->publish(this->valueRequestTopic, "request");
|
||||
}
|
||||
|
||||
void RemoteVariable::enableSetValue(char* setValueTopic) {
|
||||
void RemoteVariable::enableSetValue(const char* setValueTopic) {
|
||||
this->useSetValue = true;
|
||||
this->setValueTopic = setValueTopic;
|
||||
}
|
||||
|
||||
void RemoteVariable::setValue(char* value) {
|
||||
void RemoteVariable::setValue(const char* value) {
|
||||
if(!this->useSetValue)
|
||||
return;
|
||||
this->iot->publish(this->setValueTopic, value);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue