pairing is now working

This commit is contained in:
Boernsman 2021-01-10 17:37:36 +01:00
parent c0e53c6669
commit a3d6fa04ba
2 changed files with 43 additions and 15 deletions

View File

@ -61,10 +61,13 @@ IntegrationPluginMiele::IntegrationPluginMiele()
void IntegrationPluginMiele::startPairing(ThingPairingInfo *info) void IntegrationPluginMiele::startPairing(ThingPairingInfo *info)
{ {
if (info->thingClassId() == mieleAccountThingClassId) { if (info->thingClassId() == mieleAccountThingClassId) {
qCDebug(dcMiele()) << "Start pairing Miele account";
Miele *miele = createMieleConnection(); Miele *miele = createMieleConnection();
if (!miele) {
info->finish(Thing::ThingErrorAuthenticationFailure);
return;
}
QUrl url = miele->getLoginUrl(QUrl("https://127.0.0.1:8888")); QUrl url = miele->getLoginUrl(QUrl("https://127.0.0.1:8888"));
qCDebug(dcMiele()) << "HomeConnect url:" << url;
m_setupMieleConnections.insert(info->thingId(), miele); m_setupMieleConnections.insert(info->thingId(), miele);
info->setOAuthUrl(url); info->setOAuthUrl(url);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
@ -79,7 +82,8 @@ void IntegrationPluginMiele::confirmPairing(ThingPairingInfo *info, const QStrin
Q_UNUSED(username); Q_UNUSED(username);
if (info->thingClassId() == mieleAccountThingClassId) { if (info->thingClassId() == mieleAccountThingClassId) {
qCDebug(dcMiele()) << "Redirect url is" << secret; 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();
@ -88,11 +92,12 @@ void IntegrationPluginMiele::confirmPairing(ThingPairingInfo *info, const QStrin
if (!miele) { if (!miele) {
qWarning(dcMiele()) << "No Miele connection found for thing:" << info->thingName(); qWarning(dcMiele()) << "No Miele connection found for thing:" << info->thingName();
m_setupMieleConnections.remove(info->thingId()); m_setupMieleConnections.remove(info->thingId());
info->finish(Thing::ThingErrorHardwareFailure); info->finish(Thing::ThingErrorAuthenticationFailure);
return; return;
} }
qCDebug(dcMiele()) << "Authorization code" << authorizationCode; qCDebug(dcMiele()) << "Authorization code" << authorizationCode;
miele->getAccessTokenFromAuthorizationCode(authorizationCode); miele->getAccessTokenFromAuthorizationCode(authorizationCode);
connect(info, &ThingPairingInfo::aborted, miele, &Miele::deleteLater);
connect(miele, &Miele::receivedRefreshToken, info, [info, this](const QByteArray &refreshToken){ connect(miele, &Miele::receivedRefreshToken, info, [info, this](const QByteArray &refreshToken){
qCDebug(dcMiele()) << "Token:" << refreshToken; qCDebug(dcMiele()) << "Token:" << refreshToken;
@ -112,7 +117,14 @@ void IntegrationPluginMiele::setupThing(ThingSetupInfo *info)
Thing *thing = info->thing(); Thing *thing = info->thing();
if (thing->thingClassId() == mieleAccountThingClassId) { if (thing->thingClassId() == mieleAccountThingClassId) {
qCDebug(dcMiele()) << "Setup Miele Account";
Miele *miele; Miele *miele;
if (m_mieleConnections.contains(thing)) {
qCDebug(dcMiele()) << "Setup after reconfiguration, cleaning up";
m_mieleConnections.take(thing)->deleteLater();
}
if (m_setupMieleConnections.keys().contains(thing->id())) { if (m_setupMieleConnections.keys().contains(thing->id())) {
//Fresh device setup, has already a fresh access token //Fresh device setup, has already a fresh access token
qCDebug(dcMiele()) << "Miele OAuth setup complete"; qCDebug(dcMiele()) << "Miele OAuth setup complete";
@ -133,7 +145,9 @@ void IntegrationPluginMiele::setupThing(ThingSetupInfo *info)
return info->finish(Thing::ThingErrorSetupFailed); return info->finish(Thing::ThingErrorSetupFailed);
} }
miele->getAccessTokenFromRefreshToken(refreshToken); miele->getAccessTokenFromRefreshToken(refreshToken);
m_asyncSetup.insert(miele, info); connect(miele, &Miele::receivedAccessToken, this, [this] {
});
} }
} 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());
@ -154,9 +168,12 @@ void IntegrationPluginMiele::setupThing(ThingSetupInfo *info)
void IntegrationPluginMiele::postSetupThing(Thing *thing) void IntegrationPluginMiele::postSetupThing(Thing *thing)
{ {
qCDebug(dcMiele()) << "Post setup thing" << thing->name();
if (!m_pluginTimer15min) { if (!m_pluginTimer15min) {
qCDebug(dcMiele()) << "Registering plugin timer";
m_pluginTimer15min = hardwareManager()->pluginTimerManager()->registerTimer(60*15); m_pluginTimer15min = hardwareManager()->pluginTimerManager()->registerTimer(60*15);
connect(m_pluginTimer15min, &PluginTimer::timeout, this, [this]() { connect(m_pluginTimer15min, &PluginTimer::timeout, this, [this]() {
qCDebug(dcMiele()) << "Plugin timer triggered";
Q_FOREACH (Thing *thing, myThings().filterByThingClassId(mieleAccountThingClassId)) { Q_FOREACH (Thing *thing, myThings().filterByThingClassId(mieleAccountThingClassId)) {
Miele *miele = m_mieleConnections.value(thing); Miele *miele = m_mieleConnections.value(thing);
if (!miele) { if (!miele) {
@ -237,15 +254,22 @@ Miele *IntegrationPluginMiele::createMieleConnection()
if (clientId.isEmpty() || clientSecret.isEmpty()) { if (clientId.isEmpty() || clientSecret.isEmpty()) {
clientId = apiKeyStorage()->requestKey("miele").data("clientId"); clientId = apiKeyStorage()->requestKey("miele").data("clientId");
clientSecret = apiKeyStorage()->requestKey("miele").data("clientSecret"); clientSecret = apiKeyStorage()->requestKey("miele").data("clientSecret");
if (clientId.isEmpty() || clientSecret.isEmpty()) {
qCWarning(dcMiele()) << "No API credentials available, cannot create Miele connection";
return nullptr;
} else {
qCDebug(dcMiele()) << "Using api credentials from API key provider";
}
} else { } else {
qCDebug(dcMiele()) << "Using custom api credentials"; qCDebug(dcMiele()) << "Using custom api credentials";
} }
if (clientId.isEmpty() || clientSecret.isEmpty()) { Miele *miele = new Miele(hardwareManager()->networkManager(), clientId, clientSecret, "en", this);
return nullptr; connect(miele, &Miele::destroyed, this, [this, miele] {
} else { m_setupMieleConnections.remove(m_setupMieleConnections.key(miele));
qCDebug(dcMiele()) << "Using api credentials from API key provider"; m_mieleConnections.remove(m_mieleConnections.key(miele));
} });
return new Miele(hardwareManager()->networkManager(), clientId, clientSecret, "en", this); return miele;
} }
void IntegrationPluginMiele::onConnectionChanged(bool connected) void IntegrationPluginMiele::onConnectionChanged(bool connected)

View File

@ -121,7 +121,7 @@ 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")) {
int expireTime = data.toVariant().toMap().value("expires_in").toInt(); uint expireTime = data.toVariant().toMap().value("expires_in").toUInt();
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) { if (!m_tokenRefreshTimer) {
qWarning(dcMiele()) << "Access token refresh timer not initialized"; qWarning(dcMiele()) << "Access token refresh timer not initialized";
@ -174,7 +174,7 @@ 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")) {
int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt(); uint 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) { if (!m_tokenRefreshTimer) {
qWarning(dcMiele()) << "Token refresh timer not initialized"; qWarning(dcMiele()) << "Token refresh timer not initialized";
@ -194,6 +194,7 @@ void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationC
void Miele::getDevices() void Miele::getDevices()
{ {
qCDebug(dcMiele()) << "Get devices";
QUrl url = m_apiUrl; QUrl url = m_apiUrl;
url.setPath("/v1/devices"); url.setPath("/v1/devices");
url.setQuery("language=en"); url.setQuery("language=en");
@ -205,7 +206,7 @@ void Miele::getDevices()
QNetworkReply *reply = m_networkManager->get(request); QNetworkReply *reply = m_networkManager->get(request);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [this, reply]{ connect(reply, &QNetworkReply::finished, this, [this, reply] {
QByteArray rawData = reply->readAll(); QByteArray rawData = reply->readAll();
if (!checkStatusCode(reply, rawData)) { if (!checkStatusCode(reply, rawData)) {
@ -224,6 +225,7 @@ void Miele::getDevices()
void Miele::getDevice(const QString &deviceId) void Miele::getDevice(const QString &deviceId)
{ {
qCDebug(dcMiele()) << "Get device" << deviceId;
QUrl url = m_apiUrl; QUrl url = m_apiUrl;
url.setPath("/v1/devices/"+deviceId); url.setPath("/v1/devices/"+deviceId);
url.setQuery("language=en"); url.setQuery("language=en");
@ -248,12 +250,14 @@ void Miele::getDevice(const QString &deviceId)
void Miele::getActions(const QString &deviceId) void Miele::getActions(const QString &deviceId)
{ {
Q_UNUSED(deviceId) qCDebug(dcMiele()) << "Get actions" << deviceId;
//TODO
} }
QUuid Miele::processAction(const QString &deviceId, Miele::ProcessAction action) QUuid Miele::processAction(const QString &deviceId, Miele::ProcessAction action)
{ {
qCDebug(dcMiele()) << "Process action" << deviceId << action;
QJsonDocument doc; QJsonDocument doc;
QJsonObject object; QJsonObject object;
object.insert("processAction", action); object.insert("processAction", action);