remote variable set
This commit is contained in:
parent
dbdb8eefbe
commit
9988075791
|
@ -8,18 +8,27 @@ RemoteVariable::~RemoteVariable() {
|
|||
delete this->topic;
|
||||
delete this->value;
|
||||
}
|
||||
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot) {
|
||||
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot, bool useValueRequest, char* valueRequestTopic) {
|
||||
this->iot = iot;
|
||||
this->size = size;
|
||||
topic = (char*)calloc(size, sizeof(char));
|
||||
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);
|
||||
}
|
||||
|
||||
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot) {
|
||||
this->begin(size, topic, iot, false, nullptr);
|
||||
}
|
||||
|
||||
void RemoteVariable::subscribe() {
|
||||
this->iot->subscribe(this->topic);
|
||||
if(this->useValueRequest) {
|
||||
this->publish_value_request();
|
||||
}
|
||||
}
|
||||
char* RemoteVariable::getValue() {
|
||||
return this->getValue();
|
||||
|
@ -29,4 +38,21 @@ void RemoteVariable::mqtt_callback(char* topic, char* payload) {
|
|||
if (strcmp(topic, this->topic) == 0) {
|
||||
strcpy(this->value, payload);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteVariable::requestValue() {
|
||||
if(!this->useValueRequest)
|
||||
return;
|
||||
this->iot->publish(this->valueRequestTopic, "request");
|
||||
}
|
||||
|
||||
void RemoteVariable::enableSetValue(char* setValueTopic) {
|
||||
this->useSetValue = true;
|
||||
this->setValueTopic = setValueTopic;
|
||||
}
|
||||
|
||||
void RemoteVariable::setValue(char* value) {
|
||||
if(!this->useSetValue)
|
||||
return;
|
||||
this->iot->publish(this->setValueTopic, value);
|
||||
}
|
|
@ -6,12 +6,20 @@ class RemoteVariable
|
|||
RemoteVariable();
|
||||
~RemoteVariable();
|
||||
void begin(size_t size, char* topic, ESPMegaIoT* iot);
|
||||
void begin(size_t size, char* topic, ESPMegaIoT* iot, bool useValueRequest, char* valueRequestTopic);
|
||||
void setValue(char* value);
|
||||
void enableSetValue(char* setValueTopic);
|
||||
void subscribe();
|
||||
void requestValue();
|
||||
char* getValue();
|
||||
void mqtt_callback(char* topic, char* payload);
|
||||
private:
|
||||
void mqtt_callback(char* topic, char* payload);
|
||||
ESPMegaIoT* iot;
|
||||
char* topic;
|
||||
char* value;
|
||||
size_t size;
|
||||
bool useValueRequest;
|
||||
char* valueRequestTopic;
|
||||
bool useSetValue;
|
||||
char* setValueTopic;
|
||||
};
|
Loading…
Reference in New Issue