add rule active status to rule details

add rule active status changed notification
add RuleDescription (and change API Rules.GetRules method)
pull/135/head
Simon Stürz 2015-06-08 16:06:21 +02:00 committed by Michael Zanetti
parent 9edb3b8b40
commit 01b92e325a
6 changed files with 57 additions and 8 deletions

View File

@ -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 {

View File

@ -103,6 +103,7 @@ signals:
void ruleRemoved(const RuleId &ruleId);
void ruleAdded(const Rule &rule);
void ruleActiveChanged(const Rule &rule);
private:

View File

@ -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<bool, QString> JsonTypes::validateVariant(const QVariant &templateVariant,
qDebug() << "rule type not matching";
return result;
}
} else if (refName == ruleDescriptionRef()) {
QPair<bool, QString> result = validateMap(s_ruleDescription, variant.toMap());
if (!result.first) {
qDebug() << "ruleDescription type not matching";
return result;
}
} else if (refName == eventDescriptorRef()) {
QPair<bool, QString> result = validateMap(eventDescriptorDescription(), variant.toMap());
if (!result.first) {

View File

@ -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);

View File

@ -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 &params)
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);
}

View File

@ -45,6 +45,7 @@ public:
signals:
void RuleRemoved(const QVariantMap &params);
void RuleAdded(const QVariantMap &params);
void RuleActiveChanged(const QVariantMap &params);
private:
QVariant::Type getActionParamType(const ActionTypeId &actionTypeId, const QString &paramName);
@ -55,6 +56,7 @@ private:
private slots:
void ruleRemovedNotification(const RuleId &ruleId);
void ruleAddedNotification(const Rule &rule);
void ruleActiveChangedNotification(const Rule &rule);
};