diff --git a/libnymea-app/connection/nymeaconnection.cpp b/libnymea-app/connection/nymeaconnection.cpp index 3a97ced9..2cb3181e 100644 --- a/libnymea-app/connection/nymeaconnection.cpp +++ b/libnymea-app/connection/nymeaconnection.cpp @@ -70,6 +70,15 @@ NymeaConnection::NymeaConnection(QObject *parent) : QObject(parent) }); updateActiveBearers(); + + m_reconnectTimer.setInterval(1000); + m_reconnectTimer.setSingleShot(true); + connect(&m_reconnectTimer, &QTimer::timeout, this, [this](){ + if (m_currentHost) { + qCInfo(dcNymeaConnection()) << "Reconnecting..."; + connectInternal(m_currentHost); + } + }); } NymeaConnection::BearerTypes NymeaConnection::availableBearerTypes() const @@ -234,12 +243,7 @@ void NymeaConnection::onError(QAbstractSocket::SocketError error) emit connectionStatusChanged(); if (m_connectionStatus != ConnectionStatusSslUntrusted) { - QTimer::singleShot(1000, this, [this](){ - if (m_currentHost) { - qCInfo(dcNymeaConnection()) << "Reconnecting..."; - connectInternal(m_currentHost); - } - }); + m_reconnectTimer.start(); } } } diff --git a/libnymea-app/connection/nymeaconnection.h b/libnymea-app/connection/nymeaconnection.h index dd2ba6bf..b8987498 100644 --- a/libnymea-app/connection/nymeaconnection.h +++ b/libnymea-app/connection/nymeaconnection.h @@ -37,7 +37,7 @@ #include #include #include - +#include #include "nymeahost.h" @@ -139,6 +139,8 @@ private: NymeaTransportInterface *m_currentTransport = nullptr; NymeaHost *m_currentHost = nullptr; Connection *m_preferredConnection = nullptr; + + QTimer m_reconnectTimer; }; #endif // NYMEACONNECTION_H diff --git a/libnymea-app/connection/nymeahost.cpp b/libnymea-app/connection/nymeahost.cpp index 4004895f..80b6fb26 100644 --- a/libnymea-app/connection/nymeahost.cpp +++ b/libnymea-app/connection/nymeahost.cpp @@ -38,12 +38,15 @@ NymeaHost::NymeaHost(QObject *parent): { connect(m_connections, &Connections::dataChanged, this, [this](const QModelIndex &, const QModelIndex &, const QVector){ emit connectionChanged(); + syncOnlineState(); }); connect(m_connections, &Connections::connectionAdded, this, [this](Connection*){ emit connectionChanged(); + syncOnlineState(); }); connect(m_connections, &Connections::connectionRemoved, this, [this](Connection*){ emit connectionChanged(); + syncOnlineState(); }); } @@ -93,6 +96,28 @@ Connections* NymeaHost::connections() const return m_connections; } +bool NymeaHost::online() const +{ + return m_online; +} + +void NymeaHost::syncOnlineState() +{ + for (int i = 0; i < m_connections->rowCount(); i++) { + if (m_connections->get(i)->online()) { + if (!m_online) { + m_online = true; + emit onlineChanged(); + } + return; + } + } + if (m_online) { + m_online = false; + emit onlineChanged(); + } +} + Connections::Connections(QObject *parent): QAbstractListModel(parent) { diff --git a/libnymea-app/connection/nymeahost.h b/libnymea-app/connection/nymeahost.h index 7230a0d8..4620d1fe 100644 --- a/libnymea-app/connection/nymeahost.h +++ b/libnymea-app/connection/nymeahost.h @@ -137,6 +137,7 @@ class NymeaHost: public QObject Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(QString version READ version NOTIFY versionChanged) Q_PROPERTY(Connections* connections READ connections CONSTANT) + Q_PROPERTY(bool online READ online NOTIFY onlineChanged) public: explicit NymeaHost(QObject *parent = nullptr); @@ -153,16 +154,23 @@ public: Connections *connections() const; + bool online() const; + signals: void nameChanged(); void versionChanged(); void connectionChanged(); + void onlineChanged(); + +private: + void syncOnlineState(); private: QUuid m_uuid; QString m_name; QString m_version; Connections *m_connections = nullptr; + bool m_online = false; }; #endif // NYMEAHOST_H