From 7ef39cb2d7230fcb4f1d68fa5a267b7e163d574d Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 31 Aug 2018 20:18:26 +0200 Subject: [PATCH] nymeadiscovery doesn't use the Engine any more --- .../discovery/nymeadiscovery.cpp | 36 +++++++++++++------ libnymea-app-core/discovery/nymeadiscovery.h | 7 ++++ nymea-app/ui/connection/ConnectPage.qml | 1 + 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/libnymea-app-core/discovery/nymeadiscovery.cpp b/libnymea-app-core/discovery/nymeadiscovery.cpp index 261677e0..d2aed28f 100644 --- a/libnymea-app-core/discovery/nymeadiscovery.cpp +++ b/libnymea-app-core/discovery/nymeadiscovery.cpp @@ -1,5 +1,4 @@ #include "nymeadiscovery.h" -#include "engine.h" #include "upnpdiscovery.h" #include "zeroconfdiscovery.h" #include "bluetoothservicediscovery.h" @@ -21,12 +20,11 @@ NymeaDiscovery::NymeaDiscovery(QObject *parent) : QObject(parent) #endif m_cloudPollTimer.setInterval(5000); - connect(&m_cloudPollTimer, &QTimer::timeout, this, [](){ - if (Engine::instance()->awsClient()->isLoggedIn()) { - Engine::instance()->awsClient()->fetchDevices(); + connect(&m_cloudPollTimer, &QTimer::timeout, this, [this](){ + if (m_awsClient && m_awsClient->isLoggedIn()) { + m_awsClient->fetchDevices(); } }); - connect(Engine::instance()->awsClient(), &AWSClient::devicesFetched, this, &NymeaDiscovery::syncCloudDevices); } bool NymeaDiscovery::discovering() const @@ -62,9 +60,9 @@ void NymeaDiscovery::setDiscovering(bool discovering) // start polling cloud m_cloudPollTimer.start(); // If we're logged in, poll right away - if (Engine::instance()->awsClient()->isLoggedIn()) { + if (m_awsClient && m_awsClient->isLoggedIn()) { syncCloudDevices(); - Engine::instance()->awsClient()->fetchDevices(); + m_awsClient->fetchDevices(); } } else { if (!m_zeroConf->available()) { @@ -86,10 +84,28 @@ DiscoveryModel *NymeaDiscovery::discoveryModel() const return m_discoveryModel; } +AWSClient *NymeaDiscovery::awsClient() const +{ + return m_awsClient; +} + +void NymeaDiscovery::setAwsClient(AWSClient *awsClient) +{ + if (m_awsClient != awsClient) { + m_awsClient = awsClient; + emit awsClientChanged(); + } + + if (m_awsClient) { + connect(m_awsClient, &AWSClient::devicesFetched, this, &NymeaDiscovery::syncCloudDevices); + syncCloudDevices(); + } +} + void NymeaDiscovery::syncCloudDevices() { - for (int i = 0; i < Engine::instance()->awsClient()->awsDevices()->rowCount(); i++) { - AWSDevice *d = Engine::instance()->awsClient()->awsDevices()->get(i); + for (int i = 0; i < m_awsClient->awsDevices()->rowCount(); i++) { + AWSDevice *d = m_awsClient->awsDevices()->get(i); DiscoveryDevice *device = m_discoveryModel->find(d->id()); if (!device) { device = new DiscoveryDevice(); @@ -115,7 +131,7 @@ void NymeaDiscovery::syncCloudDevices() DiscoveryDevice *device = m_discoveryModel->get(i); for (int j = 0; j < device->connections()->rowCount(); j++) { if (device->connections()->get(j)->bearerType() == Connection::BearerTypeCloud) { - if (Engine::instance()->awsClient()->awsDevices()->getDevice(device->uuid().toString()) == nullptr) { + if (m_awsClient->awsDevices()->getDevice(device->uuid().toString()) == nullptr) { device->connections()->removeConnection(j); break; } diff --git a/libnymea-app-core/discovery/nymeadiscovery.h b/libnymea-app-core/discovery/nymeadiscovery.h index faed4787..fd451aa4 100644 --- a/libnymea-app-core/discovery/nymeadiscovery.h +++ b/libnymea-app-core/discovery/nymeadiscovery.h @@ -18,6 +18,8 @@ class NymeaDiscovery : public QObject Q_PROPERTY(bool discovering READ discovering WRITE setDiscovering NOTIFY discoveringChanged) Q_PROPERTY(DiscoveryModel *discoveryModel READ discoveryModel CONSTANT) + Q_PROPERTY(AWSClient* awsClient READ awsClient WRITE setAwsClient NOTIFY awsClientChanged) + public: explicit NymeaDiscovery(QObject *parent = nullptr); @@ -26,8 +28,12 @@ public: DiscoveryModel *discoveryModel() const; + AWSClient* awsClient() const; + void setAwsClient(AWSClient *awsClient); + signals: void discoveringChanged(); + void awsClientChanged(); private slots: void syncCloudDevices(); @@ -39,6 +45,7 @@ private: UpnpDiscovery *m_upnp = nullptr; ZeroconfDiscovery *m_zeroConf = nullptr; BluetoothServiceDiscovery *m_bluetooth = nullptr; + AWSClient *m_awsClient = nullptr; QTimer m_cloudPollTimer; diff --git a/nymea-app/ui/connection/ConnectPage.qml b/nymea-app/ui/connection/ConnectPage.qml index f72c488d..96d54deb 100644 --- a/nymea-app/ui/connection/ConnectPage.qml +++ b/nymea-app/ui/connection/ConnectPage.qml @@ -37,6 +37,7 @@ Page { NymeaDiscovery { id: discovery objectName: "discovery" + awsClient: Engine.awsClient discovering: pageStack.currentItem.objectName === "discoveryPage" }