config: add new Configuration class to simplify handling json config update api

This commit is contained in:
Torrie Fischer 2023-02-20 07:07:32 +01:00
parent 53d5775c6a
commit 64666bbfb6
8 changed files with 85 additions and 42 deletions

View File

@ -175,19 +175,6 @@ private:
std::function<InputEvent(const InputEvent)> m_func; 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 { class OnlineTaskMixin : public virtual Loopable {
public: public:
void handleEvent(const InputEvent &evt) override { void handleEvent(const InputEvent &evt) override {

View File

@ -9,6 +9,50 @@
#include <LittleFS.h> #include <LittleFS.h>
#include <vector> #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; StaticJsonDocument<1024> jsonConfig;
constexpr uint16_t HardwareConfig::MAX_LED_NUM; constexpr uint16_t HardwareConfig::MAX_LED_NUM;

View File

@ -1,6 +1,27 @@
#pragma once #pragma once
#include <Figments.h> #include <Figments.h>
#include "JsonCoordinateMapping.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 { struct HardwareConfig {
uint8_t version = 3; uint8_t version = 3;

View File

@ -3,18 +3,12 @@
#include <ArduinoJson.h> #include <ArduinoJson.h>
void void
Power::handleConfigChange(const InputEvent& event) Power::handleConfigChange(const Configuration& config)
{ {
const JsonObject& cfg = *event.as<JsonObject>(); m_milliamps = config.get("power.milliamps", m_milliamps);
if (cfg.containsKey("power.milliamps")) { m_voltage = config.get("power.volts", m_voltage);
m_milliamps = cfg["power.milliamps"]; m_useBPM = config.get("power.useBPM", m_useBPM);
}
if (cfg.containsKey("power.volts")) {
m_voltage = cfg["power.volts"];
}
if (cfg.containsKey("power.useBPM")) {
m_useBPM = cfg["power.useBPM"];
}
if (m_voltage == 0 || m_milliamps == 0) { if (m_voltage == 0 || m_milliamps == 0) {
Log.notice("power: Impossible power config: %dma @ %dv", m_milliamps, m_voltage); Log.notice("power: Impossible power config: %dma @ %dv", m_milliamps, m_voltage);
m_valid = false; m_valid = false;

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <Figments.h> #include <Figments.h>
#include "../Config.h"
class Power: public Figment, ConfigTaskMixin { class Power: public Figment, ConfigTaskMixin {
public: public:
@ -27,7 +28,7 @@ public:
} }
} }
void handleConfigChange(const InputEvent& event) override; void handleConfigChange(const Configuration& config) override;
void loop() override { void loop() override {
ConfigTaskMixin::loop(); ConfigTaskMixin::loop();

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <Figments.h> #include <Figments.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include "../Config.h"
class BPM : public InputSource, ConfigTaskMixin { class BPM : public InputSource, ConfigTaskMixin {
public: public:
@ -22,13 +23,10 @@ public:
ConfigTaskMixin::loop(); ConfigTaskMixin::loop();
} }
void handleConfigChange(const InputEvent& evt) override { void handleConfigChange(const Configuration& cfg) override {
const JsonObject& cfg = *evt.as<JsonObject>(); double requestedBPM = cfg.get("bpm.idle", msToBPM(m_msPerBeat));
if (cfg.containsKey("bpm.idle")) {
double requestedBPM = cfg["bpm.idle"];
m_msPerBeat = 60000.0 / (double)requestedBPM; 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 { InputEvent read() override {

View File

@ -200,18 +200,15 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
} }
void void
MQTTTelemetry::handleConfigChange(const InputEvent& event) MQTTTelemetry::handleConfigChange(const Configuration& cfg)
{ {
Log.notice("Config change in mqtt"); Log.notice("Config change in mqtt");
const JsonObject* obj = event.as<JsonObject>(); const char* newIP = cfg.get("mqtt.ip", m_hostBuf);
if (obj->containsKey("mqtt.ip")) {
const char* newIP = (*obj)["mqtt.ip"].as<JsonString>().c_str();
Log.notice("ip: %s", newIP); Log.notice("ip: %s", newIP);
strncpy(m_hostBuf, newIP, sizeof(m_hostBuf)); strncpy(m_hostBuf, newIP, sizeof(m_hostBuf));
m_hostBuf[sizeof(m_hostBuf)-1] = 0; m_hostBuf[sizeof(m_hostBuf)-1] = 0;
m_mqtt.disconnect(); m_mqtt.disconnect();
} }
}
void void
MQTTTelemetry::loop() MQTTTelemetry::loop()

View File

@ -3,6 +3,7 @@
#include <PubSubClient.h> #include <PubSubClient.h>
#include <ArduinoLog.h> #include <ArduinoLog.h>
#include "../../Config.h"
#include "../../Sequencer.h" #include "../../Sequencer.h"
@ -47,7 +48,7 @@ class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin, ConfigTaskMix
void handleEvent(const InputEvent& evt) override; void handleEvent(const InputEvent& evt) override;
void handleEventOnline(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 loop() override;
void loopOnline() override; void loopOnline() override;