From ce123f9352d5d16b5f96036f27e075941040b413 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 7 Jul 2017 17:36:06 +0200 Subject: [PATCH] emit a notification when a plugin config is changed fixes #418 --- libguh/devicemanager.cpp | 1 + libguh/devicemanager.h | 1 + server/guhcore.cpp | 1 + server/guhcore.h | 1 + server/jsonrpc/devicehandler.cpp | 13 ++++++++++ server/jsonrpc/devicehandler.h | 3 +++ tests/auto/jsonrpc/testjsonrpc.cpp | 40 ++++++++++++++++++++++++++---- 7 files changed, 55 insertions(+), 5 deletions(-) diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 72860941..f818ca55 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -370,6 +370,7 @@ DeviceManager::DeviceError DeviceManager::setPluginConfig(const PluginId &plugin } settings.endGroup(); settings.endGroup(); + emit pluginConfigChanged(plugin->pluginId(), pluginConfig); return result; } diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index eaca62b9..9af3006f 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -148,6 +148,7 @@ public: signals: void loaded(); void languageUpdated(); + void pluginConfigChanged(const PluginId &id, const ParamList &config); void eventTriggered(const Event &event); void deviceStateChanged(Device *device, const QUuid &stateTypeId, const QVariant &value); void deviceRemoved(const DeviceId &deviceId); diff --git a/server/guhcore.cpp b/server/guhcore.cpp index 1427a5d5..d0447b86 100644 --- a/server/guhcore.cpp +++ b/server/guhcore.cpp @@ -479,6 +479,7 @@ GuhCore::GuhCore(QObject *parent) : connect(m_configuration, &GuhConfiguration::localeChanged, this, &GuhCore::onLocaleChanged); + connect(m_deviceManager, &DeviceManager::pluginConfigChanged, this, &GuhCore::pluginConfigChanged); connect(m_deviceManager, &DeviceManager::eventTriggered, this, &GuhCore::gotEvent); connect(m_deviceManager, &DeviceManager::deviceStateChanged, this, &GuhCore::deviceStateChanged); connect(m_deviceManager, &DeviceManager::deviceAdded, this, &GuhCore::deviceAdded); diff --git a/server/guhcore.h b/server/guhcore.h index 6e65efe9..b36e6cf6 100644 --- a/server/guhcore.h +++ b/server/guhcore.h @@ -98,6 +98,7 @@ public: #endif signals: + void pluginConfigChanged(const PluginId &id, const ParamList &config); void eventTriggered(const Event &event); void deviceStateChanged(Device *device, const QUuid &stateTypeId, const QVariant &value); void deviceRemoved(const DeviceId &deviceId); diff --git a/server/jsonrpc/devicehandler.cpp b/server/jsonrpc/devicehandler.cpp index 8a747918..a2f49622 100644 --- a/server/jsonrpc/devicehandler.cpp +++ b/server/jsonrpc/devicehandler.cpp @@ -292,6 +292,7 @@ DeviceHandler::DeviceHandler(QObject *parent) : params.insert("device", JsonTypes::deviceRef()); setParams("DeviceChanged", params); + connect(GuhCore::instance(), &GuhCore::pluginConfigChanged, this, &DeviceHandler::pluginConfigChanged); connect(GuhCore::instance(), &GuhCore::deviceStateChanged, this, &DeviceHandler::deviceStateChanged); connect(GuhCore::instance(), &GuhCore::deviceRemoved, this, &DeviceHandler::deviceRemovedNotification); connect(GuhCore::instance(), &GuhCore::deviceAdded, this, &DeviceHandler::deviceAddedNotification); @@ -620,6 +621,18 @@ JsonReply *DeviceHandler::GetStateValues(const QVariantMap ¶ms) const return createReply(returns); } +void DeviceHandler::pluginConfigChanged(const PluginId &id, const ParamList &config) +{ + QVariantMap params; + params.insert("pluginId", id); + QVariantMap configMap; + foreach (const Param ¶m, config) { + configMap.insert(param.paramTypeId().toString(), param.value()); + } + params.insert("configuration", configMap); + emit PluginConfigurationChanged(params); +} + void DeviceHandler::deviceStateChanged(Device *device, const QUuid &stateTypeId, const QVariant &value) { QVariantMap params; diff --git a/server/jsonrpc/devicehandler.h b/server/jsonrpc/devicehandler.h index 3e128713..a3631702 100644 --- a/server/jsonrpc/devicehandler.h +++ b/server/jsonrpc/devicehandler.h @@ -57,12 +57,15 @@ public: Q_INVOKABLE JsonReply *GetStateValues(const QVariantMap ¶ms) const; signals: + void PluginConfigurationChanged(const QVariantMap ¶ms); void StateChanged(const QVariantMap ¶ms); void DeviceRemoved(const QVariantMap ¶ms); void DeviceAdded(const QVariantMap ¶ms); void DeviceChanged(const QVariantMap ¶ms); private slots: + void pluginConfigChanged(const PluginId &id, const ParamList &config); + void deviceStateChanged(Device *device, const QUuid &stateTypeId, const QVariant &value); void deviceRemovedNotification(const QUuid &deviceId); diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index 6f03f421..194c4194 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -59,6 +59,8 @@ private slots: void stateChangeEmitsNotifications(); + void pluginConfigChangeEmitsNotification(); + private: QStringList extractRefs(const QVariant &variant); @@ -364,9 +366,9 @@ void TestJSONRPC::ruleActiveChangedNotifications() qDebug() << "setting mock int state to 20"; QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockIntStateId.toString()).arg(20))); QNetworkReply *reply = nam.get(request); - reply->deleteLater(); + connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater())); - clientSpy.wait(); + spy.wait(); notificationVariant = checkNotification(clientSpy, "Rules.RuleActiveChanged"); verifyRuleError(response); @@ -381,7 +383,8 @@ void TestJSONRPC::ruleActiveChangedNotifications() QNetworkReply *reply2 = nam.get(request2); spy.wait(); QCOMPARE(spy.count(), 1); - reply2->deleteLater(); + connect(reply2, SIGNAL(finished()), reply2, SLOT(deleteLater())); + clientSpy.wait(); notificationVariant = checkNotification(clientSpy, "Rules.RuleActiveChanged"); @@ -502,7 +505,7 @@ void TestJSONRPC::stateChangeEmitsNotifications() QUuid stateTypeId("80baec19-54de-4948-ac46-31eabfaceb83"); QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(stateTypeId.toString()).arg(QString::number(newVal)))); QNetworkReply *reply = nam.get(request); - reply->deleteLater(); + connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater())); qDebug() << "Waiting for notifications"; @@ -569,7 +572,7 @@ void TestJSONRPC::stateChangeEmitsNotifications() newVal = 42; request.setUrl(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(stateTypeId.toString()).arg(newVal))); reply = nam.get(request); - reply->deleteLater(); + connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater())); // Lets wait a max of 500ms for notifications clientSpy.wait(500); @@ -585,6 +588,33 @@ void TestJSONRPC::stateChangeEmitsNotifications() QCOMPARE(response.toMap().value("params").toMap().value("value").toInt(), newVal); } +void TestJSONRPC::pluginConfigChangeEmitsNotification() +{ + QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + + QVariantMap params; + params.insert("enabled", true); + QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", params); + QCOMPARE(response.toMap().value("params").toMap().value("enabled").toBool(), true); + + params.clear(); + params.insert("pluginId", mockPluginId); + QVariantList pluginParams; + QVariantMap param1; + param1.insert("paramTypeId", configParamIntParamTypeId); + param1.insert("value", 42); + pluginParams.append(param1); + params.insert("configuration", pluginParams); + + response = injectAndWait("Devices.SetPluginConfiguration", params); + + qDebug() << "Waiting for notifications"; + QVERIFY(clientSpy.wait()); + + QVariantList notificationData = checkNotifications(clientSpy, "Devices.PluginConfigurationChanged"); + QCOMPARE(notificationData.first().toMap().value("notification").toString() == "Devices.PluginConfigurationChanged", true); +} + #include "testjsonrpc.moc" QTEST_MAIN(TestJSONRPC)