Add rule templates for daylight and presence sensors

This commit is contained in:
Michael Zanetti 2019-01-07 23:28:55 +01:00
parent f11a9ba35c
commit ea3e2d2106
12 changed files with 320 additions and 14 deletions

View File

@ -241,7 +241,7 @@ void DeviceManager::getConfiguredDevicesResponse(const QVariantMap &params)
device->setStateValue(stateTypeId, value);
// qDebug() << "Set device state value:" << device->stateValue(stateTypeId) << value;
}
qDebug() << "Confgured Device JSON:" << qUtf8Printable(QJsonDocument::fromVariant(deviceVariant).toJson(QJsonDocument::Indented));
// qDebug() << "Configured Device JSON:" << qUtf8Printable(QJsonDocument::fromVariant(deviceVariant).toJson(QJsonDocument::Indented));
devices()->addDevice(device);
qDebug() << "*** Added device:" << endl << device;
}

View File

@ -168,7 +168,7 @@ void RuleManager::getRuleDetailsReply(const QVariantMap &params)
void RuleManager::onAddRuleReply(const QVariantMap &params)
{
qDebug() << "Add rule reply:" << params.value("params").toMap().value("ruleError").toString();
qDebug() << "Add rule reply:" << params;//.value("params").toMap().value("ruleError").toString();
emit addRuleReply(params.value("params").toMap().value("ruleError").toString(), params.value("params").toMap().value("ruleId").toString());
}

View File

@ -62,6 +62,12 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
evpt->setParamName(eventDescriptorParamTemplate.value("name").toString());
if (eventDescriptorParamTemplate.contains("value")) {
evpt->setValue(eventDescriptorParamTemplate.value("value"));
if (!eventDescriptorParamTemplate.contains("operator")) {
qWarning() << "BROKEN Template: Operator missing for event descriptor template" << qUtf8Printable(QJsonDocument::fromVariant(eventDescriptorParamTemplate).toJson(QJsonDocument::Indented));
} else {
QMetaEnum operatorEnum = QMetaEnum::fromType<ParamDescriptor::ValueOperator>();
evpt->setOperatorType(static_cast<ParamDescriptor::ValueOperator>(operatorEnum.keyToValue(eventDescriptorParamTemplate.value("operator").toByteArray().data())));
}
}
evt->paramDescriptors()->addParamDescriptor(evpt);
}
@ -144,9 +150,9 @@ RuleTemplates::RuleTemplates(QObject *parent) : QAbstractListModel(parent)
t->ruleExitActionTemplates()->addRuleActionTemplate(rat);
}
qDebug() << "Added rule template:" << t->ruleActionTemplates()->rowCount();
m_list.append(t);
}
// qDebug() << "Loaded" << m_list.count() << "rule templates";
}
}
@ -187,7 +193,7 @@ bool RuleTemplatesFilterModel::filterAcceptsRow(int source_row, const QModelInde
return false;
}
RuleTemplate *t = m_ruleTemplates->get(source_row);
qDebug() << "Checking interface" << t->description() << t->interfaceName() << "for usage with:" << m_filterInterfaceNames;
// qDebug() << "Checking interface" << t->description() << t->interfaceName() << "for usage with:" << m_filterInterfaceNames;
if (!m_filterInterfaceNames.isEmpty()) {
if (!m_filterInterfaceNames.contains(t->interfaceName())) {
return false;

View File

@ -27,6 +27,11 @@ Interfaces::Interfaces(QObject *parent) : QAbstractListModel(parent)
addParamType(pts, "body", tr("Message body"), QVariant::String);
addActionType("notifications", "notify", tr("Send notification"), pts);
addInterface("power", tr("Powered things"));
addStateType("power", "power", QVariant::Bool, true,
tr("Thing is turned on"),
tr("A thing is turned on or off"),
tr("Turn things on or off"));
addInterface("light", tr("Lights"));
addStateType("light", "power", QVariant::Bool, true,

View File

@ -126,5 +126,7 @@ target.path = /usr/bin
INSTALLS += target
DISTFILES += \
ruletemplates/smartmetertemplates.json
ruletemplates/smartmetertemplates.json \
ruletemplates/presencesensortemplates.json \
ruletemplates/daylightsensor.json

View File

@ -4,5 +4,7 @@
<file>ruletemplates/notificationtemplates.json</file>
<file>ruletemplates/accesscontroltemplates.json</file>
<file>ruletemplates/smartmetertemplates.json</file>
<file>ruletemplates/presencesensortemplates.json</file>
<file>ruletemplates/daylightsensor.json</file>
</qresource>
</RCC>

View File

@ -0,0 +1,110 @@
{
"templates": [
{
"interfaceName": "daylightsensor",
"description": "Turn on a light while it's dark outside.",
"ruleNameTemplate": "Turn on %1 while it's dark outside",
"stateEvaluatorTemplate": {
"stateDescriptorTemplate": {
"interfaceName": "daylightsensor",
"interfaceState": "daylight",
"selectionId": 0,
"operator": "ValueOperatorEquals",
"value": false
}
},
"ruleActionTemplates": [
{
"interfaceName": "power",
"interfaceAction": "power",
"selectionId": 1,
"params": [
{
"name": "power",
"value": "true"
}
]
}
],
"ruleExitActionTemplates": [
{
"interfaceName": "power",
"interfaceAction": "power",
"selectionId": 1,
"params": [
{
"name": "power",
"value": "false"
}
]
}
]
},
{
"interfaceName": "daylightsensor",
"description": "Turn on a light when it gets dark outside.",
"ruleNameTemplate": "Turn on %1 it gets dark outside (%0).",
"eventDescriptorTemplates": [
{
"interfaceName": "daylightsensor",
"interfaceEvent": "daylight",
"selectionId": 0,
"params": [
{
"name": "daylight",
"value": false,
"operator": "ValueOperatorEquals"
}
]
}
],
"ruleActionTemplates": [
{
"interfaceName": "light",
"interfaceAction": "power",
"selectionId": 1,
"params": [
{
"name": "power",
"value": "true"
}
]
}
]
},
{
"interfaceName": "daylightsensor",
"description": "Turn on all lights when it gets dark outside.",
"ruleNameTemplate": "Turn on all lights when it gets dark outside.",
"eventDescriptorTemplates": [
{
"interfaceName": "daylightsensor",
"interfaceEvent": "daylight",
"selectionId": 0,
"params": [
{
"name": "daylight",
"value": false,
"operator": "ValueOperatorEquals"
}
]
}
],
"ruleActionTemplates": [
{
"interfaceName": "light",
"interfaceAction": "power",
"selectionId": 1,
"selectionMode": "SelectionModeInterface",
"params": [
{
"name": "power",
"value": "true"
}
]
}
]
}
]
}

View File

@ -0,0 +1,175 @@
{
"templates": [
{
"interfaceName": "presencesensor",
"description": "Turn on something while this device is present...",
"ruleNameTemplate": "Turn on %1 while %0 is present",
"stateEvaluatorTemplate": {
"stateDescriptorTemplate": {
"interfaceName": "presencesensor",
"interfaceState": "isPresent",
"selectionId": 0,
"operator": "ValueOperatorEquals",
"value": true
}
},
"ruleActionTemplates": [
{
"interfaceName": "power",
"interfaceAction": "power",
"selectionId": 1,
"params": [
{
"name": "power",
"value": "true"
}
]
}
],
"ruleExitActionTemplates": [
{
"interfaceName": "power",
"interfaceAction": "power",
"selectionId": 1,
"params": [
{
"name": "power",
"value": "false"
}
]
}
]
},
{
"interfaceName": "presencesensor",
"description": "Turn off something when this device leaves...",
"ruleNameTemplate": "Turn off %1 when %0 leaves",
"eventDescriptorTemplates": [
{
"interfaceName": "presencesensor",
"interfaceEvent": "isPresent",
"selectionId": 0,
"params": [
{
"name": "isPresent",
"value": false,
"operator": "ValueOperatorEquals"
}
]
}
],
"ruleActionTemplates": [
{
"interfaceName": "power",
"interfaceAction": "power",
"selectionId": 1,
"params": [
{
"name": "power",
"value": "false"
}
]
}
]
},
{
"interfaceName": "presencesensor",
"description": "Turn off everything when this device leaves...",
"ruleNameTemplate": "Turn off everything when %0 leaves",
"eventDescriptorTemplates": [
{
"interfaceName": "presencesensor",
"interfaceEvent": "isPresent",
"selectionId": 0,
"params": [
{
"name": "isPresent",
"value": false,
"operator": "ValueOperatorEquals"
}
]
}
],
"ruleActionTemplates": [
{
"interfaceName": "power",
"interfaceAction": "power",
"selectionId": 1,
"selectionMode": "SelectionModeInterface",
"params": [
{
"name": "power",
"value": "false"
}
]
}
]
},
{
"interfaceName": "presencesensor",
"description": "Turn off all lights when this device leaves...",
"ruleNameTemplate": "Turn off all lights when %0 leaves",
"eventDescriptorTemplates": [
{
"interfaceName": "presencesensor",
"interfaceEvent": "isPresent",
"selectionId": 0,
"params": [
{
"name": "isPresent",
"value": false,
"operator": "ValueOperatorEquals"
}
]
}
],
"ruleActionTemplates": [
{
"interfaceName": "light",
"interfaceAction": "power",
"selectionId": 1,
"selectionMode": "SelectionModeInterface",
"params": [
{
"name": "power",
"value": "false"
}
]
}
]
},
{
"interfaceName": "presencesensor",
"description": "Turn on something when this device arrives...",
"ruleNameTemplate": "Turn on %1 when %0 arrives",
"eventDescriptorTemplates": [
{
"interfaceName": "presencesensor",
"interfaceEvent": "isPresent",
"selectionId": 0,
"params": [
{
"name": "isPresent",
"value": true,
"operator": "ValueOperatorEquals"
}
]
}
],
"ruleActionTemplates": [
{
"interfaceName": "power",
"interfaceAction": "power",
"selectionId": 1,
"params": [
{
"name": "power",
"value": "true"
}
]
}
]
}
]
}

View File

@ -253,6 +253,8 @@ ApplicationWindow {
return qsTr("battery powered thing")
case "connectable":
return qsTr("connectable thing")
case "power":
return qsTr("switchable thing")
default:
console.warn("Unhandled interfaceToDisplayName:", name)
}

View File

@ -13,7 +13,6 @@ ListView {
Component.onCompleted: {
var supportedInterfaces = ["temperaturesensor", "humiditysensor", "pressuresensor", "moisturesensor", "lightsensor", "conductivitysensor", "noisesensor", "co2sensor", "presencesensor", "daylightsensor"]
for (var i = 0; i < supportedInterfaces.length; i++) {
print("checking", root.deviceClass.name, root.deviceClass.interfaces)
if (root.deviceClass.interfaces.indexOf(supportedInterfaces[i]) >= 0) {
append({name: supportedInterfaces[i]});
}

View File

@ -10,11 +10,11 @@ MeaListItemDelegate {
canDelete: true
progressive: false
property var eventDescriptor: null
readonly property var device: eventDescriptor ? engine.deviceManager.devices.getDevice(eventDescriptor.deviceId) : null
readonly property var deviceClass: device ? engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId) : null
readonly property var iface: eventDescriptor.interfaceName ? Interfaces.findByName(eventDescriptor.interfaceName) : null
readonly property var eventType: deviceClass ? deviceClass.eventTypes.getEventType(eventDescriptor.eventTypeId)
property EventDescriptor eventDescriptor: null
readonly property Device device: eventDescriptor ? engine.deviceManager.devices.getDevice(eventDescriptor.deviceId) : null
readonly property DeviceClass deviceClass: device ? engine.deviceManager.deviceClasses.getDeviceClass(device.deviceClassId) : null
readonly property Interface iface: eventDescriptor.interfaceName ? Interfaces.findByName(eventDescriptor.interfaceName) : null
readonly property EventType eventType: deviceClass ? deviceClass.eventTypes.getEventType(eventDescriptor.eventTypeId)
: iface ? iface.eventTypes.findByName(eventDescriptor.interfaceEvent) : null
signal removeEventDescriptor()
@ -51,16 +51,20 @@ MeaListItemDelegate {
operatorString = " ? ";
}
var paramType = paramDescriptor.paramTypeId
? root.eventType.paramTypes.getParamType(paramDescriptor.paramTypeId)
: root.eventType.paramTypes.findByName(paramDescriptor.paramName)
if (i === 0) {
// TRANSLATORS: example: "only if temperature > 5"
ret = qsTr("only if %1 %2 %3")
.arg(root.eventType.paramTypes.getParamType(paramDescriptor.paramTypeId).displayName)
.arg(paramType.displayName)
.arg(operatorString)
.arg(paramDescriptor.value)
} else {
// TRANSLATORS: example: "and temperature > 5"
ret += " " + qsTr("and %1 %2 %3")
.arg(root.eventType.paramTypes.getParamType(paramDescriptor.paramTypeId).displayName)
.arg(paramType.displayName)
.arg(operatorString)
.arg(model.value)
}

View File

@ -257,7 +257,8 @@ Page {
for (var j = 0; j < eventDescriptorTemplate.paramDescriptors.count; j++) {
var paramDescriptorTemplate = eventDescriptorTemplate.paramDescriptors.get(j);
if (paramDescriptorTemplate.value !== undefined) {
eventDescriptor.paramDescriptors.addParamDescriptor(paramDescriptorTemplate.paramName, paramDescriptorTemplate.value);
print("Adding operator:", paramDescriptorTemplate.operatorType)
eventDescriptor.paramDescriptors.setParamDescriptorByName(paramDescriptorTemplate.paramName, paramDescriptorTemplate.value, paramDescriptorTemplate.operatorType);
} else {
needsParams = true;
}