From 877658dd2ca850243bc26efc5ef3c0563fac6b40 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Wed, 24 Feb 2021 15:18:57 +0100 Subject: [PATCH] added more debug output --- sma/integrationpluginsma.cpp | 6 ++++-- sma/sunnywebbox.cpp | 40 ++++++++++++++++++++++++++++-------- sma/sunnywebbox.h | 3 ++- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/sma/integrationpluginsma.cpp b/sma/integrationpluginsma.cpp index 184ab7d5..59b1a863 100644 --- a/sma/integrationpluginsma.cpp +++ b/sma/integrationpluginsma.cpp @@ -139,6 +139,7 @@ void IntegrationPluginSma::postSetupThing(Thing *thing) if (!sunnyWebBox) return; sunnyWebBox->getDevices(); + thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true); } else if (thing->thingClassId() == inverterThingClassId) { } @@ -190,6 +191,7 @@ void IntegrationPluginSma::onRefreshTimer() void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, SunnyWebBox::Overview overview) { + qCDebug(dcSma()) << "Plant overview received" << overview.status; if (m_asyncSetup.contains(messageId)) { ThingSetupInfo *info = m_asyncSetup.value(messageId); info->finish(Thing::ThingErrorNoError); @@ -199,7 +201,6 @@ void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, Sun if (!thing) return; - qCDebug(dcSma()) << "Plant overview received" << overview.status; thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true); thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power); thing->setStateValue(sunnyWebBoxDayEnergyStateTypeId, overview.dailyYield); @@ -213,6 +214,7 @@ void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, Sun void IntegrationPluginSma::onDevicesReceived(const QString &messageId, QList devices) { + qCDebug(dcSma()) << "Devices received, count:" << devices.count(); if (m_asyncActions.contains(messageId)) { ThingActionInfo *info = m_asyncActions.value(messageId); info->finish(Thing::ThingErrorNoError); @@ -224,7 +226,7 @@ void IntegrationPluginSma::onDevicesReceived(const QString &messageId, QListid()); descriptors.append(descriptor); } diff --git a/sma/sunnywebbox.cpp b/sma/sunnywebbox.cpp index bc9968b6..7d120684 100644 --- a/sma/sunnywebbox.cpp +++ b/sma/sunnywebbox.cpp @@ -41,7 +41,6 @@ SunnyWebBox::SunnyWebBox(NetworkAccessManager *networkAccessManager, const QHost m_networkManager(networkAccessManager) { qCDebug(dcSma()) << "SunnyWebBox: Creating Sunny Web Box connection"; - //TODO connect communication with socket state; } SunnyWebBox::~SunnyWebBox() @@ -130,18 +129,24 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT if (messageType == "GetPlantOverview") { Overview overview; QVariantList overviewList = result.value("overview").toList(); + qCDebug(dcSma()) << "SunnyWebBox: GetPlantOverview"; Q_FOREACH(QVariant value, overviewList) { QVariantMap map = value.toMap(); if (map["meta"].toString() == "GriPwr") { overview.power = map["value"].toInt(); + qCDebug(dcSma()) << "SunnyWebBox: - Power" << overview.power; } else if (map["meta"].toString() == "GriEgyTdy") { overview.dailyYield = map["value"].toInt(); + qCDebug(dcSma()) << "SunnyWebBox: - Daily yield" << overview.dailyYield; } else if (map["meta"].toString() == "GriEgyTot") { overview.totalYield = map["value"].toInt(); + qCDebug(dcSma()) << "SunnyWebBox: - Total yield" << overview.totalYield; } else if (map["meta"].toString() == "OpStt") { overview.status = map["value"].toString(); + qCDebug(dcSma()) << "SunnyWebBox: - Status" << overview.status; } else if (map["meta"].toString() == "Msg") { overview.error = map["value"].toString(); + qCDebug(dcSma()) << "SunnyWebBox: - Error" << overview.error; } } emit plantOverviewReceived(messageId, overview); @@ -149,12 +154,14 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT } else if (messageType == "GetDevices") { QList devices; QVariantList deviceList = result.value("devices").toList(); + qCDebug(dcSma()) << "SunnyWebBox: GetDevices"; Q_FOREACH(QVariant value, deviceList) { Device device; QVariantMap map = value.toMap(); device.name = map["name"].toString(); + qCDebug(dcSma()) << "SunnyWebBox: - Name" << device.name; device.key = map["key"].toString(); - + qCDebug(dcSma()) << "SunnyWebBox: - Key" << device.key; QVariantList childrenList = map["children"].toList(); Q_FOREACH(QVariant childValue, childrenList) { Device child; @@ -214,7 +221,16 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT emit parametersReceived(messageId, key, parameters); } } else { - qCWarning(dcSma()) << "Unknown message type" << messageType; + qCWarning(dcSma()) << "SunnyWebBox: Unknown message type" << messageType; + } +} + +void SunnyWebBox::setConnectionStatus(bool connected) +{ + if (m_connected != connected) { + qCDebug(dcSma()) << "SunnyWebBox: Connection status changed" << connected; + m_connected = connected; + emit connectedChanged(m_connected); } } @@ -225,6 +241,7 @@ QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &pro QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms) { + qCDebug(dcSma()) << "SunnyWebBox: Send message to" << address.toString() << "Procedure:" << procedure << "Params:" << params; QString requestId = QUuid::createUuid().toString().remove('{').remove('-').left(14); QJsonDocument doc; @@ -252,23 +269,28 @@ QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &pro connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, this, [this, address, requestId, reply]{ - //int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if (reply->error() != QNetworkReply::NoError) { + setConnectionStatus(false); + return; + } + setConnectionStatus(true); + QByteArray data = reply->readAll(); - qCDebug(dcSma()) << "Received reply" << data; + qCDebug(dcSma()) << "SunnyWebBox: Received reply" << data; QJsonParseError error; QJsonDocument doc = QJsonDocument::fromJson(data, &error); if (error.error != QJsonParseError::NoError) { - qCWarning(dcSma()) << "Could not parse JSON" << error.errorString(); + qCWarning(dcSma()) << "SunnyWebBox: Could not parse JSON" << error.errorString(); return; } if (!doc.isObject()) { - qCWarning(dcSma()) << "JSON is not an Object"; + qCWarning(dcSma()) << "SunnyWebBox: JSON is not an Object"; return; } QVariantMap map = doc.toVariant().toMap(); if (map["version"] != "1.0") { - qCWarning(dcSma()) << "API version not supported" << map["version"]; + qCWarning(dcSma()) << "SunnyWebBox: API version not supported" << map["version"]; return; } @@ -278,7 +300,7 @@ QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &pro QVariantMap result = map.value("result").toMap(); parseMessage(requestId, requestType, result); } else { - qCWarning(dcSma()) << "Missing proc or result value"; + qCWarning(dcSma()) << "SunnyWebBox: Missing proc or result value"; } }); return requestId; diff --git a/sma/sunnywebbox.h b/sma/sunnywebbox.h index 51d0076b..8928210e 100644 --- a/sma/sunnywebbox.h +++ b/sma/sunnywebbox.h @@ -88,7 +88,7 @@ public: QHostAddress hostAddress(); private: - + bool m_connected = false; QHostAddress m_hostAddresss; NetworkAccessManager *m_networkManager = nullptr; @@ -96,6 +96,7 @@ private: QString sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms); void parseMessage(const QString &messageId, const QString &messageType, const QVariantMap &result); + void setConnectionStatus(bool connected); signals: void connectedChanged(bool connected);