diff --git a/libguh/types/ruleaction.cpp b/libguh/types/ruleaction.cpp index 150b6c86..b01387c5 100644 --- a/libguh/types/ruleaction.cpp +++ b/libguh/types/ruleaction.cpp @@ -1,3 +1,21 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * This file is part of guh. * + * * + * Guh 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 2 of the License. * + * * + * Guh 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. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + #include "ruleaction.h" RuleAction::RuleAction(const ActionTypeId &actionTypeId, const DeviceId &deviceId) : diff --git a/libguh/types/ruleaction.h b/libguh/types/ruleaction.h index d6a960ea..0ddc04f8 100644 --- a/libguh/types/ruleaction.h +++ b/libguh/types/ruleaction.h @@ -1,3 +1,21 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * This file is part of guh. * + * * + * Guh 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 2 of the License. * + * * + * Guh 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. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + #ifndef RULEACTION_H #define RULEACTION_H diff --git a/libguh/types/ruleactionparam.cpp b/libguh/types/ruleactionparam.cpp index c1806ff6..a735a77a 100644 --- a/libguh/types/ruleactionparam.cpp +++ b/libguh/types/ruleactionparam.cpp @@ -35,7 +35,8 @@ * \sa Param, */ RuleActionParam::RuleActionParam(const Param ¶m) : m_name(param.name()), - m_value(param.value()) + m_value(param.value()), + m_eventTypeId(EventTypeId()) { } diff --git a/server/jsonrpc/jsonrpcserver.cpp b/server/jsonrpc/jsonrpcserver.cpp index 882e7ad9..2deaf589 100644 --- a/server/jsonrpc/jsonrpcserver.cpp +++ b/server/jsonrpc/jsonrpcserver.cpp @@ -44,7 +44,7 @@ #include #include -#define JSON_PROTOCOL_VERSION 15 +#define JSON_PROTOCOL_VERSION 16 JsonRPCServer::JsonRPCServer(QObject *parent): JsonHandler(parent), diff --git a/server/jsonrpc/jsontypes.cpp b/server/jsonrpc/jsontypes.cpp index b285b4e4..6aead8f6 100644 --- a/server/jsonrpc/jsontypes.cpp +++ b/server/jsonrpc/jsontypes.cpp @@ -104,7 +104,7 @@ void JsonTypes::init() // RuleActionParam s_ruleActionParam.insert("name", basicTypeToString(String)); s_ruleActionParam.insert("o:value", basicTypeRef()); - s_ruleActionParam.insert("o:eventId", basicTypeToString(Uuid)); + s_ruleActionParam.insert("o:eventTypeId", basicTypeToString(Uuid)); // ParamDescriptor s_paramDescriptor.insert("name", basicTypeToString(String)); @@ -355,8 +355,13 @@ QVariantMap JsonTypes::packRuleActionParam(const RuleActionParam &ruleActionPara { QVariantMap variantMap; variantMap.insert("name", ruleActionParam.name()); - variantMap.insert("value", ruleActionParam.value()); - variantMap.insert("eventTypeId", ruleActionParam.eventTypeId()); + + // if this ruleaction param has a valid EventTypeId, there is no value + if (ruleActionParam.eventTypeId() != EventTypeId()) { + variantMap.insert("eventTypeId", ruleActionParam.eventTypeId()); + } else { + variantMap.insert("value", ruleActionParam.value()); + } return variantMap; } @@ -843,6 +848,18 @@ QPair JsonTypes::validateVariant(const QVariant &templateVariant, qDebug() << "param types not matching"; return result; } + } else if (refName == ruleActionRef()) { + QPair result = validateMap(ruleActionDescription(), variant.toMap()); + if (!result.first) { + qDebug() << "ruleAction type not matching"; + return result; + } + } else if (refName == ruleActionParamRef()) { + QPair result = validateMap(ruleActionParamDescription(), variant.toMap()); + if (!result.first) { + qDebug() << "ruleActionParam type not matching"; + return result; + } } else if (refName == actionTypeRef()) { QPair result = validateMap(actionTypeDescription(), variant.toMap()); if (!result.first) { diff --git a/server/jsonrpc/ruleshandler.cpp b/server/jsonrpc/ruleshandler.cpp index 18a56e1a..8a5ec6aa 100644 --- a/server/jsonrpc/ruleshandler.cpp +++ b/server/jsonrpc/ruleshandler.cpp @@ -50,11 +50,11 @@ RulesHandler::RulesHandler(QObject *parent) : params.insert("o:eventDescriptor", JsonTypes::eventDescriptorRef()); params.insert("o:eventDescriptorList", QVariantList() << JsonTypes::eventDescriptorRef()); params.insert("o:stateEvaluator", JsonTypes::stateEvaluatorRef()); - params.insert("o:exitActions", QVariantList() << JsonTypes::actionRef()); + params.insert("o:exitActions", QVariantList() << JsonTypes::ruleActionRef()); params.insert("o:enabled", JsonTypes::basicTypeToString(JsonTypes::Bool)); params.insert("name", JsonTypes::basicTypeToString(JsonTypes::String)); QVariantList actions; - actions.append(JsonTypes::actionRef()); + actions.append(JsonTypes::ruleActionRef()); params.insert("actions", actions); setParams("AddRule", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); @@ -173,13 +173,14 @@ JsonReply* RulesHandler::AddRule(const QVariantMap ¶ms) // We have an eventTypeId if (eventDescriptorList.isEmpty()) { QVariantMap returns; - qWarning() << "RuleAction" << ruleAction.actionTypeId() << "contains an eventTypeId, but there areno eventDescriptors."; + qWarning() << "RuleAction" << ruleAction.actionTypeId() << "contains an eventTypeId, but there are no eventDescriptors."; returns.insert("ruleErorr", JsonTypes::ruleErrorToString(RuleEngine::RuleErrorInvalidRuleActionPatameter)); return createReply(returns); } // now check if this eventType is in the eventDescriptorList of this rule foreach (const EventDescriptor eventDescriptor, eventDescriptorList) { if (eventDescriptor.eventTypeId() == ruleActionParam.eventTypeId()) { + // TODO: check if they match continue; } else { // the given eventTypeId is not in the eventDescriptorList @@ -207,7 +208,7 @@ JsonReply* RulesHandler::AddRule(const QVariantMap ¶ms) foreach (const QVariant &actionVariant, exitActionList) { QVariantMap actionMap = actionVariant.toMap(); RuleAction action(ActionTypeId(actionMap.value("actionTypeId").toString()), DeviceId(actionMap.value("deviceId").toString())); - qDebug() << "params from json" << actionMap.value("ruleActionParams"); + qDebug() << "params from json" << actionMap.value("ruleActionParams"); action.setRuleActionParams(JsonTypes::unpackRuleActionParams(actionMap.value("ruleActionParams").toList())); qDebug() << "params in action" << action.ruleActionParams(); exitActions.append(action); diff --git a/server/ruleengine.cpp b/server/ruleengine.cpp index 1b6113fd..a4af338d 100644 --- a/server/ruleengine.cpp +++ b/server/ruleengine.cpp @@ -139,7 +139,7 @@ RuleEngine::RuleEngine(QObject *parent) : foreach (QString paramNameString, settings.childGroups()) { if (paramNameString.startsWith("RuleActionParam-")) { settings.beginGroup(paramNameString); - RuleActionParam param(paramNameString.remove(QRegExp("^RuleActionParam-")), settings.value("value")); + RuleActionParam param(paramNameString.remove(QRegExp("^RuleActionParam-")), settings.value("value",QVariant()), EventTypeId(settings.value("eventTypeId", EventTypeId()).toString())); params.append(param); settings.endGroup(); } @@ -354,6 +354,7 @@ RuleEngine::RuleError RuleEngine::addRule(const RuleId &ruleId, const QString &n settings.setValue("actionTypeId", action.actionTypeId()); foreach (const RuleActionParam ¶m, action.ruleActionParams()) { settings.beginGroup("RuleActionParam-" + param.name()); + settings.setValue("eventTypeId", param.eventTypeId()); settings.setValue("value", param.value()); settings.endGroup(); } @@ -362,7 +363,7 @@ RuleEngine::RuleError RuleEngine::addRule(const RuleId &ruleId, const QString &n settings.endGroup(); - settings.beginGroup("exitActions"); + settings.beginGroup("ruleExitActions"); foreach (const RuleAction &action, rule.exitActions()) { settings.beginGroup(action.actionTypeId().toString()); settings.setValue("deviceId", action.deviceId()); diff --git a/tests/auto/api.json b/tests/auto/api.json index fa349fb2..4b78b4d9 100644 --- a/tests/auto/api.json +++ b/tests/auto/api.json @@ -1,4 +1,4 @@ -15 +16 { "methods": { "Actions.ExecuteAction": { @@ -277,7 +277,7 @@ "description": "Add a rule. You can describe rules by one or many EventDesciptors and a StateEvaluator. Note that onlyone of either eventDescriptor or eventDescriptorList may be passed at a time. A rule can be created but left disabled,meaning it won't actually be executed until set to enabled. If not given, enabled defaults to true.", "params": { "actions": [ - "$ref:Action" + "$ref:RuleAction" ], "name": "String", "o:enabled": "Bool", @@ -286,7 +286,7 @@ "$ref:EventDescriptor" ], "o:exitActions": [ - "$ref:Action" + "$ref:RuleAction" ], "o:stateEvaluator": "$ref:StateEvaluator" }, @@ -572,19 +572,31 @@ ], "Rule": { "actions": [ - "$ref:Action" + "$ref:RuleAction" ], "enabled": "Bool", "eventDescriptors": [ "$ref:EventDescriptor" ], "exitActions": [ - "$ref:Action" + "$ref:RuleAction" ], "id": "Uuid", "name": "String", "stateEvaluator": "$ref:StateEvaluator" }, + "RuleAction": { + "actionTypeId": "Uuid", + "deviceId": "Uuid", + "o:ruleActionParams": [ + "$ref:RuleActionParam" + ] + }, + "RuleActionParam": { + "name": "String", + "o:eventTypeId": "Uuid", + "o:value": "$ref:BasicType" + }, "RuleError": [ "RuleErrorNoError", "RuleErrorInvalidRuleId", @@ -594,7 +606,8 @@ "RuleErrorActionTypeNotFound", "RuleErrorInvalidParameter", "RuleErrorInvalidRuleFormat", - "RuleErrorMissingParameter" + "RuleErrorMissingParameter", + "RuleErrorInvalidRuleActionPatameter" ], "SetupMethod": [ "SetupMethodJustAdd", diff --git a/tests/auto/rules/testrules.cpp b/tests/auto/rules/testrules.cpp index 30841853..2d16c829 100644 --- a/tests/auto/rules/testrules.cpp +++ b/tests/auto/rules/testrules.cpp @@ -126,22 +126,22 @@ void TestRules::addRemoveRules_data() QVariantMap validActionNoParams; validActionNoParams.insert("actionTypeId", mockActionIdNoParams); validActionNoParams.insert("deviceId", m_mockDeviceId); - validActionNoParams.insert("params", QVariantList()); + validActionNoParams.insert("ruleActionParams", QVariantList()); QVariantMap invalidAction; invalidAction.insert("actionTypeId", ActionTypeId()); invalidAction.insert("deviceId", m_mockDeviceId); - invalidAction.insert("params", QVariantList()); + invalidAction.insert("ruleActionParams", QVariantList()); QVariantMap validExitActionNoParams; validExitActionNoParams.insert("actionTypeId", mockActionIdNoParams); validExitActionNoParams.insert("deviceId", m_mockDeviceId); - validExitActionNoParams.insert("params", QVariantList()); + validExitActionNoParams.insert("ruleActionParams", QVariantList()); QVariantMap invalidExitAction; invalidExitAction.insert("actionTypeId", ActionTypeId()); invalidExitAction.insert("deviceId", m_mockDeviceId); - invalidExitAction.insert("params", QVariantList()); + invalidExitAction.insert("ruleActionParams", QVariantList()); QVariantMap stateDescriptor; stateDescriptor.insert("stateTypeId", mockIntStateId); @@ -363,7 +363,7 @@ void TestRules::loadStoreConfig() QVariantMap action1; action1.insert("actionTypeId", mockActionIdNoParams); action1.insert("deviceId", m_mockDeviceId); - action1.insert("params", QVariantList()); + action1.insert("ruleActionParams", QVariantList()); QVariantMap action2; action2.insert("actionTypeId", mockActionIdWithParams); @@ -378,7 +378,7 @@ void TestRules::loadStoreConfig() action2Param2.insert("name", "mockActionParam2"); action2Param2.insert("value", true); action2Params.append(action2Param2); - action2.insert("params", action2Params); + action2.insert("ruleActionParams", action2Params); // First rule QVariantMap params; @@ -454,6 +454,10 @@ void TestRules::loadStoreConfig() } QVariantList replyActions = rule1.value("actions").toList(); + qDebug() << "----------------------------------------------"; + qDebug() << rule1; + qDebug() << "----------------------------------------------"; + foreach (const QVariant &actionVariant, actions) { bool found = false; foreach (const QVariant &replyActionVariant, replyActions) {