fixed crash during thing pairing

This commit is contained in:
Boernsman 2020-06-15 20:23:00 +02:00
parent ed44b0e19c
commit 8d19fe07ba
2 changed files with 46 additions and 20 deletions

View File

@ -48,6 +48,7 @@ void IntegrationPluginTado::startPairing(ThingPairingInfo *info)
// Checking the internet connection // Checking the internet connection
NetworkAccessManager *network = hardwareManager()->networkManager(); NetworkAccessManager *network = hardwareManager()->networkManager();
QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://my.tado.com/api/v2"))); QNetworkReply *reply = network->get(QNetworkRequest(QUrl("https://my.tado.com/api/v2")));
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, info, [reply, info] { connect(reply, &QNetworkReply::finished, info, [reply, info] {
if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) { if (reply->error() == QNetworkReply::NetworkError::HostNotFoundError) {
@ -56,11 +57,11 @@ void IntegrationPluginTado::startPairing(ThingPairingInfo *info)
info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Tado account.")); info->finish(Thing::ThingErrorNoError, QT_TR_NOOP("Please enter the login credentials for your Tado account."));
} }
}); });
connect(reply, &QNetworkReply::finished, this, &QNetworkReply::deleteLater);
} }
void IntegrationPluginTado::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password) void IntegrationPluginTado::confirmPairing(ThingPairingInfo *info, const QString &username, const QString &password)
{ {
qCDebug(dcTado()) << "Confirm pairing" << username << "password" << password << "Network manager available" << hardwareManager()->networkManager()->available();
Tado *tado = new Tado(hardwareManager()->networkManager(), username, this); Tado *tado = new Tado(hardwareManager()->networkManager(), username, this);
connect(tado, &Tado::tokenReceived, this, &IntegrationPluginTado::onTokenReceived); connect(tado, &Tado::tokenReceived, this, &IntegrationPluginTado::onTokenReceived);
connect(tado, &Tado::authenticationStatusChanged, this, &IntegrationPluginTado::onAuthenticationStatusChanged); connect(tado, &Tado::authenticationStatusChanged, this, &IntegrationPluginTado::onAuthenticationStatusChanged);
@ -74,6 +75,14 @@ void IntegrationPluginTado::confirmPairing(ThingPairingInfo *info, const QString
m_unfinishedDevicePairings.insert(info->thingId(), info); m_unfinishedDevicePairings.insert(info->thingId(), info);
tado->getToken(password); tado->getToken(password);
connect(info, &ThingPairingInfo::aborted, this, [info, tado, this]() {
qCWarning(dcTado()) << "Thing pairing has been aborted, going to clean-up";
m_unfinishedDevicePairings.remove(info->thingId());
m_unfinishedTadoAccounts.remove(info->thingId());
tado->deleteLater();
//TODO clean up plugin storage
});
pluginStorage()->beginGroup(info->thingId().toString()); pluginStorage()->beginGroup(info->thingId().toString());
pluginStorage()->setValue("username", username); pluginStorage()->setValue("username", username);
pluginStorage()->setValue("password", password); pluginStorage()->setValue("password", password);
@ -87,17 +96,17 @@ void IntegrationPluginTado::setupThing(ThingSetupInfo *info)
if (thing->thingClassId() == tadoConnectionThingClassId) { if (thing->thingClassId() == tadoConnectionThingClassId) {
qCDebug(dcTado) << "Setup tado connection" << thing->name() << thing->params(); qCDebug(dcTado) << "Setup tado connection" << thing->name() << thing->params();
pluginStorage()->beginGroup(thing->id().toString());
QString username = pluginStorage()->value("username").toString();
QString password = pluginStorage()->value("password").toString();
pluginStorage()->endGroup();
Tado *tado; Tado *tado;
if (m_unfinishedTadoAccounts.contains(thing->id())) { if (m_unfinishedTadoAccounts.contains(thing->id())) {
tado = m_unfinishedTadoAccounts.take(thing->id()); tado = m_unfinishedTadoAccounts.take(thing->id());
m_tadoAccounts.insert(thing->id(), tado); m_tadoAccounts.insert(thing->id(), tado);
return info->finish(Thing::ThingErrorNoError); return info->finish(Thing::ThingErrorNoError);
} else { } else {
pluginStorage()->beginGroup(thing->id().toString());
QString username = pluginStorage()->value("username").toString();
QString password = pluginStorage()->value("password").toString();
pluginStorage()->endGroup();
tado = new Tado(hardwareManager()->networkManager(), username, this); tado = new Tado(hardwareManager()->networkManager(), username, this);
connect(tado, &Tado::tokenReceived, this, &IntegrationPluginTado::onTokenReceived); connect(tado, &Tado::tokenReceived, this, &IntegrationPluginTado::onTokenReceived);
connect(tado, &Tado::authenticationStatusChanged, this, &IntegrationPluginTado::onAuthenticationStatusChanged); connect(tado, &Tado::authenticationStatusChanged, this, &IntegrationPluginTado::onAuthenticationStatusChanged);
@ -226,8 +235,10 @@ void IntegrationPluginTado::onPluginTimer()
{ {
foreach (Thing *thing, myThings().filterByThingClassId(zoneThingClassId)) { foreach (Thing *thing, myThings().filterByThingClassId(zoneThingClassId)) {
Tado *tado = m_tadoAccounts.value(thing->parentId()); Tado *tado = m_tadoAccounts.value(thing->parentId());
if (!tado) if (!tado){
qCWarning(dcTado()) << "Could not find any Tado connection to Zone" << thing->name();
continue; continue;
}
QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString(); QString homeId = thing->paramValue(zoneThingHomeIdParamTypeId).toString();
QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString(); QString zoneId = thing->paramValue(zoneThingZoneIdParamTypeId).toString();
@ -270,16 +281,23 @@ void IntegrationPluginTado::onAuthenticationStatusChanged(bool authenticated)
Tado *tado = static_cast<Tado*>(sender()); Tado *tado = static_cast<Tado*>(sender());
if (m_unfinishedTadoAccounts.values().contains(tado) && !authenticated){ if (m_unfinishedTadoAccounts.values().contains(tado) && !authenticated){
qCWarning(dcTado()) << "Authentication Status changed: " << authenticated << "removing unfinished Tado account";
ThingId id = m_unfinishedTadoAccounts.key(tado); ThingId id = m_unfinishedTadoAccounts.key(tado);
m_unfinishedTadoAccounts.remove(id); m_unfinishedTadoAccounts.remove(id);
ThingPairingInfo *info = m_unfinishedDevicePairings.take(id);
info->finish(Thing::ThingErrorSetupFailed); if (m_unfinishedDevicePairings.contains(id)) {
qCWarning(dcTado()) << "Removing unfinished device pairing";
ThingPairingInfo *info = m_unfinishedDevicePairings.take(id);
info->finish(Thing::ThingErrorSetupFailed);
}
} }
if (m_tadoAccounts.values().contains(tado)){ if (m_tadoAccounts.values().contains(tado)){
Thing *thing = myThings().findById(m_tadoAccounts.key(tado)); Thing *thing = myThings().findById(m_tadoAccounts.key(tado));
if (!thing) if (!thing){
qCWarning(dcTado()) << "OnAuthenticationChanged no thing found by ID" << m_tadoAccounts.key(tado);
return; return;
}
thing->setStateValue(tadoConnectionLoggedInStateTypeId, authenticated); thing->setStateValue(tadoConnectionLoggedInStateTypeId, authenticated);
if (!authenticated) { if (!authenticated) {
@ -320,9 +338,12 @@ void IntegrationPluginTado::onTokenReceived(Tado::Token token)
} }
if (m_unfinishedTadoAccounts.values().contains(tado)) { if (m_unfinishedTadoAccounts.values().contains(tado)) {
qCDebug(dcTado()) << "onTokenReceived: Finishing pairing process";
ThingId id = m_unfinishedTadoAccounts.key(tado); ThingId id = m_unfinishedTadoAccounts.key(tado);
ThingPairingInfo *info = m_unfinishedDevicePairings.take(id); if (m_unfinishedDevicePairings.contains(id)) {
info->finish(Thing::ThingErrorNoError); ThingPairingInfo *info = m_unfinishedDevicePairings.take(id);
info->finish(Thing::ThingErrorNoError);
}
} }
} }

View File

@ -81,10 +81,10 @@ void Tado::getToken(const QString &password)
QNetworkReply *reply = m_networkManager->post(request, body); QNetworkReply *reply = m_networkManager->post(request, body);
//qCDebug(dcTado()) << "Sending request" << request.url() << body; //qCDebug(dcTado()) << "Sending request" << request.url() << body;
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, this] { connect(reply, &QNetworkReply::finished, this, [reply, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
if (reply->error() == QNetworkReply::HostNotFoundError) { if (reply->error() == QNetworkReply::HostNotFoundError) {
@ -142,8 +142,9 @@ void Tado::getHomes()
request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit()); request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit());
QNetworkReply *reply = m_networkManager->get(request); QNetworkReply *reply = m_networkManager->get(request);
//qDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList(); //qDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList();
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, this] { connect(reply, &QNetworkReply::finished, this, [reply, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -187,8 +188,9 @@ void Tado::getZones(const QString &homeId)
request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit()); request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit());
QNetworkReply *reply = m_networkManager->get(request); QNetworkReply *reply = m_networkManager->get(request);
//qDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList(); //qDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList();
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, homeId, this] { connect(reply, &QNetworkReply::finished, this, [reply, homeId, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -233,8 +235,9 @@ void Tado::getZoneState(const QString &homeId, const QString &zoneId)
request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit()); request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit());
QNetworkReply *reply = m_networkManager->get(request); QNetworkReply *reply = m_networkManager->get(request);
//qDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList(); //qDebug(dcTado()) << "Sending request" << request.url() << request.rawHeaderList();
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, homeId, zoneId, this] { connect(reply, &QNetworkReply::finished, this, [reply, homeId, zoneId, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -308,8 +311,8 @@ QUuid Tado::setOverlay(const QString &homeId, const QString &zoneId, bool power,
body.append("{\"setting\":{\"type\":\"HEATING\",\"power\":\""+ powerString + "\",\"temperature\":{\"celsius\":" + QVariant(targetTemperature).toByteArray() + "}},\"termination\":{\"type\":\"MANUAL\"}}"); body.append("{\"setting\":{\"type\":\"HEATING\",\"power\":\""+ powerString + "\",\"temperature\":{\"celsius\":" + QVariant(targetTemperature).toByteArray() + "}},\"termination\":{\"type\":\"MANUAL\"}}");
//qCDebug(dcTado()) << "Sending request" << body; //qCDebug(dcTado()) << "Sending request" << body;
QNetworkReply *reply = m_networkManager->put(request, body); QNetworkReply *reply = m_networkManager->put(request, body);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] { connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -360,8 +363,9 @@ QUuid Tado::deleteOverlay(const QString &homeId, const QString &zoneId)
request.setUrl(QUrl(m_baseControlUrl+"/homes/"+homeId+"/zones/"+zoneId+"/overlay")); request.setUrl(QUrl(m_baseControlUrl+"/homes/"+homeId+"/zones/"+zoneId+"/overlay"));
request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit()); request.setRawHeader("Authorization", "Bearer " + m_accessToken.toLocal8Bit());
QNetworkReply *reply = m_networkManager->deleteResource(request); QNetworkReply *reply = m_networkManager->deleteResource(request);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] { connect(reply, &QNetworkReply::finished, this, [homeId, zoneId, requestId, reply, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code
@ -439,8 +443,9 @@ void Tado::onRefreshTimer()
body.append("&scope=home.user"); body.append("&scope=home.user");
QNetworkReply *reply = m_networkManager->post(request, body); QNetworkReply *reply = m_networkManager->post(request, body);
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [reply, this] { connect(reply, &QNetworkReply::finished, this, [reply, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code // Check HTTP status code