From d7d06eb84ac8726465830102739991354c85b11d Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 1 Apr 2019 10:46:34 +0200 Subject: [PATCH] Add support for state based rule action params --- .../connection/nymeaconnection.cpp | 6 +++++- libnymea-app-core/jsonrpc/jsonrpcclient.cpp | 6 ------ libnymea-app-core/jsonrpc/jsontypes.cpp | 7 +++++-- libnymea-app-core/rulemanager.cpp | 2 ++ libnymea-common/types/ruleactionparam.cpp | 21 +++++++++++++++++++ libnymea-common/types/ruleactionparam.h | 13 ++++++++++++ nymea-app/ui/magic/RuleActionDelegate.qml | 20 ++++++++++++++---- .../ui/magic/SelectRuleActionParamsPage.qml | 3 +++ 8 files changed, 65 insertions(+), 13 deletions(-) diff --git a/libnymea-app-core/connection/nymeaconnection.cpp b/libnymea-app-core/connection/nymeaconnection.cpp index 120bffc8..ba051543 100644 --- a/libnymea-app-core/connection/nymeaconnection.cpp +++ b/libnymea-app-core/connection/nymeaconnection.cpp @@ -372,7 +372,11 @@ void NymeaConnection::onDisconnected() // Try to reconnect, only if we're not waiting for SSL certs to be trusted. if (m_connectionStatus != ConnectionStatusSslUntrusted) { - connectInternal(m_currentHost); + QTimer::singleShot(1000, this, [this](){ + if (m_currentHost) { + connectInternal(m_currentHost); + } + }); } } diff --git a/libnymea-app-core/jsonrpc/jsonrpcclient.cpp b/libnymea-app-core/jsonrpc/jsonrpcclient.cpp index c77de29e..bd8ec6c2 100644 --- a/libnymea-app-core/jsonrpc/jsonrpcclient.cpp +++ b/libnymea-app-core/jsonrpc/jsonrpcclient.cpp @@ -318,14 +318,8 @@ void JsonRpcClient::onInterfaceConnectedChanged(bool connected) emit connectedChanged(false); } } else { - QVariantMap request; - request.insert("id", 0); - qDebug() << "Connected. Starting JSONRPC Handshake"; - request.insert("method", "JSONRPC.Hello"); QVariantMap params; params.insert("locale", QLocale().name()); - request.insert("params", params); -// sendRequest(request); sendCommand("JSONRPC.Hello", params, this, "helloReply"); } } diff --git a/libnymea-app-core/jsonrpc/jsontypes.cpp b/libnymea-app-core/jsonrpc/jsontypes.cpp index 7ab41211..55d3461a 100644 --- a/libnymea-app-core/jsonrpc/jsontypes.cpp +++ b/libnymea-app-core/jsonrpc/jsontypes.cpp @@ -304,11 +304,14 @@ QVariantList JsonTypes::packRuleActions(RuleActions *ruleActions) } else { ruleActionParam.insert("paramName", rap->paramName()); } - if (!rap->eventTypeId().isEmpty() && !rap->eventParamTypeId().isEmpty()) { + if (rap->isValueBased()) { + ruleActionParam.insert("value", rap->value()); + } else if (rap->isEventParamBased()) { ruleActionParam.insert("eventTypeId", rap->eventTypeId()); ruleActionParam.insert("eventParamTypeId", rap->eventParamTypeId()); } else { - ruleActionParam.insert("value", rap->value()); + ruleActionParam.insert("stateDeviceId", rap->stateDeviceId()); + ruleActionParam.insert("stateTypeId", rap->stateTypeId()); } ruleActionParams.append(ruleActionParam); } diff --git a/libnymea-app-core/rulemanager.cpp b/libnymea-app-core/rulemanager.cpp index 1e67667d..8ea580ca 100644 --- a/libnymea-app-core/rulemanager.cpp +++ b/libnymea-app-core/rulemanager.cpp @@ -297,6 +297,8 @@ RuleAction *RuleManager::parseRuleAction(const QVariantMap &ruleAction) param->setValue(ruleActionParamVariant.toMap().value("value")); param->setEventTypeId(ruleActionParamVariant.toMap().value("eventTypeId").toString()); param->setEventParamTypeId(ruleActionParamVariant.toMap().value("eventParamTypeId").toString()); + param->setStateDeviceId(ruleActionParamVariant.toMap().value("stateDeviceId").toString()); + param->setStateTypeId(ruleActionParamVariant.toMap().value("stateTypeId").toString()); ret->ruleActionParams()->addRuleActionParam(param); } return ret; diff --git a/libnymea-common/types/ruleactionparam.cpp b/libnymea-common/types/ruleactionparam.cpp index 289a7fe9..0ff29372 100644 --- a/libnymea-common/types/ruleactionparam.cpp +++ b/libnymea-common/types/ruleactionparam.cpp @@ -7,6 +7,8 @@ RuleActionParam::RuleActionParam(const QString ¶mName, const QVariant &value m_paramName(paramName) { setValue(value); + + connect(this, &Param::valueChanged, this, &RuleActionParam::isValueBasedChanged); } RuleActionParam::RuleActionParam(QObject *parent) : Param(parent) @@ -37,6 +39,7 @@ void RuleActionParam::setEventTypeId(const QString &eventTypeId) if (m_eventTypeId != eventTypeId) { m_eventTypeId = eventTypeId; emit eventTypeIdChanged(); + emit isEventParamBasedChanged(); } } @@ -50,6 +53,7 @@ void RuleActionParam::setEventParamTypeId(const QString &eventParamTypeId) if (m_eventParamTypeId != eventParamTypeId) { m_eventParamTypeId = eventParamTypeId; emit eventParamTypeIdChanged(); + emit isEventParamBasedChanged(); } } @@ -63,6 +67,7 @@ void RuleActionParam::setStateDeviceId(const QString &stateDeviceId) if (m_stateDeviceId != stateDeviceId) { m_stateDeviceId = stateDeviceId; emit stateDeviceIdChanged(); + emit isStateValueBasedChanged(); } } @@ -76,9 +81,25 @@ void RuleActionParam::setStateTypeId(const QString &stateTypeId) if (m_stateTypeId != stateTypeId) { m_stateTypeId = stateTypeId; emit stateTypeIdChanged(); + emit isStateValueBasedChanged(); } } +bool RuleActionParam::isValueBased() const +{ + return !m_value.isNull(); +} + +bool RuleActionParam::isEventParamBased() const +{ + return !m_eventTypeId.isNull() && !m_eventParamTypeId.isNull(); +} + +bool RuleActionParam::isStateValueBased() const +{ + return !m_stateDeviceId.isNull() && !m_stateTypeId.isNull(); +} + RuleActionParam *RuleActionParam::clone() const { RuleActionParam *ret = new RuleActionParam(); diff --git a/libnymea-common/types/ruleactionparam.h b/libnymea-common/types/ruleactionparam.h index a0f161bd..cfd7f8b6 100644 --- a/libnymea-common/types/ruleactionparam.h +++ b/libnymea-common/types/ruleactionparam.h @@ -15,6 +15,11 @@ class RuleActionParam : public Param Q_PROPERTY(QString eventParamTypeId READ eventParamTypeId WRITE setEventParamTypeId NOTIFY eventParamTypeIdChanged) Q_PROPERTY(QString stateDeviceId READ stateDeviceId WRITE setStateDeviceId NOTIFY stateDeviceIdChanged) Q_PROPERTY(QString stateTypeId READ stateTypeId WRITE setStateTypeId NOTIFY stateTypeIdChanged) + + Q_PROPERTY(bool isValueBased READ isValueBased NOTIFY isValueBasedChanged) + Q_PROPERTY(bool isEventParamBased READ isEventParamBased NOTIFY isEventParamBasedChanged) + Q_PROPERTY(bool isStateValueBased READ isStateValueBased NOTIFY isStateValueBasedChanged) + public: explicit RuleActionParam(const QString ¶mName, const QVariant &value, QObject *parent = nullptr); explicit RuleActionParam(QObject *parent = nullptr); @@ -34,6 +39,10 @@ public: QString stateTypeId() const; void setStateTypeId(const QString &stateTypeId); + bool isValueBased() const; + bool isEventParamBased() const; + bool isStateValueBased() const; + RuleActionParam* clone() const; bool operator==(RuleActionParam *other) const; signals: @@ -43,6 +52,10 @@ signals: void stateDeviceIdChanged(); void stateTypeIdChanged(); + void isValueBasedChanged(); + void isEventParamBasedChanged(); + void isStateValueBasedChanged(); + protected: QString m_paramName; QString m_eventTypeId; diff --git a/nymea-app/ui/magic/RuleActionDelegate.qml b/nymea-app/ui/magic/RuleActionDelegate.qml index a5f78cbc..79c56b4d 100644 --- a/nymea-app/ui/magic/RuleActionDelegate.qml +++ b/nymea-app/ui/magic/RuleActionDelegate.qml @@ -28,10 +28,22 @@ MeaListItemDelegate { var ret = []; for (var i = 0; i < root.ruleAction.ruleActionParams.count; i++) { var ruleActionParam = root.ruleAction.ruleActionParams.get(i) - print("populating subtext:", ruleActionParam.eventTypeId, ruleActionParam.eventParamTypeId) - var paramString = qsTr("%1: %2") - .arg(root.actionType.paramTypes.getParamType(ruleActionParam.paramTypeId).displayName) - .arg(ruleActionParam.eventParamTypeId.length > 0 ? qsTr("value from event") : ruleActionParam.value) + print("populating subtext:", ruleActionParam.eventTypeId, ruleActionParam.eventParamTypeId, ruleActionParam.stateDeviceId, ruleActionParam.stateTypeId, ruleActionParam.isValueBased, ruleActionParam.isEventParamBased, ruleActionParam.isStateValueBased) + + + var paramString = qsTr("%1: %2").arg(root.actionType.paramTypes.getParamType(ruleActionParam.paramTypeId).displayName) + if (ruleActionParam.isValueBased) { + paramString = paramString.arg(ruleActionParam.value) + } else if (ruleActionParam.isEventParamBased) { + paramString = paramString.arg(qsTr("value from event")) + } else if (ruleActionParam.isStateValueBased) { + var stateDevice = engine.deviceManager.devices.getDevice(ruleActionParam.stateDeviceId) + var stateType = stateDevice.deviceClass.stateTypes.getStateType(ruleActionParam.stateTypeId) + print("have state value based param:", stateDevice.name) + paramString = paramString.arg(stateDevice.name + "." + stateType.displayName) + + } + ret.push(paramString) } return ret.join(', ') diff --git a/nymea-app/ui/magic/SelectRuleActionParamsPage.qml b/nymea-app/ui/magic/SelectRuleActionParamsPage.qml index fd4383d2..9f609fbd 100644 --- a/nymea-app/ui/magic/SelectRuleActionParamsPage.qml +++ b/nymea-app/ui/magic/SelectRuleActionParamsPage.qml @@ -147,6 +147,9 @@ Page { statePickerDelegate.stateTypeId = stateTypeId; }) }) + page.backPressed.connect(function() { + pageStack.pop(); + }) } } }