From 01b92e325aa560ff27f63135504a71c91d71e6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 8 Jun 2015 16:06:21 +0200 Subject: [PATCH] add rule active status to rule details add rule active status changed notification add RuleDescription (and change API Rules.GetRules method) --- server/guhcore.cpp | 1 + server/guhcore.h | 1 + server/jsonrpc/jsontypes.cpp | 26 ++++++++++++++++++++++++++ server/jsonrpc/jsontypes.h | 2 ++ server/jsonrpc/ruleshandler.cpp | 33 +++++++++++++++++++++++++-------- server/jsonrpc/ruleshandler.h | 2 ++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/server/guhcore.cpp b/server/guhcore.cpp index f56f841f..018b4f34 100644 --- a/server/guhcore.cpp +++ b/server/guhcore.cpp @@ -437,6 +437,7 @@ void GuhCore::gotEvent(const Event &event) } else { // State based rule m_logger->logRuleActiveChanged(rule); + emit ruleActiveChanged(rule); if (rule.active()) { actions.append(rule.actions()); } else { diff --git a/server/guhcore.h b/server/guhcore.h index d9cad598..694825cc 100644 --- a/server/guhcore.h +++ b/server/guhcore.h @@ -103,6 +103,7 @@ signals: void ruleRemoved(const RuleId &ruleId); void ruleAdded(const Rule &rule); + void ruleActiveChanged(const Rule &rule); private: diff --git a/server/jsonrpc/jsontypes.cpp b/server/jsonrpc/jsontypes.cpp index 35f66efb..c434167d 100644 --- a/server/jsonrpc/jsontypes.cpp +++ b/server/jsonrpc/jsontypes.cpp @@ -67,6 +67,7 @@ QVariantMap JsonTypes::s_deviceClass; QVariantMap JsonTypes::s_device; QVariantMap JsonTypes::s_deviceDescriptor; QVariantMap JsonTypes::s_rule; +QVariantMap JsonTypes::s_ruleDescription; QVariantMap JsonTypes::s_logEntry; void JsonTypes::init() @@ -200,11 +201,18 @@ void JsonTypes::init() s_rule.insert("id", basicTypeToString(Uuid)); s_rule.insert("name", basicTypeToString(String)); s_rule.insert("enabled", basicTypeToString(Bool)); + s_rule.insert("active", basicTypeToString(Bool)); s_rule.insert("eventDescriptors", QVariantList() << eventDescriptorRef()); s_rule.insert("actions", QVariantList() << ruleActionRef()); s_rule.insert("exitActions", QVariantList() << ruleActionRef()); s_rule.insert("stateEvaluator", stateEvaluatorRef()); + // RuleDescription + s_ruleDescription.insert("id", basicTypeToString(Uuid)); + s_ruleDescription.insert("name", basicTypeToString(String)); + s_ruleDescription.insert("enabled", basicTypeToString(Bool)); + s_ruleDescription.insert("active", basicTypeToString(Bool)); + // LogEntry s_logEntry.insert("timestamp", basicTypeToString(Int)); s_logEntry.insert("loggingLevel", loggingLevelRef()); @@ -274,6 +282,7 @@ QVariantMap JsonTypes::allTypes() allTypes.insert("DeviceDescriptor", deviceDescriptorDescription()); allTypes.insert("Action", actionDescription()); allTypes.insert("Rule", ruleDescription()); + allTypes.insert("RuleDescription", ruleDescriptionDescription()); allTypes.insert("LogEntry", logEntryDescription()); return allTypes; } @@ -537,6 +546,7 @@ QVariantMap JsonTypes::packRule(const Rule &rule) ruleMap.insert("id", rule.id()); ruleMap.insert("name", rule.name()); ruleMap.insert("enabled", rule.enabled()); + ruleMap.insert("active", rule.active()); QVariantList eventDescriptorList; foreach (const EventDescriptor &eventDescriptor, rule.eventDescriptors()) { eventDescriptorList.append(JsonTypes::packEventDescriptor(eventDescriptor)); @@ -558,6 +568,16 @@ QVariantMap JsonTypes::packRule(const Rule &rule) return ruleMap; } +QVariantMap JsonTypes::packRuleDescription(const Rule &rule) +{ + QVariantMap ruleDescriptionMap; + ruleDescriptionMap.insert("id", rule.id()); + ruleDescriptionMap.insert("name", rule.name()); + ruleDescriptionMap.insert("enabled", rule.enabled()); + ruleDescriptionMap.insert("active", rule.active()); + return ruleDescriptionMap; +} + QVariantMap JsonTypes::packLogEntry(const LogEntry &logEntry) { QVariantMap logEntryMap; @@ -914,6 +934,12 @@ QPair JsonTypes::validateVariant(const QVariant &templateVariant, qDebug() << "rule type not matching"; return result; } + } else if (refName == ruleDescriptionRef()) { + QPair result = validateMap(s_ruleDescription, variant.toMap()); + if (!result.first) { + qDebug() << "ruleDescription type not matching"; + return result; + } } else if (refName == eventDescriptorRef()) { QPair result = validateMap(eventDescriptorDescription(), variant.toMap()); if (!result.first) { diff --git a/server/jsonrpc/jsontypes.h b/server/jsonrpc/jsontypes.h index b65dab5a..a8082342 100644 --- a/server/jsonrpc/jsontypes.h +++ b/server/jsonrpc/jsontypes.h @@ -128,6 +128,7 @@ public: DECLARE_OBJECT(device, "Device") DECLARE_OBJECT(deviceDescriptor, "DeviceDescriptor") DECLARE_OBJECT(rule, "Rule") + DECLARE_OBJECT(ruleDescription, "RuleDescription") DECLARE_OBJECT(logEntry, "LogEntry") static QVariantMap packEventType(const EventType &eventType); @@ -149,6 +150,7 @@ public: static QVariantMap packDevice(Device *device); static QVariantMap packDeviceDescriptor(const DeviceDescriptor &descriptor); static QVariantMap packRule(const Rule &rule); + static QVariantMap packRuleDescription(const Rule &rule); static QVariantMap packLogEntry(const LogEntry &logEntry); static QVariantList packCreateMethods(DeviceClass::CreateMethods createMethods); diff --git a/server/jsonrpc/ruleshandler.cpp b/server/jsonrpc/ruleshandler.cpp index 6070f571..0e838d32 100644 --- a/server/jsonrpc/ruleshandler.cpp +++ b/server/jsonrpc/ruleshandler.cpp @@ -33,9 +33,10 @@ RulesHandler::RulesHandler(QObject *parent) : QVariantMap returns; params.clear(); returns.clear(); - setDescription("GetRules", "Get all configured rules"); + setDescription("GetRules", "Get the descriptions of all configured rules. If you need more information about a specific rule use the " + "method Rules.GetRuleDetails."); setParams("GetRules", params); - returns.insert("ruleIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid)); + returns.insert("ruleDescriptions", QVariantList() << JsonTypes::ruleDescriptionRef()); setReturns("GetRules", returns); params.clear(); returns.clear(); @@ -47,14 +48,15 @@ RulesHandler::RulesHandler(QObject *parent) : setReturns("GetRuleDetails", returns); params.clear(); returns.clear(); - setDescription("AddRule", "Add a rule. You can describe rules by one or many EventDesciptors and a StateEvaluator. Note that only" - "one of either eventDescriptor or eventDescriptorList may be passed at a time. A rule can be created but left disabled," + setDescription("AddRule", "Add a rule. You can describe rules by one or many EventDesciptors and a StateEvaluator. Note that only " + "one 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.insert("o:eventDescriptor", JsonTypes::eventDescriptorRef()); params.insert("o:eventDescriptorList", QVariantList() << JsonTypes::eventDescriptorRef()); params.insert("o:stateEvaluator", JsonTypes::stateEvaluatorRef()); params.insert("o:exitActions", QVariantList() << JsonTypes::ruleActionRef()); params.insert("o:enabled", JsonTypes::basicTypeToString(JsonTypes::Bool)); + params.insert("o:active", JsonTypes::basicTypeToString(JsonTypes::Bool)); params.insert("name", JsonTypes::basicTypeToString(JsonTypes::String)); QVariantList actions; actions.append(JsonTypes::ruleActionRef()); @@ -103,9 +105,15 @@ RulesHandler::RulesHandler(QObject *parent) : params.insert("rule", JsonTypes::ruleRef()); setParams("RuleAdded", params); + params.clear(); returns.clear(); + setDescription("RuleActiveChanged", "Emitted whenever the active state of a Rule changed."); + params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); + params.insert("active", JsonTypes::basicTypeToString(JsonTypes::Bool)); + setParams("RuleActiveChanged", params); + connect(GuhCore::instance(), &GuhCore::ruleAdded, this, &RulesHandler::ruleAddedNotification); connect(GuhCore::instance(), &GuhCore::ruleRemoved, this, &RulesHandler::ruleRemovedNotification); - + connect(GuhCore::instance(), &GuhCore::ruleActiveChanged, this, &RulesHandler::ruleActiveChangedNotification); } QString RulesHandler::name() const @@ -118,11 +126,11 @@ JsonReply* RulesHandler::GetRules(const QVariantMap ¶ms) Q_UNUSED(params) QVariantList rulesList; - foreach (const RuleId &ruleId, GuhCore::instance()->ruleIds()) { - rulesList.append(ruleId); + foreach (const Rule &rule, GuhCore::instance()->rules()) { + rulesList.append(JsonTypes::packRuleDescription(rule)); } QVariantMap returns; - returns.insert("ruleIds", rulesList); + returns.insert("ruleDescriptions", rulesList); return createReply(returns); } @@ -365,3 +373,12 @@ void RulesHandler::ruleAddedNotification(const Rule &rule) emit RuleAdded(params); } + +void RulesHandler::ruleActiveChangedNotification(const Rule &rule) +{ + QVariantMap params; + params.insert("ruleId", rule.id()); + params.insert("active", rule.active()); + + emit RuleActiveChanged(params); +} diff --git a/server/jsonrpc/ruleshandler.h b/server/jsonrpc/ruleshandler.h index 97e32725..ac864d57 100644 --- a/server/jsonrpc/ruleshandler.h +++ b/server/jsonrpc/ruleshandler.h @@ -45,6 +45,7 @@ public: signals: void RuleRemoved(const QVariantMap ¶ms); void RuleAdded(const QVariantMap ¶ms); + void RuleActiveChanged(const QVariantMap ¶ms); private: QVariant::Type getActionParamType(const ActionTypeId &actionTypeId, const QString ¶mName); @@ -55,6 +56,7 @@ private: private slots: void ruleRemovedNotification(const RuleId &ruleId); void ruleAddedNotification(const Rule &rule); + void ruleActiveChangedNotification(const Rule &rule); };