Connect the app to MQTT and show the connection status (iteration 2, step 4)

Dashboard creates the MQTT client and the HA device with the dummy
"Uptime" sensor (D41) in overlay and both desktop modes, connects in
started() and disconnects gracefully on aboutToQuit. The new platform
mode --desktop-novr behaves like --desktop for now (D22). The main tab
shows device, broker, connection state and last error (UI-1) through
the ConnectionStatus QML singleton.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 16:18:17 +02:00
co-authored by Claude Opus 5.5
parent 466743eff9
commit 943e90c1b1
15 changed files with 335 additions and 32 deletions
+48
View File
@@ -0,0 +1,48 @@
#include "ConnectionStatus.h"
#include <QQmlEngine>
namespace {
ConnectionStatus* g_instance = nullptr;
} // namespace
ConnectionStatus::ConnectionStatus(QObject* parent) : QObject(parent) {
Q_ASSERT(!g_instance);
g_instance = this;
}
ConnectionStatus::~ConnectionStatus() { g_instance = nullptr; }
void ConnectionStatus::setDevice(const QString& deviceName,
const QString& broker) {
m_configured = true;
m_deviceName = deviceName;
m_broker = broker;
emit changed();
}
void ConnectionStatus::setConnection(MqttClient::State state,
const QString& error) {
switch (state) {
case MqttClient::State::Disconnected:
m_state = State::Disconnected;
break;
case MqttClient::State::Connecting:
m_state = State::Connecting;
break;
case MqttClient::State::Connected:
m_state = State::Connected;
break;
}
if (!error.isEmpty()) m_lastError = error;
emit changed();
}
ConnectionStatus* ConnectionStatus::create(QQmlEngine*, QJSEngine*) {
Q_ASSERT(g_instance);
// Owned by the app, not by the QML engine.
QJSEngine::setObjectOwnership(g_instance, QJSEngine::CppOwnership);
return g_instance;
}