diff --git a/server/jsonrpc/actionhandler.cpp b/server/jsonrpc/actionhandler.cpp index da8a2872..628248fd 100644 --- a/server/jsonrpc/actionhandler.cpp +++ b/server/jsonrpc/actionhandler.cpp @@ -97,10 +97,3 @@ void ActionHandler::actionExecuted(const ActionId &id, DeviceManager::DeviceErro reply->setData(statusToReply(status)); reply->finished(); } - -QVariantMap ActionHandler::statusToReply(DeviceManager::DeviceError status) const -{ - QVariantMap returns; - returns.insert("deviceError", JsonTypes::deviceErrorToString(status)); - return returns; -} diff --git a/server/jsonrpc/actionhandler.h b/server/jsonrpc/actionhandler.h index 173bd4d7..f6b64366 100644 --- a/server/jsonrpc/actionhandler.h +++ b/server/jsonrpc/actionhandler.h @@ -37,9 +37,6 @@ public: private slots: void actionExecuted(const ActionId &id, DeviceManager::DeviceError status); -private: - QVariantMap statusToReply(DeviceManager::DeviceError status) const; - private: QHash m_asyncActionExecutions; }; diff --git a/server/jsonrpc/eventhandler.cpp b/server/jsonrpc/eventhandler.cpp index 193862de..840f719a 100644 --- a/server/jsonrpc/eventhandler.cpp +++ b/server/jsonrpc/eventhandler.cpp @@ -31,6 +31,14 @@ EventHandler::EventHandler(QObject *parent) : params.insert("event", JsonTypes::eventRef()); setParams("EventTriggered", params); + params.clear(); returns.clear(); + setDescription("GetEventType", "Get the EventType for the given eventTypeId."); + params.insert("eventTypeId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); + setParams("GetEventType", params); + returns.insert("deviceError", JsonTypes::deviceErrorRef()); + returns.insert("o:eventType", JsonTypes::eventTypeRef()); + setReturns("GetEventType", returns); + connect(GuhCore::instance(), &GuhCore::eventTriggered, this, &EventHandler::eventTriggered); } @@ -46,3 +54,17 @@ void EventHandler::eventTriggered(const Event &event) emit EventTriggered(params); } +JsonReply* EventHandler::GetEventType(const QVariantMap ¶ms) const +{ + EventTypeId eventTypeId(params.value("eventTypeId").toString()); + foreach (const DeviceClass &deviceClass, GuhCore::instance()->supportedDevices()) { + foreach (const EventType &eventType, deviceClass.eventTypes()) { + if (eventType.id() == eventTypeId) { + QVariantMap data = statusToReply(DeviceManager::DeviceErrorNoError); + data.insert("eventType", JsonTypes::packEventType(eventType)); + return createReply(data); + } + } + } + return createReply(statusToReply(DeviceManager::DeviceErrorEventTypeNotFound)); +} diff --git a/server/jsonrpc/eventhandler.h b/server/jsonrpc/eventhandler.h index d0d52b1e..32b76589 100644 --- a/server/jsonrpc/eventhandler.h +++ b/server/jsonrpc/eventhandler.h @@ -28,6 +28,7 @@ public: explicit EventHandler(QObject *parent = 0); QString name() const override; + Q_INVOKABLE JsonReply *GetEventType(const QVariantMap ¶ms) const; signals: void EventTriggered(const QVariantMap ¶ms); diff --git a/server/jsonrpc/jsonhandler.cpp b/server/jsonrpc/jsonhandler.cpp index ed64fbb4..73f5a149 100644 --- a/server/jsonrpc/jsonhandler.cpp +++ b/server/jsonrpc/jsonhandler.cpp @@ -132,6 +132,13 @@ JsonReply* JsonHandler::createAsyncReply(const QString &method) const return JsonReply::createAsyncReply(const_cast(this), method); } +QVariantMap JsonHandler::statusToReply(DeviceManager::DeviceError status) const +{ + QVariantMap returns; + returns.insert("deviceError", JsonTypes::deviceErrorToString(status)); + return returns; +} + JsonReply::JsonReply(Type type, JsonHandler *handler, const QString &method, const QVariantMap &data): m_type(type), diff --git a/server/jsonrpc/jsonhandler.h b/server/jsonrpc/jsonhandler.h index 935f3fb0..05c02cb4 100644 --- a/server/jsonrpc/jsonhandler.h +++ b/server/jsonrpc/jsonhandler.h @@ -103,6 +103,7 @@ protected: JsonReply *createReply(const QVariantMap &data) const; JsonReply *createAsyncReply(const QString &method) const; + QVariantMap statusToReply(DeviceManager::DeviceError status) const; private: QHash m_descriptions; diff --git a/server/jsonrpc/jsonrpcserver.cpp b/server/jsonrpc/jsonrpcserver.cpp index f700db25..47252391 100644 --- a/server/jsonrpc/jsonrpcserver.cpp +++ b/server/jsonrpc/jsonrpcserver.cpp @@ -42,7 +42,7 @@ #include #include -#define JSON_PROTOCOL_VERSION 8 +#define JSON_PROTOCOL_VERSION 9 JsonRPCServer::JsonRPCServer(QObject *parent): JsonHandler(parent), diff --git a/tests/auto/actions/testactions.cpp b/tests/auto/actions/testactions.cpp index d3ba631d..3c2e54a7 100644 --- a/tests/auto/actions/testactions.cpp +++ b/tests/auto/actions/testactions.cpp @@ -37,8 +37,8 @@ private slots: void executeAction_data(); void executeAction(); - void getActionTypes_data(); - void getActionTypes(); + void getActionType_data(); + void getActionType(); }; @@ -120,7 +120,7 @@ void TestActions::executeAction() } -void TestActions::getActionTypes_data() +void TestActions::getActionType_data() { QTest::addColumn("actionTypeId"); QTest::addColumn("error"); @@ -129,7 +129,7 @@ void TestActions::getActionTypes_data() QTest::newRow("invalid actiontypeid") << ActionTypeId::createActionTypeId() << DeviceManager::DeviceErrorActionTypeNotFound; } -void TestActions::getActionTypes() +void TestActions::getActionType() { QFETCH(ActionTypeId, actionTypeId); QFETCH(DeviceManager::DeviceError, error); diff --git a/tests/auto/api.json b/tests/auto/api.json index 96c9a0c1..ad4e4c72 100644 --- a/tests/auto/api.json +++ b/tests/auto/api.json @@ -1,4 +1,4 @@ -8 +9 { "methods": { "Actions.ExecuteAction": { @@ -225,6 +225,16 @@ "deviceError": "$ref:DeviceError" } }, + "Events.GetEventType": { + "description": "Get the EventType for the given eventTypeId.", + "params": { + "eventTypeId": "Uuid" + }, + "returns": { + "deviceError": "$ref:DeviceError", + "o:eventType": "$ref:EventType" + } + }, "JSONRPC.Introspect": { "description": "Introspect this API.", "params": { diff --git a/tests/auto/events/testevents.cpp b/tests/auto/events/testevents.cpp index 6ae1544f..fc45d06e 100644 --- a/tests/auto/events/testevents.cpp +++ b/tests/auto/events/testevents.cpp @@ -38,6 +38,9 @@ private slots: void triggerStateChangeEvent(); void params(); + + void getEventType_data(); + void getEventType(); }; void TestEvents::triggerEvent() @@ -107,5 +110,30 @@ void TestEvents::params() QVERIFY(!event.param("baz").value().isValid()); } +void TestEvents::getEventType_data() +{ + QTest::addColumn("eventTypeId"); + QTest::addColumn("error"); + + QTest::newRow("valid eventypeid") << mockEvent1Id << DeviceManager::DeviceErrorNoError; + QTest::newRow("invalid eventypeid") << EventTypeId::createEventTypeId() << DeviceManager::DeviceErrorEventTypeNotFound; +} + +void TestEvents::getEventType() +{ + QFETCH(EventTypeId, eventTypeId); + QFETCH(DeviceManager::DeviceError, error); + + QVariantMap params; + params.insert("eventTypeId", eventTypeId.toString()); + QVariant response = injectAndWait("Events.GetEventType", params); + + verifyDeviceError(response, error); + + if (error == DeviceManager::DeviceErrorNoError) { + QVERIFY2(EventTypeId(response.toMap().value("params").toMap().value("eventType").toMap().value("id").toString()) == eventTypeId, "Didnt get reply for same actionTypeId as requested."); + } +} + #include "testevents.moc" QTEST_MAIN(TestEvents)