diff --git a/libnymea-core/cloud/cloudtransport.cpp b/libnymea-core/cloud/cloudtransport.cpp index cf0e6631..b9c3e214 100644 --- a/libnymea-core/cloud/cloudtransport.cpp +++ b/libnymea-core/cloud/cloudtransport.cpp @@ -1,6 +1,7 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 2018 Michael Zanetti * + * Copyright (C) 2018 Simon Stürz * * * * This file is part of nymea. * * * @@ -21,6 +22,8 @@ #include "cloudtransport.h" #include "loggingcategories.h" +#include "nymeacore.h" + using namespace remoteproxyclient; namespace nymeaserver { @@ -28,11 +31,12 @@ namespace nymeaserver { CloudTransport::CloudTransport(const ServerConfiguration &config, QObject *parent): TransportInterface(config, parent) { + m_proxyUrl = QUrl("wss://remoteproxy.nymea.io"); } void CloudTransport::sendData(const QUuid &clientId, const QByteArray &data) { - qCDebug(dcCloud) << "Should send data" << clientId << data; + qCDebug(dcCloudTraffic()) << "Sending data" << clientId << data; foreach (const ConnectionContext &ctx, m_connections) { if (ctx.clientId == clientId) { ctx.proxyConnection->sendData(data); @@ -51,30 +55,38 @@ void CloudTransport::sendData(const QList &clientIds, const QByteArray &d bool CloudTransport::startServer() { - qCDebug(dcCloud) << "Should start cloud server"; + qCDebug(dcCloud()) << "Start cloud server"; return true; } bool CloudTransport::stopServer() { - qCDebug(dcCloud) << "Should stop cloud server"; + qCDebug(dcCloud()) << "Stop cloud server"; return true; } void CloudTransport::connectToCloud(const QString &token) { - qCDebug(dcCloud) << "Should connect to cloud"; + qCDebug(dcCloud()) << "Start connecting to remote proxy server" << m_proxyUrl.toString(); + + foreach (const ConnectionContext &connectionContext, m_connections.values()) { + if (connectionContext.token == token) { + qCWarning(dcCloud()) << "There is already a remote connection for this token. This is not allowed."; + return; + } + } + ConnectionContext context; context.clientId = QUuid::createUuid(); context.token = token; - context.proxyConnection = new RemoteProxyConnection(QUuid::createUuid(), "nymea:core", this); + context.proxyConnection = new RemoteProxyConnection(NymeaCore::instance()->configuration()->serverUuid().toString(), NymeaCore::instance()->configuration()->serverName(), this); m_connections.insert(context.proxyConnection, context); connect(context.proxyConnection, &RemoteProxyConnection::ready, this, &CloudTransport::transportReady); connect(context.proxyConnection, &RemoteProxyConnection::stateChanged, this, &CloudTransport::remoteConnectionStateChanged); connect(context.proxyConnection, &RemoteProxyConnection::dataReady, this, &CloudTransport::transportDataReady); - context.proxyConnection->connectServer(QUrl("wss://dev-remoteproxy.nymea.io")); + context.proxyConnection->connectServer(m_proxyUrl); } void CloudTransport::remoteConnectionStateChanged(RemoteProxyConnection::State state) @@ -83,19 +95,28 @@ void CloudTransport::remoteConnectionStateChanged(RemoteProxyConnection::State s RemoteProxyConnection *proxyConnection = qobject_cast(sender()); ConnectionContext context = m_connections.value(proxyConnection); - if (state == RemoteProxyConnection::StateRemoteConnected) { + switch (state) { + case RemoteProxyConnection::StateRemoteConnected: + qCDebug(dcCloud()) << "The remote client connected successfully" << proxyConnection->tunnelPartnerName() << proxyConnection->tunnelPartnerUuid(); emit clientConnected(context.clientId); - } else if (state ==RemoteProxyConnection::StateDisconnected) { + break; + case RemoteProxyConnection::StateDisconnected: + qCDebug(dcCloud()) << "The remote connection disconnected."; emit clientDisconnected(context.clientId); + break; + default: + qCDebug(dcCloud()) << state; + break; } } void CloudTransport::transportReady() { - qCDebug(dcCloud) << "Transport ready"; - RemoteProxyConnection *proxyConnection = qobject_cast(sender()); - ConnectionContext context = m_connections.value(proxyConnection); + RemoteProxyConnection *proxyConnection = static_cast(sender()); + qCDebug(dcCloud()) << "Connected successfully to remote proxy server" << proxyConnection->proxyServerName() << + proxyConnection->proxyServerVersion() << "API version:" << proxyConnection->proxyServerApiVersion(); + ConnectionContext context = m_connections.value(proxyConnection); context.proxyConnection->authenticate(context.token); } @@ -103,7 +124,7 @@ void CloudTransport::transportDataReady(const QByteArray &data) { RemoteProxyConnection *proxyConnection = qobject_cast(sender()); ConnectionContext context = m_connections.value(proxyConnection); - + qCDebug(dcCloudTraffic()) << "Date received:" << context.clientId.toString() << data; emit dataAvailable(context.clientId, data); } diff --git a/libnymea-core/cloud/cloudtransport.h b/libnymea-core/cloud/cloudtransport.h index 081ae37a..686050cc 100644 --- a/libnymea-core/cloud/cloudtransport.h +++ b/libnymea-core/cloud/cloudtransport.h @@ -38,6 +38,7 @@ public: bool startServer() override; bool stopServer() override; + signals: public slots: @@ -49,6 +50,8 @@ private slots: void transportDataReady(const QByteArray &data); private: + QUrl m_proxyUrl; + class ConnectionContext { public: QUuid clientId; diff --git a/libnymea-core/jsonrpc/jsonrpcserver.cpp b/libnymea-core/jsonrpc/jsonrpcserver.cpp index 40707620..3a21fa82 100644 --- a/libnymea-core/jsonrpc/jsonrpcserver.cpp +++ b/libnymea-core/jsonrpc/jsonrpcserver.cpp @@ -472,7 +472,7 @@ QVariantMap JsonRPCServer::createWelcomeMessage(TransportInterface *interface) c handshake.insert("language", NymeaCore::instance()->configuration()->locale().name()); handshake.insert("protocol version", JSON_PROTOCOL_VERSION); handshake.insert("initialSetupRequired", (interface->configuration().authenticationEnabled ? NymeaCore::instance()->userManager()->initRequired() : false)); - handshake.insert("authenticationRequired", interface->configuration().authenticationEnabled); + handshake.insert("authenticationRequired", m_interfaces.value(interface)); handshake.insert("pushButtonAuthAvailable", NymeaCore::instance()->userManager()->pushButtonAuthAvailable()); return handshake; } diff --git a/libnymea/loggingcategories.cpp b/libnymea/loggingcategories.cpp index 1a6cd237..108aa880 100644 --- a/libnymea/loggingcategories.cpp +++ b/libnymea/loggingcategories.cpp @@ -43,6 +43,7 @@ Q_LOGGING_CATEGORY(dcAvahi, "Avahi") Q_LOGGING_CATEGORY(dcUpnp, "UPnP") Q_LOGGING_CATEGORY(dcBluetooth, "Bluetooth") Q_LOGGING_CATEGORY(dcCloud, "Cloud") +Q_LOGGING_CATEGORY(dcCloudTraffic, "CloudTraffic") Q_LOGGING_CATEGORY(dcNetworkManager, "NetworkManager") Q_LOGGING_CATEGORY(dcUserManager, "UserManager") Q_LOGGING_CATEGORY(dcAWS, "AWS") diff --git a/libnymea/loggingcategories.h b/libnymea/loggingcategories.h index 84a06329..01731caf 100644 --- a/libnymea/loggingcategories.h +++ b/libnymea/loggingcategories.h @@ -51,6 +51,7 @@ Q_DECLARE_LOGGING_CATEGORY(dcAvahi) Q_DECLARE_LOGGING_CATEGORY(dcUpnp) Q_DECLARE_LOGGING_CATEGORY(dcBluetooth) Q_DECLARE_LOGGING_CATEGORY(dcCloud) +Q_DECLARE_LOGGING_CATEGORY(dcCloudTraffic) Q_DECLARE_LOGGING_CATEGORY(dcNetworkManager) Q_DECLARE_LOGGING_CATEGORY(dcUserManager) Q_DECLARE_LOGGING_CATEGORY(dcAWS)