From 6a287007d7aa9c48d1deb6d311ac6d640dd9fb02 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Wed, 15 May 2024 10:19:03 +0200 Subject: [PATCH] unify execution of containers inside the list --- gateway/include/mqtt.h | 18 ++++++++++-------- gateway/include/utils.h | 6 ++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/gateway/include/mqtt.h b/gateway/include/mqtt.h index 6698770..fecea33 100644 --- a/gateway/include/mqtt.h +++ b/gateway/include/mqtt.h @@ -45,12 +45,12 @@ namespace Mqtt { (new PollinSwitch{"Train", "11111", 4, "Playroom"})->withStateTopic() }; - Ha::Sensor* sensors[] = { + Sensor* sensors[] = { OilTankRoomSensorBuilder::build("4"), OilSensorBuilder::build("7") }; - void publishComponentConfig(Ha::Component& component) { + void publishComponentConfig(Component& component) { StaticJsonDocument jsonDoc; component.buildConfig(jsonDoc); @@ -60,18 +60,20 @@ namespace Mqtt { publish(component.configTopic, message); } + void publishCleanupComponentConfig(Component& component) { + publish(component.configTopic, ""); + } + void publishInit() { Ha::publisher = publish; - for (List::Container* c = Ha::Component::components.first; c; c = c->next) { - publishComponentConfig(*c->t); - } + List::exec(Component::components, [](Component* c) + { publishComponentConfig(*c); }); ts.deleteTask(tPublishInit); } void publishCleanupConfig() { - for (List::Container* c = Ha::Component::components.first; c; c = c->next) { - publish(c->t->configTopic, ""); - } + List::exec(Component::components, [](Component* c) + { publishCleanupComponentConfig(*c); }); } void onMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { diff --git a/gateway/include/utils.h b/gateway/include/utils.h index 0c0bbe5..b7d4076 100644 --- a/gateway/include/utils.h +++ b/gateway/include/utils.h @@ -17,4 +17,10 @@ struct List { last = c; } + static void exec(List list, void(*f)(T*)) { + for (List::Container *c = list.first; c; c = c->next) { + f(c->t); + } + } + };