fix rules again

pull/1/head
Michael Zanetti 2014-04-13 14:07:35 +02:00
parent b69a640aa1
commit b85aa3072e
7 changed files with 60 additions and 34 deletions

View File

@ -88,6 +88,19 @@ GuhCore::GuhCore(QObject *parent) :
void GuhCore::gotEvent(const Event &event)
{
foreach (const Action &action, m_ruleEngine->evaluateEvent(event)) {
m_deviceManager->executeAction(action);
qDebug() << "executing action" << action.actionTypeId();
DeviceManager::DeviceError status = m_deviceManager->executeAction(action);
switch(status) {
case DeviceManager::DeviceErrorNoError:
break;
case DeviceManager::DeviceErrorSetupFailed:
qDebug() << "Error executing action. Device setup failed.";
break;
case DeviceManager::DeviceErrorActionParameterError:
qDebug() << "Error executing action. Invalid action parameter.";
break;
default:
qDebug() << "Error executing action:" << status;
}
}
}

View File

@ -83,7 +83,7 @@ void JsonTypes::init()
// Event
s_event.insert("eventTypeId", "uuid");
s_event.insert("deviceId", "uuid");
s_event.insert("params", QVariantList() << paramRef());
s_event.insert("o:params", QVariantList() << paramRef());
// ActionType
s_actionType.insert("id", "uuid");
@ -122,7 +122,7 @@ void JsonTypes::init()
s_rule.insert("id", "uuid");
s_rule.insert("ruleType", ruleTypesRef());
s_rule.insert("event", eventRef());
s_rule.insert("events", QVariantList() << eventRef());
s_rule.insert("actions", QVariantList() << actionRef());
s_rule.insert("states", QVariantList() << stateRef());
@ -256,8 +256,14 @@ QVariantMap JsonTypes::packRule(const Rule &rule)
{
QVariantMap ruleMap;
ruleMap.insert("id", rule.id());
// ruleMap.insert("event", JsonTypes::packEvent(rule.event()));
QVariantList eventList;
foreach (const Event &event, rule.events()) {
eventList.append(JsonTypes::packEvent(event));
}
ruleMap.insert("events", eventList);
ruleMap.insert("ruleType", s_ruleTypes.at(rule.ruleType()));
QVariantList actionList;
foreach (const Action &action, rule.actions()) {
actionList.append(JsonTypes::packAction(action));
@ -277,7 +283,9 @@ QPair<bool, QString> JsonTypes::validateMap(const QVariantMap &templateMap, cons
strippedKey.remove(QRegExp("^o:"));
if (!key.startsWith("o:") && !map.contains(strippedKey)) {
qDebug() << "missing key" << key << templateMap << map;
qDebug() << "*** missing key" << key;
qDebug() << "Expected:" << templateMap;
qDebug() << "Got:" << map;
QJsonDocument jsonDoc = QJsonDocument::fromVariant(map);
return report(false, QString("Missing key \"%1\" in %2").arg(key).arg(QString(jsonDoc.toJson())));
}
@ -346,6 +354,7 @@ QPair<bool, QString> JsonTypes::validateVariant(const QVariant &templateVariant,
return result;
}
} else if (refName == JsonTypes::eventRef()) {
qDebug() << "validating event";
QPair<bool, QString> result = validateMap(eventDescription(), variant.toMap());
if (!result.first) {
qDebug() << "event not valid";

View File

@ -105,6 +105,7 @@ QVariantMap RulesHandler::AddRule(const QVariantMap &params)
switch(GuhCore::instance()->ruleEngine()->addRule(event, actions)) {
case RuleEngine::RuleErrorNoError:
returns.insert("success", true);
returns.insert("errorMessage", "");
break;
case RuleEngine::RuleErrorDeviceNotFound:
returns.insert("success", false);

View File

@ -52,7 +52,7 @@
\l{Rule::RuleTypeAll}.*/
Rule::Rule(const QUuid &id, const Event &event, const QList<State> &states, const QList<Action> &actions):
m_id(id),
m_event(event),
m_events(QList<Event>() << event),
m_states(states),
m_actions(actions),
m_ruleType(RuleTypeAll)
@ -66,9 +66,9 @@ QUuid Rule::id() const
}
/*! Returns the \l{Event} that events this Rule.*/
Event Rule::event() const
QList<Event> Rule::events() const
{
return m_event;
return m_events;
}
/*! Returns the \l{State}{States} that need to be matching in order for this to Rule apply. */

View File

@ -37,7 +37,7 @@ public:
Rule(const QUuid &id, const Event &event, const QList<State> &states, const QList<Action> &actions);
QUuid id() const;
Event event() const;
QList<Event> events() const;
QList<State> states() const;
QList<Action> actions() const;
@ -46,7 +46,7 @@ public:
private:
QUuid m_id;
Event m_event;
QList<Event> m_events;
QList<State> m_states;
StateEvaluator stateEvaluator;
QList<Action> m_actions;

View File

@ -120,28 +120,28 @@ RuleEngine::RuleEngine(QObject *parent) :
QList<Action> RuleEngine::evaluateEvent(const Event &event)
{
QList<Action> actions;
// for (int i = 0; i < m_rules.count(); ++i) {
// if (m_rules.at(i).events().contains(event)) {
// bool statesMatching = true;
// qDebug() << "checking states";
// foreach (const State &state, m_rules.at(i).stateChanges()) {
// Device *device = GuhCore::instance()->deviceManager()->findConfiguredDevice(state.deviceId());
// if (!device) {
// qWarning() << "Device referenced in rule cannot be found";
// break;
// }
// if (state.value() != device->stateValue(state.stateTypeId())) {
// statesMatching = false;
// break;
// }
// }
for (int i = 0; i < m_rules.count(); ++i) {
if (m_rules.at(i).events().contains(event)) {
bool statesMatching = true;
qDebug() << "checking states";
foreach (const State &state, m_rules.at(i).states()) {
Device *device = GuhCore::instance()->deviceManager()->findConfiguredDevice(state.deviceId());
if (!device) {
qWarning() << "Device referenced in rule cannot be found";
break;
}
if (state.value() != device->stateValue(state.stateTypeId())) {
statesMatching = false;
break;
}
}
// qDebug() << "states matching" << statesMatching;
// if (statesMatching) {
// actions.append(m_rules.at(i).actions());
// }
// }
// }
qDebug() << "states matching" << statesMatching;
if (statesMatching) {
actions.append(m_rules.at(i).actions());
}
}
}
qDebug() << "found" << actions.count() << "actions";
return actions;
}

View File

@ -1,9 +1,12 @@
#!/bin/bash
if test -z $5; then
echo "usage: $0 host sourceDevice eventTypeId targetDeviceId actionTypeId"
echo "usage: $0 host sourceDevice eventTypeId targetDeviceId actionTypeId [paramname paramvalue]"
elif test -z $6; then
(echo '{"id":1, "method":"Rules.AddRule", "params":{"event": {"eventTypeId": "$3", "deviceId":"'$2'"}, "actions": [ { "deviceId":"'$4'", "actionTypeId":"'$5'"}]}}'; sleep 1) | nc $1 1234
elif test -z $7; then
echo "usage: $0 host sourceDevice eventTypeId targetDeviceId actionTypeId [paramname paramvalue]"
else
(echo '{"id":1, "method":"Rules.AddRule", "params":{"event": {"eventTypeId": "$3", "deviceId":"'$2'"}, "actions": [ { "deviceId":"'$4'", "actionTypeId":"'$5'", "params":{"power":"true"}}]}}'; sleep 1) | nc $1 1234
# (echo '{"id":1, "method":"Rules.AddRule", "params":{"event": {"eventTypeId": "'$3'", "deviceId":"'$2'"}, "actions": [ { "deviceId":"'$4'", "actionTypeId":"'$5'", "params":{"power":"true"}}]}}'; sleep 1) | nc $1 1234
(echo '{"id":1, "method":"Rules.AddRule", "params":{"event": {"eventTypeId": "'$3'", "deviceId":"'$2'"}, "actions": [ { "deviceId":"'$4'", "actionTypeId":"'$5'", "params":{"'$6'":"'$7'"}}]}}'; sleep 1) | nc $1 1234
# (echo '{"id":1, "method":"Rules.AddRule", "params":{"event": {"eventTypeId": "'$2'", "deviceId":"'$3'", "params":{"power":"false"}}, "actions": [ { "deviceId":"'$4'", "name":"rule 1", "params":{"power":"false"}},{ "deviceId":"'$5'", "name":"rule 1", "params":{"power":"true"}}]}}'; sleep 1) | nc $1 1234
fi