added more debug output

This commit is contained in:
Boernsman 2021-02-24 15:18:57 +01:00 committed by Simon Stürz
parent cd122e89b0
commit 877658dd2c
3 changed files with 37 additions and 12 deletions

View File

@ -139,6 +139,7 @@ void IntegrationPluginSma::postSetupThing(Thing *thing)
if (!sunnyWebBox) if (!sunnyWebBox)
return; return;
sunnyWebBox->getDevices(); sunnyWebBox->getDevices();
thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true);
} else if (thing->thingClassId() == inverterThingClassId) { } else if (thing->thingClassId() == inverterThingClassId) {
} }
@ -190,6 +191,7 @@ void IntegrationPluginSma::onRefreshTimer()
void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, SunnyWebBox::Overview overview) void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, SunnyWebBox::Overview overview)
{ {
qCDebug(dcSma()) << "Plant overview received" << overview.status;
if (m_asyncSetup.contains(messageId)) { if (m_asyncSetup.contains(messageId)) {
ThingSetupInfo *info = m_asyncSetup.value(messageId); ThingSetupInfo *info = m_asyncSetup.value(messageId);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
@ -199,7 +201,6 @@ void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, Sun
if (!thing) if (!thing)
return; return;
qCDebug(dcSma()) << "Plant overview received" << overview.status;
thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true); thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true);
thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power); thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power);
thing->setStateValue(sunnyWebBoxDayEnergyStateTypeId, overview.dailyYield); thing->setStateValue(sunnyWebBoxDayEnergyStateTypeId, overview.dailyYield);
@ -213,6 +214,7 @@ void IntegrationPluginSma::onPlantOverviewReceived(const QString &messageId, Sun
void IntegrationPluginSma::onDevicesReceived(const QString &messageId, QList<SunnyWebBox::Device> devices) void IntegrationPluginSma::onDevicesReceived(const QString &messageId, QList<SunnyWebBox::Device> devices)
{ {
qCDebug(dcSma()) << "Devices received, count:" << devices.count();
if (m_asyncActions.contains(messageId)) { if (m_asyncActions.contains(messageId)) {
ThingActionInfo *info = m_asyncActions.value(messageId); ThingActionInfo *info = m_asyncActions.value(messageId);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
@ -224,7 +226,7 @@ void IntegrationPluginSma::onDevicesReceived(const QString &messageId, QList<Sun
ThingDescriptors descriptors; ThingDescriptors descriptors;
Q_FOREACH(SunnyWebBox::Device device, devices){ Q_FOREACH(SunnyWebBox::Device device, devices){
qCDebug(dcSma()) << "Device received" << device.name << device.key; qCDebug(dcSma()) << " - Device received" << device.name << device.key;
ThingDescriptor descriptor(inverterThingClassId, device.name, device.key ,thing->id()); ThingDescriptor descriptor(inverterThingClassId, device.name, device.key ,thing->id());
descriptors.append(descriptor); descriptors.append(descriptor);
} }

View File

@ -41,7 +41,6 @@ SunnyWebBox::SunnyWebBox(NetworkAccessManager *networkAccessManager, const QHost
m_networkManager(networkAccessManager) m_networkManager(networkAccessManager)
{ {
qCDebug(dcSma()) << "SunnyWebBox: Creating Sunny Web Box connection"; qCDebug(dcSma()) << "SunnyWebBox: Creating Sunny Web Box connection";
//TODO connect communication with socket state;
} }
SunnyWebBox::~SunnyWebBox() SunnyWebBox::~SunnyWebBox()
@ -130,18 +129,24 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT
if (messageType == "GetPlantOverview") { if (messageType == "GetPlantOverview") {
Overview overview; Overview overview;
QVariantList overviewList = result.value("overview").toList(); QVariantList overviewList = result.value("overview").toList();
qCDebug(dcSma()) << "SunnyWebBox: GetPlantOverview";
Q_FOREACH(QVariant value, overviewList) { Q_FOREACH(QVariant value, overviewList) {
QVariantMap map = value.toMap(); QVariantMap map = value.toMap();
if (map["meta"].toString() == "GriPwr") { if (map["meta"].toString() == "GriPwr") {
overview.power = map["value"].toInt(); overview.power = map["value"].toInt();
qCDebug(dcSma()) << "SunnyWebBox: - Power" << overview.power;
} else if (map["meta"].toString() == "GriEgyTdy") { } else if (map["meta"].toString() == "GriEgyTdy") {
overview.dailyYield = map["value"].toInt(); overview.dailyYield = map["value"].toInt();
qCDebug(dcSma()) << "SunnyWebBox: - Daily yield" << overview.dailyYield;
} else if (map["meta"].toString() == "GriEgyTot") { } else if (map["meta"].toString() == "GriEgyTot") {
overview.totalYield = map["value"].toInt(); overview.totalYield = map["value"].toInt();
qCDebug(dcSma()) << "SunnyWebBox: - Total yield" << overview.totalYield;
} else if (map["meta"].toString() == "OpStt") { } else if (map["meta"].toString() == "OpStt") {
overview.status = map["value"].toString(); overview.status = map["value"].toString();
qCDebug(dcSma()) << "SunnyWebBox: - Status" << overview.status;
} else if (map["meta"].toString() == "Msg") { } else if (map["meta"].toString() == "Msg") {
overview.error = map["value"].toString(); overview.error = map["value"].toString();
qCDebug(dcSma()) << "SunnyWebBox: - Error" << overview.error;
} }
} }
emit plantOverviewReceived(messageId, overview); emit plantOverviewReceived(messageId, overview);
@ -149,12 +154,14 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT
} else if (messageType == "GetDevices") { } else if (messageType == "GetDevices") {
QList<Device> devices; QList<Device> devices;
QVariantList deviceList = result.value("devices").toList(); QVariantList deviceList = result.value("devices").toList();
qCDebug(dcSma()) << "SunnyWebBox: GetDevices";
Q_FOREACH(QVariant value, deviceList) { Q_FOREACH(QVariant value, deviceList) {
Device device; Device device;
QVariantMap map = value.toMap(); QVariantMap map = value.toMap();
device.name = map["name"].toString(); device.name = map["name"].toString();
qCDebug(dcSma()) << "SunnyWebBox: - Name" << device.name;
device.key = map["key"].toString(); device.key = map["key"].toString();
qCDebug(dcSma()) << "SunnyWebBox: - Key" << device.key;
QVariantList childrenList = map["children"].toList(); QVariantList childrenList = map["children"].toList();
Q_FOREACH(QVariant childValue, childrenList) { Q_FOREACH(QVariant childValue, childrenList) {
Device child; Device child;
@ -214,7 +221,16 @@ void SunnyWebBox::parseMessage(const QString &messageId, const QString &messageT
emit parametersReceived(messageId, key, parameters); emit parametersReceived(messageId, key, parameters);
} }
} else { } 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 &params) QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params)
{ {
qCDebug(dcSma()) << "SunnyWebBox: Send message to" << address.toString() << "Procedure:" << procedure << "Params:" << params;
QString requestId = QUuid::createUuid().toString().remove('{').remove('-').left(14); QString requestId = QUuid::createUuid().toString().remove('{').remove('-').left(14);
QJsonDocument doc; 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, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [this, address, requestId, reply]{ 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(); QByteArray data = reply->readAll();
qCDebug(dcSma()) << "Received reply" << data; qCDebug(dcSma()) << "SunnyWebBox: Received reply" << data;
QJsonParseError error; QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(data, &error); QJsonDocument doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) { if (error.error != QJsonParseError::NoError) {
qCWarning(dcSma()) << "Could not parse JSON" << error.errorString(); qCWarning(dcSma()) << "SunnyWebBox: Could not parse JSON" << error.errorString();
return; return;
} }
if (!doc.isObject()) { if (!doc.isObject()) {
qCWarning(dcSma()) << "JSON is not an Object"; qCWarning(dcSma()) << "SunnyWebBox: JSON is not an Object";
return; return;
} }
QVariantMap map = doc.toVariant().toMap(); QVariantMap map = doc.toVariant().toMap();
if (map["version"] != "1.0") { if (map["version"] != "1.0") {
qCWarning(dcSma()) << "API version not supported" << map["version"]; qCWarning(dcSma()) << "SunnyWebBox: API version not supported" << map["version"];
return; return;
} }
@ -278,7 +300,7 @@ QString SunnyWebBox::sendMessage(const QHostAddress &address, const QString &pro
QVariantMap result = map.value("result").toMap(); QVariantMap result = map.value("result").toMap();
parseMessage(requestId, requestType, result); parseMessage(requestId, requestType, result);
} else { } else {
qCWarning(dcSma()) << "Missing proc or result value"; qCWarning(dcSma()) << "SunnyWebBox: Missing proc or result value";
} }
}); });
return requestId; return requestId;

View File

@ -88,7 +88,7 @@ public:
QHostAddress hostAddress(); QHostAddress hostAddress();
private: private:
bool m_connected = false;
QHostAddress m_hostAddresss; QHostAddress m_hostAddresss;
NetworkAccessManager *m_networkManager = nullptr; NetworkAccessManager *m_networkManager = nullptr;
@ -96,6 +96,7 @@ private:
QString sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params); QString sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params);
void parseMessage(const QString &messageId, const QString &messageType, const QVariantMap &result); void parseMessage(const QString &messageId, const QString &messageType, const QVariantMap &result);
void setConnectionStatus(bool connected);
signals: signals:
void connectedChanged(bool connected); void connectedChanged(bool connected);