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

View File

@ -121,7 +121,7 @@ void Miele::getAccessTokenFromRefreshToken(const QByteArray &refreshToken)
emit receivedAccessToken(m_accessToken);
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();
if (!m_tokenRefreshTimer) {
qWarning(dcMiele()) << "Access token refresh timer not initialized";
@ -174,7 +174,7 @@ void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationC
receivedRefreshToken(m_refreshToken);
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();
if (!m_tokenRefreshTimer) {
qWarning(dcMiele()) << "Token refresh timer not initialized";
@ -194,6 +194,7 @@ void Miele::getAccessTokenFromAuthorizationCode(const QByteArray &authorizationC
void Miele::getDevices()
{
qCDebug(dcMiele()) << "Get devices";
QUrl url = m_apiUrl;
url.setPath("/v1/devices");
url.setQuery("language=en");
@ -205,7 +206,7 @@ void Miele::getDevices()
QNetworkReply *reply = m_networkManager->get(request);
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();
if (!checkStatusCode(reply, rawData)) {
@ -224,6 +225,7 @@ void Miele::getDevices()
void Miele::getDevice(const QString &deviceId)
{
qCDebug(dcMiele()) << "Get device" << deviceId;
QUrl url = m_apiUrl;
url.setPath("/v1/devices/"+deviceId);
url.setQuery("language=en");
@ -248,12 +250,14 @@ void Miele::getDevice(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)
{
qCDebug(dcMiele()) << "Process action" << deviceId << action;
QJsonDocument doc;
QJsonObject object;
object.insert("processAction", action);