From ed4e04cbd702880d6880a8ad5a3e115de10bf566 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 2 Feb 2021 23:20:58 +0100 Subject: [PATCH] Allow disabling individual discovery methods Also don't actually start scanning the network while discovering is false so we have a chance to pop up the permission dialog beforehand --- .../connection/discovery/nymeadiscovery.cpp | 77 ++++++++++++++++--- .../connection/discovery/nymeadiscovery.h | 20 +++++ 2 files changed, 86 insertions(+), 11 deletions(-) diff --git a/libnymea-app/connection/discovery/nymeadiscovery.cpp b/libnymea-app/connection/discovery/nymeadiscovery.cpp index f2db34bc..2e66a651 100644 --- a/libnymea-app/connection/discovery/nymeadiscovery.cpp +++ b/libnymea-app/connection/discovery/nymeadiscovery.cpp @@ -48,13 +48,6 @@ NymeaDiscovery::NymeaDiscovery(QObject *parent) : QObject(parent) loadFromDisk(); - m_upnp = new UpnpDiscovery(m_nymeaHosts, this); - m_zeroConf = new ZeroconfDiscovery(m_nymeaHosts, this); - -#ifndef Q_OS_IOS - m_bluetooth = new BluetoothServiceDiscovery(m_nymeaHosts, this); -#endif - m_cloudPollTimer.setInterval(5000); connect(&m_cloudPollTimer, &QTimer::timeout, this, [this](){ if (m_awsClient && m_awsClient->isLoggedIn()) { @@ -80,13 +73,25 @@ void NymeaDiscovery::setDiscovering(bool discovering) m_discovering = discovering; if (discovering) { - // ZeroConf is always in discovery mode, nothing to do... + if (m_zeroconfDiscoveryEnabled) { + if (!m_zeroConf) { + m_zeroConf = new ZeroconfDiscovery(m_nymeaHosts, this); + } + } // Start UPnP discovery - m_upnp->discover(); + if (m_upnpDiscoveryEnabled) { + if (!m_upnp) { + m_upnp = new UpnpDiscovery(m_nymeaHosts, this); + } + m_upnp->discover(); + } // Start Bluetooth discovery if HW is available - if (m_bluetooth) { + if (m_bluetoothDiscoveryEnabled) { + if (!m_bluetooth) { + m_bluetooth = new BluetoothServiceDiscovery(m_nymeaHosts, this); + } m_bluetooth->discover(); } @@ -97,10 +102,21 @@ void NymeaDiscovery::setDiscovering(bool discovering) m_awsClient->fetchDevices(); } } else { - m_upnp->stopDiscovery(); + if (m_zeroConf) { + m_zeroConf->deleteLater(); + m_zeroConf = nullptr; + } + + if (m_upnp) { + m_upnp->stopDiscovery(); + m_upnp->deleteLater(); + m_upnp = nullptr; + } if (m_bluetooth) { m_bluetooth->stopDiscovery(); + m_bluetooth->deleteLater(); + m_bluetooth = nullptr; } m_cloudPollTimer.stop(); @@ -171,6 +187,45 @@ void NymeaDiscovery::cacheHost(NymeaHost *host) settings.endGroup(); } +bool NymeaDiscovery::zeroconfDiscoveryEnable() const +{ + return m_zeroconfDiscoveryEnabled; +} + +bool NymeaDiscovery::bluetoothDiscoveryEnabled() const +{ + return m_bluetoothDiscoveryEnabled; +} + +bool NymeaDiscovery::upnpDiscoveryEnabled() const +{ + return m_upnpDiscoveryEnabled; +} + +void NymeaDiscovery::setZeroconfDiscoveryEnabled(bool zeroconfDiscoveryEnabled) +{ + if (m_zeroconfDiscoveryEnabled != zeroconfDiscoveryEnabled) { + m_zeroconfDiscoveryEnabled = zeroconfDiscoveryEnabled; + emit zeroconfDiscoveryEnabledChanged(m_zeroconfDiscoveryEnabled); + } +} + +void NymeaDiscovery::setBluetoothDiscoveryEnabled(bool bluetoothDiscoveryEnabled) +{ + if (m_bluetoothDiscoveryEnabled != bluetoothDiscoveryEnabled) { + m_bluetoothDiscoveryEnabled = bluetoothDiscoveryEnabled; + emit bluetoothDiscoveryEnabledChanged(m_bluetoothDiscoveryEnabled); + } +} + +void NymeaDiscovery::setUpnpDiscoveryEnabled(bool upnpDiscoveryEnabled) +{ + if (m_upnpDiscoveryEnabled != upnpDiscoveryEnabled) { + m_upnpDiscoveryEnabled = upnpDiscoveryEnabled; + emit upnpDiscoveryEnabledChanged(m_upnpDiscoveryEnabled); + } +} + void NymeaDiscovery::syncCloudDevices() { for (int i = 0; i < m_awsClient->awsDevices()->rowCount(); i++) { diff --git a/libnymea-app/connection/discovery/nymeadiscovery.h b/libnymea-app/connection/discovery/nymeadiscovery.h index c2b57458..c4aa4a9c 100644 --- a/libnymea-app/connection/discovery/nymeadiscovery.h +++ b/libnymea-app/connection/discovery/nymeadiscovery.h @@ -48,6 +48,10 @@ class NymeaDiscovery : public QObject { Q_OBJECT Q_PROPERTY(bool discovering READ discovering WRITE setDiscovering NOTIFY discoveringChanged) + Q_PROPERTY(bool bluetoothDiscoveryEnabled READ bluetoothDiscoveryEnabled WRITE setBluetoothDiscoveryEnabled NOTIFY bluetoothDiscoveryEnabledChanged) + Q_PROPERTY(bool zeroconfDiscoveryEnabled READ zeroconfDiscoveryEnable WRITE setZeroconfDiscoveryEnabled NOTIFY zeroconfDiscoveryEnabledChanged) + Q_PROPERTY(bool upnpDiscoveryEnabled READ upnpDiscoveryEnabled WRITE setUpnpDiscoveryEnabled NOTIFY upnpDiscoveryEnabledChanged) + Q_PROPERTY(AWSClient* awsClient READ awsClient WRITE setAwsClient NOTIFY awsClientChanged) Q_PROPERTY(NymeaHosts* nymeaHosts READ nymeaHosts CONSTANT) @@ -66,12 +70,25 @@ public: Q_INVOKABLE void cacheHost(NymeaHost* host); + bool zeroconfDiscoveryEnable() const; + bool bluetoothDiscoveryEnabled() const; + bool upnpDiscoveryEnabled() const; + +public slots: + void setZeroconfDiscoveryEnabled(bool zeroconfDiscoveryEnabled); + void setBluetoothDiscoveryEnabled(bool bluetoothDiscoveryEnabled); + void setUpnpDiscoveryEnabled(bool upnpDiscoveryEnabled); + signals: void discoveringChanged(); void awsClientChanged(); void serverUuidResolved(const QUuid &uuid, const QString &url); + void zeroconfDiscoveryEnabledChanged(bool zeroconfDiscoveryEnabled); + void bluetoothDiscoveryEnabledChanged(bool bluetoothDiscoveryEnabled); + void upnpDiscoveryEnabledChanged(bool upnpDiscoveryEnabled); + private slots: void syncCloudDevices(); @@ -93,6 +110,9 @@ private: QList m_pendingHostResolutions; + bool m_zeroconfDiscoveryEnabled = true; + bool m_bluetoothDiscoveryEnabled = true; + bool m_upnpDiscoveryEnabled = true; }; #endif // NYMEADISCOVERY_H