From 4b4bcf8f0a64d2dbc4a6b41d57315e36b0c0f98b Mon Sep 17 00:00:00 2001 From: Boernsman Date: Mon, 11 Jan 2021 11:49:46 +0100 Subject: [PATCH] fixed setup after restart and refresh timer --- miele/integrationpluginmiele.cpp | 7 ++++--- miele/miele.cpp | 35 +++++++++++++++----------------- miele/miele.h | 2 ++ 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/miele/integrationpluginmiele.cpp b/miele/integrationpluginmiele.cpp index 81838fb6..d2adbac9 100644 --- a/miele/integrationpluginmiele.cpp +++ b/miele/integrationpluginmiele.cpp @@ -83,7 +83,6 @@ void IntegrationPluginMiele::confirmPairing(ThingPairingInfo *info, const QStrin if (info->thingClassId() == mieleAccountThingClassId) { qCDebug(dcMiele()) << "Confirm pairing Miele account"; - qCDebug(dcMiele()) << "Secret" << secret << "username" << username; QUrl url(secret); QUrlQuery query(url); QByteArray authorizationCode = query.queryItemValue("code").toLocal8Bit(); @@ -145,9 +144,11 @@ void IntegrationPluginMiele::setupThing(ThingSetupInfo *info) return info->finish(Thing::ThingErrorSetupFailed); } miele->getAccessTokenFromRefreshToken(refreshToken); - connect(miele, &Miele::receivedAccessToken, this, [this] { - + connect(miele, &Miele::receivedAccessToken, info, [this, info, miele] { + m_mieleConnections.insert(info->thing(), miele); + info->finish(Thing::ThingErrorNoError); }); + connect(info, &ThingSetupInfo::aborted, miele, &Miele::deleteLater); } } else if (m_idParamTypeIds.contains(thing->thingClassId())) { Thing *parentThing = myThings().findById(thing->parentId()); diff --git a/miele/miele.cpp b/miele/miele.cpp index 8a23af32..09922753 100644 --- a/miele/miele.cpp +++ b/miele/miele.cpp @@ -46,7 +46,7 @@ Miele::Miele(NetworkAccessManager *networkmanager, const QByteArray &clientId, c m_networkManager(networkmanager) { m_tokenRefreshTimer = new QTimer(this); - m_tokenRefreshTimer->setSingleShot(true); + m_tokenRefreshTimer->start(m_refreshInterval*1000); // 10 minutes connect(m_tokenRefreshTimer, &QTimer::timeout, this, &Miele::onRefreshTimeout); } @@ -86,6 +86,7 @@ QUrl Miele::getLoginUrl(const QUrl &redirectUrl, const QString &state) void Miele::getAccessTokenFromRefreshToken(const QByteArray &refreshToken) { + qCDebug(dcMiele()) << "Getting access token from refresh token"; if (refreshToken.isEmpty()) { qCWarning(dcMiele()) << "No refresh token given!"; setAuthenticated(false); @@ -121,23 +122,21 @@ void Miele::getAccessTokenFromRefreshToken(const QByteArray &refreshToken) emit receivedAccessToken(m_accessToken); if (data.toVariant().toMap().contains("expires_in")) { - uint expireTime = data.toVariant().toMap().value("expires_in").toUInt(); + int expireTime = data.toVariant().toMap().value("expires_in").toInt(); qCDebug(dcMiele) << "Access token expires int" << expireTime << "s, at" << QDateTime::currentDateTime().addSecs(expireTime).toString(); - if (!m_tokenRefreshTimer) { - qWarning(dcMiele()) << "Access token refresh timer not initialized"; - return; - } - if (expireTime < 20) { + + if (expireTime < m_refreshInterval) { qCWarning(dcMiele()) << "Expire time too short"; return; } - m_tokenRefreshTimer->start((expireTime - 20) * 1000); + m_accessTokenExpireTime = QDateTime::currentMSecsSinceEpoch() + (expireTime-(m_refreshInterval*1000)); } }); } void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationCode) { + qCDebug(dcMiele()) << "Getting accsss token from authorization code"; if(authorizationCode.isEmpty()) qCWarning(dcMiele) << "No authorization code given!"; @@ -146,7 +145,7 @@ void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationC query.clear(); query.addQueryItem("client_id", m_clientId); query.addQueryItem("client_secret", m_clientSecret); - query.addQueryItem("vg", "de-DE"); + //query.addQueryItem("vg", "de-DE"); query.addQueryItem("redirect_uri", m_redirectUri); query.addQueryItem("grant_type", "authorization_code"); query.addQueryItem("code", authorizationCode); @@ -174,18 +173,14 @@ void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationC receivedRefreshToken(m_refreshToken); if (jsonDoc.toVariant().toMap().contains("expires_in")) { - uint expireTime = jsonDoc.toVariant().toMap().value("expires_in").toUInt(); + int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toUInt(); qCDebug(dcMiele()) << "Token expires in" << expireTime << "s, at" << QDateTime::currentDateTime().addSecs(expireTime).toString(); - if (!m_tokenRefreshTimer) { - qWarning(dcMiele()) << "Token refresh timer not initialized"; - setAuthenticated(false); - return; - } - if (expireTime < 20) { + + if (expireTime < m_refreshInterval) { qCWarning(dcMiele()) << "Expire time too short"; return; } - m_tokenRefreshTimer->start((expireTime - 20) * 1000); + m_accessTokenExpireTime = QDateTime::currentMSecsSinceEpoch() + (expireTime-(m_refreshInterval*1000)); } }); } @@ -507,6 +502,8 @@ bool Miele::checkStatusCode(QNetworkReply *reply, const QByteArray &rawData) void Miele::onRefreshTimeout() { - qCDebug(dcMiele()) << "Refresh authentication token"; - getAccessTokenFromRefreshToken(m_refreshToken); + if (QDateTime::currentMSecsSinceEpoch() > m_accessTokenExpireTime) { + qCDebug(dcMiele()) << "Refresh access token"; + getAccessTokenFromRefreshToken(m_refreshToken); + } } diff --git a/miele/miele.h b/miele/miele.h index 533348e0..1652b72f 100644 --- a/miele/miele.h +++ b/miele/miele.h @@ -135,6 +135,8 @@ private: QByteArray m_redirectUri = "https://127.0.0.1:8888"; QTimer *m_tokenRefreshTimer = nullptr; + int m_refreshInterval = (60 * 10); //10 minutes + qint64 m_accessTokenExpireTime = 0; bool m_authenticated = false; bool m_connected = false;