From 9e3cf02e3ceae894c1c97cf7a622e3be226c2ac7 Mon Sep 17 00:00:00 2001 From: "bernhard.trinnes" Date: Tue, 18 Aug 2020 12:28:23 +0200 Subject: [PATCH] added async setup --- sma/integrationpluginsma.cpp | 29 ++++++++++++++++++++++------- sma/integrationpluginsma.h | 3 ++- sma/sunnywebboxcommunication.cpp | 4 ++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/sma/integrationpluginsma.cpp b/sma/integrationpluginsma.cpp index 01eb5bac..0e880ff6 100644 --- a/sma/integrationpluginsma.cpp +++ b/sma/integrationpluginsma.cpp @@ -48,7 +48,7 @@ void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info) connect(discovery, &Discovery::finished, info, [this, info](const QList &hosts) { qCDebug(dcSma()) << "Discovery finished. Found" << hosts.count() << "devices"; foreach (const Host &host, hosts) { - if (host.hostName().contains("SMA Regelsysteme Gmbh")){ + if (host.hostName().contains("SMA")){ ThingDescriptor descriptor(info->thingClassId(), host.hostName(), host.address() + " (" + host.macAddress() + ")"); foreach (Thing *existingThing, myThings()) { @@ -67,7 +67,6 @@ void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info) } info->finish(Thing::ThingErrorNoError); }); - info->finish(Thing::ThingErrorNoError); } } @@ -95,8 +94,14 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info) } } SunnyWebBox *sunnyWebBox = new SunnyWebBox(m_sunnyWebBoxCommunication, QHostAddress(thing->paramValue(sunnyWebBoxThingHostParamTypeId).toString()), this); + connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, this, &IntegrationPluginSma::onPlantOverviewReceived); + connect(sunnyWebBox, &SunnyWebBox::devicesReceived, this, &IntegrationPluginSma::onDevicesReceived); + connect(sunnyWebBox, &SunnyWebBox::processDataReceived, this, &IntegrationPluginSma::onProcessDataReceived); + connect(sunnyWebBox, &SunnyWebBox::parameterChannelsReceived, this, &IntegrationPluginSma::onParameterChannelsReceived); m_sunnyWebBoxes.insert(thing, sunnyWebBox); - //TODO m_asyncSetup + connect(info, &ThingSetupInfo::aborted, this, [thing, this] { m_sunnyWebBoxes.remove(thing);}); + int requestId = sunnyWebBox->getPlantOverview(); + m_asyncSetup.insert(requestId, info); return info->finish(Thing::ThingErrorNoError); } else if (thing->thingClassId() == inverterThingClassId) { @@ -142,9 +147,11 @@ void IntegrationPluginSma::executeAction(ThingActionInfo *info) if (!sunnyWebBox) return; if (action.actionTypeId() == sunnyWebBoxSearchDevicesActionTypeId) { - sunnyWebBox->getDevices(); + int requestId = sunnyWebBox->getDevices(); + m_asyncActions.insert(requestId, info); + connect(info, &ThingActionInfo::aborted, info, [requestId, this] {m_asyncActions.remove(requestId);}); } else { - //Unhandled actionTypeId + Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8()); } } else { Q_ASSERT_X(false, "executeAction", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8()); @@ -172,12 +179,16 @@ void IntegrationPluginSma::onRefreshTimer() void IntegrationPluginSma::onPlantOverviewReceived(int messageId, SunnyWebBox::Overview overview) { - Q_UNUSED(messageId) + if (m_asyncSetup.contains(messageId)) { + ThingSetupInfo *info = m_asyncSetup.value(messageId); + info->finish(Thing::ThingErrorNoError); + } Thing *thing = m_sunnyWebBoxes.key(static_cast(sender())); if (!thing) return; + qCDebug(dcSma()) << "Plant overview received" << overview.status; thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true); thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power); thing->setStateValue(sunnyWebBoxDayEnergyStateTypeId, overview.dailyYield); @@ -191,7 +202,11 @@ void IntegrationPluginSma::onPlantOverviewReceived(int messageId, SunnyWebBox::O void IntegrationPluginSma::onDevicesReceived(int messageId, QList devices) { - Q_UNUSED(messageId) + if (m_asyncActions.contains(messageId)) { + ThingActionInfo *info = m_asyncActions.value(messageId); + info->finish(Thing::ThingErrorNoError); + } + Thing *thing = m_sunnyWebBoxes.key(static_cast(sender())); if (!thing) diff --git a/sma/integrationpluginsma.h b/sma/integrationpluginsma.h index 523ae9e6..7bcf1695 100644 --- a/sma/integrationpluginsma.h +++ b/sma/integrationpluginsma.h @@ -67,7 +67,8 @@ private slots: private: PluginTimer *m_refreshTimer = nullptr; QHash m_sunnyWebBoxes; - QHash m_asyncSetup; + QHash m_asyncSetup; + QHash m_asyncActions; SunnyWebBoxCommunication *m_sunnyWebBoxCommunication = nullptr; SunnyWebBox *createSunnyWebBoxConnection(Thing *thing); diff --git a/sma/sunnywebboxcommunication.cpp b/sma/sunnywebboxcommunication.cpp index 8edce319..b3ae8449 100644 --- a/sma/sunnywebboxcommunication.cpp +++ b/sma/sunnywebboxcommunication.cpp @@ -55,6 +55,7 @@ SunnyWebBoxCommunication::SunnyWebBoxCommunication(QObject *parent) : QObject(pa qCWarning(dcSma()) << "Error reading pending datagram"; } } + datagramReceived(address, data); }); } @@ -68,6 +69,7 @@ int SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QSt obj["proc"] = procedure; obj["id"] = requestId; obj["format"] = "JSON"; + qCDebug(dcSma()) << "Send message" << doc.toJson() << address << m_port; m_udpSocket->writeDatagram(doc.toJson(), address, m_port); return requestId; } @@ -85,12 +87,14 @@ int SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QSt if (!params.isEmpty()) { obj.insert("params", params); } + qCDebug(dcSma()) << "Send message" << doc.toJson() << address << m_port; m_udpSocket->writeDatagram(doc.toJson(), address, m_port); return requestId; } void SunnyWebBoxCommunication::datagramReceived(const QHostAddress &address, const QByteArray &data) { + qCDebug(dcSma()) << "Datagram received" << data; QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(data, &error); if (error.error != QJsonParseError::NoError) {