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