mirror of
https://github.com/nymea/nymea-plugins.git
synced 2026-08-05 03:33:51 +02:00
fixed setup after restart and refresh timer
This commit is contained in:
parent
a3d6fa04ba
commit
4b4bcf8f0a
@ -83,7 +83,6 @@ void IntegrationPluginMiele::confirmPairing(ThingPairingInfo *info, const QStrin
|
|||||||
|
|
||||||
if (info->thingClassId() == mieleAccountThingClassId) {
|
if (info->thingClassId() == mieleAccountThingClassId) {
|
||||||
qCDebug(dcMiele()) << "Confirm pairing Miele account";
|
qCDebug(dcMiele()) << "Confirm pairing Miele account";
|
||||||
qCDebug(dcMiele()) << "Secret" << secret << "username" << username;
|
|
||||||
QUrl url(secret);
|
QUrl url(secret);
|
||||||
QUrlQuery query(url);
|
QUrlQuery query(url);
|
||||||
QByteArray authorizationCode = query.queryItemValue("code").toLocal8Bit();
|
QByteArray authorizationCode = query.queryItemValue("code").toLocal8Bit();
|
||||||
@ -145,9 +144,11 @@ void IntegrationPluginMiele::setupThing(ThingSetupInfo *info)
|
|||||||
return info->finish(Thing::ThingErrorSetupFailed);
|
return info->finish(Thing::ThingErrorSetupFailed);
|
||||||
}
|
}
|
||||||
miele->getAccessTokenFromRefreshToken(refreshToken);
|
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())) {
|
} else if (m_idParamTypeIds.contains(thing->thingClassId())) {
|
||||||
Thing *parentThing = myThings().findById(thing->parentId());
|
Thing *parentThing = myThings().findById(thing->parentId());
|
||||||
|
|||||||
@ -46,7 +46,7 @@ Miele::Miele(NetworkAccessManager *networkmanager, const QByteArray &clientId, c
|
|||||||
m_networkManager(networkmanager)
|
m_networkManager(networkmanager)
|
||||||
{
|
{
|
||||||
m_tokenRefreshTimer = new QTimer(this);
|
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);
|
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)
|
void Miele::getAccessTokenFromRefreshToken(const QByteArray &refreshToken)
|
||||||
{
|
{
|
||||||
|
qCDebug(dcMiele()) << "Getting access token from refresh token";
|
||||||
if (refreshToken.isEmpty()) {
|
if (refreshToken.isEmpty()) {
|
||||||
qCWarning(dcMiele()) << "No refresh token given!";
|
qCWarning(dcMiele()) << "No refresh token given!";
|
||||||
setAuthenticated(false);
|
setAuthenticated(false);
|
||||||
@ -121,23 +122,21 @@ void Miele::getAccessTokenFromRefreshToken(const QByteArray &refreshToken)
|
|||||||
emit receivedAccessToken(m_accessToken);
|
emit receivedAccessToken(m_accessToken);
|
||||||
|
|
||||||
if (data.toVariant().toMap().contains("expires_in")) {
|
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();
|
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";
|
if (expireTime < m_refreshInterval) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (expireTime < 20) {
|
|
||||||
qCWarning(dcMiele()) << "Expire time too short";
|
qCWarning(dcMiele()) << "Expire time too short";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_tokenRefreshTimer->start((expireTime - 20) * 1000);
|
m_accessTokenExpireTime = QDateTime::currentMSecsSinceEpoch() + (expireTime-(m_refreshInterval*1000));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationCode)
|
void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationCode)
|
||||||
{
|
{
|
||||||
|
qCDebug(dcMiele()) << "Getting accsss token from authorization code";
|
||||||
if(authorizationCode.isEmpty())
|
if(authorizationCode.isEmpty())
|
||||||
qCWarning(dcMiele) << "No authorization code given!";
|
qCWarning(dcMiele) << "No authorization code given!";
|
||||||
|
|
||||||
@ -146,7 +145,7 @@ void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationC
|
|||||||
query.clear();
|
query.clear();
|
||||||
query.addQueryItem("client_id", m_clientId);
|
query.addQueryItem("client_id", m_clientId);
|
||||||
query.addQueryItem("client_secret", m_clientSecret);
|
query.addQueryItem("client_secret", m_clientSecret);
|
||||||
query.addQueryItem("vg", "de-DE");
|
//query.addQueryItem("vg", "de-DE");
|
||||||
query.addQueryItem("redirect_uri", m_redirectUri);
|
query.addQueryItem("redirect_uri", m_redirectUri);
|
||||||
query.addQueryItem("grant_type", "authorization_code");
|
query.addQueryItem("grant_type", "authorization_code");
|
||||||
query.addQueryItem("code", authorizationCode);
|
query.addQueryItem("code", authorizationCode);
|
||||||
@ -174,18 +173,14 @@ void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationC
|
|||||||
receivedRefreshToken(m_refreshToken);
|
receivedRefreshToken(m_refreshToken);
|
||||||
|
|
||||||
if (jsonDoc.toVariant().toMap().contains("expires_in")) {
|
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();
|
qCDebug(dcMiele()) << "Token expires in" << expireTime << "s, at" << QDateTime::currentDateTime().addSecs(expireTime).toString();
|
||||||
if (!m_tokenRefreshTimer) {
|
|
||||||
qWarning(dcMiele()) << "Token refresh timer not initialized";
|
if (expireTime < m_refreshInterval) {
|
||||||
setAuthenticated(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (expireTime < 20) {
|
|
||||||
qCWarning(dcMiele()) << "Expire time too short";
|
qCWarning(dcMiele()) << "Expire time too short";
|
||||||
return;
|
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()
|
void Miele::onRefreshTimeout()
|
||||||
{
|
{
|
||||||
qCDebug(dcMiele()) << "Refresh authentication token";
|
if (QDateTime::currentMSecsSinceEpoch() > m_accessTokenExpireTime) {
|
||||||
getAccessTokenFromRefreshToken(m_refreshToken);
|
qCDebug(dcMiele()) << "Refresh access token";
|
||||||
|
getAccessTokenFromRefreshToken(m_refreshToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -135,6 +135,8 @@ private:
|
|||||||
QByteArray m_redirectUri = "https://127.0.0.1:8888";
|
QByteArray m_redirectUri = "https://127.0.0.1:8888";
|
||||||
|
|
||||||
QTimer *m_tokenRefreshTimer = nullptr;
|
QTimer *m_tokenRefreshTimer = nullptr;
|
||||||
|
int m_refreshInterval = (60 * 10); //10 minutes
|
||||||
|
qint64 m_accessTokenExpireTime = 0;
|
||||||
|
|
||||||
bool m_authenticated = false;
|
bool m_authenticated = false;
|
||||||
bool m_connected = false;
|
bool m_connected = false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user