From 647edaf72ed88bac452cd8ecf3d6451521ace6bc Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 31 Aug 2018 20:00:48 +0200 Subject: [PATCH] BluetoothDiscovery is not a member of Engine any more rather a standalone tool --- libnymea-app-core/engine.cpp | 6 --- libnymea-app-core/engine.h | 3 -- libnymea-app-core/libnymea-app-core.h | 2 +- .../wifisetup/bluetoothdeviceinfos.h | 2 +- .../wifisetup/networkmanagercontroler.cpp | 41 +++++++------------ .../wifisetup/networkmanagercontroler.h | 16 +++----- .../ui/connection/BluetoothDiscoveryPage.qml | 34 +++++++-------- 7 files changed, 41 insertions(+), 63 deletions(-) diff --git a/libnymea-app-core/engine.cpp b/libnymea-app-core/engine.cpp index 315c40f9..fbdb4c40 100644 --- a/libnymea-app-core/engine.cpp +++ b/libnymea-app-core/engine.cpp @@ -71,11 +71,6 @@ BasicConfiguration *Engine::basicConfiguration() const return m_basicConfiguration; } -BluetoothDiscovery *Engine::bluetoothDiscovery() const -{ - return m_bluetoothDiscovery; -} - AWSClient *Engine::awsClient() const { return m_aws; @@ -111,7 +106,6 @@ Engine::Engine(QObject *parent) : m_logManager(new LogManager(m_jsonRpcClient, this)), m_tagsManager(new TagsManager(m_jsonRpcClient, this)), m_basicConfiguration(new BasicConfiguration(m_jsonRpcClient, this)), - m_bluetoothDiscovery(new BluetoothDiscovery(this)), m_aws(new AWSClient(this)) { m_connection->registerTransport(new TcpSocketTransportFactory()); diff --git a/libnymea-app-core/engine.h b/libnymea-app-core/engine.h index cd46c176..85f877b3 100644 --- a/libnymea-app-core/engine.h +++ b/libnymea-app-core/engine.h @@ -43,7 +43,6 @@ class Engine : public QObject Q_PROPERTY(TagsManager* tagsManager READ tagsManager CONSTANT) Q_PROPERTY(JsonRpcClient* jsonRpcClient READ jsonRpcClient CONSTANT) Q_PROPERTY(BasicConfiguration* basicConfiguration READ basicConfiguration CONSTANT) - Q_PROPERTY(BluetoothDiscovery* bluetoothDiscovery READ bluetoothDiscovery CONSTANT) Q_PROPERTY(AWSClient* awsClient READ awsClient CONSTANT) public: @@ -59,7 +58,6 @@ public: JsonRpcClient *jsonRpcClient() const; LogManager *logManager() const; BasicConfiguration *basicConfiguration() const; - BluetoothDiscovery *bluetoothDiscovery() const; AWSClient* awsClient() const; Q_INVOKABLE void deployCertificate(); @@ -75,7 +73,6 @@ private: LogManager *m_logManager; TagsManager *m_tagsManager; BasicConfiguration *m_basicConfiguration; - BluetoothDiscovery *m_bluetoothDiscovery; AWSClient *m_aws; private slots: diff --git a/libnymea-app-core/libnymea-app-core.h b/libnymea-app-core/libnymea-app-core.h index 560668b4..85a56e6a 100644 --- a/libnymea-app-core/libnymea-app-core.h +++ b/libnymea-app-core/libnymea-app-core.h @@ -165,7 +165,7 @@ void registerQmlTypes() { qmlRegisterType(uri, 1, 0, "TagsProxyModel"); qmlRegisterType(uri, 1, 0, "NetworkManagerControler"); - qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDiscovery", "Can't create this in QML. Get it from the Engine instance."); + qmlRegisterType(uri, 1, 0, "BluetoothDiscovery"); qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDeviceInfo", "Can't create this in QML. Get it from the DeviceInfos."); qmlRegisterUncreatableType(uri, 1, 0, "BluetoothDeviceInfos", "Can't create this in QML. Get it from the BluetoothDiscovery."); qmlRegisterUncreatableType(uri, 1, 0, "WirelessSetupManager", "Can't create this in QML. Get it from the NetworkManagerControler."); diff --git a/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h b/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h index 35cd2d9a..4e256cd3 100644 --- a/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h +++ b/libnymea-app-core/wifisetup/bluetoothdeviceinfos.h @@ -38,7 +38,7 @@ public: BluetoothDeviceInfoRoleLe }; - explicit BluetoothDeviceInfos(QObject *parent = 0); + explicit BluetoothDeviceInfos(QObject *parent = nullptr); QList deviceInfos(); diff --git a/libnymea-app-core/wifisetup/networkmanagercontroler.cpp b/libnymea-app-core/wifisetup/networkmanagercontroler.cpp index cbe2dc1d..b7b3525e 100644 --- a/libnymea-app-core/wifisetup/networkmanagercontroler.cpp +++ b/libnymea-app-core/wifisetup/networkmanagercontroler.cpp @@ -28,25 +28,17 @@ NetworkManagerControler::NetworkManagerControler(QObject *parent) : QObject(pare } -QString NetworkManagerControler::name() const +BluetoothDeviceInfo *NetworkManagerControler::bluetoothDeviceInfo() const { - return m_name; + return m_bluetoothDeviceInfo; } -void NetworkManagerControler::setName(const QString &name) +void NetworkManagerControler::setBluetoothDeviceInfo(BluetoothDeviceInfo *bluetoothDeviceInfo) { - m_name = name; - emit nameChanged(); -} - -QString NetworkManagerControler::address() const -{ - return m_address; -} - -void NetworkManagerControler::setAddress(const QString &address) -{ - m_address = address; + if (m_bluetoothDeviceInfo != bluetoothDeviceInfo) { + m_bluetoothDeviceInfo = bluetoothDeviceInfo; + emit bluetoothDeviceInfoChanged(); + } } WirelessSetupManager *NetworkManagerControler::manager() @@ -56,26 +48,23 @@ WirelessSetupManager *NetworkManagerControler::manager() void NetworkManagerControler::connectDevice() { + if (!m_bluetoothDeviceInfo) { + qWarning() << "Can't connect to device. bluetoothDeviceInfo not set."; + return; + } + if (m_wirelessSetupManager) { delete m_wirelessSetupManager; m_wirelessSetupManager = nullptr; emit managerChanged(); } - // find device info for this address and name - BluetoothDeviceInfo *deviceInfo = nullptr; - foreach (BluetoothDeviceInfo *deviceInformation, Engine::instance()->bluetoothDiscovery()->deviceInfos()->deviceInfos()) { - if (deviceInformation->address() == address() && deviceInformation->name() == name()) { - deviceInfo = deviceInformation; - } - } - - if (!deviceInfo) { - qDebug() << "Could not connect to device. There is no device info for" << name() << address(); + if (!m_bluetoothDeviceInfo) { + qDebug() << "Could not connect to device. There is no device info for" << m_bluetoothDeviceInfo->name() << m_bluetoothDeviceInfo->address(); return; } - m_wirelessSetupManager = new WirelessSetupManager(deviceInfo->getBluetoothDeviceInfo(), this); + m_wirelessSetupManager = new WirelessSetupManager(m_bluetoothDeviceInfo->getBluetoothDeviceInfo(), this); emit managerChanged(); m_wirelessSetupManager->connectDevice(); diff --git a/libnymea-app-core/wifisetup/networkmanagercontroler.h b/libnymea-app-core/wifisetup/networkmanagercontroler.h index b3921cf5..626a0273 100644 --- a/libnymea-app-core/wifisetup/networkmanagercontroler.h +++ b/libnymea-app-core/wifisetup/networkmanagercontroler.h @@ -26,38 +26,34 @@ #include #include +#include "bluetoothdeviceinfo.h" #include "wirelesssetupmanager.h" class NetworkManagerControler : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) - Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged) + Q_PROPERTY(BluetoothDeviceInfo* bluetoothDeviceInfo READ bluetoothDeviceInfo WRITE setBluetoothDeviceInfo) Q_PROPERTY(WirelessSetupManager *manager READ manager NOTIFY managerChanged) public: explicit NetworkManagerControler(QObject *parent = nullptr); - QString name() const; - void setName(const QString &name); - - QString address() const; - void setAddress(const QString &address); + BluetoothDeviceInfo* bluetoothDeviceInfo() const; + void setBluetoothDeviceInfo(BluetoothDeviceInfo* bluetoothDeviceInfo); WirelessSetupManager *manager(); Q_INVOKABLE void connectDevice(); private: - QString m_name; - QString m_address; + BluetoothDeviceInfo* m_bluetoothDeviceInfo = nullptr; WirelessSetupManager *m_wirelessSetupManager = nullptr; signals: void managerChanged(); void nameChanged(); - void addressChanged(); + void bluetoothDeviceInfoChanged(); }; diff --git a/nymea-app/ui/connection/BluetoothDiscoveryPage.qml b/nymea-app/ui/connection/BluetoothDiscoveryPage.qml index fa933e12..95f55cff 100644 --- a/nymea-app/ui/connection/BluetoothDiscoveryPage.qml +++ b/nymea-app/ui/connection/BluetoothDiscoveryPage.qml @@ -12,18 +12,22 @@ Page { onBackPressed: pageStack.pop() } - Component.onCompleted: Engine.bluetoothDiscovery.start() + Component.onCompleted: bluetoothDiscovery.start() - function setupDevice(name, btAddress) { - Engine.bluetoothDiscovery.stop() - pageStack.push(connectingPageComponent, { name: name, address: btAddress } ) + BluetoothDiscovery { + id: bluetoothDiscovery + } + + function setupDevice(btDeviceInfo) { + bluetoothDiscovery.stop() + pageStack.push(connectingPageComponent, { bluetoothDeviceInfo: btDeviceInfo } ) } Connections { target: pageStack onCurrentItemChanged: { if (pageStack.currentItem === root) { - Engine.bluetoothDiscovery.start(); + bluetoothDiscovery.start(); } } } @@ -43,17 +47,17 @@ Page { text: { if (Qt.platform.os === "ios") { - if (Engine.bluetoothDiscovery.bluetoothAvailable && Engine.bluetoothDiscovery.bluetoothEnabled) { + if (bluetoothDiscovery.bluetoothAvailable && bluetoothDiscovery.bluetoothEnabled) { return qsTr("Searching for %1 boxes via Bluetooth LE.").arg(app.systemName) - } if (Engine.bluetoothDiscovery.bluetoothAvailable && !Engine.bluetoothDiscovery.bluetoothEnabled) { + } if (bluetoothDiscovery.bluetoothAvailable && !bluetoothDiscovery.bluetoothEnabled) { return qsTr("Uh oh! Bluetooth is not enabled. Please enable the Bluetooth on this device and restart the application.") } else { return qsTr("Uh oh! Bluetooth is not available. Please make sure Bluetooth is enabled on this device and restart the application.") } } else { - if (Engine.bluetoothDiscovery.bluetoothAvailable && Engine.bluetoothDiscovery.bluetoothEnabled) { + if (bluetoothDiscovery.bluetoothAvailable && bluetoothDiscovery.bluetoothEnabled) { return qsTr("Searching for %1 boxes via Bluetooth LE.").arg(app.systemName) - } if (Engine.bluetoothDiscovery.bluetoothAvailable && !Engine.bluetoothDiscovery.bluetoothEnabled) { + } if (bluetoothDiscovery.bluetoothAvailable && !bluetoothDiscovery.bluetoothEnabled) { return qsTr("Uh oh! Bluetooth is not enabled. Please enable the Bluetooth on this device.") } else { return qsTr("Uh oh! Bluetooth is not available. Please make sure Bluetooth is enabled on this device.") @@ -67,7 +71,7 @@ Page { } BusyIndicator { - running: Engine.bluetoothDiscovery.discovering + running: bluetoothDiscovery.discovering } } @@ -77,7 +81,7 @@ Page { Layout.fillWidth: true Layout.fillHeight: true - model: Engine.bluetoothDiscovery.deviceInfos + model: bluetoothDiscovery.deviceInfos clip: true delegate: MeaListItemDelegate { @@ -87,7 +91,7 @@ Page { subText: model.address onClicked: { - root.setupDevice(model.name, model.address) + root.setupDevice(bluetoothDiscovery.deviceInfos.get(index)) } } } @@ -198,13 +202,11 @@ Page { onBackPressed: pageStack.pop() } - property string name - property string address + property var bluetoothDeviceInfo NetworkManagerControler { id: networkManger - name: connectingPage.name - address: connectingPage.address + bluetoothDeviceInfo: connectingPage.bluetoothDeviceInfo Component.onCompleted: networkManger.connectDevice() }