From 64666bbfb6668bce437ffd38f711d260c1adbe1a Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Mon, 20 Feb 2023 07:07:32 +0100 Subject: [PATCH] config: add new Configuration class to simplify handling json config update api --- lib/Figments/Input.h | 13 -------- src/Config.cpp | 44 ++++++++++++++++++++++++++ src/Config.h | 21 ++++++++++++ src/animations/Power.cpp | 16 +++------- src/animations/Power.h | 3 +- src/inputs/BPM.h | 12 +++---- src/platform/arduino/MQTTTelemetry.cpp | 15 ++++----- src/platform/arduino/MQTTTelemetry.h | 3 +- 8 files changed, 85 insertions(+), 42 deletions(-) diff --git a/lib/Figments/Input.h b/lib/Figments/Input.h index bfab017..1df8704 100644 --- a/lib/Figments/Input.h +++ b/lib/Figments/Input.h @@ -175,19 +175,6 @@ private: std::function m_func; }; -class ConfigTaskMixin : public virtual Loopable { - public: - void handleEvent(const InputEvent &evt) override { - if (evt.intent == InputEvent::ConfigurationChanged) { - handleConfigChange(evt); - } - } - - void loop() override {} - - virtual void handleConfigChange(const InputEvent& evt) {} -}; - class OnlineTaskMixin : public virtual Loopable { public: void handleEvent(const InputEvent &evt) override { diff --git a/src/Config.cpp b/src/Config.cpp index faa18a6..09148bd 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -9,6 +9,50 @@ #include #include +void +ConfigTaskMixin::handleEvent(const InputEvent &evt) +{ + if (evt.intent == InputEvent::ConfigurationChanged) { + const JsonObject& cfg = *evt.as(); + handleConfigChange(Configuration(cfg)); + } +} + +Configuration::Configuration(const JsonObject& data) + : m_json(data) +{ +} + +const char* +Configuration::get(const char* key, const char* defaultVal) const +{ + if (m_json.containsKey(key)) { + return m_json[key]; + } else { + return defaultVal; + } +} + +int +Configuration::get(const char* key, int defaultVal) const +{ + if (m_json.containsKey(key)) { + return m_json[key]; + } else { + return defaultVal; + } +} + +bool +Configuration::get(const char* key, bool defaultVal) const +{ + if (m_json.containsKey(key)) { + return m_json[key]; + } else { + return defaultVal; + } +} + StaticJsonDocument<1024> jsonConfig; constexpr uint16_t HardwareConfig::MAX_LED_NUM; diff --git a/src/Config.h b/src/Config.h index 2a36d69..d4e48bf 100644 --- a/src/Config.h +++ b/src/Config.h @@ -1,6 +1,27 @@ #pragma once #include #include "JsonCoordinateMapping.h" +#include + +class Configuration { + public: + Configuration(const JsonObject& data); + const char* get(const char* key, const char* defaultVal) const; + int get(const char* key, int defaultVal) const; + bool get(const char* key, bool defaultVal) const; + + private: + const JsonObject& m_json; +}; + +class ConfigTaskMixin : public virtual Loopable { + public: + void handleEvent(const InputEvent &evt) override; + + void loop() override {} + + virtual void handleConfigChange(const Configuration& config) {} +}; struct HardwareConfig { uint8_t version = 3; diff --git a/src/animations/Power.cpp b/src/animations/Power.cpp index 24a0044..6cd1fe1 100644 --- a/src/animations/Power.cpp +++ b/src/animations/Power.cpp @@ -3,18 +3,12 @@ #include void -Power::handleConfigChange(const InputEvent& event) +Power::handleConfigChange(const Configuration& config) { - const JsonObject& cfg = *event.as(); - if (cfg.containsKey("power.milliamps")) { - m_milliamps = cfg["power.milliamps"]; - } - if (cfg.containsKey("power.volts")) { - m_voltage = cfg["power.volts"]; - } - if (cfg.containsKey("power.useBPM")) { - m_useBPM = cfg["power.useBPM"]; - } + m_milliamps = config.get("power.milliamps", m_milliamps); + m_voltage = config.get("power.volts", m_voltage); + m_useBPM = config.get("power.useBPM", m_useBPM); + if (m_voltage == 0 || m_milliamps == 0) { Log.notice("power: Impossible power config: %dma @ %dv", m_milliamps, m_voltage); m_valid = false; diff --git a/src/animations/Power.h b/src/animations/Power.h index f05cd51..92c1fb3 100644 --- a/src/animations/Power.h +++ b/src/animations/Power.h @@ -1,5 +1,6 @@ #pragma once #include +#include "../Config.h" class Power: public Figment, ConfigTaskMixin { public: @@ -27,7 +28,7 @@ public: } } - void handleConfigChange(const InputEvent& event) override; + void handleConfigChange(const Configuration& config) override; void loop() override { ConfigTaskMixin::loop(); diff --git a/src/inputs/BPM.h b/src/inputs/BPM.h index df23c4c..464d4ec 100644 --- a/src/inputs/BPM.h +++ b/src/inputs/BPM.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include "../Config.h" class BPM : public InputSource, ConfigTaskMixin { public: @@ -22,13 +23,10 @@ public: ConfigTaskMixin::loop(); } - void handleConfigChange(const InputEvent& evt) override { - const JsonObject& cfg = *evt.as(); - if (cfg.containsKey("bpm.idle")) { - double requestedBPM = cfg["bpm.idle"]; - m_msPerBeat = 60000.0 / (double)requestedBPM; - Log.notice("bpm: idle BPM set to %u (requested %lf)", msToBPM(m_msPerBeat), requestedBPM); - } + void handleConfigChange(const Configuration& cfg) override { + double requestedBPM = cfg.get("bpm.idle", msToBPM(m_msPerBeat)); + m_msPerBeat = 60000.0 / (double)requestedBPM; + Log.notice("bpm: idle BPM set to %d (requested %d)", (int)msToBPM(m_msPerBeat), (int)requestedBPM); } InputEvent read() override { diff --git a/src/platform/arduino/MQTTTelemetry.cpp b/src/platform/arduino/MQTTTelemetry.cpp index 8722143..440d042 100644 --- a/src/platform/arduino/MQTTTelemetry.cpp +++ b/src/platform/arduino/MQTTTelemetry.cpp @@ -200,17 +200,14 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt) } void -MQTTTelemetry::handleConfigChange(const InputEvent& event) +MQTTTelemetry::handleConfigChange(const Configuration& cfg) { Log.notice("Config change in mqtt"); - const JsonObject* obj = event.as(); - if (obj->containsKey("mqtt.ip")) { - const char* newIP = (*obj)["mqtt.ip"].as().c_str(); - Log.notice("ip: %s", newIP); - strncpy(m_hostBuf, newIP, sizeof(m_hostBuf)); - m_hostBuf[sizeof(m_hostBuf)-1] = 0; - m_mqtt.disconnect(); - } + const char* newIP = cfg.get("mqtt.ip", m_hostBuf); + Log.notice("ip: %s", newIP); + strncpy(m_hostBuf, newIP, sizeof(m_hostBuf)); + m_hostBuf[sizeof(m_hostBuf)-1] = 0; + m_mqtt.disconnect(); } void diff --git a/src/platform/arduino/MQTTTelemetry.h b/src/platform/arduino/MQTTTelemetry.h index fe1541b..6a3ec29 100644 --- a/src/platform/arduino/MQTTTelemetry.h +++ b/src/platform/arduino/MQTTTelemetry.h @@ -3,6 +3,7 @@ #include #include +#include "../../Config.h" #include "../../Sequencer.h" @@ -47,7 +48,7 @@ class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin, ConfigTaskMix void handleEvent(const InputEvent& evt) override; void handleEventOnline(const InputEvent& evt) override; - void handleConfigChange(const InputEvent& evt) override; + void handleConfigChange(const Configuration& cfg) override; void loop() override; void loopOnline() override;