From 938633ee61120b2576aa9ffcdfe3c834d392c2e1 Mon Sep 17 00:00:00 2001 From: Matthias Bolte Date: Tue, 26 Nov 2024 19:01:11 +0100 Subject: [PATCH] mqtt-auto-discovery: Use event system instead of open coding it --- software/src/modules/mqtt_auto_discovery/module.ini | 3 ++- .../mqtt_auto_discovery/mqtt_auto_discovery.cpp | 10 ++++++++++ .../mqtt_auto_discovery/mqtt_auto_discovery.h | 1 + software/src/modules/system/module.ini | 1 - software/src/modules/system/system.cpp | 12 +----------- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/software/src/modules/mqtt_auto_discovery/module.ini b/software/src/modules/mqtt_auto_discovery/module.ini index c18f34f98..acf606e2f 100644 --- a/software/src/modules/mqtt_auto_discovery/module.ini +++ b/software/src/modules/mqtt_auto_discovery/module.ini @@ -1,7 +1,8 @@ [Dependencies] Requires = Task Scheduler Event Log + Event API Mqtt -Optional = System \ No newline at end of file +Optional = System diff --git a/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.cpp b/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.cpp index 2b12cd1f2..cf6755716 100644 --- a/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.cpp +++ b/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.cpp @@ -82,6 +82,16 @@ void MqttAutoDiscovery::register_urls() api.addPersistentConfig("mqtt/auto_discovery_config", &config); } +void MqttAutoDiscovery::register_events() +{ +#if MODULE_SYSTEM_AVAILABLE() + event.registerEvent("system/i18n_config", {"language"}, [this](const Config */*language*/) { + reschedule_announce_next_topic(); + return EventResult::OK; + }); +#endif +} + void MqttAutoDiscovery::prepare_topics() { const String &auto_discovery_prefix = config_in_use.get("auto_discovery_prefix")->asString(); diff --git a/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.h b/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.h index f62ff3f43..168a7e68d 100644 --- a/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.h +++ b/software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.h @@ -31,6 +31,7 @@ class MqttAutoDiscovery final : public IModule void pre_setup() override; void setup() override; void register_urls() override; + void register_events() override; ConfigRoot config; ConfigRoot config_in_use; diff --git a/software/src/modules/system/module.ini b/software/src/modules/system/module.ini index 0d3dd06c0..b67726af6 100644 --- a/software/src/modules/system/module.ini +++ b/software/src/modules/system/module.ini @@ -7,4 +7,3 @@ Requires = Task Scheduler Optional = Evse Common Users Charge Tracker - Mqtt Auto Discovery diff --git a/software/src/modules/system/system.cpp b/software/src/modules/system/system.cpp index 92c6ef0ae..1da8c82dc 100644 --- a/software/src/modules/system/system.cpp +++ b/software/src/modules/system/system.cpp @@ -78,17 +78,7 @@ void System::pre_setup() i18n_config = ConfigRoot{Config::Object({ {"language", Config::Enum(Language::German, Language::German, Language::English)}, {"detect_browser_language", Config::Bool(true)} - }) -#if MODULE_MQTT_AUTO_DISCOVERY_AVAILABLE() - , [this](Config &update, ConfigSource source) -> String { - if (update.get("language")->asEnum() != i18n_config.get("language")->asEnum()) { - mqtt_auto_discovery.reschedule_announce_next_topic(); - } - - return ""; - } -#endif - }; + })}; } void System::setup()