diff --git a/sma/integrationpluginsma.cpp b/sma/integrationpluginsma.cpp index c286a5d..d504725 100644 --- a/sma/integrationpluginsma.cpp +++ b/sma/integrationpluginsma.cpp @@ -63,7 +63,7 @@ void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info) webBoxDiscovery->deleteLater(); ThingDescriptors descriptors; foreach (const NetworkDeviceInfo &networkDeviceInfo, webBoxDiscovery->discoveryResults()) { - QString title = networkDeviceInfo.hostName() + " (" + networkDeviceInfo.address().toString() + ")"; + QString title = "SMA Sunny WebBox (" + networkDeviceInfo.address().toString() + ")"; QString description; if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { description = networkDeviceInfo.macAddress(); @@ -290,13 +290,12 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info) QString requestId = sunnyWebBox->getPlantOverview(); connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, info, [=] (const QString &messageId, SunnyWebBox::Overview overview) { - qCDebug(dcSma()) << "Received plant overview" << messageId << "Finish setup"; - Q_UNUSED(overview) - + qCDebug(dcSma()) << "Received plant overview" << messageId << "finish setup"; info->finish(Thing::ThingErrorNoError); connect(sunnyWebBox, &SunnyWebBox::connectedChanged, this, &IntegrationPluginSma::onConnectedChanged); connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, this, &IntegrationPluginSma::onPlantOverviewReceived); m_sunnyWebBoxes.insert(info->thing(), sunnyWebBox); + onPlantOverviewReceived(messageId, overview); }); } else if (thing->thingClassId() == speedwireMeterThingClassId) { @@ -569,7 +568,7 @@ void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, Sun if (!thing) return; - thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power); + thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, -overview.power); thing->setStateValue(sunnyWebBoxDayEnergyProducedStateTypeId, overview.dailyYield); thing->setStateValue(sunnyWebBoxTotalEnergyProducedStateTypeId, overview.totalYield); thing->setStateValue(sunnyWebBoxModeStateTypeId, overview.status); @@ -587,9 +586,9 @@ void IntegrationPluginSma::setupRefreshTimer() m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(5); connect(m_refreshTimer, &PluginTimer::timeout, this, [=](){ - foreach (Thing *thing, myThings().filterByThingClassId(sunnyWebBoxThingClassId)) { - SunnyWebBox *sunnyWebBox = m_sunnyWebBoxes.value(thing); - sunnyWebBox->getPlantOverview(); + foreach (SunnyWebBox *webbox, m_sunnyWebBoxes) { + // Max refresh rate according to docs should be 30 seconds, will be handled in the webbox class + webbox->getPlantOverview(); } foreach (SpeedwireInverter *inverter, m_speedwireInverters) { diff --git a/sma/sunnywebbox/sunnywebbox.cpp b/sma/sunnywebbox/sunnywebbox.cpp index d148ea2..b75f096 100644 --- a/sma/sunnywebbox/sunnywebbox.cpp +++ b/sma/sunnywebbox/sunnywebbox.cpp @@ -37,8 +37,8 @@ SunnyWebBox::SunnyWebBox(NetworkAccessManager *networkAccessManager, const QHostAddress &hostAddress, QObject *parrent) : QObject(parrent), - m_hostAddresss(hostAddress), - m_networkManager(networkAccessManager) + m_networkManager(networkAccessManager), + m_hostAddresss(hostAddress) { qCDebug(dcSma()) << "SunnyWebBox: Creating Sunny Web Box connection"; } @@ -50,6 +50,16 @@ SunnyWebBox::~SunnyWebBox() QString SunnyWebBox::getPlantOverview() { + QDateTime currentDateTime = QDateTime::currentDateTime(); + + if (!m_lastRequest.isNull()) { + // According to the documentation, the interval between 2 requests should not be less than 30 seconds. + if (QDateTime::currentDateTime().toMSecsSinceEpoch() - m_lastRequest.toMSecsSinceEpoch() < 30000) { + return QString(); + } + } + + m_lastRequest = currentDateTime; return sendMessage(m_hostAddresss, "GetPlantOverview"); } @@ -163,10 +173,10 @@ QNetworkReply *SunnyWebBox::sendRequest(const QHostAddress &address, const QStri doc.setObject(obj); QUrl url; + url.setScheme("http"); url.setHost(address.toString()); url.setPath("/rpc"); url.setPort(80); - url.setScheme("http"); QNetworkRequest request(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact); @@ -309,6 +319,7 @@ QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &pro setConnectionStatus(false); return; } + setConnectionStatus(true); QByteArray data = reply->readAll(); diff --git a/sma/sunnywebbox/sunnywebbox.h b/sma/sunnywebbox/sunnywebbox.h index 9981aeb..9446530 100644 --- a/sma/sunnywebbox/sunnywebbox.h +++ b/sma/sunnywebbox/sunnywebbox.h @@ -38,11 +38,11 @@ #include #include #include +#include class SunnyWebBox : public QObject { Q_OBJECT - public: struct Overview { int power; @@ -96,10 +96,11 @@ public: static QString generateRequestId(); private: + NetworkAccessManager *m_networkManager = nullptr; bool m_connected = false; QHostAddress m_hostAddresss; QString m_macAddress; - NetworkAccessManager *m_networkManager = nullptr; + QDateTime m_lastRequest; QString sendMessage(const QHostAddress &address, const QString &procedure); QString sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms);