diff --git a/powerfox/integrationpluginpowerfox.cpp b/powerfox/integrationpluginpowerfox.cpp index 6334e2d5..6ed80b08 100644 --- a/powerfox/integrationpluginpowerfox.cpp +++ b/powerfox/integrationpluginpowerfox.cpp @@ -158,17 +158,14 @@ void IntegrationPluginPowerfox::postSetupThing(Thing */*thing*/) query.addQueryItem("unit", "kWh"); // Can be called at max once per 3 secs. Not sure if that's per account or per meter ID yet. Assuming per meter ID for now. QNetworkReply *reply = request(account, "/" + powerMeter->paramValue(powerMeterThingIdParamTypeId).toString() + "/current", query); - connect(reply, &QNetworkReply::finished, powerMeter, [account, powerMeter, reply](){ + connect(reply, &QNetworkReply::finished, powerMeter, [this, account, powerMeter, reply](){ if (reply->error() == QNetworkReply::AuthenticationRequiredError) { - account->setStateValue(accountConnectedStateTypeId, false); account->setStateValue(accountLoggedInStateTypeId, false); + markAsDisconnected(powerMeter); } if (reply->error() != QNetworkReply::NoError) { qCWarning(dcPowerfox()) << "Failed to poll power meter:" << reply->error() << reply->errorString(); - powerMeter->setStateValue(powerMeterConnectedStateTypeId, false); - powerMeter->setStateValue(powerMeterCurrentPowerStateTypeId, 0); - powerMeter->setStateValue(powerMeterCurrentPhaseAStateTypeId, 0); - powerMeter->setStateValue(powerMeterVoltagePhaseAStateTypeId, 0); + markAsDisconnected(powerMeter); return; } @@ -230,3 +227,13 @@ QNetworkReply *IntegrationPluginPowerfox::request(Thing *thing, const QString &p return reply; } +void IntegrationPluginPowerfox::markAsDisconnected(Thing *thing) +{ + qCDebug(dcPowerfox()) << "Mark thing as disconnected" << thing; + thing->setStateValue(powerMeterConnectedStateTypeId, false); + thing->setStateValue(powerMeterCurrentPowerStateTypeId, 0); + thing->setStateValue(powerMeterCurrentPhaseAStateTypeId, 0); + thing->setStateValue(powerMeterVoltagePhaseAStateTypeId, 0); +} + + diff --git a/powerfox/integrationpluginpowerfox.h b/powerfox/integrationpluginpowerfox.h index cf703002..271125c4 100644 --- a/powerfox/integrationpluginpowerfox.h +++ b/powerfox/integrationpluginpowerfox.h @@ -69,6 +69,7 @@ public: private: QNetworkReply *request(Thing *thing, const QString &path, const QUrlQuery &query = QUrlQuery()); + void markAsDisconnected(Thing *thing); private: PluginTimer *m_pollTimer = nullptr;