diff --git a/data/profiles/home-lighting.json b/data/profiles/home-lighting.json index 5b038e9..b18aa3e 100644 --- a/data/profiles/home-lighting.json +++ b/data/profiles/home-lighting.json @@ -13,5 +13,8 @@ "Acid": ["Chimes", "Pulse", "MPU5060", "IdleColors", "Rainbow"], "Flashlight": ["Flashlight"] }, - "surfaceMap": "default" + "surfaceMap": "default", + "defaults": { + "mqtt.ip": "10.0.0.2" + } } diff --git a/src/Config.cpp b/src/Config.cpp index 93ed57f..a8b6907 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -146,6 +146,9 @@ ConfigService::loadProfile(const char* profileName) Log.warning("config: Could not load profile %s!", profileName); } + JsonObject defaults = jsonConfig["defaults"]; + MainLoop::instance()->dispatch(InputEvent{InputEvent::ConfigurationChanged, &defaults}); + String configName = jsonConfig["surfaceMap"]; jsonConfig.clear(); loadMap(configName); diff --git a/src/Config.h b/src/Config.h index 63f3e5a..058f59a 100644 --- a/src/Config.h +++ b/src/Config.h @@ -37,6 +37,7 @@ struct ConfigService: public Task { const CoordinateMapping* coordMap() const { return &m_jsonMap; } const char* loadedProfile() const; void overrideProfile(const char* profileName); + const char* getConfigValue(const char* key) const; private: HardwareConfig m_config; diff --git a/src/platform/arduino/MQTTTelemetry.cpp b/src/platform/arduino/MQTTTelemetry.cpp index 065f400..00eaedb 100644 --- a/src/platform/arduino/MQTTTelemetry.cpp +++ b/src/platform/arduino/MQTTTelemetry.cpp @@ -198,20 +198,27 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt) } } +void +MQTTTelemetry::handleConfigChange(const InputEvent& event) +{ + const JsonObject* obj = static_cast(event.value().asPointer()); + strncpy(m_hostBuf, obj["mqtt.ip"].c_str(), sizeof(m_hostBuf)); + m_mqtt.disconnect(); +} + void MQTTTelemetry::loop() { BufferedInputSource::loop(); OnlineTaskMixin::loop(); + ConfigTaskMixin::loop(); } void MQTTTelemetry::onOnline() { - const IPAddress server(10, 0, 0, 2); - m_needHeartbeat = true; - m_mqtt.setServer(server, 1883); + m_mqtt.setServer(m_hostBuf, 1883); m_mqtt.setBufferSize(1024); m_mqtt.setCallback(&MQTTTelemetry::s_callback); } diff --git a/src/platform/arduino/MQTTTelemetry.h b/src/platform/arduino/MQTTTelemetry.h index 12dc17d..692f325 100644 --- a/src/platform/arduino/MQTTTelemetry.h +++ b/src/platform/arduino/MQTTTelemetry.h @@ -12,7 +12,7 @@ #include #endif -class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin { +class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin, ConfigTaskMixin { public: MQTTTelemetry(); void setSequencer(Sequencer* seq) { m_sequencer = seq; } @@ -46,6 +46,7 @@ class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin { } void handleEventOnline(const InputEvent& evt) override; + void handleConfigChange(const InputEvent& evt) override; void loop() override; void loopOnline() override; @@ -65,6 +66,7 @@ class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin { bool m_isOn = true; static char s_topicBuf[128]; static char s_payloadBuf[512]; + static char s_hostBuf[15]; void publishDoc(const char* topic); void publishDoc(const char* topic, bool retain);