From 24aca10d09c0d8148ee3e443591df0b8e3cfc08b Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 2 Jul 2018 17:55:09 +0200 Subject: [PATCH] alow using hostnames for the manual connection and improve connection a bit --- libnymea-app-core/devices.cpp | 3 +++ libnymea-app-core/jsonrpc/jsonrpcclient.cpp | 6 ++--- libnymea-common/types/plugins.cpp | 3 +++ nymea-app/ui/ConnectPage.qml | 29 ++++++++++++++++++--- nymea-app/ui/Nymea.qml | 11 -------- tests/testrunner/testrunner.pro | 2 +- 6 files changed, 36 insertions(+), 18 deletions(-) diff --git a/libnymea-app-core/devices.cpp b/libnymea-app-core/devices.cpp index 365f0d4c..9100f159 100644 --- a/libnymea-app-core/devices.cpp +++ b/libnymea-app-core/devices.cpp @@ -37,6 +37,9 @@ QList Devices::devices() Device *Devices::get(int index) const { + if (index < 0 || index >= m_devices.count()) { + return nullptr; + } return m_devices.at(index); } diff --git a/libnymea-app-core/jsonrpc/jsonrpcclient.cpp b/libnymea-app-core/jsonrpc/jsonrpcclient.cpp index dbc8ae79..91773949 100644 --- a/libnymea-app-core/jsonrpc/jsonrpcclient.cpp +++ b/libnymea-app-core/jsonrpc/jsonrpcclient.cpp @@ -244,7 +244,7 @@ void JsonRpcClient::sendRequest(const QVariantMap &request) QVariantMap newRequest = request; newRequest.insert("token", m_token); // qDebug() << "Sending request" << qUtf8Printable(QJsonDocument::fromVariant(newRequest).toJson()); - m_connection->sendData(QJsonDocument::fromVariant(newRequest).toJson()); + m_connection->sendData(QJsonDocument::fromVariant(newRequest).toJson(QJsonDocument::Compact) + "\n"); } void JsonRpcClient::onInterfaceConnectedChanged(bool connected) @@ -259,6 +259,7 @@ void JsonRpcClient::onInterfaceConnectedChanged(bool connected) } else { QVariantMap request; request.insert("id", 0); + qDebug() << "Connected. Starting JSONRPC Handshake"; request.insert("method", "JSONRPC.Hello"); sendRequest(request); } @@ -275,7 +276,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data) QJsonParseError error; QJsonDocument jsonDoc = QJsonDocument::fromJson(m_receiveBuffer.left(splitIndex), &error); if (error.error != QJsonParseError::NoError) { - // qWarning() << "Could not parse json data from nymea" << data << error.errorString(); +// qWarning() << "Could not parse json data from nymea" << m_receiveBuffer.left(splitIndex) << error.errorString(); return; } // qDebug() << "received response" << m_receiveBuffer.left(splitIndex); @@ -286,7 +287,6 @@ void JsonRpcClient::dataReceived(const QByteArray &data) QVariantMap dataMap = jsonDoc.toVariant().toMap(); - // Check if this is the initial handshake if (dataMap.value("id").toInt() == 0 && dataMap.contains("params") && !dataMap.contains("notification")) { dataMap = dataMap.value("params").toMap(); diff --git a/libnymea-common/types/plugins.cpp b/libnymea-common/types/plugins.cpp index 214dcb22..657d382e 100644 --- a/libnymea-common/types/plugins.cpp +++ b/libnymea-common/types/plugins.cpp @@ -42,6 +42,9 @@ int Plugins::count() const Plugin *Plugins::get(int index) const { + if (index < 0 || index >= m_plugins.count()) { + return nullptr; + } return m_plugins.at(index); } diff --git a/nymea-app/ui/ConnectPage.qml b/nymea-app/ui/ConnectPage.qml index a70d1f2b..0014e34d 100644 --- a/nymea-app/ui/ConnectPage.qml +++ b/nymea-app/ui/ConnectPage.qml @@ -20,6 +20,12 @@ Page { } } + NymeaDiscovery { + id: discovery + objectName: "discovery" + discovering: pageStack.currentItem.objectName === "discoveryPage" + } + Connections { target: Engine.connection onVerifyConnectionCertificate: { @@ -37,7 +43,19 @@ Page { case "SslHandshakeFailedError": // silently ignore. They'll be handled by the SSL logic return; + case "HostNotFoundError": + errorMessage = qsTr("The %1 box could not be found on this address. Please make sure you entered the address correctly and that the box is powered on.").arg(app.systemName); + break; + case "NetworkError": + errorMessage = qsTr("It seems you're not connected to the network."); + break; + case "RemoteHostClosedError": + errorMessage = qsTr("The %1 box has closed the connection. This probably means it has been turned off or restarted.").arg(app.systemName); + break; + default: + errorMessage = qsTr("Un unknown error happened. We're very sorry for that. (Error code: %1)").arg(error); } + print("opening ErrorDialog with message:", errorMessage, error) var comp = Qt.createComponent(Qt.resolvedUrl("components/ErrorDialog.qml")) var popup = comp.createObject(app, {text: errorMessage}) popup.open() @@ -45,6 +63,12 @@ Page { pageStack.pop(root) pageStack.push(discoveryPage) } + onConnectedChanged: { + if (!connected) { + pageStack.pop(root) + pageStack.push(discoveryPage) + } + } } Component { @@ -89,7 +113,7 @@ Page { text: qsTr("Demo mode") onTriggered: { pageStack.push(connectingPage) - Engine.connection.connect("nymea://83.169.2.242:2222") + Engine.connection.connect("nymea://nymea.nymea.io:2222") } } @@ -258,7 +282,7 @@ Page { text: qsTr("Demo mode (online)") onClicked: { pageStack.push(connectingPage) - Engine.connection.connect("nymea://83.169.2.242:2222") + Engine.connection.connect("nymea://nymea.nymea.io:2222") } } @@ -315,7 +339,6 @@ Page { objectName: "addressTextInput" Layout.fillWidth: true placeholderText: "127.0.0.1" - validator: RegExpValidator { regExp: /^((?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){0,3}(?:[0-1]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$/ } } Label { text: qsTr("Port:") } diff --git a/nymea-app/ui/Nymea.qml b/nymea-app/ui/Nymea.qml index b40eafda..7d30812f 100644 --- a/nymea-app/ui/Nymea.qml +++ b/nymea-app/ui/Nymea.qml @@ -36,7 +36,6 @@ ApplicationWindow { Component.onCompleted: { pageStack.push(Qt.resolvedUrl("ConnectPage.qml")) - discovery.discovering = true } Connections { @@ -70,11 +69,8 @@ ApplicationWindow { function init() { print("calling init. Auth required:", Engine.jsonRpcClient.authenticationRequired, "initial setup required:", Engine.jsonRpcClient.initialSetupRequired, "jsonrpc connected:", Engine.jsonRpcClient.connected) pageStack.clear() - discovery.discovering = false; if (!Engine.connection.connected) { pageStack.push(Qt.resolvedUrl("ConnectPage.qml")) - print("starting discovery") - discovery.discovering = true; return; } @@ -99,8 +95,6 @@ ApplicationWindow { pageStack.push(Qt.resolvedUrl("MainPage.qml")) } else { pageStack.push(Qt.resolvedUrl("ConnectPage.qml")) - print("starting discovery") - discovery.discovering = true; } } @@ -129,11 +123,6 @@ ApplicationWindow { } } - NymeaDiscovery { - id: discovery - objectName: "discovery" - } - Connections { target: Qt.application enabled: Engine.jsonRpcClient.connected && settings.returnToHome diff --git a/tests/testrunner/testrunner.pro b/tests/testrunner/testrunner.pro index 0a1fbf28..3bbfb163 100644 --- a/tests/testrunner/testrunner.pro +++ b/tests/testrunner/testrunner.pro @@ -1,7 +1,7 @@ TEMPLATE = app TARGET = meatestrunner -include(../../nymea-app.pri) +include(../../config.pri) QT += core gui testlib bluetooth websockets CONFIG += qmltestcase