tune script types a bit

This commit is contained in:
Michael Zanetti 2019-11-18 21:50:58 +01:00
parent d4081195d4
commit 6ab6f8a80b
6 changed files with 128 additions and 14 deletions

View File

@ -5,6 +5,8 @@
#include <QQmlEngine>
#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 &params)
{
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) {

View File

@ -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 &params);
signals:
void deviceIdChanged();
void actionTypeIdChanged();
void actionNameChanged();
public:
DeviceManager *m_deviceManager = nullptr;
QString m_deviceId;
QString m_actionTypeId;
QString m_actionName;
};
}

View File

@ -7,6 +7,8 @@
#include <QQmlApplicationEngine>
#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<quint64>(m_deviceManager));
qCWarning(dcScriptEngine()) << "Loading script";
engine->load(fileName);
}

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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;