From d4053297c818f639a6758713b64be67dfce4e4ed Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Fri, 25 Apr 2014 00:45:31 +0200 Subject: [PATCH] add eventdescriptor and paramdescriptor classes to be used with Rules --- libguh/devicemanager.cpp | 2 - libguh/libguh.pro | 8 +- libguh/types/eventdescriptor.cpp | 152 +++++++++++++++++++++++++++++++ libguh/types/eventdescriptor.h | 54 +++++++++++ libguh/types/param.cpp | 13 +-- libguh/types/param.h | 13 --- libguh/types/paramdescriptor.cpp | 18 ++++ libguh/types/paramdescriptor.h | 26 ++++++ server/jsonrpc/jsontypes.cpp | 106 +++++++++++++++------ server/jsonrpc/jsontypes.h | 7 ++ server/jsonrpc/ruleshandler.cpp | 8 +- server/rule.cpp | 18 ++-- server/rule.h | 8 +- server/ruleengine.cpp | 63 +++++++------ server/ruleengine.h | 7 +- tests/auto/testjsonrpc.cpp | 9 +- 16 files changed, 406 insertions(+), 106 deletions(-) create mode 100644 libguh/types/eventdescriptor.cpp create mode 100644 libguh/types/eventdescriptor.h create mode 100644 libguh/types/paramdescriptor.cpp create mode 100644 libguh/types/paramdescriptor.h diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 9062dba3..5a6429b8 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -412,7 +412,6 @@ void DeviceManager::loadConfiguredDevices() settings.beginGroup(paramNameString); Param param(paramNameString.remove(QRegExp("Param-"))); param.setValue(settings.value("value")); - param.setOperand((Param::OperandType)settings.value("operand").toInt()); params.append(param); settings.endGroup(); } @@ -439,7 +438,6 @@ void DeviceManager::storeConfiguredDevices() foreach (const Param ¶m, device->params()) { settings.beginGroup("Param-" + param.name()); settings.setValue("value", param.value()); - settings.setValue("operand", param.operand()); settings.endGroup(); } settings.endGroup(); diff --git a/libguh/libguh.pro b/libguh/libguh.pro index bd873d29..0889914c 100644 --- a/libguh/libguh.pro +++ b/libguh/libguh.pro @@ -19,9 +19,11 @@ SOURCES += plugin/device.cpp \ types/statetype.cpp \ types/eventtype.cpp \ types/event.cpp \ + types/eventdescriptor.cpp \ types/vendor.cpp \ types/paramtype.cpp \ - types/param.cpp + types/param.cpp \ + types/paramdescriptor.cpp \ HEADERS += plugin/device.h \ plugin/deviceclass.h \ @@ -36,8 +38,10 @@ HEADERS += plugin/device.h \ types/statetype.h \ types/eventtype.h \ types/event.h \ + types/eventdescriptor.h \ types/vendor.h \ types/typeutils.h \ types/paramtype.h \ - types/param.h + types/param.h \ + types/paramdescriptor.h diff --git a/libguh/types/eventdescriptor.cpp b/libguh/types/eventdescriptor.cpp new file mode 100644 index 00000000..a9668c4d --- /dev/null +++ b/libguh/types/eventdescriptor.cpp @@ -0,0 +1,152 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * 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 . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/*! + \class EventDescriptor + \brief Describes a certain \l{Event}. + + \ingroup types + \inmodule libguh + + An EventDescriptor describes an \l{Event} in order to match it with a \l{Rule}. + + \sa Event, Rule +*/ + +#include "eventdescriptor.h" + +/*! Constructs an EventDescriptor describing an Event. + */ +EventDescriptor::EventDescriptor(const EventTypeId &eventTypeId, const DeviceId &deviceId, const QList ¶mDescriptors): + m_eventTypeId(eventTypeId), + m_deviceId(deviceId), + m_paramDescriptors(paramDescriptors) +{ +} + +/*! Returns the id of the \l{EventType} which describes this Event.*/ +EventTypeId EventDescriptor::eventTypeId() const +{ + return m_eventTypeId; +} + +/*! Returns the id of the \l{Device} associated with this Event.*/ +DeviceId EventDescriptor::deviceId() const +{ + return m_deviceId; +} + +/*! Returns the parameters of this Event.*/ +QList EventDescriptor::paramDescriptors() const +{ + return m_paramDescriptors; +} + +/*! Set the parameters of this Event to \a params.*/ +void EventDescriptor::setParamDescriptors(const QList ¶mDescriptors) +{ + m_paramDescriptors = paramDescriptors; +} + +ParamDescriptor EventDescriptor::paramDescriptor(const QString ¶mDescriptorName) const +{ + foreach (const ParamDescriptor ¶mDescriptor, m_paramDescriptors) { + if (paramDescriptor.name() == paramDescriptorName) { + return paramDescriptor; + } + } + return ParamDescriptor(QString()); +} + +/*! Compare this Event to the Event given by \a other. + Events are equal (returns true) if eventTypeId, deviceId and params match. */ +bool EventDescriptor::operator ==(const EventDescriptor &other) const +{ + bool paramsMatch = true; + foreach (const ParamDescriptor &otherParamDescriptor, other.paramDescriptors()) { + ParamDescriptor paramDescriptor = this->paramDescriptor(otherParamDescriptor.name()); + if (!paramDescriptor.isValid() || paramDescriptor.value() != otherParamDescriptor.value()) { + paramsMatch = false; + break; + } + } + + return m_eventTypeId == other.eventTypeId() + && m_deviceId == other.deviceId() + && paramsMatch; +} + +bool EventDescriptor::operator ==(const Event &event) const +{ + if (m_eventTypeId != event.eventTypeId() || m_deviceId != event.deviceId()) { + return false; + } + + foreach (const ParamDescriptor ¶mDescriptor, m_paramDescriptors) { + switch (paramDescriptor.operand()) { + case ParamDescriptor::OperandTypeEquals: + if (event.param(paramDescriptor.name()).value() != paramDescriptor.value()) { + return false; + } + break; + case ParamDescriptor::OperandTypeNotEquals: + if (event.param(paramDescriptor.name()).value() == paramDescriptor.value()) { + return false; + } + break; + case ParamDescriptor::OperandTypeGreater: + if (event.param(paramDescriptor.name()).value() <= paramDescriptor.value()) { + return false; + } + break; + case ParamDescriptor::OperandTypeGreaterOrEqual: + if (event.param(paramDescriptor.name()).value() < paramDescriptor.value()) { + return false; + } + break; + case ParamDescriptor::OperandTypeLess: + if (event.param(paramDescriptor.name()).value() >= paramDescriptor.value()) { + return false; + } + break; + case ParamDescriptor::OperandTypeLessOrEqual: + if (event.param(paramDescriptor.name()).value() < paramDescriptor.value()) { + return false; + } + break; + } + } + return true; +} + +QDebug operator<<(QDebug dbg, const EventDescriptor &eventDescriptor) +{ + dbg.nospace() << "EventDescriptor(EventTypeId: " << eventDescriptor.eventTypeId().toString() << ", DeviceId" << eventDescriptor.deviceId() << ")"; + + return dbg.space(); +} + +QDebug operator<<(QDebug dbg, const QList &eventDescriptors) +{ + dbg.nospace() << "EventDescriptorList (count:" << eventDescriptors.count() << ")"; + for (int i = 0; i < eventDescriptors.count(); i++ ) { + dbg.nospace() << " " << i << ": " << eventDescriptors.at(i); + } + + return dbg.space(); +} diff --git a/libguh/types/eventdescriptor.h b/libguh/types/eventdescriptor.h new file mode 100644 index 00000000..b666a748 --- /dev/null +++ b/libguh/types/eventdescriptor.h @@ -0,0 +1,54 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * 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 EVENTDESCRIPTOR_H +#define EVENTDESCRIPTOR_H + +#include "typeutils.h" +#include "paramdescriptor.h" +#include "event.h" + +#include +#include +#include + +class EventDescriptor +{ +public: + EventDescriptor(const EventTypeId &eventTypeId, const DeviceId &deviceId, const QList ¶mDescriptors = QList()); + + EventTypeId eventTypeId() const; + DeviceId deviceId() const; + + QList paramDescriptors() const; + void setParamDescriptors(const QList ¶mDescriptors); + ParamDescriptor paramDescriptor(const QString ¶mDescriptorName) const; + + bool operator ==(const EventDescriptor &other) const; + + bool operator ==(const Event &event) const; + +private: + EventTypeId m_eventTypeId; + DeviceId m_deviceId; + QList m_paramDescriptors; +}; +QDebug operator<<(QDebug dbg, const EventDescriptor &eventDescriptor); +QDebug operator<<(QDebug dbg, const QList &eventDescriptors); + +#endif // EVENTDESCRIPTOR_H diff --git a/libguh/types/param.cpp b/libguh/types/param.cpp index 07645788..6bb19f28 100644 --- a/libguh/types/param.cpp +++ b/libguh/types/param.cpp @@ -22,8 +22,7 @@ Param::Param(const QString &name, const QVariant &value): m_name (name), - m_value(value), - m_operand(OperandTypeEquals) + m_value(value) { } @@ -47,16 +46,6 @@ void Param::setValue(const QVariant &value) m_value = value; } -Param::OperandType Param::operand() const -{ - return m_operand; -} - -void Param::setOperand(Param::OperandType operand) -{ - m_operand = operand; -} - bool Param::isValid() const { return !m_name.isEmpty() && m_value.isValid(); diff --git a/libguh/types/param.h b/libguh/types/param.h index b1733824..e17ad4bd 100644 --- a/libguh/types/param.h +++ b/libguh/types/param.h @@ -25,15 +25,6 @@ class Param { public: - enum OperandType { - OperandTypeEquals, - OperandTypeNotEquals, - OperandTypeLess, - OperandTypeGreater, - OperandTypeLessThan, - OperandTypeGreaterThan - }; - Param(const QString &name, const QVariant &value = QVariant()); QString name() const; @@ -42,15 +33,11 @@ public: QVariant value() const; void setValue(const QVariant &value); - OperandType operand() const; - void setOperand(OperandType operand); - bool isValid() const; private: QString m_name; QVariant m_value; - OperandType m_operand; }; QDebug operator<<(QDebug dbg, const Param ¶m); diff --git a/libguh/types/paramdescriptor.cpp b/libguh/types/paramdescriptor.cpp new file mode 100644 index 00000000..c041edc6 --- /dev/null +++ b/libguh/types/paramdescriptor.cpp @@ -0,0 +1,18 @@ +#include "paramdescriptor.h" + +ParamDescriptor::ParamDescriptor(const QString &name, const QVariant &value): + Param(name, value), + m_operand(OperandTypeEquals) +{ +} + +ParamDescriptor::OperandType ParamDescriptor::operand() const +{ + return m_operand; +} + +void ParamDescriptor::setOperand(ParamDescriptor::OperandType operand) +{ + m_operand = operand; +} + diff --git a/libguh/types/paramdescriptor.h b/libguh/types/paramdescriptor.h new file mode 100644 index 00000000..f1dd4d82 --- /dev/null +++ b/libguh/types/paramdescriptor.h @@ -0,0 +1,26 @@ +#ifndef PARAMDESCRIPTOR_H +#define PARAMDESCRIPTOR_H + +#include "param.h" + +class ParamDescriptor : public Param +{ +public: + enum OperandType { + OperandTypeEquals, + OperandTypeNotEquals, + OperandTypeLess, + OperandTypeGreater, + OperandTypeLessOrEqual, + OperandTypeGreaterOrEqual + }; + ParamDescriptor(const QString &name, const QVariant &value = QVariant()); + + OperandType operand() const; + void setOperand(OperandType operand); + +private: + OperandType m_operand; +}; + +#endif // PARAMDESCRIPTOR_H diff --git a/server/jsonrpc/jsontypes.cpp b/server/jsonrpc/jsontypes.cpp index 6b41533f..e895f577 100644 --- a/server/jsonrpc/jsontypes.cpp +++ b/server/jsonrpc/jsontypes.cpp @@ -35,10 +35,12 @@ QVariantList JsonTypes::s_operandTypes; QVariantMap JsonTypes::s_paramType; QVariantMap JsonTypes::s_param; +QVariantMap JsonTypes::s_paramDescriptor; QVariantMap JsonTypes::s_stateType; QVariantMap JsonTypes::s_state; QVariantMap JsonTypes::s_eventType; QVariantMap JsonTypes::s_event; +QVariantMap JsonTypes::s_eventDescriptor; QVariantMap JsonTypes::s_actionType; QVariantMap JsonTypes::s_action; QVariantMap JsonTypes::s_plugin; @@ -67,7 +69,11 @@ void JsonTypes::init() // Param s_param.insert("name", "string"); s_param.insert("value", basicTypesRef()); - s_param.insert("operand", operandTypesRef()); + + // ParamDescriptor + s_paramDescriptor.insert("name", "string"); + s_paramDescriptor.insert("value", basicTypesRef()); + s_paramDescriptor.insert("operand", operandTypesRef()); // StateType s_stateType.insert("id", "uuid"); @@ -90,6 +96,11 @@ void JsonTypes::init() s_event.insert("deviceId", "uuid"); s_event.insert("o:params", QVariantList() << paramRef()); + // EventDescriptor + s_eventDescriptor.insert("eventTypeId", "uuid"); + s_eventDescriptor.insert("deviceId", "uuid"); + s_eventDescriptor.insert("o:paramDescriptors", QVariantList() << paramDescriptorRef()); + // ActionType s_actionType.insert("id", "uuid"); s_actionType.insert("name", "string"); @@ -130,9 +141,10 @@ void JsonTypes::init() s_deviceDescriptor.insert("title", "string"); s_deviceDescriptor.insert("description", "string"); + // Rule s_rule.insert("id", "uuid"); s_rule.insert("ruleType", ruleTypesRef()); - s_rule.insert("events", QVariantList() << eventRef()); + s_rule.insert("eventDescriptors", QVariantList() << eventDescriptorRef()); s_rule.insert("actions", QVariantList() << actionRef()); s_rule.insert("states", QVariantList() << stateRef()); @@ -153,14 +165,16 @@ QVariantMap JsonTypes::allTypes() allTypes.insert("SetupMethodType", setupMethodTypes()); allTypes.insert("OperandType", operandTypes()); allTypes.insert("StateType", stateTypeDescription()); + allTypes.insert("Event", eventDescription()); allTypes.insert("EventType", eventTypeDescription()); + allTypes.insert("EventDescriptor", eventDescriptorDescription()); allTypes.insert("ActionType", actionTypeDescription()); allTypes.insert("Vendor", vendorDescription()); allTypes.insert("DeviceClass", deviceClassDescription()); allTypes.insert("Plugin", pluginDescription()); allTypes.insert("Param", paramDescription()); + allTypes.insert("ParamDescriptor", paramDescriptorDescription()); allTypes.insert("State", stateDescription()); - allTypes.insert("Event", eventDescription()); allTypes.insert("Device", deviceDescription()); allTypes.insert("DeviceDescriptor", deviceDescriptorDescription()); allTypes.insert("Action", actionDescription()); @@ -191,6 +205,19 @@ QVariantMap JsonTypes::packEvent(const Event &event) return variant; } +QVariantMap JsonTypes::packEventDescriptor(const EventDescriptor &eventDescriptor) +{ + QVariantMap variant; + variant.insert("eventTypeId", eventDescriptor.eventTypeId()); + variant.insert("deviceId", eventDescriptor.deviceId()); + QVariantList params; + foreach (const ParamDescriptor ¶mDescriptor, eventDescriptor.paramDescriptors()) { + params.append(packParamDescriptor(paramDescriptor)); + } + variant.insert("params", params); + return variant; +} + QVariantMap JsonTypes::packActionType(const ActionType &actionType) { QVariantMap variantMap; @@ -226,9 +253,16 @@ QVariantMap JsonTypes::packStateType(const StateType &stateType) QVariantMap JsonTypes::packParam(const Param ¶m) { QVariantMap variantMap; - variantMap.insert("name", param.name()); - variantMap.insert("value", param.value()); - variantMap.insert("operand", s_operandTypes.at(param.operand())); + variantMap.insert(param.name(), param.value()); + return variantMap; +} + +QVariantMap JsonTypes::packParamDescriptor(const ParamDescriptor ¶mDescriptor) +{ + QVariantMap variantMap; + variantMap.insert("name", paramDescriptor.name()); + variantMap.insert("value", paramDescriptor.value()); + variantMap.insert("operand", s_operandTypes.at(paramDescriptor.operand())); return variantMap; } @@ -322,11 +356,11 @@ QVariantMap JsonTypes::packRule(const Rule &rule) { QVariantMap ruleMap; ruleMap.insert("id", rule.id()); - QVariantList eventList; - foreach (const Event &event, rule.events()) { - eventList.append(JsonTypes::packEvent(event)); + QVariantList eventDescriptorList; + foreach (const EventDescriptor &eventDescriptor, rule.eventDescriptors()) { + eventDescriptorList.append(JsonTypes::packEventDescriptor(eventDescriptor)); } - ruleMap.insert("events", eventList); + ruleMap.insert("eventDescriptors", eventDescriptorList); ruleMap.insert("ruleType", s_ruleTypes.at(rule.ruleType())); @@ -343,22 +377,7 @@ QVariantMap JsonTypes::packRule(const Rule &rule) Param JsonTypes::unpackParam(const QVariantMap ¶mMap) { - Param param(paramMap.value("name").toString(), paramMap.value("value")); - QString operandString = paramMap.value("operand").toString(); - if (operandString == "OperandTypeEquals") { - param.setOperand(Param::OperandTypeEquals); - } else if (operandString == "OperandTypeNotEquals") { - param.setOperand(Param::OperandTypeNotEquals); - } else if (operandString == "OperandTypeLess") { - param.setOperand(Param::OperandTypeLess); - } else if (operandString == "OperandTypeGreater") { - param.setOperand(Param::OperandTypeGreater); - } else if (operandString == "OperandTypeLessThan") { - param.setOperand(Param::OperandTypeLessThan); - } else if (operandString == "OperandTypeGreaterThan") { - param.setOperand(Param::OperandTypeGreaterThan); - } - return param; + return Param(paramMap.value("name").toString(), paramMap.value("value")); } QList JsonTypes::unpackParams(const QVariantList ¶mList) @@ -370,6 +389,35 @@ QList JsonTypes::unpackParams(const QVariantList ¶mList) return params; } +ParamDescriptor JsonTypes::unpackParamDescriptor(const QVariantMap ¶mMap) +{ + ParamDescriptor param(paramMap.value("name").toString(), paramMap.value("value")); + QString operandString = paramMap.value("operand").toString(); + if (operandString == "OperandTypeEquals") { + param.setOperand(ParamDescriptor::OperandTypeEquals); + } else if (operandString == "OperandTypeNotEquals") { + param.setOperand(ParamDescriptor::OperandTypeNotEquals); + } else if (operandString == "OperandTypeLess") { + param.setOperand(ParamDescriptor::OperandTypeLess); + } else if (operandString == "OperandTypeGreater") { + param.setOperand(ParamDescriptor::OperandTypeGreater); + } else if (operandString == "OperandTypeLessOrEqual") { + param.setOperand(ParamDescriptor::OperandTypeLessOrEqual); + } else if (operandString == "OperandTypeGreaterOrEqual") { + param.setOperand(ParamDescriptor::OperandTypeGreaterOrEqual); + } + return param; +} + +QList JsonTypes::unpackParamDescriptors(const QVariantList ¶mList) +{ + QList params; + foreach (const QVariant ¶mVariant, paramList) { + params.append(unpackParamDescriptor(paramVariant.toMap())); + } + return params; +} + QPair JsonTypes::validateMap(const QVariantMap &templateMap, const QVariantMap &map) { s_lastError.clear(); @@ -471,10 +519,8 @@ QPair JsonTypes::validateVariant(const QVariant &templateVariant, return result; } } else if (refName == paramRef()) { - QPair result = validateMap(paramDescription(), variant.toMap()); - if (!result.first) { - qDebug() << "Param not valid"; - return result; + if (!variant.canConvert(QVariant::Map)) { + report(false, "Param not valid. Should be a map."); } } else if (refName == deviceRef()) { QPair result = validateMap(deviceDescription(), variant.toMap()); diff --git a/server/jsonrpc/jsontypes.h b/server/jsonrpc/jsontypes.h index b3b03779..e241ec81 100644 --- a/server/jsonrpc/jsontypes.h +++ b/server/jsonrpc/jsontypes.h @@ -27,6 +27,7 @@ #include "types/action.h" #include "types/actiontype.h" #include "types/paramtype.h" +#include "types/paramdescriptor.h" #include @@ -70,10 +71,12 @@ public: DECLARE_TYPE(operandTypes, "OperandType") DECLARE_OBJECT(paramType, "ParamType") DECLARE_OBJECT(param, "Param") + DECLARE_OBJECT(paramDescriptor, "ParamDescriptor") DECLARE_OBJECT(stateType, "StateType") DECLARE_OBJECT(state, "State") DECLARE_OBJECT(eventType, "EventType") DECLARE_OBJECT(event, "Event") + DECLARE_OBJECT(eventDescriptor, "EventDescriptor") DECLARE_OBJECT(actionType, "ActionType") DECLARE_OBJECT(action, "Action") DECLARE_OBJECT(plugin, "Plugin") @@ -85,11 +88,13 @@ public: static QVariantMap packEventType(const EventType &eventType); static QVariantMap packEvent(const Event &event); + static QVariantMap packEventDescriptor(const EventDescriptor &event); static QVariantMap packActionType(const ActionType &actionType); static QVariantMap packAction(const Action &action); static QVariantMap packStateType(const StateType &stateType); static QVariantMap packParam(const Param ¶m); static QVariantMap packParamType(const ParamType ¶mType); + static QVariantMap packParamDescriptor(const ParamDescriptor ¶mDescriptor); static QVariantMap packVendor(const Vendor &vendor); static QVariantMap packDeviceClass(const DeviceClass &deviceClass); static QVariantMap packPlugin(DevicePlugin *plugin); @@ -99,6 +104,8 @@ public: static Param unpackParam(const QVariantMap ¶mMap); static QList unpackParams(const QVariantList ¶mList); + static ParamDescriptor unpackParamDescriptor(const QVariantMap ¶mDescriptorMap); + static QList unpackParamDescriptors(const QVariantList ¶mDescriptorList); static QPair validateMap(const QVariantMap &templateMap, const QVariantMap &map); static QPair validateProperty(const QVariant &templateValue, const QVariant &value); diff --git a/server/jsonrpc/ruleshandler.cpp b/server/jsonrpc/ruleshandler.cpp index 16f213f1..9c017fa7 100644 --- a/server/jsonrpc/ruleshandler.cpp +++ b/server/jsonrpc/ruleshandler.cpp @@ -39,7 +39,7 @@ RulesHandler::RulesHandler(QObject *parent) : params.clear(); returns.clear(); setDescription("AddRule", "Add a rule"); - params.insert("event", JsonTypes::eventRef()); + params.insert("eventDescriptor", JsonTypes::eventDescriptorRef()); QVariantList actions; actions.append(JsonTypes::actionRef()); params.insert("actions", actions); @@ -82,8 +82,8 @@ JsonReply* RulesHandler::AddRule(const QVariantMap ¶ms) EventTypeId eventTypeId(eventMap.value("eventTypeId").toString()); DeviceId eventDeviceId(eventMap.value("deviceId").toString()); - QList eventParams = JsonTypes::unpackParams(eventMap.value("params").toList()); - Event event(eventTypeId, eventDeviceId, eventParams); + QList eventParams = JsonTypes::unpackParamDescriptors(eventMap.value("paramDescriptors").toList()); + EventDescriptor eventDescriptor(eventTypeId, eventDeviceId, eventParams); QList actions; QVariantList actionList = params.value("actions").toList(); @@ -102,7 +102,7 @@ JsonReply* RulesHandler::AddRule(const QVariantMap ¶ms) return createReply(returns); } - switch(GuhCore::instance()->ruleEngine()->addRule(event, actions)) { + switch(GuhCore::instance()->ruleEngine()->addRule(eventDescriptor, actions)) { case RuleEngine::RuleErrorNoError: returns.insert("success", true); returns.insert("errorMessage", ""); diff --git a/server/rule.cpp b/server/rule.cpp index 1aaf1afc..1347aa10 100644 --- a/server/rule.cpp +++ b/server/rule.cpp @@ -23,13 +23,13 @@ \ingroup rules \inmodule server - A Rule is always evented by an \l{Event}, has \l{State}{States} + A Rule is always triggered by an \l{EventDescriptor}, has \l{State}{States} to be compared and \l{Action}{Actions} to be executed. Additionally a Rule is either of type \l{Rule::RuleTypeAll} or \l{Rule::RuleTypeAny} which determines if all or any of the \l{State}{States} must be matching in order for the \l{Action}{Actions} to be executed. - \sa Event, State, Action + \sa EventDescriptor, State, Action */ /*! \enum Rule::RuleType @@ -48,11 +48,11 @@ #include -/*! Constructs a Rule with the given \a id, \a event, \a states and \a actions. The ruleType will default to +/*! Constructs a Rule with the given \a id, \a eventDescriptor, \a states and \a actions. The ruleType will default to \l{Rule::RuleTypeAll}.*/ -Rule::Rule(const QUuid &id, const Event &event, const QList &states, const QList &actions): +Rule::Rule(const QUuid &id, const EventDescriptor &eventDescriptor, const QList &states, const QList &actions): m_id(id), - m_events(QList() << event), + m_eventDescriptors(QList() << eventDescriptor), m_states(states), m_actions(actions), m_ruleType(RuleTypeAll) @@ -65,10 +65,10 @@ QUuid Rule::id() const return m_id; } -/*! Returns the \l{Event} that events this Rule.*/ -QList Rule::events() const +/*! Returns the \l{EventDescriptor} for this Rule.*/ +QList Rule::eventDescriptors() const { - return m_events; + return m_eventDescriptors; } /*! Returns the \l{State}{States} that need to be matching in order for this to Rule apply. */ @@ -77,7 +77,7 @@ QList Rule::states() const return m_states; } -/*! Returns the \l{Action}{Actions} to be executed when this Rule is eventd and states match. */ +/*! Returns the \l{Action}{Actions} to be executed when this Rule is matched and states match. */ QList Rule::actions() const { return m_actions; diff --git a/server/rule.h b/server/rule.h index 548ec184..d5c24a8a 100644 --- a/server/rule.h +++ b/server/rule.h @@ -21,7 +21,7 @@ #include "types/state.h" #include "types/action.h" -#include "types/event.h" +#include "types/eventdescriptor.h" #include "stateevaluator.h" #include @@ -34,10 +34,10 @@ public: RuleTypeAny }; - Rule(const QUuid &id, const Event &event, const QList &states, const QList &actions); + Rule(const QUuid &id, const EventDescriptor &eventDescriptor, const QList &states, const QList &actions); QUuid id() const; - QList events() const; + QList eventDescriptors() const; QList states() const; QList actions() const; @@ -46,7 +46,7 @@ public: private: QUuid m_id; - QList m_events; + QList m_eventDescriptors; QList m_states; StateEvaluator stateEvaluator; QList m_actions; diff --git a/server/ruleengine.cpp b/server/ruleengine.cpp index fb556ccd..71761385 100644 --- a/server/ruleengine.cpp +++ b/server/ruleengine.cpp @@ -25,9 +25,9 @@ \inmodule server You can add, remove and update rules and query the engine for actions to be executed - for a given \l{Event}. + for a given \l{Event} described by an \l{EventDescriptor}. - \sa Event, Rule, Action + \sa Event, EventDescriptor, Rule, Action */ /*! \fn void RuleEngine::ruleAdded(const QUuid &ruleId) @@ -51,6 +51,8 @@ */ #include "ruleengine.h" +#include "types/paramdescriptor.h" +#include "types/eventdescriptor.h" #include "guhcore.h" @@ -80,17 +82,17 @@ RuleEngine::RuleEngine(QObject *parent) : settings.beginGroup("event"); EventTypeId eventTypeId(settings.value("eventTypeId").toString()); DeviceId deviceId(settings.value("deviceId").toString()); - QList params; + QList params; foreach (QString groupName, settings.childGroups()) { - if (groupName.startsWith("Param-")) { + if (groupName.startsWith("ParamDescriptor-")) { settings.beginGroup(groupName); - Param param(groupName.remove(QRegExp("^Param-")), settings.value("value")); - param.setOperand((Param::OperandType)settings.value("operand").toInt()); - params.append(param); + ParamDescriptor paramDescriptor(groupName.remove(QRegExp("^ParamDescriptor-")), settings.value("value")); + paramDescriptor.setOperand((ParamDescriptor::OperandType)settings.value("operand").toInt()); + params.append(paramDescriptor); settings.endGroup(); } } - Event event(eventTypeId, deviceId, params); + EventDescriptor eventDescriptor(eventTypeId, deviceId, params); settings.endGroup(); settings.beginGroup("states"); @@ -114,7 +116,6 @@ RuleEngine::RuleEngine(QObject *parent) : if (paramNameString.startsWith("Param-")) { settings.beginGroup(paramNameString); Param param(paramNameString.remove(QRegExp("^Param-")), settings.value("value")); - param.setOperand((Param::OperandType)settings.value("operand").toInt()); params.append(param); settings.endGroup(); } @@ -128,7 +129,7 @@ RuleEngine::RuleEngine(QObject *parent) : settings.endGroup(); - Rule rule = Rule(QUuid(idString), event, states, actions); + Rule rule = Rule(QUuid(idString), eventDescriptor, states, actions); m_rules.append(rule); } @@ -143,8 +144,8 @@ QList RuleEngine::evaluateEvent(const Event &event) qDebug() << "got event:" << event; QList actions; for (int i = 0; i < m_rules.count(); ++i) { - qDebug() << "evaluating rule" << i << m_rules.at(i).events(); - if (m_rules.at(i).events().contains(event)) { + qDebug() << "evaluating rule" << i << m_rules.at(i).eventDescriptors(); + if (containsEvent(m_rules.at(i), event)) { bool statesMatching = true; qDebug() << "checking states:" << m_rules.at(i).states(); foreach (const State &state, m_rules.at(i).states()) { @@ -172,17 +173,17 @@ QList RuleEngine::evaluateEvent(const Event &event) /*! Add a new \l{Rule} with the given \a event and \a actions to the engine. For convenience, this creates a Rule without any \l{State} comparison. */ -RuleEngine::RuleError RuleEngine::addRule(const Event &event, const QList &actions) +RuleEngine::RuleError RuleEngine::addRule(const EventDescriptor &eventDescriptor, const QList &actions) { - return addRule(event, QList(), actions); + return addRule(eventDescriptor, QList(), actions); } /*! Add a new \l{Rule} with the given \a event, \a states and \a actions to the engine. */ -RuleEngine::RuleError RuleEngine::addRule(const Event &event, const QList &states, const QList &actions) +RuleEngine::RuleError RuleEngine::addRule(const EventDescriptor &eventDescriptor, const QList &states, const QList &actions) { - Device *device = GuhCore::instance()->deviceManager()->findConfiguredDevice(event.deviceId()); + Device *device = GuhCore::instance()->deviceManager()->findConfiguredDevice(eventDescriptor.deviceId()); if (!device) { - qWarning() << "Cannot create rule. No configured device for eventTypeId" << event.eventTypeId(); + qWarning() << "Cannot create rule. No configured device for eventTypeId" << eventDescriptor.eventTypeId(); return RuleErrorDeviceNotFound; } DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(device->deviceClassId()); @@ -190,28 +191,28 @@ RuleEngine::RuleError RuleEngine::addRule(const Event &event, const QList bool eventTypeFound = false; foreach (const EventType &eventType, deviceClass.events()) { - if (eventType.id() == event.eventTypeId()) { + if (eventType.id() == eventDescriptor.eventTypeId()) { eventTypeFound = true; } } if (!eventTypeFound) { - qWarning() << "Cannot create rule. Device " + device->name() + " has no event type:" << event.eventTypeId(); + qWarning() << "Cannot create rule. Device " + device->name() + " has no event type:" << eventDescriptor.eventTypeId(); return RuleErrorEventTypeNotFound; } - Rule rule = Rule(QUuid::createUuid(), event, states, actions); + Rule rule = Rule(QUuid::createUuid(), eventDescriptor, states, actions); m_rules.append(rule); emit ruleAdded(rule.id()); QSettings settings(m_settingsFile); settings.beginGroup(rule.id().toString()); settings.beginGroup("event"); - settings.setValue("eventTypeId", event.eventTypeId()); - settings.setValue("deviceId", event.deviceId()); - foreach (const Param ¶m, event.params()) { - settings.beginGroup("Param-" + param.name()); - settings.setValue("value", param.value()); - settings.setValue("operand", param.operand()); + settings.setValue("eventTypeId", eventDescriptor.eventTypeId()); + settings.setValue("deviceId", eventDescriptor.deviceId()); + foreach (const ParamDescriptor ¶mDescriptor, eventDescriptor.paramDescriptors()) { + settings.beginGroup("ParamDescriptor-" + paramDescriptor.name()); + settings.setValue("value", paramDescriptor.value()); + settings.setValue("operand", paramDescriptor.operand()); settings.endGroup(); } @@ -274,3 +275,13 @@ RuleEngine::RuleError RuleEngine::removeRule(const QUuid &ruleId) } return RuleErrorRuleNotFound; } + +bool RuleEngine::containsEvent(const Rule &rule, const Event &event) +{ + foreach (const EventDescriptor &eventDescriptor, rule.eventDescriptors()) { + if (eventDescriptor == event) { + return true; + } + } + return false; +} diff --git a/server/ruleengine.h b/server/ruleengine.h index 35870044..9b9fb1a2 100644 --- a/server/ruleengine.h +++ b/server/ruleengine.h @@ -41,8 +41,8 @@ public: QList evaluateEvent(const Event &event); - RuleError addRule(const Event &event, const QList &actions); - RuleError addRule(const Event &event, const QList &states, const QList &actions); + RuleError addRule(const EventDescriptor &eventDescriptor, const QList &actions); + RuleError addRule(const EventDescriptor &eventDescriptor, const QList &states, const QList &actions); QList rules() const; RuleError removeRule(const QUuid &ruleId); @@ -51,6 +51,9 @@ signals: void ruleAdded(const QUuid &ruleId); void ruleRemoved(const QUuid &ruleId); +private: + bool containsEvent(const Rule &rule, const Event &event); + private: QString m_settingsFile; QList m_rules; diff --git a/tests/auto/testjsonrpc.cpp b/tests/auto/testjsonrpc.cpp index 3d6cced4..47993c5e 100644 --- a/tests/auto/testjsonrpc.cpp +++ b/tests/auto/testjsonrpc.cpp @@ -566,8 +566,13 @@ void TestJSONRPC::storedDevices() foreach (const QVariant device, response.toMap().value("params").toMap().value("devices").toList()) { qDebug() << "found stored device" << device; if (DeviceId(device.toMap().value("id").toString()) == addedDeviceId) { - qDebug() << "found added device" << device.toMap().value("params").toList().first(); - QCOMPARE(device.toMap().value("params").toMap(), deviceParams); +// foreach (const QVariant ¶mVariant, device.toMap().value("params").toList()) { +// if () +// } + + qDebug() << "found added device" << device.toMap().value("params").toList().first().toMap(); + qDebug() << "expected deviceParams:" << deviceParams; + QCOMPARE(device.toMap().value("params").toList().first().toMap(), deviceParams); } }