diff --git a/netatmo/integrationpluginnetatmo.cpp b/netatmo/integrationpluginnetatmo.cpp index 1d4ae0ac..9479ff10 100644 --- a/netatmo/integrationpluginnetatmo.cpp +++ b/netatmo/integrationpluginnetatmo.cpp @@ -44,7 +44,19 @@ IntegrationPluginNetatmo::IntegrationPluginNetatmo() void IntegrationPluginNetatmo::startPairing(ThingPairingInfo *info) { - info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Netatmo account.")); + // Checking the internet connection + NetworkAccessManager *network = hardwareManager()->networkManager(); + QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://api.netatmo.net"))); + connect(reply, &QNetworkReply::finished, this, [reply, info] { + reply->deleteLater(); + //The server replies usually 404 not found on this request + //int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) { + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("Netatmo server is not reachable.")); + } else { + info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Netatmo account.")); + } + }); } void IntegrationPluginNetatmo::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password) @@ -55,6 +67,7 @@ void IntegrationPluginNetatmo::confirmPairing(ThingPairingInfo *info, const QStr authentication->setPassword(password); authentication->setScope("read_station read_thermostat write_thermostat"); + connect(authentication, &OAuth2::authenticationChanged, info, [this, info, username, password, authentication](){ if (authentication->authenticated()) { pluginStorage()->beginGroup(info->thingId().toString()); @@ -116,7 +129,6 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info) authentication->setUsername(username); authentication->setPassword(password); authentication->setScope("read_station read_thermostat write_thermostat"); - m_authentications.insert(authentication, thing); // Update thing connected state based on OAuth connected state connect(authentication, &OAuth2::authenticationChanged, thing, [this, thing, authentication](){ @@ -129,15 +141,15 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info) authentication->startAuthentication(); // Report thing setup finished when authentication reports success - connect(authentication, &OAuth2::authenticationChanged, info, [info, authentication](){ + connect(authentication, &OAuth2::authenticationChanged, info, [this, info, thing, authentication](){ if (!authentication->authenticated()) { authentication->deleteLater(); info->finish(Thing::ThingErrorAuthenticationFailure, QT_TR_NOOP("Error logging in to Netatmo server.")); return; } + m_authentications.insert(authentication, thing); info->finish(Thing::ThingErrorNoError); }); - return; } else if (thing->thingClassId() == indoorThingClassId) { @@ -186,6 +198,7 @@ void IntegrationPluginNetatmo::setupThing(ThingSetupInfo *info) return info->finish(Thing::ThingErrorNoError); } + info->finish(Thing::ThingErrorThingClassNotFound); qCWarning(dcNetatmo()) << "Unhandled thing class in setupDevice"; } @@ -240,9 +253,29 @@ void IntegrationPluginNetatmo::refreshData(Thing *thing, const QString &token) url.setQuery(query); QNetworkReply *reply = hardwareManager()->networkManager()->get(QNetworkRequest(url)); - connect(reply, &QNetworkReply::finished, this, &IntegrationPluginNetatmo::onNetworkReplyFinished); + connect(reply, &QNetworkReply::finished, this, [this, reply, thing] { + reply->deleteLater(); - m_refreshRequest.insert(reply, thing); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + // check HTTP status code + if (status != 200) { + qCWarning(dcNetatmo) << "Refresh data reply HTTP error:" << status << reply->errorString(); + thing->setStateValue(netatmoConnectionConnectedStateTypeId, false); + return; + } + thing->setStateValue(netatmoConnectionConnectedStateTypeId, true); + // check JSON file + QJsonParseError error; + QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qCWarning(dcNetatmo) << "Refresh data reply JSON error:" << error.errorString(); + return; + } + + qCDebug(dcNetatmo) << qUtf8Printable(jsonDoc.toJson()); + processRefreshData(jsonDoc.toVariant().toMap(), thing); + }); } void IntegrationPluginNetatmo::processRefreshData(const QVariantMap &data, Thing *connectionDevice) @@ -333,44 +366,20 @@ Thing *IntegrationPluginNetatmo::findOutdoorDevice(const QString &macAddress) void IntegrationPluginNetatmo::onPluginTimer() { foreach (OAuth2 *authentication, m_authentications.keys()) { + Thing *thing = m_authentications.value(authentication); + if (!thing) { + qCWarning(dcNetatmo()) << "Authentication without an associated Netatmo connection thing" << authentication->username(); + m_authentications.remove(authentication); + continue; + } if (authentication->authenticated()) { - refreshData(m_authentications.value(authentication), authentication->token()); + refreshData(thing, authentication->token()); } else { authentication->startAuthentication(); } } } -void IntegrationPluginNetatmo::onNetworkReplyFinished() -{ - QNetworkReply *reply = static_cast(sender()); - reply->deleteLater(); - - int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - // update values request - if (m_refreshRequest.keys().contains(reply)) { - Thing *thing = m_refreshRequest.take(reply); - - // check HTTP status code - if (status != 200) { - qCWarning(dcNetatmo) << "Device list reply HTTP error:" << status << reply->errorString(); - thing->setStateValue(netatmoConnectionConnectedStateTypeId, false); - return; - } - thing->setStateValue(netatmoConnectionConnectedStateTypeId, true); - // check JSON file - QJsonParseError error; - QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error); - if (error.error != QJsonParseError::NoError) { - qCWarning(dcNetatmo) << "Device list reply JSON error:" << error.errorString(); - return; - } - - qCDebug(dcNetatmo) << qUtf8Printable(jsonDoc.toJson()); - processRefreshData(jsonDoc.toVariant().toMap(), thing); - } -} - void IntegrationPluginNetatmo::onIndoorStatesChanged() { NetatmoBaseStation *indoor = static_cast(sender()); diff --git a/netatmo/integrationpluginnetatmo.h b/netatmo/integrationpluginnetatmo.h index 25aff367..7d439c44 100644 --- a/netatmo/integrationpluginnetatmo.h +++ b/netatmo/integrationpluginnetatmo.h @@ -75,7 +75,6 @@ private: private slots: void onPluginTimer(); - void onNetworkReplyFinished(); void onIndoorStatesChanged(); void onOutdoorStatesChanged(); }; diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-cs.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-cs.ts index 470b37b3..1518150e 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-cs.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-cs.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-da.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-da.ts index 23771d92..4d0609fc 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-da.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-da.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-de_DE.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-de_DE.ts index 7c176ae4..3d4d52ee 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-de_DE.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-de_DE.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + Netatmo Server ist nicht erreichbar. + + + Please enter the login credentials for your Netatmo account. Bitte geben Sie die Anmeldeinformationen für Ihr Netatmo-Konto ein. - + Wrong username or password Benutzername oder Passwort falsch - + Error logging in to Netatmo server. Fehler beim Anmelden am Netatmo-Server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-en_US.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-en_US.ts index a7c44584..7ccd1bff 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-en_US.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-en_US.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-es.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-es.ts index 1f721732..4a597713 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-es.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-es.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-fr.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-fr.ts index 3faf455c..be429147 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-fr.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-fr.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-it.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-it.ts index fc7b44e6..1c7a4dbb 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-it.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-it.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-nl.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-nl.ts index e0fea15b..99384d49 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-nl.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-nl.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server. diff --git a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-pt.ts b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-pt.ts index c1cf5134..55efa709 100644 --- a/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-pt.ts +++ b/netatmo/translations/69d14951-0c02-4877-bcef-dffdf48b7ccb-pt.ts @@ -4,17 +4,22 @@ IntegrationPluginNetatmo - + + Netatmo server is not reachable. + + + + Please enter the login credentials for your Netatmo account. - + Wrong username or password - + Error logging in to Netatmo server.