remote variable set

This commit is contained in:
Siwat Sirichai 2024-01-16 12:48:06 +07:00
parent dbdb8eefbe
commit 9988075791
2 changed files with 36 additions and 2 deletions

View File

@ -8,18 +8,27 @@ RemoteVariable::~RemoteVariable() {
delete this->topic; delete this->topic;
delete this->value; 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->iot = iot;
this->size = size; this->size = size;
topic = (char*)calloc(size, sizeof(char)); topic = (char*)calloc(size, sizeof(char));
value = (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); auto bindedMqttCallback = std::bind(&RemoteVariable::mqtt_callback, this, std::placeholders::_1, std::placeholders::_2);
this->iot->registerMqttCallback(bindedMqttCallback); this->iot->registerMqttCallback(bindedMqttCallback);
auto bindedSubscribe = std::bind(&RemoteVariable::subscribe, this); auto bindedSubscribe = std::bind(&RemoteVariable::subscribe, this);
this->iot->registerSubscribeCallback(bindedSubscribe); this->iot->registerSubscribeCallback(bindedSubscribe);
} }
void RemoteVariable::begin(size_t size, char* topic, ESPMegaIoT* iot) {
this->begin(size, topic, iot, false, nullptr);
}
void RemoteVariable::subscribe() { void RemoteVariable::subscribe() {
this->iot->subscribe(this->topic); this->iot->subscribe(this->topic);
if(this->useValueRequest) {
this->publish_value_request();
}
} }
char* RemoteVariable::getValue() { char* RemoteVariable::getValue() {
return this->getValue(); return this->getValue();
@ -29,4 +38,21 @@ void RemoteVariable::mqtt_callback(char* topic, char* payload) {
if (strcmp(topic, this->topic) == 0) { if (strcmp(topic, this->topic) == 0) {
strcpy(this->value, payload); 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);
} }

View File

@ -6,12 +6,20 @@ class RemoteVariable
RemoteVariable(); RemoteVariable();
~RemoteVariable(); ~RemoteVariable();
void begin(size_t size, char* topic, ESPMegaIoT* iot); 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 subscribe();
void requestValue();
char* getValue(); char* getValue();
void mqtt_callback(char* topic, char* payload);
private: private:
void mqtt_callback(char* topic, char* payload);
ESPMegaIoT* iot; ESPMegaIoT* iot;
char* topic; char* topic;
char* value; char* value;
size_t size; size_t size;
bool useValueRequest;
char* valueRequestTopic;
bool useSetValue;
char* setValueTopic;
}; };