From 1b438fb14173073c32ab82a7e3e081641cddc4cb Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 15 May 2024 10:33:09 +0200 Subject: [PATCH] move config publishing inside Component --- gateway/include/ha.h | 14 ++++++++++++++ gateway/include/mqtt.h | 18 ++---------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/gateway/include/ha.h b/gateway/include/ha.h index 64296b6..8ee972d 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -92,6 +92,20 @@ namespace Ha { buildUniqueId(uniqueId); jsonDoc["unique_id"] = uniqueId; } + + void publishConfig() { + StaticJsonDocument jsonDoc; + buildConfig(jsonDoc); + + char message[JSON_SIZE]; + serializeJson(jsonDoc, message); + + publisher(configTopic, message); + } + + void publishCleanupConfig() { + publisher(configTopic, ""); + } }; List Component::components; diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index a3553ea..a10e07e 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -50,31 +50,17 @@ namespace Mqtt { OilSensorBuilder::build("7") }; - void publishComponentConfig(Component& component) { - StaticJsonDocument jsonDoc; - component.buildConfig(jsonDoc); - - char message[JSON_SIZE]; - serializeJson(jsonDoc, message); - - publish(component.configTopic, message); - } - - void publishCleanupComponentConfig(Component& component) { - publish(component.configTopic, ""); - } - void publishInit() { Ha::publisher = publish; List::exec(Component::components, [](Component* c) - { publishComponentConfig(*c); }); + { c->publishConfig(); }); AbstractBuilder::deleteAll(); ts.deleteTask(tPublishInit); } void publishCleanupConfig() { List::exec(Component::components, [](Component* c) - { publishCleanupComponentConfig(*c); }); + { c->publishCleanupConfig(); }); } void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {