From 4a0541bf1813ced61b8da8420ba66d22dde153d1 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Wed, 24 Feb 2021 16:21:16 +0100 Subject: [PATCH] fixed inverter thing setup --- sma/integrationpluginsma.cpp | 43 ++++++++++++++++++++++++++--------- sma/integrationpluginsma.h | 3 ++- sma/integrationpluginsma.json | 3 +-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/sma/integrationpluginsma.cpp b/sma/integrationpluginsma.cpp index 1637431b..952951cf 100644 --- a/sma/integrationpluginsma.cpp +++ b/sma/integrationpluginsma.cpp @@ -102,6 +102,7 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info) connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, info, [sunnyWebBox, info, this] { qCDebug(dcSma()) << "Received plant overview, finishing setup"; info->finish(Thing::ThingErrorNoError); + connect(sunnyWebBox, &SunnyWebBox::connectedChanged, this, &IntegrationPluginSma::onConnectedChanged); connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, this, &IntegrationPluginSma::onPlantOverviewReceived); connect(sunnyWebBox, &SunnyWebBox::devicesReceived, this, &IntegrationPluginSma::onDevicesReceived); connect(sunnyWebBox, &SunnyWebBox::processDataReceived, this, &IntegrationPluginSma::onProcessDataReceived); @@ -115,16 +116,17 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info) qCWarning(dcSma()) << "Could not find parentThing for thing " << thing->name(); return info->finish(Thing::ThingErrorHardwareNotAvailable, "Please try again"); } - if (!parentThing->setupComplete()) { + if (parentThing->setupComplete()) { + info->finish(Thing::ThingErrorNoError); + } else { //wait for the parent to finish the setup process connect(parentThing, &Thing::setupStatusChanged, info, [this, info, parentThing] { if (parentThing->setupComplete()) - setupChild(info, parentThing); + info->finish(Thing::ThingErrorNoError); }); return; } - setupChild(info, parentThing); } else { Q_ASSERT_X(false, "setupThing", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8()); } @@ -140,7 +142,11 @@ void IntegrationPluginSma::postSetupThing(Thing *thing) sunnyWebBox->getDevices(); thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true); } else if (thing->thingClassId() == inverterThingClassId) { - + SunnyWebBox *sunnyWebBox = m_sunnyWebBoxes.value(myThings().findById(thing->parentId())); + if (!sunnyWebBox) + return; + QString key = thing->paramValue(inverterThingIdParamTypeId).toString(); + sunnyWebBox->getParameters(QStringList() << key); } } @@ -188,6 +194,14 @@ void IntegrationPluginSma::onRefreshTimer() } } +void IntegrationPluginSma::onConnectedChanged(bool connected) +{ + Thing *thing = m_sunnyWebBoxes.key(static_cast(sender())); + if (!thing) + return; + thing->setStateValue(sunnyWebBoxConnectedStateTypeId, connected); +} + void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, SunnyWebBox::Overview overview) { qCDebug(dcSma()) << "Plant overview received" << overview.status; @@ -200,7 +214,6 @@ void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, Sun if (!thing) return; - thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true); thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power); thing->setStateValue(sunnyWebBoxDayEnergyStateTypeId, overview.dailyYield); thing->setStateValue(sunnyWebBoxTotalEnergyStateTypeId, overview.totalYield); @@ -226,6 +239,8 @@ void IntegrationPluginSma::onDevicesReceived(const QString &messageId, QListid()); ParamList params; params << Param(inverterThingIdParamTypeId, device.key); @@ -235,6 +250,18 @@ void IntegrationPluginSma::onDevicesReceived(const QString &messageId, QList ¶meters) +{ + Q_UNUSED(messageId); + Thing *thing = myThings().findByParams(ParamList() << Param(inverterThingIdParamTypeId, deviceKey)); + if (!thing) + return; + qCDebug(dcSma()) << "Parameters received"; + Q_FOREACH(SunnyWebBox::Parameter parameter, parameters) { + qCDebug(dcSma()) << " - " << parameter.name << parameter.value << parameter.unit; + } +} + void IntegrationPluginSma::onProcessDataReceived(const QString &messageId, const QString &deviceKey, const QHash &channels) { Q_UNUSED(messageId) @@ -265,12 +292,6 @@ void IntegrationPluginSma::onParameterChannelsReceived(const QString &messageId, qCDebug(dcSma()) << "Parameter channels received" << deviceKey << parameterChannels; } -void IntegrationPluginSma::setupChild(ThingSetupInfo *info, Thing *parentThing) -{ - Q_UNUSED(info) - Q_UNUSED(parentThing) -} - void IntegrationPluginSma::getData(Thing *thing) { Q_UNUSED(thing) diff --git a/sma/integrationpluginsma.h b/sma/integrationpluginsma.h index 4f3b30cd..8e572c60 100644 --- a/sma/integrationpluginsma.h +++ b/sma/integrationpluginsma.h @@ -59,8 +59,10 @@ public: private slots: void onRefreshTimer(); + void onConnectedChanged(bool connected); void onPlantOverviewReceived(const QString &messageId, SunnyWebBox::Overview overview); void onDevicesReceived(const QString &messageId, QList devices); + void onParametersReceived(const QString &messageId, const QString &deviceKey, const QList ¶meters); void onProcessDataReceived(const QString &messageId, const QString &deviceKey, const QHash &channels); void onParameterChannelsReceived(const QString &messageId, const QString &deviceKey, QStringList parameterChannels); @@ -71,7 +73,6 @@ private: QHash m_asyncActions; SunnyWebBoxCommunication *m_sunnyWebBoxCommunication = nullptr; - void setupChild(ThingSetupInfo *info, Thing *parentThing); void getData(Thing *thing); }; diff --git a/sma/integrationpluginsma.json b/sma/integrationpluginsma.json index 6b9a9920..bb40003a 100644 --- a/sma/integrationpluginsma.json +++ b/sma/integrationpluginsma.json @@ -105,8 +105,7 @@ "name": "id", "displayName": "Device ID", "type": "QString", - "inputType": "TextLine", - "readOnly": true + "readOnly": true } ], "stateTypes": [