Skip to content

Commit

Permalink
mqtt-auto-discovery: Use event system instead of open coding it
Browse files Browse the repository at this point in the history
  • Loading branch information
photron committed Nov 26, 2024
1 parent 5c06250 commit 938633e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion software/src/modules/mqtt_auto_discovery/module.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[Dependencies]
Requires = Task Scheduler
Event Log
Event
API
Mqtt

Optional = System
Optional = System
10 changes: 10 additions & 0 deletions software/src/modules/mqtt_auto_discovery/mqtt_auto_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion software/src/modules/system/module.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ Requires = Task Scheduler
Optional = Evse Common
Users
Charge Tracker
Mqtt Auto Discovery
12 changes: 1 addition & 11 deletions software/src/modules/system/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Language>() != i18n_config.get("language")->asEnum<Language>()) {
mqtt_auto_discovery.reschedule_announce_next_topic();
}

return "";
}
#endif
};
})};
}

void System::setup()
Expand Down

0 comments on commit 938633e

Please sign in to comment.