diff --git a/main.qml b/main.qml index 4423868c..aedb3b63 100644 --- a/main.qml +++ b/main.qml @@ -192,11 +192,17 @@ Window { } onClicked: { - featureComingSoon.open() -// if (Network.isActive) -// Network.isActive = false -// else -// networkDialog.open() + if (Network.isActive) + Network.isActive = false + else + networkDialog.open() + } + } + + Connections { + target: Network + function onHealthCheckFailed(code) { + healthCheckFailed.open(); } } @@ -237,9 +243,9 @@ Window { } PopupDialog { - id: featureComingSoon + id: healthCheckFailed anchors.centerIn: parent - text: qsTr("Feature coming soon!") + text: qsTr("Connection to datalake failed.") } Button { @@ -278,12 +284,6 @@ Window { copyEdit.selectAll() copyEdit.copy() copyMessage.open() - timer.start() - } - Timer { - id: timer - interval: 500; running: false; repeat: false - onTriggered: copyMessage.close() } } diff --git a/network.cpp b/network.cpp index f5bc331d..cde7e170 100644 --- a/network.cpp +++ b/network.cpp @@ -24,11 +24,10 @@ Network::Network() { QSettings settings; settings.sync(); - m_isActive = settings.value("network/isActive", false).toBool(); m_uniqueId = settings.value("uniqueId", generateUniqueId()).toString(); settings.setValue("uniqueId", m_uniqueId); settings.sync(); - emit activeChanged(); + setActive(settings.value("network/isActive", false).toBool()); } void Network::setActive(bool b) @@ -38,6 +37,8 @@ void Network::setActive(bool b) settings.sync(); m_isActive = b; emit activeChanged(); + if (m_isActive) + sendHealth(); } QString Network::generateUniqueId() const @@ -101,8 +102,10 @@ void Network::handleJsonUploadFinished() int code = response.toInt(&ok); if (!ok) qWarning() << "ERROR: Invalid response."; - if (code != 200) + if (code != 200) { qWarning() << "ERROR: response != 200 code:" << code; + sendHealth(); + } QByteArray jsonData = jsonReply->readAll(); QJsonParseError err; @@ -124,3 +127,31 @@ bool Network::sendConversation(const QString &conversation) { return packageAndSendJson(conversation); } + +void Network::sendHealth() +{ + QUrl healthUrl("http://localhost/v1/health"); + QNetworkRequest request(healthUrl); + QNetworkReply *healthReply = m_networkManager.get(request); + connect(healthReply, &QNetworkReply::finished, this, &Network::handleHealthFinished); +} + +void Network::handleHealthFinished() +{ + QNetworkReply *healthReply = qobject_cast(sender()); + if (!healthReply) + return; + + QVariant response = healthReply->attribute(QNetworkRequest::HttpStatusCodeAttribute); + Q_ASSERT(response.isValid()); + bool ok; + int code = response.toInt(&ok); + if (!ok) + qWarning() << "ERROR: Invalid response."; + if (code != 200) { + qWarning() << "ERROR: response != 200 code:" << code; + emit healthCheckFailed(code); + setActive(false); + } + healthReply->deleteLater(); +} diff --git a/network.h b/network.h index bc5bd233..b2fa63a0 100644 --- a/network.h +++ b/network.h @@ -10,7 +10,6 @@ class Network : public QObject Q_OBJECT Q_PROPERTY(bool isActive READ isActive WRITE setActive NOTIFY activeChanged) public: - static Network *globalInstance(); bool isActive() const { return m_isActive; } @@ -21,11 +20,14 @@ public: Q_SIGNALS: void activeChanged(); + void healthCheckFailed(int code); private Q_SLOTS: + void handleHealthFinished(); void handleJsonUploadFinished(); private: + void sendHealth(); bool packageAndSendJson(const QString &json); private: diff --git a/qml/PopupDialog.qml b/qml/PopupDialog.qml index 60be8871..de45cc53 100644 --- a/qml/PopupDialog.qml +++ b/qml/PopupDialog.qml @@ -4,7 +4,7 @@ import QtQuick.Controls import QtQuick.Layouts Dialog { - id: copyMessage + id: popupDialog anchors.centerIn: parent modal: false opacity: 0.9 @@ -29,4 +29,14 @@ Dialog { exit: Transition { NumberAnimation { duration: 500; property: "opacity"; from: 1.0; to: 0.0 } } + + onOpened: { + timer.start() + } + + Timer { + id: timer + interval: 500; running: false; repeat: false + onTriggered: popupDialog.close() + } } \ No newline at end of file