Update Sunny WebBox power and discovery

This commit is contained in:
Simon Stürz 2022-12-07 17:31:43 +01:00
parent 0e7390555d
commit cf748ba347
3 changed files with 24 additions and 13 deletions

View File

@ -63,7 +63,7 @@ void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info)
webBoxDiscovery->deleteLater(); webBoxDiscovery->deleteLater();
ThingDescriptors descriptors; ThingDescriptors descriptors;
foreach (const NetworkDeviceInfo &networkDeviceInfo, webBoxDiscovery->discoveryResults()) { foreach (const NetworkDeviceInfo &networkDeviceInfo, webBoxDiscovery->discoveryResults()) {
QString title = networkDeviceInfo.hostName() + " (" + networkDeviceInfo.address().toString() + ")"; QString title = "SMA Sunny WebBox (" + networkDeviceInfo.address().toString() + ")";
QString description; QString description;
if (networkDeviceInfo.macAddressManufacturer().isEmpty()) { if (networkDeviceInfo.macAddressManufacturer().isEmpty()) {
description = networkDeviceInfo.macAddress(); description = networkDeviceInfo.macAddress();
@ -290,13 +290,12 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
QString requestId = sunnyWebBox->getPlantOverview(); QString requestId = sunnyWebBox->getPlantOverview();
connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, info, [=] (const QString &messageId, SunnyWebBox::Overview overview) { connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, info, [=] (const QString &messageId, SunnyWebBox::Overview overview) {
qCDebug(dcSma()) << "Received plant overview" << messageId << "Finish setup"; qCDebug(dcSma()) << "Received plant overview" << messageId << "finish setup";
Q_UNUSED(overview)
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
connect(sunnyWebBox, &SunnyWebBox::connectedChanged, this, &IntegrationPluginSma::onConnectedChanged); connect(sunnyWebBox, &SunnyWebBox::connectedChanged, this, &IntegrationPluginSma::onConnectedChanged);
connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, this, &IntegrationPluginSma::onPlantOverviewReceived); connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, this, &IntegrationPluginSma::onPlantOverviewReceived);
m_sunnyWebBoxes.insert(info->thing(), sunnyWebBox); m_sunnyWebBoxes.insert(info->thing(), sunnyWebBox);
onPlantOverviewReceived(messageId, overview);
}); });
} else if (thing->thingClassId() == speedwireMeterThingClassId) { } else if (thing->thingClassId() == speedwireMeterThingClassId) {
@ -569,7 +568,7 @@ void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, Sun
if (!thing) if (!thing)
return; return;
thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power); thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, -overview.power);
thing->setStateValue(sunnyWebBoxDayEnergyProducedStateTypeId, overview.dailyYield); thing->setStateValue(sunnyWebBoxDayEnergyProducedStateTypeId, overview.dailyYield);
thing->setStateValue(sunnyWebBoxTotalEnergyProducedStateTypeId, overview.totalYield); thing->setStateValue(sunnyWebBoxTotalEnergyProducedStateTypeId, overview.totalYield);
thing->setStateValue(sunnyWebBoxModeStateTypeId, overview.status); thing->setStateValue(sunnyWebBoxModeStateTypeId, overview.status);
@ -587,9 +586,9 @@ void IntegrationPluginSma::setupRefreshTimer()
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(5); m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(5);
connect(m_refreshTimer, &PluginTimer::timeout, this, [=](){ connect(m_refreshTimer, &PluginTimer::timeout, this, [=](){
foreach (Thing *thing, myThings().filterByThingClassId(sunnyWebBoxThingClassId)) { foreach (SunnyWebBox *webbox, m_sunnyWebBoxes) {
SunnyWebBox *sunnyWebBox = m_sunnyWebBoxes.value(thing); // Max refresh rate according to docs should be 30 seconds, will be handled in the webbox class
sunnyWebBox->getPlantOverview(); webbox->getPlantOverview();
} }
foreach (SpeedwireInverter *inverter, m_speedwireInverters) { foreach (SpeedwireInverter *inverter, m_speedwireInverters) {

View File

@ -37,8 +37,8 @@
SunnyWebBox::SunnyWebBox(NetworkAccessManager *networkAccessManager, const QHostAddress &hostAddress, QObject *parrent) : SunnyWebBox::SunnyWebBox(NetworkAccessManager *networkAccessManager, const QHostAddress &hostAddress, QObject *parrent) :
QObject(parrent), QObject(parrent),
m_hostAddresss(hostAddress), m_networkManager(networkAccessManager),
m_networkManager(networkAccessManager) m_hostAddresss(hostAddress)
{ {
qCDebug(dcSma()) << "SunnyWebBox: Creating Sunny Web Box connection"; qCDebug(dcSma()) << "SunnyWebBox: Creating Sunny Web Box connection";
} }
@ -50,6 +50,16 @@ SunnyWebBox::~SunnyWebBox()
QString SunnyWebBox::getPlantOverview() 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"); return sendMessage(m_hostAddresss, "GetPlantOverview");
} }
@ -163,10 +173,10 @@ QNetworkReply *SunnyWebBox::sendRequest(const QHostAddress &address, const QStri
doc.setObject(obj); doc.setObject(obj);
QUrl url; QUrl url;
url.setScheme("http");
url.setHost(address.toString()); url.setHost(address.toString());
url.setPath("/rpc"); url.setPath("/rpc");
url.setPort(80); url.setPort(80);
url.setScheme("http");
QNetworkRequest request(url); QNetworkRequest request(url);
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact); QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact);
@ -309,6 +319,7 @@ QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &pro
setConnectionStatus(false); setConnectionStatus(false);
return; return;
} }
setConnectionStatus(true); setConnectionStatus(true);
QByteArray data = reply->readAll(); QByteArray data = reply->readAll();

View File

@ -38,11 +38,11 @@
#include <QJsonObject> #include <QJsonObject>
#include <QHostAddress> #include <QHostAddress>
#include <QUdpSocket> #include <QUdpSocket>
#include <QDateTime>
class SunnyWebBox : public QObject class SunnyWebBox : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
struct Overview { struct Overview {
int power; int power;
@ -96,10 +96,11 @@ public:
static QString generateRequestId(); static QString generateRequestId();
private: private:
NetworkAccessManager *m_networkManager = nullptr;
bool m_connected = false; bool m_connected = false;
QHostAddress m_hostAddresss; QHostAddress m_hostAddresss;
QString m_macAddress; 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);
QString sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params); QString sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params);