From 47298be52c7c59d4ec0abc0a12eb84176618a38a Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 23 Oct 2017 15:52:38 +0200 Subject: [PATCH] restructure things a bit --- guh-control/devicediscovery.cpp | 22 +- guh-control/devicediscovery.h | 6 +- guh-control/devicemanager.cpp | 185 ++++++++++++++++- guh-control/devicemanager.h | 31 ++- guh-control/engine.cpp | 14 +- guh-control/engine.h | 2 +- guh-control/guhconnection.cpp | 1 + guh-control/jsonrpc/actionhandler.cpp | 40 ---- guh-control/jsonrpc/actionhandler.h | 40 ---- guh-control/jsonrpc/configurationhandler.cpp | 12 -- guh-control/jsonrpc/configurationhandler.h | 21 -- guh-control/jsonrpc/devicehandler.cpp | 170 ---------------- guh-control/jsonrpc/devicehandler.h | 56 ----- guh-control/jsonrpc/eventhandler.cpp | 38 ---- guh-control/jsonrpc/eventhandler.h | 40 ---- guh-control/jsonrpc/jsonrpcclient.cpp | 192 +++++------------- guh-control/jsonrpc/jsonrpcclient.h | 59 +++--- guh-control/jsonrpc/logginghandler.cpp | 43 ---- guh-control/jsonrpc/logginghandler.h | 41 ---- guh-control/jsonrpc/networkmanagerhandler.cpp | 19 -- guh-control/jsonrpc/networkmanagerhandler.h | 23 --- guh-control/resources.qrc | 2 + guh-control/ui/MagicPage.qml | 21 ++ guh-control/ui/MainPage.qml | 2 +- .../customviews/ExtendedVolumeController.qml | 4 +- .../ui/customviews/MediaControllerView.qml | 2 +- .../devicelistpages/LightsDeviceListPage.qml | 12 +- .../ui/devicepages/GenericDevicePage.qml | 2 +- guh-control/ui/magic/NewRulePage.qml | 11 + 29 files changed, 350 insertions(+), 761 deletions(-) delete mode 100644 guh-control/jsonrpc/actionhandler.cpp delete mode 100644 guh-control/jsonrpc/actionhandler.h delete mode 100644 guh-control/jsonrpc/configurationhandler.cpp delete mode 100644 guh-control/jsonrpc/configurationhandler.h delete mode 100644 guh-control/jsonrpc/devicehandler.cpp delete mode 100644 guh-control/jsonrpc/devicehandler.h delete mode 100644 guh-control/jsonrpc/eventhandler.cpp delete mode 100644 guh-control/jsonrpc/eventhandler.h delete mode 100644 guh-control/jsonrpc/logginghandler.cpp delete mode 100644 guh-control/jsonrpc/logginghandler.h delete mode 100644 guh-control/jsonrpc/networkmanagerhandler.cpp delete mode 100644 guh-control/jsonrpc/networkmanagerhandler.h create mode 100644 guh-control/ui/MagicPage.qml create mode 100644 guh-control/ui/magic/NewRulePage.qml diff --git a/guh-control/devicediscovery.cpp b/guh-control/devicediscovery.cpp index 081537ba..977adab1 100644 --- a/guh-control/devicediscovery.cpp +++ b/guh-control/devicediscovery.cpp @@ -5,8 +5,6 @@ DeviceDiscovery::DeviceDiscovery(QObject *parent) : QAbstractListModel(parent) { - - connect(Engine::instance()->jsonRpcClient(), &JsonRpcClient::responseReceived, this, &DeviceDiscovery::responseReceived); } int DeviceDiscovery::rowCount(const QModelIndex &parent) const @@ -38,25 +36,27 @@ QHash DeviceDiscovery::roleNames() const return roles; } -void DeviceDiscovery::discoverDevices(const QUuid &deviceClassId, const QVariantList ¶ms) +void DeviceDiscovery::discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams) { - int request = Engine::instance()->jsonRpcClient()->discoverDevices(deviceClassId, params); - m_requests.append(request); + QVariantMap params; + params.insert("deviceClassId", deviceClassId.toString()); + if (!discoveryParams.isEmpty()) { + params.insert("discoveryParams", discoveryParams); + } + Engine::instance()->jsonRpcClient()->sendCommand("Devices.DiscoverDevices", params, this, "discoverDevicesResponse"); + m_busy = true; emit busyChanged(); emit countChanged(); } bool DeviceDiscovery::busy() const { - return m_requests.count() > 0; + return m_busy; } -void DeviceDiscovery::responseReceived(int id, const QVariantMap ¶ms) +void DeviceDiscovery::discoverDevicesResponse(const QVariantMap ¶ms) { - if (!m_requests.contains(id)) { - return; - } - m_requests.removeAll(id); + m_busy = false; emit busyChanged(); qDebug() << "response received" << params; diff --git a/guh-control/devicediscovery.h b/guh-control/devicediscovery.h index 0528a8c5..9e85f4a0 100644 --- a/guh-control/devicediscovery.h +++ b/guh-control/devicediscovery.h @@ -23,12 +23,12 @@ public: QHash roleNames() const; - Q_INVOKABLE void discoverDevices(const QUuid &deviceClassId, const QVariantList ¶ms = {}); + Q_INVOKABLE void discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams = {}); bool busy() const; private slots: - void responseReceived(int id, const QVariantMap ¶ms); + void discoverDevicesResponse(const QVariantMap ¶ms); signals: void busyChanged(); @@ -43,7 +43,7 @@ private: QString m_description; }; - QList m_requests; + bool m_busy = false; bool contains(const QUuid &deviceDescriptorId) const; QList m_foundDevices; diff --git a/guh-control/devicemanager.cpp b/guh-control/devicemanager.cpp index d45ce258..745fcce3 100644 --- a/guh-control/devicemanager.cpp +++ b/guh-control/devicemanager.cpp @@ -20,14 +20,35 @@ #include "devicemanager.h" #include "engine.h" +#include "jsonrpc/jsontypes.h" -DeviceManager::DeviceManager(QObject *parent) : - QObject(parent), +DeviceManager::DeviceManager(JsonRpcClient* jsonclient, QObject *parent) : + JsonHandler(parent), m_vendors(new Vendors(this)), m_plugins(new Plugins(this)), m_devices(new Devices(this)), - m_deviceClasses(new DeviceClasses(this)) + m_deviceClasses(new DeviceClasses(this)), + m_jsonClient(jsonclient) { + m_jsonClient->registerNotificationHandler(this, "notificationReceived"); +} + +void DeviceManager::clear() +{ + m_devices->clearModel(); + m_deviceClasses->clearModel(); + m_vendors->clearModel(); + m_plugins->clearModel(); +} + +void DeviceManager::init() +{ + m_jsonClient->sendCommand("Devices.GetPlugins", this, "getPluginsResponse"); +} + +QString DeviceManager::nameSpace() const +{ + return "Devices"; } Vendors *DeviceManager::vendors() const @@ -49,3 +70,161 @@ DeviceClasses *DeviceManager::deviceClasses() const { return m_deviceClasses; } + +void DeviceManager::addDevice(const QUuid &deviceClassId, const QVariantList &deviceParams) +{ + qDebug() << "add device " << deviceClassId.toString(); + QVariantMap params; + params.insert("deviceClassId", deviceClassId.toString()); + params.insert("deviceParams", deviceParams); + m_jsonClient->sendCommand("Devices.AddConfiguredDevice", params, this, "addDeviceResponse"); +} + +void DeviceManager::notificationReceived(const QVariantMap &data) +{ + if (data.value("notification").toString() == "Devices.StateChanged") { + qDebug() << "Device state changed" << data.value("params"); + Device *dev = m_devices->getDevice(data.value("params").toMap().value("deviceId").toUuid()); + if (!dev) { + qWarning() << "Device state change notification received for an unknown device"; + return; + } + dev->setStateValue(data.value("params").toMap().value("stateTypeId").toUuid(), data.value("params").toMap().value("value")); + } else { + qWarning() << "DeviceManager unhandled device notification received" << data; + } +} + +void DeviceManager::getVendorsResponse(const QVariantMap ¶ms) +{ + qDebug() << "Got GetSupportedVendors response"; + if (params.value("params").toMap().keys().contains("vendors")) { + QVariantList vendorList = params.value("params").toMap().value("vendors").toList(); + foreach (QVariant vendorVariant, vendorList) { + Vendor *vendor = JsonTypes::unpackVendor(vendorVariant.toMap(), Engine::instance()->deviceManager()->vendors()); + m_vendors->addVendor(vendor); + } + } + + qDebug() << "start getting deviceClass at" << QDateTime::currentDateTime(); + + m_jsonClient->sendCommand("Devices.GetSupportedDevices", this, "getSupportedDevicesResponse"); +} + +void DeviceManager::getSupportedDevicesResponse(const QVariantMap ¶ms) +{ + if (params.value("params").toMap().keys().contains("deviceClasses")) { + QVariantList deviceClassList = params.value("params").toMap().value("deviceClasses").toList(); + foreach (QVariant deviceClassVariant, deviceClassList) { + DeviceClass *deviceClass = JsonTypes::unpackDeviceClass(deviceClassVariant.toMap(), Engine::instance()->deviceManager()->deviceClasses()); + qDebug() << "Server has device class:" << deviceClass->name() << deviceClass->id(); + m_deviceClasses->addDeviceClass(deviceClass); + } + } + m_jsonClient->sendCommand("Devices.GetConfiguredDevices", this, "getConfiguredDevicesResponse"); +} + +void DeviceManager::getPluginsResponse(const QVariantMap ¶ms) +{ + qDebug() << "received plugins"; + if (params.value("params").toMap().keys().contains("plugins")) { + QVariantList pluginList = params.value("params").toMap().value("plugins").toList(); + foreach (QVariant pluginVariant, pluginList) { + Plugin *plugin = JsonTypes::unpackPlugin(pluginVariant.toMap(), Engine::instance()->deviceManager()->plugins()); + m_plugins->addPlugin(plugin); + } + } + m_jsonClient->sendCommand("Devices.GetVendors", this, "getVendorsResponse"); +} + +void DeviceManager::getConfiguredDevicesResponse(const QVariantMap ¶ms) +{ + if (params.value("params").toMap().keys().contains("devices")) { + QVariantList deviceList = params.value("params").toMap().value("devices").toList(); + foreach (QVariant deviceVariant, deviceList) { + Device *device = JsonTypes::unpackDevice(deviceVariant.toMap(), Engine::instance()->deviceManager()->devices()); + if (!device) continue; + Engine::instance()->deviceManager()->devices()->addDevice(device); + + //qDebug() << QJsonDocument::fromVariant(deviceVariant).toJson(); + + // set initial state values + QVariantList stateVariantList = deviceVariant.toMap().value("states").toList(); + foreach (const QVariant &stateMap, stateVariantList) { + QUuid stateTypeId = stateMap.toMap().value("stateTypeId").toUuid(); + QVariant value = stateMap.toMap().value("value"); + device->setStateValue(stateTypeId, value); + } + } + } +} + +void DeviceManager::addDeviceResponse(const QVariantMap ¶ms) +{ + if (params.value("params").toMap().keys().contains("device")) { + QVariantMap deviceVariant = params.value("params").toMap().value("device").toMap(); + Device *device = JsonTypes::unpackDevice(deviceVariant, m_devices); + qDebug() << "Device added" << device->id().toString(); + m_devices->addDevice(device); + } +} + +void DeviceManager::removeDeviceResponse(const QVariantMap ¶ms) +{ + QUuid deviceId = params.value("params").toMap().value("deviceId").toUuid(); + qDebug() << "JsonRpc: Notification: Device removed" << deviceId.toString(); + Device *device = m_devices->getDevice(deviceId); + m_devices->removeDevice(device); + device->deleteLater(); +} + +void DeviceManager::addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name) +{ + qDebug() << "JsonRpc: add discovered device " << deviceClassId.toString(); + QVariantMap params; + params.insert("deviceClassId", deviceClassId.toString()); + params.insert("name", name); + params.insert("deviceDescriptorId", deviceDescriptorId.toString()); + m_jsonClient->sendCommand("Devices.AddConfiguredDevice", params); +} + +void DeviceManager::pairDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId) +{ + qDebug() << "JsonRpc: pair device " << deviceClassId.toString(); + QVariantMap params; + params.insert("name", "name"); + params.insert("deviceClassId", deviceClassId.toString()); + params.insert("deviceDescriptorId", deviceDescriptorId.toString()); + m_jsonClient->sendCommand("Devices.PairDevice", params); +} + +void DeviceManager::confirmPairing(const QUuid &pairingTransactionId, const QString &secret) +{ + qDebug() << "JsonRpc: confirm pairing" << pairingTransactionId.toString(); + QVariantMap params; + params.insert("pairingTransactionId", pairingTransactionId.toString()); + params.insert("secret", secret); + m_jsonClient->sendCommand("Devices.ConfirmPairing", params); +} + +void DeviceManager::removeDevice(const QUuid &deviceId) +{ + qDebug() << "JsonRpc: delete device" << deviceId.toString(); + QVariantMap params; + params.insert("deviceId", deviceId.toString()); + m_jsonClient->sendCommand("Devices.RemoveConfiguredDevice", params, this, "removeDeviceResponse"); +} + +void DeviceManager::executeAction(const QUuid &deviceId, const QUuid &actionTypeId, const QVariantList ¶ms) +{ + qDebug() << "JsonRpc: execute action " << deviceId.toString() << actionTypeId.toString() << params; + QVariantMap p; + p.insert("deviceId", deviceId.toString()); + p.insert("actionTypeId", actionTypeId.toString()); + if (!params.isEmpty()) { + p.insert("params", params); + } + + qDebug() << "Params:" << p; + m_jsonClient->sendCommand("Actions.ExecuteAction", p); +} diff --git a/guh-control/devicemanager.h b/guh-control/devicemanager.h index 5c18b0df..b277895d 100644 --- a/guh-control/devicemanager.h +++ b/guh-control/devicemanager.h @@ -27,8 +27,10 @@ #include "devices.h" #include "deviceclasses.h" #include "types/plugins.h" +#include "jsonrpc/jsonhandler.h" +#include "jsonrpc/jsonrpcclient.h" -class DeviceManager : public QObject +class DeviceManager : public JsonHandler { Q_OBJECT Q_PROPERTY(Vendors *vendors READ vendors CONSTANT) @@ -37,19 +39,44 @@ class DeviceManager : public QObject Q_PROPERTY(DeviceClasses *deviceClasses READ deviceClasses CONSTANT) public: - explicit DeviceManager(QObject *parent = 0); + explicit DeviceManager(JsonRpcClient *jsonclient, QObject *parent = 0); + + void clear(); + void init(); + + QString nameSpace() const override; Vendors *vendors() const; Plugins *plugins() const; Devices *devices() const; DeviceClasses *deviceClasses() const; + Q_INVOKABLE void addDevice(const QUuid &deviceClassId, const QVariantList &deviceParams); + Q_INVOKABLE void addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name); + Q_INVOKABLE void pairDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId); + Q_INVOKABLE void confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString()); + Q_INVOKABLE void removeDevice(const QUuid &deviceId); + Q_INVOKABLE void executeAction(const QUuid &deviceId, const QUuid &actionTypeId, const QVariantList ¶ms = QVariantList()); + +private: + Q_INVOKABLE void notificationReceived(const QVariantMap &data); + Q_INVOKABLE void getVendorsResponse(const QVariantMap ¶ms); + Q_INVOKABLE void getSupportedDevicesResponse(const QVariantMap ¶ms); + Q_INVOKABLE void getPluginsResponse(const QVariantMap ¶ms); + Q_INVOKABLE void getConfiguredDevicesResponse(const QVariantMap ¶ms); + Q_INVOKABLE void addDeviceResponse(const QVariantMap ¶ms); + Q_INVOKABLE void removeDeviceResponse(const QVariantMap ¶ms); + + + private: Vendors *m_vendors; Plugins *m_plugins; Devices *m_devices; DeviceClasses *m_deviceClasses; + JsonRpcClient *m_jsonClient = nullptr; + }; #endif // DEVICEMANAGER_H diff --git a/guh-control/engine.cpp b/guh-control/engine.cpp index ba162d94..dafaa900 100644 --- a/guh-control/engine.cpp +++ b/guh-control/engine.cpp @@ -58,8 +58,8 @@ GuhConnection *Engine::connection() const Engine::Engine(QObject *parent) : QObject(parent), m_connection(new GuhConnection(this)), - m_deviceManager(new DeviceManager(this)), - m_jsonRpcClient(new JsonRpcClient(m_connection, this)) + m_jsonRpcClient(new JsonRpcClient(m_connection, this)), + m_deviceManager(new DeviceManager(m_jsonRpcClient, this)) { connect(m_jsonRpcClient, &JsonRpcClient::connectedChanged, this, &Engine::onConnectedChanged); } @@ -67,14 +67,10 @@ Engine::Engine(QObject *parent) : void Engine::onConnectedChanged(bool connected) { qDebug() << "Engine: connected changed:" << connected; - if (!connected) { - deviceManager()->devices()->clearModel(); - deviceManager()->deviceClasses()->clearModel(); - deviceManager()->vendors()->clearModel(); - deviceManager()->plugins()->clearModel(); - } else { + deviceManager()->clear(); + if (connected) { if (!jsonRpcClient()->initialSetupRequired() && !jsonRpcClient()->authenticationRequired()) { - jsonRpcClient()->getVendors(); + deviceManager()->init(); } } } diff --git a/guh-control/engine.h b/guh-control/engine.h index ec79697e..fb525708 100644 --- a/guh-control/engine.h +++ b/guh-control/engine.h @@ -52,8 +52,8 @@ private: static Engine *s_instance; GuhConnection *m_connection; - DeviceManager *m_deviceManager; JsonRpcClient *m_jsonRpcClient; + DeviceManager *m_deviceManager; private slots: void onConnectedChanged(bool connected); diff --git a/guh-control/guhconnection.cpp b/guh-control/guhconnection.cpp index 996ad223..c889d343 100644 --- a/guh-control/guhconnection.cpp +++ b/guh-control/guhconnection.cpp @@ -61,6 +61,7 @@ QString GuhConnection::url() const void GuhConnection::sendData(const QByteArray &data) { if (connected()) { + qDebug() << "sending data:" << data; m_currentInterface->sendData(data); } else { qWarning() << "Not connected. Cannot send."; diff --git a/guh-control/jsonrpc/actionhandler.cpp b/guh-control/jsonrpc/actionhandler.cpp deleted file mode 100644 index a5615e82..00000000 --- a/guh-control/jsonrpc/actionhandler.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "actionhandler.h" - -#include - -ActionHandler::ActionHandler(QObject *parent) : - JsonHandler(parent) -{ -} - -QString ActionHandler::nameSpace() const -{ - return "Actions"; -} - -void ActionHandler::processExecuteAction(const QVariantMap ¶ms) -{ - // response handled in the ui - Q_UNUSED(params); -} - diff --git a/guh-control/jsonrpc/actionhandler.h b/guh-control/jsonrpc/actionhandler.h deleted file mode 100644 index 755113c0..00000000 --- a/guh-control/jsonrpc/actionhandler.h +++ /dev/null @@ -1,40 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef ACTIONHANDLER_H -#define ACTIONHANDLER_H - -#include - -#include "jsonhandler.h" - -class ActionHandler : public JsonHandler -{ - Q_OBJECT -public: - explicit ActionHandler(QObject *parent = 0); - - QString nameSpace() const; - - Q_INVOKABLE void processExecuteAction(const QVariantMap ¶ms); - -}; - -#endif // ACTIONHANDLER_H diff --git a/guh-control/jsonrpc/configurationhandler.cpp b/guh-control/jsonrpc/configurationhandler.cpp deleted file mode 100644 index 3c95b2c4..00000000 --- a/guh-control/jsonrpc/configurationhandler.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "configurationhandler.h" - -ConfigurationHandler::ConfigurationHandler(QObject *parent) : - JsonHandler(parent) -{ - -} - -QString ConfigurationHandler::nameSpace() const -{ - return "Configuration"; -} diff --git a/guh-control/jsonrpc/configurationhandler.h b/guh-control/jsonrpc/configurationhandler.h deleted file mode 100644 index 2b6f2a11..00000000 --- a/guh-control/jsonrpc/configurationhandler.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef CONFIGURATIONHANDLER_H -#define CONFIGURATIONHANDLER_H - -#include - -#include "jsonhandler.h" - -class ConfigurationHandler : public JsonHandler -{ - Q_OBJECT -public: - explicit ConfigurationHandler(QObject *parent = 0); - - QString nameSpace() const; - -signals: - -public slots: -}; - -#endif // CONFIGURATIONHANDLER_H diff --git a/guh-control/jsonrpc/devicehandler.cpp b/guh-control/jsonrpc/devicehandler.cpp deleted file mode 100644 index 05a27d1f..00000000 --- a/guh-control/jsonrpc/devicehandler.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "devicehandler.h" -#include "jsontypes.h" -#include "engine.h" -#include "types/states.h" -#include "types/deviceclass.h" - -#include - -DeviceHandler::DeviceHandler(QObject *parent) : - JsonHandler(parent) -{ - -} - -QString DeviceHandler::nameSpace() const -{ - return QString("Devices"); -} - -void DeviceHandler::processGetSupportedVendors(const QVariantMap ¶ms) -{ - qDebug() << "Got GetSupportedVendors response"; - if (params.value("params").toMap().keys().contains("vendors")) { - QVariantList vendorList = params.value("params").toMap().value("vendors").toList(); - foreach (QVariant vendorVariant, vendorList) { - Vendor *vendor = JsonTypes::unpackVendor(vendorVariant.toMap(), Engine::instance()->deviceManager()->vendors()); - Engine::instance()->deviceManager()->vendors()->addVendor(vendor); - } - } - - qDebug() << "start getting deviceClass at" << QDateTime::currentDateTime(); - - Engine::instance()->jsonRpcClient()->getDeviceClasses(); - qDebug() << "call done at" << QDateTime::currentDateTime(); -} - -void DeviceHandler::processGetPlugins(const QVariantMap ¶ms) -{ - if (params.value("params").toMap().keys().contains("plugins")) { - QVariantList pluginList = params.value("params").toMap().value("plugins").toList(); - foreach (QVariant pluginVariant, pluginList) { - Plugin *plugin = JsonTypes::unpackPlugin(pluginVariant.toMap(), Engine::instance()->deviceManager()->plugins()); - Engine::instance()->deviceManager()->plugins()->addPlugin(plugin); - } - } - Engine::instance()->jsonRpcClient()->getDevices(); - -} - -void DeviceHandler::processGetSupportedDevices(const QVariantMap ¶ms) -{ - if (params.value("params").toMap().keys().contains("deviceClasses")) { - QVariantList deviceClassList = params.value("params").toMap().value("deviceClasses").toList(); - foreach (QVariant deviceClassVariant, deviceClassList) { - DeviceClass *deviceClass = JsonTypes::unpackDeviceClass(deviceClassVariant.toMap(), Engine::instance()->deviceManager()->deviceClasses()); - qDebug() << "Server has device class:" << deviceClass->name() << deviceClass->id(); - Engine::instance()->deviceManager()->deviceClasses()->addDeviceClass(deviceClass); - } - } - Engine::instance()->jsonRpcClient()->getPlugins(); -} - -void DeviceHandler::processGetConfiguredDevices(const QVariantMap ¶ms) -{ - if (params.value("params").toMap().keys().contains("devices")) { - QVariantList deviceList = params.value("params").toMap().value("devices").toList(); - foreach (QVariant deviceVariant, deviceList) { - Device *device = JsonTypes::unpackDevice(deviceVariant.toMap(), Engine::instance()->deviceManager()->devices()); - if (!device) continue; - Engine::instance()->deviceManager()->devices()->addDevice(device); - - //qDebug() << QJsonDocument::fromVariant(deviceVariant).toJson(); - - // set initial state values - QVariantList stateVariantList = deviceVariant.toMap().value("states").toList(); - foreach (const QVariant &stateMap, stateVariantList) { - QUuid stateTypeId = stateMap.toMap().value("stateTypeId").toUuid(); - QVariant value = stateMap.toMap().value("value"); - device->setStateValue(stateTypeId, value); - } - } - } -} - -void DeviceHandler::processRemoveConfiguredDevice(const QVariantMap ¶ms) -{ - // response handled in the ui - Q_UNUSED(params); -} - -void DeviceHandler::processAddConfiguredDevice(const QVariantMap ¶ms) -{ - // response handled in the ui - Q_UNUSED(params); -} - -void DeviceHandler::processGetDiscoveredDevices(const QVariantMap ¶ms) -{ - // response handled in the ui - Q_UNUSED(params); -} - -void DeviceHandler::processPairDevice(const QVariantMap ¶ms) -{ - // response handled in the ui - Q_UNUSED(params); -} - -void DeviceHandler::processConfirmPairing(const QVariantMap ¶ms) -{ - // response handled in the ui - Q_UNUSED(params); -} - -void DeviceHandler::processDeviceRemoved(const QVariantMap ¶ms) -{ - QUuid deviceId = params.value("params").toMap().value("deviceId").toUuid(); - qDebug() << "JsonRpc: Notification: Device removed" << deviceId.toString(); - Device *device = Engine::instance()->deviceManager()->devices()->getDevice(deviceId); - Engine::instance()->deviceManager()->devices()->removeDevice(device); - device->deleteLater(); -} - -void DeviceHandler::processDeviceAdded(const QVariantMap ¶ms) -{ - if (params.value("params").toMap().keys().contains("device")) { - QVariantMap deviceVariant = params.value("params").toMap().value("device").toMap(); - Device *device = JsonTypes::unpackDevice(deviceVariant, Engine::instance()->deviceManager()->devices()); - qDebug() << "JsonRpc: Notification: Device added" << device->id().toString(); - Engine::instance()->deviceManager()->devices()->addDevice(device); - } -} - -void DeviceHandler::processStateChanged(const QVariantMap ¶ms) -{ - QVariantMap notification = params.value("params").toMap(); - QUuid deviceId = notification.value("deviceId").toUuid(); - - Device *device = Engine::instance()->deviceManager()->devices()->getDevice(deviceId); - - if (!device) { - qWarning() << "JsonRpc: ERROR: could not find device for state changed notification"; - return; - } - - QUuid stateTypeId = notification.value("stateTypeId").toUuid(); - QVariant value = notification.value("value"); - - device->setStateValue(stateTypeId, value); -} diff --git a/guh-control/jsonrpc/devicehandler.h b/guh-control/jsonrpc/devicehandler.h deleted file mode 100644 index 4e823296..00000000 --- a/guh-control/jsonrpc/devicehandler.h +++ /dev/null @@ -1,56 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef DEVICEHANDLER_H -#define DEVICEHANDLER_H - -#include - -#include "jsonhandler.h" - -class DeviceHandler : public JsonHandler -{ - Q_OBJECT -public: - explicit DeviceHandler(QObject *parent = 0); - - QString nameSpace() const; - - // Get methods internal - Q_INVOKABLE void processGetSupportedVendors(const QVariantMap ¶ms); - Q_INVOKABLE void processGetPlugins(const QVariantMap ¶ms); - Q_INVOKABLE void processGetSupportedDevices(const QVariantMap ¶ms); - Q_INVOKABLE void processGetConfiguredDevices(const QVariantMap ¶ms); - - // Methods ui - Q_INVOKABLE void processRemoveConfiguredDevice(const QVariantMap ¶ms); - Q_INVOKABLE void processAddConfiguredDevice(const QVariantMap ¶ms); - Q_INVOKABLE void processGetDiscoveredDevices(const QVariantMap ¶ms); - Q_INVOKABLE void processPairDevice(const QVariantMap ¶ms); - Q_INVOKABLE void processConfirmPairing(const QVariantMap ¶ms); - - // Notifications - Q_INVOKABLE void processDeviceRemoved(const QVariantMap ¶ms); - Q_INVOKABLE void processDeviceAdded(const QVariantMap ¶ms); - Q_INVOKABLE void processStateChanged(const QVariantMap ¶ms); - -}; - -#endif // DEVICEHANDLER_H diff --git a/guh-control/jsonrpc/eventhandler.cpp b/guh-control/jsonrpc/eventhandler.cpp deleted file mode 100644 index b3e6b101..00000000 --- a/guh-control/jsonrpc/eventhandler.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "eventhandler.h" - -EventHandler::EventHandler(QObject *parent) : - JsonHandler(parent) -{ - -} - -QString EventHandler::nameSpace() const -{ - return "Events"; -} - -void EventHandler::processEventTriggered(const QVariantMap ¶ms) -{ - Q_UNUSED(params); -} - diff --git a/guh-control/jsonrpc/eventhandler.h b/guh-control/jsonrpc/eventhandler.h deleted file mode 100644 index f3c2c49c..00000000 --- a/guh-control/jsonrpc/eventhandler.h +++ /dev/null @@ -1,40 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef EVENTHANDLER_H -#define EVENTHANDLER_H - -#include - -#include "jsonhandler.h" - -class EventHandler : public JsonHandler -{ - Q_OBJECT -public: - explicit EventHandler(QObject *parent = 0); - - QString nameSpace() const; - - Q_INVOKABLE void processEventTriggered(const QVariantMap ¶ms); - -}; - -#endif // EVENTHANDLER_H diff --git a/guh-control/jsonrpc/jsonrpcclient.cpp b/guh-control/jsonrpc/jsonrpcclient.cpp index 1a39580e..709cd65a 100644 --- a/guh-control/jsonrpc/jsonrpcclient.cpp +++ b/guh-control/jsonrpc/jsonrpcclient.cpp @@ -34,19 +34,6 @@ JsonRpcClient::JsonRpcClient(GuhConnection *connection, QObject *parent) : m_id(0), m_connection(connection) { - m_deviceHandler = new DeviceHandler(this); - m_actionHandler = new ActionHandler(this); - m_eventHandler = new EventHandler(this); - m_loggingHandler = new LoggingHandler(this); - m_networkManagerHandler = new NetworkManagerHandler(this); - - m_handlers.insert(m_deviceHandler->nameSpace(), m_deviceHandler); - m_handlers.insert(m_actionHandler->nameSpace(), m_actionHandler); - m_handlers.insert(m_eventHandler->nameSpace(), m_eventHandler); - m_handlers.insert(m_loggingHandler->nameSpace(), m_loggingHandler); - m_handlers.insert(m_networkManagerHandler->nameSpace(), m_networkManagerHandler); - m_handlers.insert(nameSpace(), this); - connect(m_connection, &GuhConnection::connectedChanged, this, &JsonRpcClient::onInterfaceConnectedChanged); connect(m_connection, &GuhConnection::dataAvailable, this, &JsonRpcClient::dataReceived); } @@ -56,47 +43,42 @@ QString JsonRpcClient::nameSpace() const return QStringLiteral("JSONRPC"); } -void JsonRpcClient::getVendors() +void JsonRpcClient::registerNotificationHandler(JsonHandler *handler, const QString &method) { - qDebug() << "JsonRpc: get vendors"; - JsonRpcReply *reply = createReply("Devices", "GetSupportedVendors"); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); + if (m_notificationHandlers.contains(handler->nameSpace())) { + qWarning() << "Already have a notification handler for" << handler->nameSpace(); + return; + } + m_notificationHandlers.insert(handler->nameSpace(), qMakePair(handler, method)); } -void JsonRpcClient::getPlugins() +JsonRpcReply *JsonRpcClient::sendCommand(const QString &method, const QVariantMap ¶ms, QObject *caller, const QString &callbackMethod) { - qDebug() << "JsonRpc: get plugins"; - JsonRpcReply *reply = createReply("Devices", "GetPlugins"); + JsonRpcReply *reply = createReply(method, params, caller, callbackMethod); m_replies.insert(reply->commandId(), reply); sendRequest(reply->requestMap()); + } -void JsonRpcClient::getDevices() +JsonRpcReply *JsonRpcClient::sendCommand(const QString &method, QObject *caller, const QString &callbackMethod) { - qDebug() << "JsonRpc: get devices"; - JsonRpcReply *reply = createReply("Devices", "GetConfiguredDevices"); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); -} - -void JsonRpcClient::getDeviceClasses() -{ - qDebug() << "JsonRpc: get device classes"; - JsonRpcReply *reply = createReply("Devices", "GetSupportedDevices"); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); + return sendCommand(method, QVariantMap(), caller, callbackMethod); } void JsonRpcClient::setNotificationsEnabled(bool enabled) { QVariantMap params; params.insert("notificationsEnabled", enabled); - JsonRpcReply *reply = createReply("JSONRPC", "SetNotificationsEnabled", params); + JsonRpcReply *reply = createReply("JSONRPC.SetNotificationsEnabled", params, this, "setNotificationsEnabledResponse"); m_replies.insert(reply->commandId(), reply); sendRequest(reply->requestMap()); } +void JsonRpcClient::setNotificationsEnabledResponse(const QVariantMap ¶ms) +{ + qDebug() << "Notifications enabled:" << params; +} + bool JsonRpcClient::connected() const { return m_connected; @@ -117,7 +99,7 @@ int JsonRpcClient::createUser(const QString &username, const QString &password) QVariantMap params; params.insert("username", username); params.insert("password", password); - JsonRpcReply* reply = createReply("JSONRPC", "CreateUser", params); + JsonRpcReply* reply = createReply("JSONRPC.CreateUser", params, this, "processCreateUser"); m_replies.insert(reply->commandId(), reply); m_connection->sendData(QJsonDocument::fromVariant(reply->requestMap()).toJson()); return reply->commandId(); @@ -129,104 +111,12 @@ int JsonRpcClient::authenticate(const QString &username, const QString &password params.insert("username", username); params.insert("password", password); params.insert("deviceName", deviceName); - JsonRpcReply* reply = createReply("JSONRPC", "Authenticate", params); + JsonRpcReply* reply = createReply("JSONRPC.Authenticate", params, this, "processAuthenticate"); m_replies.insert(reply->commandId(), reply); m_connection->sendData(QJsonDocument::fromVariant(reply->requestMap()).toJson()); return reply->commandId(); } -int JsonRpcClient::addDevice(const QUuid &deviceClassId, const QVariantList &deviceParams) -{ - qDebug() << "JsonRpc: add device " << deviceClassId.toString(); - QVariantMap params; - params.insert("deviceClassId", deviceClassId.toString()); - params.insert("deviceParams", deviceParams); - JsonRpcReply *reply = createReply("Devices", "AddConfiguredDevice", params); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); - return reply->commandId(); -} - -int JsonRpcClient::addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name) -{ - qDebug() << "JsonRpc: add discovered device " << deviceClassId.toString(); - QVariantMap params; - params.insert("deviceClassId", deviceClassId.toString()); - params.insert("name", name); - params.insert("deviceDescriptorId", deviceDescriptorId.toString()); - JsonRpcReply *reply = createReply("Devices", "AddConfiguredDevice", params); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); - return reply->commandId(); -} - -int JsonRpcClient::pairDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId) -{ - qDebug() << "JsonRpc: pair device " << deviceClassId.toString(); - QVariantMap params; - params.insert("name", "name"); - params.insert("deviceClassId", deviceClassId.toString()); - params.insert("deviceDescriptorId", deviceDescriptorId.toString()); - JsonRpcReply *reply = createReply("Devices", "PairDevice", params); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); - return reply->commandId(); -} - -int JsonRpcClient::confirmPairing(const QUuid &pairingTransactionId, const QString &secret) -{ - qDebug() << "JsonRpc: confirm pairing" << pairingTransactionId.toString(); - QVariantMap params; - params.insert("pairingTransactionId", pairingTransactionId.toString()); - params.insert("secret", secret); - JsonRpcReply *reply = createReply("Devices", "ConfirmPairing", params); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); - return reply->commandId(); -} - -int JsonRpcClient::removeDevice(const QUuid &deviceId) -{ - qDebug() << "JsonRpc: delete device" << deviceId.toString(); - QVariantMap params; - params.insert("deviceId", deviceId.toString()); - JsonRpcReply *reply = createReply("Devices", "RemoveConfiguredDevice", params); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); - return reply->commandId(); -} - -int JsonRpcClient::discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams) -{ - qDebug() << "JsonRpc: discover devices " << deviceClassId.toString(); - QVariantMap params; - params.insert("deviceClassId", deviceClassId.toString()); - if (!discoveryParams.isEmpty()) { - params.insert("discoveryParams", discoveryParams); - } - - JsonRpcReply *reply = createReply("Devices", "GetDiscoveredDevices", params); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); - return reply->commandId(); -} - -int JsonRpcClient::executeAction(const QUuid &deviceId, const QUuid &actionTypeId, const QVariantList ¶ms) -{ - qDebug() << "JsonRpc: execute action " << deviceId.toString() << actionTypeId.toString() << params; - QVariantMap p; - p.insert("deviceId", deviceId.toString()); - p.insert("actionTypeId", actionTypeId.toString()); - if (!params.isEmpty()) { - p.insert("params", params); - } - - qDebug() << "Params:" << p; - JsonRpcReply *reply = createReply("Actions", "ExecuteAction", p); - m_replies.insert(reply->commandId(), reply); - sendRequest(reply->requestMap()); - return reply->commandId(); -} void JsonRpcClient::processAuthenticate(const QVariantMap &data) { @@ -252,10 +142,15 @@ void JsonRpcClient::processCreateUser(const QVariantMap &data) } } -JsonRpcReply *JsonRpcClient::createReply(QString nameSpace, QString method, QVariantMap params) +JsonRpcReply *JsonRpcClient::createReply(const QString &method, const QVariantMap ¶ms, QObject* caller, const QString &callback) { + QStringList callParts = method.split('.'); + if (callParts.count() != 2) { + qWarning() << "Invalid method. Must be Namespace.Method"; + return nullptr; + } m_id++; - return new JsonRpcReply(m_id, nameSpace, method, params, this); + return new JsonRpcReply(m_id, callParts.first(), callParts.last(), params, caller, callback); } void JsonRpcClient::sendRequest(const QVariantMap &request) @@ -281,7 +176,6 @@ void JsonRpcClient::onInterfaceConnectedChanged(bool connected) void JsonRpcClient::dataReceived(const QByteArray &data) { m_receiveBuffer.append(data); -// qDebug() << "received response" << m_receiveBuffer; int splitIndex = m_receiveBuffer.indexOf("}\n{") + 1; if (splitIndex <= 0) { @@ -293,6 +187,7 @@ void JsonRpcClient::dataReceived(const QByteArray &data) // qWarning() << "Could not parse json data from guh" << data << error.errorString(); return; } +// qDebug() << "received response" << m_receiveBuffer.left(splitIndex); m_receiveBuffer = m_receiveBuffer.right(m_receiveBuffer.length() - splitIndex - 1); if (!m_receiveBuffer.isEmpty()) { staticMetaObject.invokeMethod(this, "dataReceived", Qt::QueuedConnection, Q_ARG(QByteArray, QByteArray())); @@ -329,11 +224,11 @@ void JsonRpcClient::dataReceived(const QByteArray &data) int commandId = dataMap.value("id").toInt(); JsonRpcReply *reply = m_replies.take(commandId); if (reply) { -// qDebug() << QString("JsonRpc: got response for %1.%2").arg(reply->nameSpace(), reply->method()) << data; - JsonHandler *handler = m_handlers.value(reply->nameSpace()); + qDebug() << QString("JsonRpc: got response for %1.%2").arg(reply->nameSpace(), reply->method()) << reply->callback() << reply->callback(); - if (!QMetaObject::invokeMethod(handler, QString("process" + reply->method()).toLatin1().data(), Q_ARG(QVariantMap, dataMap))) - qWarning() << "JsonRpc: method not implemented:" << reply->method(); + if (reply->caller() != nullptr && !reply->callback().isEmpty()) { + QMetaObject::invokeMethod(reply->caller(), reply->callback().toLatin1().data(), Q_ARG(QVariantMap, dataMap)); + } emit responseReceived(reply->commandId(), dataMap.value("params").toMap()); return; @@ -343,26 +238,25 @@ void JsonRpcClient::dataReceived(const QByteArray &data) if (dataMap.contains("notification")) { QStringList notification = dataMap.value("notification").toString().split("."); QString nameSpace = notification.first(); - QString method = notification.last(); - JsonHandler *handler = m_handlers.value(nameSpace); + JsonHandler *handler = m_notificationHandlers.value(nameSpace).first; if (!handler) { qWarning() << "JsonRpc: handler not implemented:" << nameSpace; return; } - if (!QMetaObject::invokeMethod(handler, QString("process" + method).toLatin1().data(), Q_ARG(QVariantMap, dataMap))) - qWarning() << "method not implemented"; - + QMetaObject::invokeMethod(handler, m_notificationHandlers.value(nameSpace).second.toLatin1().data(), Q_ARG(QVariantMap, dataMap)); } } -JsonRpcReply::JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params, QObject *parent): - QObject(parent), +JsonRpcReply::JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params, QObject *caller, const QString &callback): + QObject(caller), m_commandId(commandId), m_nameSpace(nameSpace), m_method(method), - m_params(params) + m_params(params), + m_caller(caller), + m_callback(callback) { } @@ -396,3 +290,13 @@ QVariantMap JsonRpcReply::requestMap() return request; } + +QObject* JsonRpcReply::caller() const +{ + return m_caller; +} + +QString JsonRpcReply::callback() const +{ + return m_callback; +} diff --git a/guh-control/jsonrpc/jsonrpcclient.h b/guh-control/jsonrpc/jsonrpcclient.h index 67a6c671..1f03a8b2 100644 --- a/guh-control/jsonrpc/jsonrpcclient.h +++ b/guh-control/jsonrpc/jsonrpcclient.h @@ -24,12 +24,8 @@ #include #include -#include "devicehandler.h" -#include "actionhandler.h" -#include "eventhandler.h" -#include "logginghandler.h" -#include "networkmanagerhandler.h" #include "guhconnection.h" +#include "jsonhandler.h" class JsonRpcReply; class Param; @@ -47,12 +43,10 @@ public: QString nameSpace() const override; - // internal - void getVendors(); - void getPlugins(); - void getDevices(); - void getDeviceClasses(); - void setNotificationsEnabled(bool enabled); + void registerNotificationHandler(JsonHandler *handler, const QString &method); + + JsonRpcReply* sendCommand(const QString &method, const QVariantMap ¶ms, QObject *caller = nullptr, const QString &callbackMethod = QString()); + JsonRpcReply* sendCommand(const QString &method, QObject *caller = nullptr, const QString &callbackMethod = QString()); void setConnection(GuhConnection *connection); bool connected() const; @@ -62,37 +56,31 @@ public: // ui methods Q_INVOKABLE int createUser(const QString &username, const QString &password); Q_INVOKABLE int authenticate(const QString &username, const QString &password, const QString &deviceName); - Q_INVOKABLE int addDevice(const QUuid &deviceClassId, const QVariantList &deviceParams); - Q_INVOKABLE int addDiscoveredDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId, const QString &name); - Q_INVOKABLE int pairDevice(const QUuid &deviceClassId, const QUuid &deviceDescriptorId); - Q_INVOKABLE int confirmPairing(const QUuid &pairingTransactionId, const QString &secret = QString()); - Q_INVOKABLE int removeDevice(const QUuid &deviceId); - Q_INVOKABLE int discoverDevices(const QUuid &deviceClassId, const QVariantList &discoveryParams = QVariantList()); - Q_INVOKABLE int executeAction(const QUuid &deviceId, const QUuid &actionTypeId, const QVariantList ¶ms = QVariantList()); // json handler Q_INVOKABLE void processAuthenticate(const QVariantMap &data); Q_INVOKABLE void processCreateUser(const QVariantMap &data); + signals: void initialSetupRequiredChanged(); void authenticationRequiredChanged(); void connectedChanged(bool connected); void tokenChanged(); + void responseReceived(const int &commandId, const QVariantMap &response); + +private slots: + void onInterfaceConnectedChanged(bool connected); + void dataReceived(const QByteArray &data); + private: int m_id; - QHash m_handlers; + // < namespace, > + QHash > m_notificationHandlers; QHash m_replies; - GuhConnection *m_connection = nullptr; - DeviceHandler *m_deviceHandler; - ActionHandler *m_actionHandler; - EventHandler *m_eventHandler; - LoggingHandler *m_loggingHandler; - NetworkManagerHandler *m_networkManagerHandler; - - JsonRpcReply *createReply(QString nameSpace, QString method, QVariantMap params = QVariantMap()); + JsonRpcReply *createReply(const QString &method, const QVariantMap ¶ms, QObject *caller, const QString &callback); bool m_connected = false; bool m_initialSetupRequired = false; @@ -101,15 +89,10 @@ private: QByteArray m_token; QByteArray m_receiveBuffer; + void setNotificationsEnabled(bool enabled); + Q_INVOKABLE void setNotificationsEnabledResponse(const QVariantMap ¶ms); void sendRequest(const QVariantMap &request); -signals: - void responseReceived(const int &commandId, const QVariantMap &response); - -public slots: - void onInterfaceConnectedChanged(bool connected); - void dataReceived(const QByteArray &data); - }; @@ -117,7 +100,7 @@ class JsonRpcReply : public QObject { Q_OBJECT public: - explicit JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params = QVariantMap(), QObject *parent = 0); + explicit JsonRpcReply(int commandId, QString nameSpace, QString method, QVariantMap params = QVariantMap(), QObject *caller = 0, const QString &callback = QString()); int commandId() const; QString nameSpace() const; @@ -125,11 +108,17 @@ public: QVariantMap params() const; QVariantMap requestMap(); + QObject *caller() const; + QString callback() const; + private: int m_commandId; QString m_nameSpace; QString m_method; QVariantMap m_params; + + QObject *m_caller; + QString m_callback; }; diff --git a/guh-control/jsonrpc/logginghandler.cpp b/guh-control/jsonrpc/logginghandler.cpp deleted file mode 100644 index 484b6e13..00000000 --- a/guh-control/jsonrpc/logginghandler.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "logginghandler.h" - -LoggingHandler::LoggingHandler(QObject *parent) : - JsonHandler(parent) -{ - -} - -QString LoggingHandler::nameSpace() const -{ - return "Logging"; -} - -void LoggingHandler::processLogEntryAdded(const QVariantMap ¶ms) -{ - Q_UNUSED(params); -} - -void LoggingHandler::processLogDatabaseUpdated(const QVariantMap ¶ms) -{ - Q_UNUSED(params); -} - diff --git a/guh-control/jsonrpc/logginghandler.h b/guh-control/jsonrpc/logginghandler.h deleted file mode 100644 index 77f3e6aa..00000000 --- a/guh-control/jsonrpc/logginghandler.h +++ /dev/null @@ -1,41 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2017 Simon Stuerz * - * * - * This file is part of guh-control. * - * * - * guh-control is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, version 3 of the License. * - * * - * guh-control is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with guh-control. If not, see . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef LOGGINGHANDLER_H -#define LOGGINGHANDLER_H - -#include - -#include "jsonhandler.h" - -class LoggingHandler : public JsonHandler -{ - Q_OBJECT -public: - explicit LoggingHandler(QObject *parent = 0); - - QString nameSpace() const; - - Q_INVOKABLE void processLogEntryAdded(const QVariantMap ¶ms); - Q_INVOKABLE void processLogDatabaseUpdated(const QVariantMap ¶ms); - -}; - -#endif // LOGGINGHANDLER_H diff --git a/guh-control/jsonrpc/networkmanagerhandler.cpp b/guh-control/jsonrpc/networkmanagerhandler.cpp deleted file mode 100644 index bf4d29a3..00000000 --- a/guh-control/jsonrpc/networkmanagerhandler.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "networkmanagerhandler.h" - -#include - -NetworkManagerHandler::NetworkManagerHandler(QObject *parent) : - JsonHandler(parent) -{ - -} - -QString NetworkManagerHandler::nameSpace() const -{ - return "NetworkManager"; -} - -void NetworkManagerHandler::processWirelessNetworkDeviceChanged(const QVariantMap ¶ms) -{ - Q_UNUSED(params); -} diff --git a/guh-control/jsonrpc/networkmanagerhandler.h b/guh-control/jsonrpc/networkmanagerhandler.h deleted file mode 100644 index 637c7154..00000000 --- a/guh-control/jsonrpc/networkmanagerhandler.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef NETWORKMANAGERHANDLER_H -#define NETWORKMANAGERHANDLER_H - -#include - -#include "jsonhandler.h" - -class NetworkManagerHandler : public JsonHandler -{ - Q_OBJECT -public: - explicit NetworkManagerHandler(QObject *parent = 0); - - QString nameSpace() const; - - Q_INVOKABLE void processWirelessNetworkDeviceChanged(const QVariantMap ¶ms); - -signals: - -public slots: -}; - -#endif // NETWORKMANAGERHANDLER_H diff --git a/guh-control/resources.qrc b/guh-control/resources.qrc index c23c5641..6271db2b 100644 --- a/guh-control/resources.qrc +++ b/guh-control/resources.qrc @@ -50,5 +50,7 @@ ui/components/ColorIcon.qml ui/images/torch-on.svg ui/images/media-preview-start.svg + ui/MagicPage.qml + ui/magic/NewRulePage.qml diff --git a/guh-control/ui/MagicPage.qml b/guh-control/ui/MagicPage.qml new file mode 100644 index 00000000..4f61a566 --- /dev/null +++ b/guh-control/ui/MagicPage.qml @@ -0,0 +1,21 @@ +import QtQuick 2.8 +import QtQuick.Controls 2.2 +import "components" + +Page { + id: root + header: GuhHeader { + text: "Magic" + backButtonVisible: false + + HeaderButton { + imageSource: "images/add.svg" + onClicked: pageStack.push(Qt.resolvedUrl("magic/NewRulePage.qml")) + } + } + + ListView { + anchors.fill: parent +// model: Engine. + } +} diff --git a/guh-control/ui/MainPage.qml b/guh-control/ui/MainPage.qml index 600ec789..7eb0174f 100644 --- a/guh-control/ui/MainPage.qml +++ b/guh-control/ui/MainPage.qml @@ -29,7 +29,7 @@ Page { } - Item { + MagicPage { } diff --git a/guh-control/ui/customviews/ExtendedVolumeController.qml b/guh-control/ui/customviews/ExtendedVolumeController.qml index 75df6a95..6c3d36a9 100644 --- a/guh-control/ui/customviews/ExtendedVolumeController.qml +++ b/guh-control/ui/customviews/ExtendedVolumeController.qml @@ -20,7 +20,7 @@ CustomViewBase { muteParam["paramTypeId"] = deviceClass.stateTypes.findByName("mute").id muteParam["value"] = !isMuted paramList.push(muteParam) - Engine.jsonRpcClient.executeAction(root.device.id, deviceClass.actionTypes.findByName("mute").id, paramList) + Engine.deviceManager.executeAction(root.device.id, deviceClass.actionTypes.findByName("mute").id, paramList) } } @@ -36,7 +36,7 @@ CustomViewBase { muteParam["paramTypeId"] = deviceClass.stateTypes.findByName("volume").id muteParam["value"] = value paramList.push(muteParam) - Engine.jsonRpcClient.executeAction(root.device.id, deviceClass.actionTypes.findByName("volume").id, paramList) + Engine.deviceManager.executeAction(root.device.id, deviceClass.actionTypes.findByName("volume").id, paramList) } } } diff --git a/guh-control/ui/customviews/MediaControllerView.qml b/guh-control/ui/customviews/MediaControllerView.qml index c83c96f4..4f7ead41 100644 --- a/guh-control/ui/customviews/MediaControllerView.qml +++ b/guh-control/ui/customviews/MediaControllerView.qml @@ -10,7 +10,7 @@ CustomViewBase { function executeAction(actionName) { var actionTypeId = deviceClass.actionTypes.findByName(actionName).id; print("executing", device, device.id, actionTypeId, actionName, deviceClass.actionTypes) - Engine.jsonRpcClient.executeAction(device.id, actionTypeId) + Engine.deviceManager.executeAction(device.id, actionTypeId) } property var playbackState: device.states.getState(deviceClass.stateTypes.findByName("playbackStatus").id) diff --git a/guh-control/ui/devicelistpages/LightsDeviceListPage.qml b/guh-control/ui/devicelistpages/LightsDeviceListPage.qml index 9c2c754a..fc41cba8 100644 --- a/guh-control/ui/devicelistpages/LightsDeviceListPage.qml +++ b/guh-control/ui/devicelistpages/LightsDeviceListPage.qml @@ -32,7 +32,7 @@ Page { param1["paramTypeId"] = actionType.paramTypes.get(0).id; param1["value"] = checked; params.push(param1) - Engine.jsonRpcClient.executeAction(device.id, actionType.id, params) + Engine.deviceManager.executeAction(device.id, actionType.id, params) } } } @@ -66,8 +66,9 @@ Page { visible: model.interfaces.indexOf("dimmablelight") >= 0 property var stateType: deviceClass.stateTypes.findByName("brightness"); property var actionType: deviceClass.actionTypes.findByName("brightness"); + property var actionState: device.states.getState(stateType.id) from: 0; to: 100 - value: device.stateValue(stateType.id) + value: actionState.value onValueChanged: { if (pressed) { var params = []; @@ -75,21 +76,22 @@ Page { param1["paramTypeId"] = actionType.paramTypes.get(0).id; param1["value"] = value; params.push(param1) - Engine.jsonRpcClient.executeAction(device.id, actionType.id, params) + Engine.deviceManager.executeAction(device.id, actionType.id, params) } } } Switch { property var stateType: deviceClass.stateTypes.findByName("power"); property var actionType: deviceClass.actionTypes.findByName("power"); - checked: device.stateValue(stateType.id) === true + property var actionState: device.states.getState(stateType.id) + checked: actionState.value === true onClicked: { var params = []; var param1 = {}; param1["paramTypeId"] = actionType.paramTypes.get(0).id; param1["value"] = checked; params.push(param1) - Engine.jsonRpcClient.executeAction(device.id, actionType.id, params) + Engine.deviceManager.executeAction(device.id, actionType.id, params) } } diff --git a/guh-control/ui/devicepages/GenericDevicePage.qml b/guh-control/ui/devicepages/GenericDevicePage.qml index 696198cf..7951efae 100644 --- a/guh-control/ui/devicepages/GenericDevicePage.qml +++ b/guh-control/ui/devicepages/GenericDevicePage.qml @@ -117,7 +117,7 @@ Page { Connections { target: delegateLoader.item ? delegateLoader.item : null onExecuteAction: { - delegateLoader.commandId = Engine.jsonRpcClient.executeAction(root.device.id, model.id, params) + delegateLoader.commandId = Engine.deviceManager.executeAction(root.device.id, model.id, params) } } Connections { diff --git a/guh-control/ui/magic/NewRulePage.qml b/guh-control/ui/magic/NewRulePage.qml new file mode 100644 index 00000000..fc855805 --- /dev/null +++ b/guh-control/ui/magic/NewRulePage.qml @@ -0,0 +1,11 @@ +import QtQuick 2.8 +import QtQuick.Controls 2.2 +import "../components" + +Page { + id: root + header: GuhHeader { + text: "New rule" + onBackPressed: pageStack.pop() + } +}