From 944c0b8b5a6247bdab17ea0fe27a0c2ce0b6376d Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 30 Mar 2020 22:06:52 +0200 Subject: [PATCH] Properly initialize ScriptState values --- .../thingmanagerimplementation.cpp | 2 + libnymea-core/scriptengine/scriptstate.cpp | 61 +++++++++++++++++-- libnymea-core/scriptengine/scriptstate.h | 3 + 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/libnymea-core/integrations/thingmanagerimplementation.cpp b/libnymea-core/integrations/thingmanagerimplementation.cpp index 6fd0609a..1eb95662 100644 --- a/libnymea-core/integrations/thingmanagerimplementation.cpp +++ b/libnymea-core/integrations/thingmanagerimplementation.cpp @@ -1375,6 +1375,8 @@ void ThingManagerImplementation::loadConfiguredThings() // it means that it was working at some point so lets still add it as there might // be rules associated with this thing. m_configuredThings.insert(thing->id(), thing); + + emit thingAdded(thing); } settings.endGroup(); diff --git a/libnymea-core/scriptengine/scriptstate.cpp b/libnymea-core/scriptengine/scriptstate.cpp index 1fea731c..da14e223 100644 --- a/libnymea-core/scriptengine/scriptstate.cpp +++ b/libnymea-core/scriptengine/scriptstate.cpp @@ -47,6 +47,13 @@ void ScriptState::classBegin() { m_thingManager = reinterpret_cast(qmlEngine(this)->property("thingManager").toULongLong()); connect(m_thingManager, &ThingManager::thingStateChanged, this, &ScriptState::onThingStateChanged); + + connect(m_thingManager, &ThingManager::thingAdded, this, [this](Thing *newThing){ + if (newThing->id() == ThingId(m_thingId)) { + qCDebug(dcScriptEngine()) << "Thing" << newThing->name() << "appeared in system"; + connectToThing(); + } + }); } void ScriptState::componentComplete() @@ -65,6 +72,11 @@ void ScriptState::setThingId(const QString &thingId) m_thingId = thingId; emit thingIdChanged(); store(); + if (!m_valueCache.isNull()) { + setValue(m_valueCache); + } + + connectToThing(); } } @@ -79,6 +91,9 @@ void ScriptState::setStateTypeId(const QString &stateTypeId) m_stateTypeId = stateTypeId; emit stateTypeChanged(); store(); + if (!m_valueCache.isNull()) { + setValue(m_valueCache); + } } } @@ -93,6 +108,9 @@ void ScriptState::setStateName(const QString &stateName) m_stateName = stateName; emit stateTypeChanged(); store(); + if (!m_valueCache.isNull()) { + setValue(m_valueCache); + } } } @@ -119,7 +137,14 @@ void ScriptState::setValue(const QVariant &value) Thing* thing = m_thingManager->findConfiguredThing(ThingId(m_thingId)); if (!thing) { - qCWarning(dcScriptEngine()) << "No thing with id" << m_thingId << "found."; + m_valueCache = value; + qCDebug(dcScriptEngine()) << "No thing with id" << m_thingId << "found."; + return; + } + + if (thing->setupStatus() != Thing::ThingSetupStatusComplete) { + m_valueCache = value; + qCDebug(dcScriptEngine()) << "Thing is not ready yet..."; return; } @@ -127,18 +152,19 @@ void ScriptState::setValue(const QVariant &value) if (!m_stateTypeId.isNull()) { actionTypeId = thing->thingClass().stateTypes().findById(StateTypeId(m_stateTypeId)).id(); if (actionTypeId.isNull()) { - qCWarning(dcScriptEngine) << "Thing" << thing->name() << "does not have a state with type id" << m_stateTypeId; + qCDebug(dcScriptEngine) << "Thing" << thing->name() << "does not have a state with type id" << m_stateTypeId; } } if (actionTypeId.isNull()) { actionTypeId = thing->thingClass().stateTypes().findByName(stateName()).id(); if (actionTypeId.isNull()) { - qCWarning(dcScriptEngine) << "Thing" << thing->name() << "does not have a state named" << m_stateName; + qCDebug(dcScriptEngine) << "Thing" << thing->name() << "does not have a state named" << m_stateName; } } if (actionTypeId.isNull()) { - qCWarning(dcScriptEngine()) << "Either stateTypeId or stateName is required to be valid."; + m_valueCache = value; + qCDebug(dcScriptEngine()) << "Either stateTypeId or stateName is required to be valid."; return; } @@ -148,6 +174,7 @@ void ScriptState::setValue(const QVariant &value) ParamList params = ParamList() << Param(ParamTypeId(actionTypeId), value); action.setParams(params); + qCDebug(dcScriptEngine()) << "Executing action on" << thing->name(); m_valueCache = QVariant(); m_pendingActionInfo = m_thingManager->executeAction(action); connect(m_pendingActionInfo, &ThingActionInfo::finished, this, [this](){ @@ -211,4 +238,30 @@ void ScriptState::onThingStateChanged(Thing *thing, const StateTypeId &stateType } } +void ScriptState::connectToThing() +{ + Thing *thing = m_thingManager->findConfiguredThing(ThingId(m_thingId)); + if (!thing) { + qCDebug(dcScriptEngine()) << "Can't find thing with id" << m_thingId << "(yet)"; + return; + } + + if (thing->setupStatus() == Thing::ThingSetupStatusComplete) { + if (!m_valueCache.isNull()) { + setValue(m_valueCache); + } + } else { + qCDebug(dcScriptEngine()) << "Thing setup for" << thing->name() << "not complete yet"; + } + + connect(thing, &Thing::setupStatusChanged, this, [this, thing](){ + if (thing->setupStatus() == Thing::ThingSetupStatusComplete) { + qCDebug(dcScriptEngine()) << "Thing setup for" << thing->name() << "completed"; + if (!m_valueCache.isNull()) { + setValue(m_valueCache); + } + } + }); +} + } diff --git a/libnymea-core/scriptengine/scriptstate.h b/libnymea-core/scriptengine/scriptstate.h index dd5bc2f8..2913504a 100644 --- a/libnymea-core/scriptengine/scriptstate.h +++ b/libnymea-core/scriptengine/scriptstate.h @@ -84,6 +84,8 @@ signals: private slots: void onThingStateChanged(Thing *thing, const StateTypeId &stateTypeId); + void connectToThing(); + private: ThingManager *m_thingManager = nullptr; @@ -91,6 +93,7 @@ private: QString m_stateTypeId; QString m_stateName; + ThingActionInfo *m_pendingActionInfo = nullptr; QVariant m_valueCache;