remoteVar Callbacks

This commit is contained in:
Siwat Sirichai 2024-02-12 16:40:46 +07:00
parent becdf35a27
commit 57dfa30cf0
2 changed files with 17 additions and 0 deletions

View file

@ -95,6 +95,9 @@ 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);
for (auto& callback : this->valueChangeCallback) {
callback.second(this->value);
}
}
}
@ -130,4 +133,12 @@ void RemoteVariable::setValue(const char* value) {
if(!this->useSetValue)
return;
this->iot->publish(this->setValueTopic, value);
}
uint8_t RemoteVariable::registerCallback(std::function<void(char*)> callback) {
this->valueChangeCallback[this->valueChangeCallbackCount] = callback;
return valueChangeCallbackCount++;
}
void RemoteVariable::unregisterCallback(uint8_t handler) {
this->valueChangeCallback.erase(handler);
}