config: add new Configuration class to simplify handling json config update api
This commit is contained in:
		| @@ -175,19 +175,6 @@ private: | ||||
|     std::function<InputEvent(const InputEvent)> 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 { | ||||
|   | ||||
| @@ -9,6 +9,50 @@ | ||||
| #include <LittleFS.h> | ||||
| #include <vector> | ||||
|  | ||||
| void | ||||
| ConfigTaskMixin::handleEvent(const InputEvent &evt) | ||||
| { | ||||
|   if (evt.intent == InputEvent::ConfigurationChanged) { | ||||
|       const JsonObject& cfg = *evt.as<JsonObject>(); | ||||
|       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; | ||||
|   | ||||
							
								
								
									
										21
									
								
								src/Config.h
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/Config.h
									
									
									
									
									
								
							| @@ -1,6 +1,27 @@ | ||||
| #pragma once | ||||
| #include <Figments.h> | ||||
| #include "JsonCoordinateMapping.h" | ||||
| #include <ArduinoJson.h> | ||||
|  | ||||
| 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; | ||||
|   | ||||
| @@ -3,18 +3,12 @@ | ||||
| #include <ArduinoJson.h> | ||||
|  | ||||
| void | ||||
| Power::handleConfigChange(const InputEvent& event) | ||||
| Power::handleConfigChange(const Configuration& config) | ||||
| { | ||||
|     const JsonObject& cfg = *event.as<JsonObject>(); | ||||
|     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; | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| #pragma once | ||||
| #include <Figments.h> | ||||
| #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(); | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| #pragma once | ||||
| #include <Figments.h> | ||||
| #include <ArduinoJson.h> | ||||
| #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<JsonObject>(); | ||||
|     if (cfg.containsKey("bpm.idle")) { | ||||
|         double requestedBPM = cfg["bpm.idle"]; | ||||
|   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 %u (requested %lf)", msToBPM(m_msPerBeat), requestedBPM); | ||||
|     } | ||||
|     Log.notice("bpm: idle BPM set to %d (requested %d)", (int)msToBPM(m_msPerBeat), (int)requestedBPM); | ||||
|   } | ||||
|  | ||||
|   InputEvent read() override { | ||||
|   | ||||
| @@ -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<JsonObject>(); | ||||
|   if (obj->containsKey("mqtt.ip")) { | ||||
|     const char* newIP = (*obj)["mqtt.ip"].as<JsonString>().c_str(); | ||||
|   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 | ||||
|   | ||||
| @@ -3,6 +3,7 @@ | ||||
|  | ||||
| #include <PubSubClient.h> | ||||
| #include <ArduinoLog.h> | ||||
| #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; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user