platform: arduino: mqtt: allow configuration through json

This commit is contained in:
Torrie Fischer 2023-02-19 18:43:26 +01:00
parent f9add1f684
commit d89630a340

View File

@ -108,6 +108,7 @@ MQTTTelemetry::MQTTTelemetry() : BufferedInputSource("MQTT"),
m_logPrinter(this) m_logPrinter(this)
{ {
m_debugTopic = String("renderbug/") + Platform::deviceID(); m_debugTopic = String("renderbug/") + Platform::deviceID();
memset(m_hostBuf, 0, sizeof(m_hostBuf));
} }
void void
@ -201,10 +202,16 @@ MQTTTelemetry::handleEventOnline(const InputEvent& evt)
void void
MQTTTelemetry::handleConfigChange(const InputEvent& event) MQTTTelemetry::handleConfigChange(const InputEvent& event)
{ {
const JsonObject& obj = event.as<JsonObject>(); Log.notice("Config change in mqtt");
strncpy(m_hostBuf, obj["mqtt.ip"].as<JsonString>().c_str(), sizeof(m_hostBuf)); const JsonObject* obj = event.as<JsonObject>();
if (obj->containsKey("mqtt.ip")) {
const char* newIP = (*obj)["mqtt.ip"].as<JsonString>().c_str();
Log.notice("ip: %s", newIP);
strncpy(m_hostBuf, newIP, sizeof(m_hostBuf));
m_hostBuf[sizeof(m_hostBuf)-1] = 0;
m_mqtt.disconnect(); m_mqtt.disconnect();
} }
}
void void
MQTTTelemetry::loop() MQTTTelemetry::loop()
@ -217,6 +224,7 @@ MQTTTelemetry::loop()
void void
MQTTTelemetry::handleEvent(const InputEvent& evt) MQTTTelemetry::handleEvent(const InputEvent& evt)
{ {
BufferedInputSource::handleEvent(evt);
OnlineTaskMixin::handleEvent(evt); OnlineTaskMixin::handleEvent(evt);
ConfigTaskMixin::handleEvent(evt); ConfigTaskMixin::handleEvent(evt);
} }
@ -225,7 +233,11 @@ void
MQTTTelemetry::onOnline() MQTTTelemetry::onOnline()
{ {
m_needHeartbeat = true; m_needHeartbeat = true;
if (strlen(m_hostBuf) > 0) {
m_mqtt.setServer(m_hostBuf, 1883); m_mqtt.setServer(m_hostBuf, 1883);
} else {
Log.warning("mqtt.ip is not configured, no connection possible");
}
m_mqtt.setBufferSize(1024); m_mqtt.setBufferSize(1024);
m_mqtt.setCallback(&MQTTTelemetry::s_callback); m_mqtt.setCallback(&MQTTTelemetry::s_callback);
} }