fix comparison of eventDescriptor params in rule evaluation

pull/135/head
Michael Zanetti 2018-02-07 13:44:25 +01:00
parent 8862e357a6
commit a1880c1be9
2 changed files with 84 additions and 7 deletions

View File

@ -1101,42 +1101,45 @@ bool RuleEngine::containsEvent(const Rule &rule, const Event &event, const Devic
}
// Ok, either device/eventTypeId or interface/interfaceEvent are matching. Compare the paramdescriptor
bool allOK = true;
foreach (const ParamDescriptor &paramDescriptor, eventDescriptor.paramDescriptors()) {
switch (paramDescriptor.operatorType()) {
case Types::ValueOperatorEquals:
if (event.param(paramDescriptor.paramTypeId()).value() != paramDescriptor.value()) {
continue;
allOK = false;
}
break;
case Types::ValueOperatorNotEquals:
if (event.param(paramDescriptor.paramTypeId()).value() == paramDescriptor.value()) {
continue;
allOK = false;
}
break;
case Types::ValueOperatorGreater:
if (event.param(paramDescriptor.paramTypeId()).value() <= paramDescriptor.value()) {
continue;
allOK = false;
}
break;
case Types::ValueOperatorGreaterOrEqual:
if (event.param(paramDescriptor.paramTypeId()).value() < paramDescriptor.value()) {
continue;
allOK = false;
}
break;
case Types::ValueOperatorLess:
if (event.param(paramDescriptor.paramTypeId()).value() >= paramDescriptor.value()) {
continue;
allOK = false;
}
break;
case Types::ValueOperatorLessOrEqual:
if (event.param(paramDescriptor.paramTypeId()).value() < paramDescriptor.value()) {
continue;
allOK = false;
}
break;
}
}
// All matching!
return true;
if (allOK) {
return true;
}
}
return false;

View File

@ -78,6 +78,8 @@ private slots:
void evaluateEvent();
void evaluateEventParams();
void testStateEvaluator_data();
void testStateEvaluator();
@ -1403,6 +1405,78 @@ void TestRules::evaluateEvent()
verifyRuleExecuted(mockActionIdNoParams);
}
void TestRules::evaluateEventParams()
{
// Init bool state to true
QNetworkAccessManager nam;
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("true")));
QNetworkReply *reply = nam.get(request);
spy.wait();
QCOMPARE(spy.count(), 1);
reply->deleteLater();
// Add a rule
QVariantMap addRuleParams;
addRuleParams.insert("name", "TestRule");
QVariantList params;
QVariantMap boolParam;
boolParam.insert("paramTypeId", mockBoolStateId);
boolParam.insert("operator", "ValueOperatorEquals");
boolParam.insert("value", true);
params.append(boolParam);
QVariantMap event1;
event1.insert("eventTypeId", mockBoolStateId);
event1.insert("deviceId", m_mockDeviceId);
event1.insert("paramDescriptors", params);
QVariantList events;
events.append(event1);
addRuleParams.insert("eventDescriptors", events);
QVariantList actions;
QVariantMap action;
action.insert("actionTypeId", mockActionIdNoParams);
action.insert("deviceId", m_mockDeviceId);
actions.append(action);
addRuleParams.insert("actions", actions);
QVariant response = injectAndWait("Rules.AddRule", addRuleParams);
verifyRuleError(response);
// Trigger a non matching param
spy.clear();
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("false")));
reply = nam.get(request);
spy.wait();
QCOMPARE(spy.count(), 1);
reply->deleteLater();
verifyRuleNotExecuted();
// Trigger a matching param
spy.clear();
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("true")));
reply = nam.get(request);
spy.wait();
QCOMPARE(spy.count(), 1);
reply->deleteLater();
verifyRuleExecuted(mockActionIdNoParams);
// Reset back to false to not mess with other tests
spy.clear();
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("false")));
reply = nam.get(request);
spy.wait();
QCOMPARE(spy.count(), 1);
reply->deleteLater();
}
void TestRules::testStateChange() {
// Add a rule
QVariantMap addRuleParams;