allow passing params if QVariant can convert them

pull/135/head
Michael Zanetti 2018-06-06 01:37:37 +02:00
parent 23316942ce
commit 11d56d1020
3 changed files with 66 additions and 2 deletions

View File

@ -617,7 +617,7 @@ void NymeaCore::gotEvent(const Event &event)
// something like a EventParamDescriptor
ruleActionParam.setValue(eventValue);
qCDebug(dcRuleEngine) << "take over event param value" << ruleActionParam.value();
qCDebug(dcRuleEngine) << "Using param value from event:" << ruleActionParam.value();
}
newParams.append(ruleActionParam);
}

View File

@ -595,8 +595,9 @@ RuleEngine::RuleError RuleEngine::addRule(const Rule &rule, bool fromEdit)
// check if the param type of the event and the action match
QVariant::Type eventParamType = getEventParamType(ruleActionParam.eventTypeId(), ruleActionParam.eventParamTypeId());
QVariant v(eventParamType);
QVariant::Type actionParamType = getActionParamType(action.actionTypeId(), ruleActionParam.paramTypeId());
if (eventParamType != actionParamType) {
if (eventParamType != actionParamType && !v.canConvert(actionParamType)) {
qCWarning(dcRuleEngine) << "Cannot create rule. RuleActionParam" << ruleActionParam.paramTypeId().toString() << " and given event param " << ruleActionParam.eventParamTypeId().toString() << "have not the same type:";
qCWarning(dcRuleEngine) << " -> actionParamType:" << actionParamType;
qCWarning(dcRuleEngine) << " -> eventParamType:" << eventParamType;

View File

@ -104,6 +104,9 @@ private slots:
void testRuleActionParams_data();
void testRuleActionParams();
void testRuleActionPAramsFromEventParameter_data();
void testRuleActionPAramsFromEventParameter();
void testInterfaceBasedRule();
void testHousekeeping_data();
@ -2175,6 +2178,66 @@ void TestRules::testRuleActionParams()
verifyRuleError(response, error);
}
void TestRules::testRuleActionPAramsFromEventParameter_data() {
QTest::addColumn<QVariantMap>("event");
QTest::addColumn<QVariantMap>("action");
QTest::addColumn<RuleEngine::RuleError>("error");
QVariantMap intEvent;
intEvent.insert("eventTypeId", mockIntStateId);
intEvent.insert("deviceId", m_mockDeviceId);
QVariantMap intAction;
intAction.insert("actionTypeId", mockActionIdWithParams);
intAction.insert("deviceId", m_mockDeviceId);
QVariantList ruleActionParams;
QVariantMap intParam;
intParam.insert("paramTypeId", mockActionParam1ParamTypeId);
intParam.insert("eventTypeId", mockIntStateId);
intParam.insert("eventParamTypeId", mockIntStateId);
ruleActionParams.append(intParam);
QVariantMap boolParam;
boolParam.insert("paramTypeId", mockActionParam2ParamTypeId);
boolParam.insert("value", true);
ruleActionParams.append(boolParam);
intAction.insert("ruleActionParams", ruleActionParams);
QVariantMap boolAction;
boolAction.insert("actionTypeId", mockActionIdWithParams);
boolAction.insert("deviceId", m_mockDeviceId);
ruleActionParams.clear();
intParam.clear();
intParam.insert("paramTypeId", mockActionParam1ParamTypeId);
intParam.insert("value", 5);
ruleActionParams.append(intParam);
boolParam.clear();
boolParam.insert("paramTypeId", mockActionParam2ParamTypeId);
boolParam.insert("eventTypeId", mockIntStateId);
boolParam.insert("eventParamTypeId", mockIntStateId);
ruleActionParams.append(boolParam);
boolAction.insert("ruleActionParams", ruleActionParams);
QTest::newRow("int -> int") << intEvent << intAction << RuleEngine::RuleErrorNoError;
QTest::newRow("int -> bool") << intEvent << boolAction << RuleEngine::RuleErrorNoError;
}
void TestRules::testRuleActionPAramsFromEventParameter()
{
QFETCH(QVariantMap, event);
QFETCH(QVariantMap, action);
QFETCH(RuleEngine::RuleError, error);
QVariantMap addRuleParams;
addRuleParams.insert("name", "TestRule");
addRuleParams.insert("enabled", true);
addRuleParams.insert("eventDescriptors", QVariantList() << event);
addRuleParams.insert("actions", QVariantList() << action);
QVariant response = injectAndWait("Rules.AddRule", addRuleParams);
verifyRuleError(response, error);
}
void TestRules::testInterfaceBasedRule()
{
QVariantMap powerAction;