From 6ab6f8a80be5fcba78f0be15c585925d3390d2f7 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 18 Nov 2019 21:50:58 +0100 Subject: [PATCH] tune script types a bit --- libnymea-core/scriptengine/scriptaction.cpp | 30 +++++++- libnymea-core/scriptengine/scriptaction.h | 6 ++ libnymea-core/scriptengine/scriptengine.cpp | 3 + libnymea-core/scriptengine/scriptevent.cpp | 2 +- libnymea-core/scriptengine/scriptstate.cpp | 85 +++++++++++++++++++-- libnymea-core/scriptengine/scriptstate.h | 16 +++- 6 files changed, 128 insertions(+), 14 deletions(-) diff --git a/libnymea-core/scriptengine/scriptaction.cpp b/libnymea-core/scriptengine/scriptaction.cpp index da88df82..7c65d081 100644 --- a/libnymea-core/scriptengine/scriptaction.cpp +++ b/libnymea-core/scriptengine/scriptaction.cpp @@ -5,6 +5,8 @@ #include +#include "loggingcategories.h" + namespace nymeaserver { ScriptAction::ScriptAction(QObject *parent) : QObject(parent) @@ -48,10 +50,36 @@ void ScriptAction::setActionTypeId(const QString &actionTypeId) } } +QString ScriptAction::actionName() const +{ + return m_actionName; +} + +void ScriptAction::setActionName(const QString &actionName) +{ + if (m_actionName != actionName) { + m_actionName = actionName; + emit actionNameChanged(); + } +} + void ScriptAction::execute(const QVariantList ¶ms) { + Device *device = m_deviceManager->configuredDevices().findById(DeviceId(m_deviceId)); + if (!device) { + qCWarning(dcScriptEngine) << "No device with id" << m_deviceId; + return; + } + ActionTypeId actionTypeId = ActionTypeId(m_actionTypeId); + if (actionTypeId.isNull()) { + actionTypeId = device->deviceClass().actionTypes().findByName(m_actionName).id(); + } + if (actionTypeId.isNull()) { + qCWarning(dcScriptEngine()) << "Either a valid actionTypeId or actionName is required"; + return; + } Action action; - action.setActionTypeId(ActionTypeId(m_actionTypeId)); + action.setActionTypeId(actionTypeId); action.setDeviceId(DeviceId(m_deviceId)); ParamList paramList; foreach (const QVariant &p, params) { diff --git a/libnymea-core/scriptengine/scriptaction.h b/libnymea-core/scriptengine/scriptaction.h index 98baab7e..36a455fe 100644 --- a/libnymea-core/scriptengine/scriptaction.h +++ b/libnymea-core/scriptengine/scriptaction.h @@ -13,6 +13,7 @@ class ScriptAction : public QObject, public QQmlParserStatus Q_OBJECT Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged) Q_PROPERTY(QString actionTypeId READ actionTypeId WRITE setActionTypeId NOTIFY actionTypeIdChanged) + Q_PROPERTY(QString actionName READ actionName WRITE setActionName NOTIFY actionNameChanged) public: explicit ScriptAction(QObject *parent = nullptr); void classBegin() override; @@ -24,17 +25,22 @@ public: QString actionTypeId() const; void setActionTypeId(const QString &actionTypeId); + QString actionName() const; + void setActionName(const QString &actionName); + public slots: void execute(const QVariantList ¶ms); signals: void deviceIdChanged(); void actionTypeIdChanged(); + void actionNameChanged(); public: DeviceManager *m_deviceManager = nullptr; QString m_deviceId; QString m_actionTypeId; + QString m_actionName; }; } diff --git a/libnymea-core/scriptengine/scriptengine.cpp b/libnymea-core/scriptengine/scriptengine.cpp index 4f994a98..dbe1ddc7 100644 --- a/libnymea-core/scriptengine/scriptengine.cpp +++ b/libnymea-core/scriptengine/scriptengine.cpp @@ -7,6 +7,8 @@ #include +#include "loggingcategories.h" + namespace nymeaserver { ScriptEngine::ScriptEngine(DeviceManager *deviceManager, QObject *parent) : QObject(parent), @@ -26,6 +28,7 @@ void ScriptEngine::loadScripts() QQmlApplicationEngine *engine = new QQmlApplicationEngine(this); engine->setProperty("deviceManager", reinterpret_cast(m_deviceManager)); + qCWarning(dcScriptEngine()) << "Loading script"; engine->load(fileName); } diff --git a/libnymea-core/scriptengine/scriptevent.cpp b/libnymea-core/scriptengine/scriptevent.cpp index cb9a8cb0..5665197c 100644 --- a/libnymea-core/scriptengine/scriptevent.cpp +++ b/libnymea-core/scriptengine/scriptevent.cpp @@ -58,7 +58,7 @@ void ScriptEvent::setEventName(const QString &eventName) void ScriptEvent::onEventTriggered(const Event &event) { - if (QUuid(m_deviceId) != event.deviceId()) { + if (DeviceId(m_deviceId) != event.deviceId()) { return; } diff --git a/libnymea-core/scriptengine/scriptstate.cpp b/libnymea-core/scriptengine/scriptstate.cpp index b161ffd8..dbf1e5f8 100644 --- a/libnymea-core/scriptengine/scriptstate.cpp +++ b/libnymea-core/scriptengine/scriptstate.cpp @@ -45,7 +45,21 @@ void ScriptState::setStateTypeId(const QString &stateTypeId) { if (m_stateTypeId != stateTypeId) { m_stateTypeId = stateTypeId; - emit stateTypeIdChanged(); + emit stateTypeChanged(); + store(); + } +} + +QString ScriptState::stateName() const +{ + return m_stateName; +} + +void ScriptState::setStateName(const QString &stateName) +{ + if (m_stateName != stateName) { + m_stateName = stateName; + emit stateTypeChanged(); store(); } } @@ -56,7 +70,12 @@ QVariant ScriptState::value() const if (!device) { return QVariant(); } - return device->stateValue(StateTypeId(m_stateTypeId)); + StateTypeId stateTypeId = StateTypeId(m_stateTypeId); + if (stateTypeId.isNull()) { + stateTypeId = device->deviceClass().stateTypes().findByName(m_stateName).id(); + } + + return device->stateValue(stateTypeId); } void ScriptState::setValue(const QVariant &value) @@ -73,17 +92,31 @@ void ScriptState::setValue(const QVariant &value) return; } - if (device->deviceClass().stateTypes().findById(StateTypeId(m_stateTypeId)).id().isNull()) { - qCWarning(dcScriptEngine) << "Device" << device->name() << "does not have a state with type id" << m_stateTypeId; + ActionTypeId actionTypeId; + if (!m_stateTypeId.isNull()) { + actionTypeId = device->deviceClass().stateTypes().findById(StateTypeId(m_stateTypeId)).id(); + if (actionTypeId.isNull()) { + qCWarning(dcScriptEngine) << "Device" << device->name() << "does not have a state with type id" << m_stateTypeId; + } + } + if (actionTypeId.isNull()) { + actionTypeId = device->deviceClass().stateTypes().findByName(stateName()).id(); + if (actionTypeId.isNull()) { + qCWarning(dcScriptEngine) << "Device" << device->name() << "does not have a state named" << m_stateName; + } + } + + if (actionTypeId.isNull()) { + qCWarning(dcScriptEngine()) << "Either stateTypeId or stateName is required to be valid."; return; } + Action action; action.setDeviceId(DeviceId(m_deviceId)); - action.setActionTypeId(ActionTypeId(m_stateTypeId)); - ParamList params = ParamList() << Param(ParamTypeId(m_stateTypeId), value); + action.setActionTypeId(ActionTypeId(actionTypeId)); + ParamList params = ParamList() << Param(ParamTypeId(actionTypeId), value); action.setParams(params); - qCDebug(dcScriptEngine()) << "setValueCalled2" << value; m_valueCache = QVariant(); m_pendingActionInfo = m_deviceManager->executeAction(action); connect(m_pendingActionInfo, &DeviceActionInfo::finished, this, [this](){ @@ -94,6 +127,32 @@ void ScriptState::setValue(const QVariant &value) }); } +QVariant ScriptState::minimumValue() const +{ + Device *device = m_deviceManager->configuredDevices().findById(DeviceId(m_deviceId)); + if (!device) { + return QVariant(); + } + StateType stateType = device->deviceClass().stateTypes().findById(StateTypeId(m_stateTypeId)); + if (stateType.id().isNull()) { + stateType = device->deviceClass().stateTypes().findByName(m_stateName); + } + return stateType.minValue(); +} + +QVariant ScriptState::maximumValue() const +{ + Device *device = m_deviceManager->configuredDevices().findById(DeviceId(m_deviceId)); + if (!device) { + return QVariant(); + } + StateType stateType = device->deviceClass().stateTypes().findById(StateTypeId(m_stateTypeId)); + if (stateType.id().isNull()) { + stateType = device->deviceClass().stateTypes().findByName(m_stateName); + } + return stateType.minValue(); +} + void ScriptState::store() { m_valueStore = value(); @@ -108,7 +167,17 @@ void ScriptState::restore() void nymeaserver::ScriptState::onDeviceStateChanged(Device *device, const StateTypeId &stateTypeId) { - if (device->id() == DeviceId(m_deviceId) && stateTypeId == StateTypeId(m_stateTypeId)) { + if (device->id() != DeviceId(m_deviceId)) { + return; + } + StateTypeId localStateTypeId = StateTypeId(m_stateTypeId); + if (localStateTypeId.isNull()) { + localStateTypeId = device->deviceClass().stateTypes().findByName(m_stateName).id(); + } + if (localStateTypeId.isNull()) { + return; + } + if (stateTypeId == localStateTypeId) { emit valueChanged(); } } diff --git a/libnymea-core/scriptengine/scriptstate.h b/libnymea-core/scriptengine/scriptstate.h index 30bd28a6..9ba75d9d 100644 --- a/libnymea-core/scriptengine/scriptstate.h +++ b/libnymea-core/scriptengine/scriptstate.h @@ -15,9 +15,11 @@ class ScriptState : public QObject, public QQmlParserStatus Q_OBJECT Q_INTERFACES(QQmlParserStatus) Q_PROPERTY(QString deviceId READ deviceId WRITE setDeviceId NOTIFY deviceIdChanged) - Q_PROPERTY(QString stateTypeId READ stateTypeId WRITE setStateTypeId NOTIFY stateTypeIdChanged) - Q_PROPERTY(QString stateName READ stateName WRITE setStateName NOTIFY stateNameChanged) + Q_PROPERTY(QString stateTypeId READ stateTypeId WRITE setStateTypeId NOTIFY stateTypeChanged) + Q_PROPERTY(QString stateName READ stateName WRITE setStateName NOTIFY stateTypeChanged) Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged) + Q_PROPERTY(QVariant minimumValue READ minimumValue NOTIFY stateTypeChanged) + Q_PROPERTY(QVariant maximumValue READ maximumValue NOTIFY stateTypeChanged) public: explicit ScriptState(QObject *parent = nullptr); @@ -30,17 +32,22 @@ public: QString stateTypeId() const; void setStateTypeId(const QString &stateTypeId); + QString stateName() const; + void setStateName(const QString &stateName); + QVariant value() const; void setValue(const QVariant &value); + QVariant minimumValue() const; + QVariant maximumValue() const; + public slots: void store(); void restore(); signals: void deviceIdChanged(); - void stateTypeIdChanged(); - void stateNameChanged(); + void stateTypeChanged(); void valueChanged(); private slots: @@ -51,6 +58,7 @@ private: QString m_deviceId; QString m_stateTypeId; + QString m_stateName; DeviceActionInfo *m_pendingActionInfo = nullptr; QVariant m_valueCache;