addded get home appliances

This commit is contained in:
bernhard.trinnes 2020-07-11 15:38:17 +02:00 committed by Boernsman
parent df203dfcc0
commit 214b1052a2
5 changed files with 276 additions and 84 deletions

View File

@ -36,17 +36,19 @@
#include <QJsonArray> #include <QJsonArray>
#include <QUrlQuery> #include <QUrlQuery>
HomeConnect::HomeConnect(NetworkAccessManager *networkmanager, const QByteArray &clientKey, const QByteArray &clientSecret, QObject *parent) : HomeConnect::HomeConnect(NetworkAccessManager *networkmanager, const QByteArray &clientKey, const QByteArray &clientSecret, bool simulationMode, QObject *parent) :
QObject(parent), QObject(parent),
m_clientKey(clientKey), m_clientKey(clientKey),
m_clientSecret(clientSecret), m_clientSecret(clientSecret),
m_networkManager(networkmanager) m_networkManager(networkmanager)
{ {
if(!m_tokenRefreshTimer) { if(!m_tokenRefreshTimer) {
m_tokenRefreshTimer = new QTimer(this); m_tokenRefreshTimer = new QTimer(this);
m_tokenRefreshTimer->setSingleShot(true); m_tokenRefreshTimer->setSingleShot(true);
connect(m_tokenRefreshTimer, &QTimer::timeout, this, &HomeConnect::onRefreshTimeout); connect(m_tokenRefreshTimer, &QTimer::timeout, this, &HomeConnect::onRefreshTimeout);
} }
setSimulationMode(simulationMode);
} }
QByteArray HomeConnect::accessToken() QByteArray HomeConnect::accessToken()
@ -59,6 +61,20 @@ QByteArray HomeConnect::refreshToken()
return m_refreshToken; return m_refreshToken;
} }
void HomeConnect::setSimulationMode(bool simulation)
{
m_simulationMode = simulation;
if (simulation) {
m_baseAuthorizationUrl = "https://simulator.home-connect.com/security/oauth/authorize";
m_baseTokenUrl = "https://simulator.home-connect.com/security/oauth/token";
m_baseControlUrl = "https://simulator.home-connect.com";
} else {
m_baseAuthorizationUrl = "https://api.home-connect.com/security/oauth/authorize";
m_baseTokenUrl = "https://api.home-connect.com/security/oauth/token";
m_baseControlUrl = "https://api.home-connect.com";
}
}
QUrl HomeConnect::getLoginUrl(const QUrl &redirectUrl, const QString &scope) QUrl HomeConnect::getLoginUrl(const QUrl &redirectUrl, const QString &scope)
{ {
if (m_clientKey.isEmpty()) { if (m_clientKey.isEmpty()) {
@ -130,40 +146,44 @@ void HomeConnect::getAccessTokenFromRefreshToken(const QByteArray &refreshToken)
return; return;
} }
QUrl url(m_baseAuthorizationUrl); QUrl url(m_baseTokenUrl);
QUrlQuery query; QUrlQuery query;
query.clear(); query.clear();
query.addQueryItem("grant_type", "refresh_token"); query.addQueryItem("grant_type", "refresh_token");
query.addQueryItem("refresh_token", refreshToken); query.addQueryItem("refresh_token", refreshToken);
url.setQuery(query); query.addQueryItem("client_secret", m_clientSecret);
QNetworkRequest request(url); QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded; charset=UTF-8"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QByteArray auth = QByteArray(m_clientKey + ':' + m_clientSecret).toBase64(QByteArray::Base64Encoding | QByteArray::KeepTrailingEquals); QNetworkReply *reply = m_networkManager->post(request, query.toString().toUtf8());
request.setRawHeader("Authorization", QString("Basic %1").arg(QString(auth)).toUtf8());
QNetworkReply *reply = m_networkManager->post(request, QByteArray());
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](){
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status != 200 || reply->error() != QNetworkReply::NoError) { if (status != 200 || reply->error() != QNetworkReply::NoError) {
if(jsonDoc.toVariant().toMap().contains("error_description")) { qWarning(dcHomeConnect()) << "Access token error:" << reply->errorString() << reply->readAll();
qWarning(dcHomeConnect()) << "Access token error:" << jsonDoc.toVariant().toMap().value("error_description").toString();
}
emit authenticationStatusChanged(false); emit authenticationStatusChanged(false);
return; return;
} }
if(!jsonDoc.toVariant().toMap().contains("access_token")) { QJsonParseError error;
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError) {
emit authenticationStatusChanged(false); emit authenticationStatusChanged(false);
qCDebug(dcHomeConnect()) << "Received invalide JSON object" << data.toJson();
return; return;
} }
m_accessToken = jsonDoc.toVariant().toMap().value("access_token").toByteArray(); qCDebug(dcHomeConnect()) << "get access token from refresh token" << data.toJson();
if (jsonDoc.toVariant().toMap().contains("expires_in")) { if(!data.toVariant().toMap().contains("access_token")) {
int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt(); emit authenticationStatusChanged(false);
return;
}
m_accessToken = data.toVariant().toMap().value("access_token").toByteArray();
if (data.toVariant().toMap().contains("expires_in")) {
int expireTime = data.toVariant().toMap().value("expires_in").toInt();
qCDebug(dcHomeConnect) << "Access token expires at" << QDateTime::currentDateTime().addSecs(expireTime).toString(); qCDebug(dcHomeConnect) << "Access token expires at" << QDateTime::currentDateTime().addSecs(expireTime).toString();
if (!m_tokenRefreshTimer) { if (!m_tokenRefreshTimer) {
qWarning(dcHomeConnect()) << "Access token refresh timer not initialized"; qWarning(dcHomeConnect()) << "Access token refresh timer not initialized";
@ -195,10 +215,9 @@ void HomeConnect::getAccessTokenFromAuthorizationCode(const QByteArray &authoriz
query.addQueryItem("grant_type", "authorization_code"); query.addQueryItem("grant_type", "authorization_code");
query.addQueryItem("code", authorizationCode); query.addQueryItem("code", authorizationCode);
query.addQueryItem("code_verifier", m_codeChallenge); query.addQueryItem("code_verifier", m_codeChallenge);
/* Code verifier which was used during code challenge creation on client side. Please note that it is required if the code_challenge parameter was included in the authorization request. */
QNetworkRequest request(url); QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
qCDebug(dcHomeConnect()) << "Get access token" << url.toString();
QNetworkReply *reply = m_networkManager->post(request, query.toString().toUtf8()); QNetworkReply *reply = m_networkManager->post(request, query.toString().toUtf8());
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
@ -210,8 +229,6 @@ void HomeConnect::getAccessTokenFromAuthorizationCode(const QByteArray &authoriz
return; return;
} }
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll()); QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
qCDebug(dcHomeConnect()) << "HomeConnect accessToken reply:" << this << reply->error() << reply->errorString() << jsonDoc.toJson();
if(!jsonDoc.toVariant().toMap().contains("access_token") || !jsonDoc.toVariant().toMap().contains("refresh_token") ) { if(!jsonDoc.toVariant().toMap().contains("access_token") || !jsonDoc.toVariant().toMap().contains("refresh_token") ) {
emit authenticationStatusChanged(false); emit authenticationStatusChanged(false);
return; return;
@ -224,7 +241,7 @@ void HomeConnect::getAccessTokenFromAuthorizationCode(const QByteArray &authoriz
if (jsonDoc.toVariant().toMap().contains("expires_in")) { if (jsonDoc.toVariant().toMap().contains("expires_in")) {
int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt(); int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt();
qCDebug(dcHomeConnect()) << "expires at" << QDateTime::currentDateTime().addSecs(expireTime).toString(); qCDebug(dcHomeConnect()) << "Token expires at" << QDateTime::currentDateTime().addSecs(expireTime).toString();
if (!m_tokenRefreshTimer) { if (!m_tokenRefreshTimer) {
qWarning(dcHomeConnect()) << "Token refresh timer not initialized"; qWarning(dcHomeConnect()) << "Token refresh timer not initialized";
emit authenticationStatusChanged(false); emit authenticationStatusChanged(false);
@ -238,40 +255,42 @@ void HomeConnect::getAccessTokenFromAuthorizationCode(const QByteArray &authoriz
void HomeConnect::getHomeAppliances() void HomeConnect::getHomeAppliances()
{ {
QUrl url = QUrl(m_baseAuthorizationUrl); QUrl url = QUrl(m_baseControlUrl+"/api/homeappliances");
QNetworkRequest request(url); QNetworkRequest request(url);
request.setRawHeader("Authorization", "Bearer "+m_accessToken);
QByteArray auth = QByteArray(m_clientKey + ':' + m_clientSecret).toBase64(QByteArray::Base64Encoding | QByteArray::KeepTrailingEquals);
request.setRawHeader("Authorization", QString("Basic %1").arg(QString(auth)).toUtf8());
request.setRawHeader("accept", "application/vnd.bsh.sdk.v1+json"); request.setRawHeader("accept", "application/vnd.bsh.sdk.v1+json");
QNetworkReply *reply = m_networkManager->get(request); QNetworkReply *reply = m_networkManager->get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply](){ connect(reply, &QNetworkReply::finished, this, [this, reply](){
reply->deleteLater(); reply->deleteLater();
QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
qCDebug(dcHomeConnect()) << "HomeConnect accessToken reply:" << this << reply->error() << reply->errorString() << jsonDoc.toJson(); QJsonParseError error;
if(!jsonDoc.toVariant().toMap().contains("access_token") || !jsonDoc.toVariant().toMap().contains("refresh_token") ) { QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
emit authenticationStatusChanged(false); if (error.error != QJsonParseError::NoError) {
qCDebug(dcHomeConnect()) << "Get home appliances: Recieved invalide JSON object";
return; return;
} }
qCDebug(dcHomeConnect()) << "Access token:" << jsonDoc.toVariant().toMap().value("access_token").toString(); qCDebug(dcHomeConnect()) << "Get home appliances" << data.toJson();
m_accessToken = jsonDoc.toVariant().toMap().value("access_token").toByteArray(); if (data.toVariant().toMap().contains("data")) {
QVariantMap dataMap = data.toVariant().toMap().value("data").toMap();
qCDebug(dcHomeConnect()) << "Refresh token:" << jsonDoc.toVariant().toMap().value("refresh_token").toString(); QList<HomeAppliance> appliances;
m_refreshToken = jsonDoc.toVariant().toMap().value("refresh_token").toByteArray(); foreach (const QVariant &variant, dataMap.value("homeappliances").toList()) {
QVariantMap obj = variant.toMap();
if (jsonDoc.toVariant().toMap().contains("expires_in")) { HomeAppliance appliance;
int expireTime = jsonDoc.toVariant().toMap().value("expires_in").toInt(); appliance.name = obj["name"].toString();
qCDebug(dcHomeConnect()) << "expires at" << QDateTime::currentDateTime().addSecs(expireTime).toString(); appliance.brand = obj["brand"].toString();
if (!m_tokenRefreshTimer) { appliance.vib = obj["vib"].toString();
qWarning(dcHomeConnect()) << "Token refresh timer not initialized"; appliance.type = obj["type"].toString();
emit authenticationStatusChanged(false); appliance.homeApplianceId = obj["haId"].toString();
return; appliance.enumber = obj["enumber"].toString();
appliance.connected = obj["connected"].toBool();
appliances.append(appliance);
} }
m_tokenRefreshTimer->start((expireTime - 20) * 1000); if (!appliances.isEmpty())
emit receivedHomeAppliances(appliances);
} else if (data.toVariant().toMap().contains("error")) {
qCWarning(dcHomeConnect()) << "Get home appliences" << data.toVariant().toMap().value("error").toMap().value("description").toString();
} }
emit authenticationStatusChanged(true);
}); });
} }

View File

@ -41,19 +41,34 @@ class HomeConnect : public QObject
Q_OBJECT Q_OBJECT
public: public:
enum Type { enum Type {
Oven,
Dishwasher,
Washer,
Dryer,
WasherDryer,
FridgeFreezer,
Refrigerator,
Freezer,
WineCooler,
CoffeeMaker,
Hood,
CleaningRobot,
CookProcessor
}; };
struct HomeAppliance { struct HomeAppliance {
QString name; QString name;
QString brand; QString brand;
QString typeCode; QString enumber;
QString vib;
bool connected; bool connected;
QString type; QString type;
QString homeApplianceId;
}; };
HomeConnect(NetworkAccessManager *networkmanager, const QByteArray &clientKey, const QByteArray &clientSecret, QObject *parent = nullptr); HomeConnect(NetworkAccessManager *networkmanager, const QByteArray &clientKey, const QByteArray &clientSecret, bool simulationMode = false, QObject *parent = nullptr);
QByteArray accessToken(); QByteArray accessToken();
QByteArray refreshToken(); QByteArray refreshToken();
void setSimulationMode(bool simulation);
QUrl getLoginUrl(const QUrl &redirectUrl, const QString &scope); QUrl getLoginUrl(const QUrl &redirectUrl, const QString &scope);
void checkStatusCode(int status, const QByteArray &payload); void checkStatusCode(int status, const QByteArray &payload);
@ -64,9 +79,10 @@ public:
void getHomeAppliance(const QString &haid); void getHomeAppliance(const QString &haid);
private: private:
QByteArray m_baseAuthorizationUrl = "https://api.home-connect.com/security/oauth/authorize"; bool m_simulationMode = false;
QByteArray m_baseTokenUrl = "https://api.home-connect.com/security/oauth/token"; QByteArray m_baseAuthorizationUrl;
QByteArray m_baseControlUrl = "https://api.home-connect.com"; QByteArray m_baseTokenUrl;
QByteArray m_baseControlUrl;
QByteArray m_clientKey; QByteArray m_clientKey;
QByteArray m_clientSecret; QByteArray m_clientSecret;

View File

@ -40,28 +40,28 @@
IntegrationPluginHomeConnect::IntegrationPluginHomeConnect() IntegrationPluginHomeConnect::IntegrationPluginHomeConnect()
{ {
} m_idParamTypeIds.insert(fridgeThingClassId, fridgeThingIdParamTypeId);
m_idParamTypeIds.insert(dryerThingClassId, dryerThingIdParamTypeId);
m_idParamTypeIds.insert(coffeMakerThingClassId, coffeMakerThingIdParamTypeId);
m_idParamTypeIds.insert(dishwasherThingClassId, dishwasherThingIdParamTypeId);
m_idParamTypeIds.insert(washerThingClassId, washerThingIdParamTypeId);
m_idParamTypeIds.insert(ovenThingClassId, ovenThingIdParamTypeId);
m_connectedStateTypeIds.insert(fridgeThingClassId, fridgeConnectedStateTypeId);
IntegrationPluginHomeConnect::~IntegrationPluginHomeConnect() m_connectedStateTypeIds.insert(dryerThingClassId, dryerConnectedStateTypeId);
{ m_connectedStateTypeIds.insert(coffeMakerThingClassId, coffeMakerConnectedStateTypeId);
if (m_pluginTimer5sec) m_connectedStateTypeIds.insert(dishwasherThingClassId, dishwasherConnectedStateTypeId);
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer5sec); m_connectedStateTypeIds.insert(washerThingClassId, washerConnectedStateTypeId);
if (m_pluginTimer60sec) m_connectedStateTypeIds.insert(ovenThingClassId, ovenConnectedStateTypeId);
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer60sec);
}
void IntegrationPluginHomeConnect::discoverThings(ThingDiscoveryInfo *info)
{
Q_UNUSED(info)
} }
void IntegrationPluginHomeConnect::startPairing(ThingPairingInfo *info) void IntegrationPluginHomeConnect::startPairing(ThingPairingInfo *info)
{ {
if (info->thingClassId() == homeConnectConnectionThingClassId) { if (info->thingClassId() == homeConnectConnectionThingClassId) {
HomeConnect *homeConnect = new HomeConnect(hardwareManager()->networkManager(), "423713AB3EDA5B44BCE6E7B3546C43DADCB27A156C681E30455250637B2213DB", "AE182EA9F1CB99416DFD62CE61BF6DCDB3BB7D4697B58D4499D3792EC9F7412D", this); bool simulationMode = configValue(homeConnectPluginSimulationModeParamTypeId).toBool();
QUrl url = homeConnect->getLoginUrl(QUrl("https://127.0.0.1:8888"), "Monitor"); HomeConnect *homeConnect = new HomeConnect(hardwareManager()->networkManager(), "423713AB3EDA5B44BCE6E7B3546C43DADCB27A156C681E30455250637B2213DB", "AE182EA9F1CB99416DFD62CE61BF6DCDB3BB7D4697B58D4499D3792EC9F7412D", simulationMode, this);
QUrl url = homeConnect->getLoginUrl(QUrl("https://127.0.0.1:8888"), "IdentifyAppliance Monitor Settings");
qCDebug(dcHomeConnect()) << "HomeConnect url:" << url; qCDebug(dcHomeConnect()) << "HomeConnect url:" << url;
info->setOAuthUrl(url); info->setOAuthUrl(url);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
@ -115,8 +115,7 @@ void IntegrationPluginHomeConnect::confirmPairing(ThingPairingInfo *info, const
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
}); });
} else { } else {
qCWarning(dcHomeConnect()) << "Invalid thingClassId -> no pairing possible with this device"; Q_ASSERT_X(false, "confirmPairing", QString("Unhandled thingClassId: %1").arg(info->thingClassId().toString()).toUtf8());
info->finish(Thing::ThingErrorThingClassNotFound);
} }
} }
@ -125,7 +124,8 @@ void IntegrationPluginHomeConnect::setupThing(ThingSetupInfo *info)
{ {
Thing *thing = info->thing(); Thing *thing = info->thing();
if (info->thing()->thingClassId() == homeConnectConnectionThingClassId) { if (thing->thingClassId() == homeConnectConnectionThingClassId) {
bool simulationMode = configValue(homeConnectPluginSimulationModeParamTypeId).toBool();
HomeConnect *homeConnect; HomeConnect *homeConnect;
if (m_setupHomeConnectConnections.keys().contains(thing->id())) { if (m_setupHomeConnectConnections.keys().contains(thing->id())) {
//Fresh device setup, has already a fresh access token //Fresh device setup, has already a fresh access token
@ -134,24 +134,38 @@ void IntegrationPluginHomeConnect::setupThing(ThingSetupInfo *info)
connect(homeConnect, &HomeConnect::connectionChanged, this, &IntegrationPluginHomeConnect::onConnectionChanged); connect(homeConnect, &HomeConnect::connectionChanged, this, &IntegrationPluginHomeConnect::onConnectionChanged);
connect(homeConnect, &HomeConnect::actionExecuted, this, &IntegrationPluginHomeConnect::onRequestExecuted); connect(homeConnect, &HomeConnect::actionExecuted, this, &IntegrationPluginHomeConnect::onRequestExecuted);
connect(homeConnect, &HomeConnect::authenticationStatusChanged, this, &IntegrationPluginHomeConnect::onAuthenticationStatusChanged); connect(homeConnect, &HomeConnect::authenticationStatusChanged, this, &IntegrationPluginHomeConnect::onAuthenticationStatusChanged);
connect(homeConnect, &HomeConnect::receivedHomeAppliances, this, &IntegrationPluginHomeConnect::onReceivedHomeAppliances);
m_homeConnectConnections.insert(thing, homeConnect); m_homeConnectConnections.insert(thing, homeConnect);
return; m_homeConnectConnections.insert(thing, homeConnect);
info->finish(Thing::ThingErrorNoError);
} else { } else {
//device loaded from the device database, needs a new access token; //device loaded from the device database, needs a new access token;
pluginStorage()->beginGroup(thing->id().toString()); pluginStorage()->beginGroup(thing->id().toString());
QByteArray refreshToken = pluginStorage()->value("refresh_token").toByteArray(); QByteArray refreshToken = pluginStorage()->value("refresh_token").toByteArray();
pluginStorage()->endGroup(); pluginStorage()->endGroup();
homeConnect = new HomeConnect(hardwareManager()->networkManager(), "423713AB3EDA5B44BCE6E7B3546C43DADCB27A156C681E30455250637B2213DB", "AE182EA9F1CB99416DFD62CE61BF6DCDB3BB7D4697B58D4499D3792EC9F7412D", this); homeConnect = new HomeConnect(hardwareManager()->networkManager(), "423713AB3EDA5B44BCE6E7B3546C43DADCB27A156C681E30455250637B2213DB", "AE182EA9F1CB99416DFD62CE61BF6DCDB3BB7D4697B58D4499D3792EC9F7412D", simulationMode, this);
connect(homeConnect, &HomeConnect::connectionChanged, this, &IntegrationPluginHomeConnect::onConnectionChanged); connect(homeConnect, &HomeConnect::connectionChanged, this, &IntegrationPluginHomeConnect::onConnectionChanged);
connect(homeConnect, &HomeConnect::actionExecuted, this, &IntegrationPluginHomeConnect::onRequestExecuted); connect(homeConnect, &HomeConnect::actionExecuted, this, &IntegrationPluginHomeConnect::onRequestExecuted);
connect(homeConnect, &HomeConnect::authenticationStatusChanged, this, &IntegrationPluginHomeConnect::onAuthenticationStatusChanged); connect(homeConnect, &HomeConnect::authenticationStatusChanged, this, &IntegrationPluginHomeConnect::onAuthenticationStatusChanged);
connect(homeConnect, &HomeConnect::receivedHomeAppliances, this, &IntegrationPluginHomeConnect::onReceivedHomeAppliances);
homeConnect->getAccessTokenFromRefreshToken(refreshToken); homeConnect->getAccessTokenFromRefreshToken(refreshToken);
m_homeConnectConnections.insert(thing, homeConnect); m_asyncSetup.insert(homeConnect, info);
info->finish(Thing::ThingErrorNoError);
} }
} else if (thing->thingClassId() == dryerThingClassId) {
info->finish(Thing::ThingErrorNoError);
} else if (thing->thingClassId() == fridgeThingClassId) {
info->finish(Thing::ThingErrorNoError);
} else if (thing->thingClassId() == washerThingClassId) {
info->finish(Thing::ThingErrorNoError);
} else if (thing->thingClassId() == dishwasherThingClassId) {
info->finish(Thing::ThingErrorNoError);
} else if (thing->thingClassId() == coffeMakerThingClassId) {
info->finish(Thing::ThingErrorNoError);
} else if (thing->thingClassId() == ovenThingClassId) {
info->finish(Thing::ThingErrorNoError);
} else { } else {
info->finish(Thing::ThingErrorThingClassNotFound); Q_ASSERT_X(false, "setupThing", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
} }
} }
@ -167,6 +181,7 @@ void IntegrationPluginHomeConnect::postSetupThing(Thing *thing)
qWarning(dcHomeConnect()) << "No HomeConnect account found for" << connectionThing->name(); qWarning(dcHomeConnect()) << "No HomeConnect account found for" << connectionThing->name();
continue; continue;
} }
//TODO upate thing states
} }
}); });
} }
@ -180,14 +195,28 @@ void IntegrationPluginHomeConnect::postSetupThing(Thing *thing)
qWarning(dcHomeConnect()) << "No HomeConnect account found for" << thing->name(); qWarning(dcHomeConnect()) << "No HomeConnect account found for" << thing->name();
continue; continue;
} }
homeConnect->getHomeAppliances();
} }
}); });
} }
if (thing->thingClassId() == homeConnectConnectionThingClassId) { if (thing->thingClassId() == homeConnectConnectionThingClassId) {
HomeConnect *homeConnect = m_homeConnectConnections.value(thing); HomeConnect *homeConnect = m_homeConnectConnections.value(thing);
Q_UNUSED(homeConnect) homeConnect->getHomeAppliances();
thing->setStateValue(homeConnectConnectionConnectedStateTypeId, true);
thing->setStateValue(homeConnectConnectionLoggedInStateTypeId, true);
} else if ((thing->thingClassId() == dryerThingClassId) ||
(thing->thingClassId() == fridgeThingClassId) ||
(thing->thingClassId() == washerThingClassId) ||
(thing->thingClassId() == dishwasherThingClassId) ||
(thing->thingClassId() == coffeMakerThingClassId) ||
(thing->thingClassId() == ovenThingClassId)) {
Thing *parentThing = myThings().findById(thing->parentId());
HomeConnect *homeConnect = m_homeConnectConnections.value(parentThing);
if (homeConnect)
homeConnect->getHomeAppliances();
} else {
Q_ASSERT_X(false, "postSetupThing", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
} }
} }
@ -214,10 +243,14 @@ void IntegrationPluginHomeConnect::thingRemoved(Thing *thing)
} }
if (myThings().empty()) { if (myThings().empty()) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer5sec); if (m_pluginTimer5sec) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer60sec); m_pluginTimer5sec = nullptr;
m_pluginTimer5sec = nullptr; hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer5sec);
m_pluginTimer60sec = nullptr; }
if (m_pluginTimer60sec) {
hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer60sec);
m_pluginTimer60sec = nullptr;
}
} }
} }
@ -228,6 +261,11 @@ void IntegrationPluginHomeConnect::onConnectionChanged(bool connected)
if (!thing) if (!thing)
return; return;
thing->setStateValue(homeConnectConnectionConnectedStateTypeId, connected); thing->setStateValue(homeConnectConnectionConnectedStateTypeId, connected);
if (!connected) {
Q_FOREACH(Thing *child, myThings().filterByParentId(thing->id())) {
child->setStateValue(m_connectedStateTypeIds.value(child->thingClassId()), connected);
}
}
} }
void IntegrationPluginHomeConnect::onAuthenticationStatusChanged(bool authenticated) void IntegrationPluginHomeConnect::onAuthenticationStatusChanged(bool authenticated)
@ -236,8 +274,10 @@ void IntegrationPluginHomeConnect::onAuthenticationStatusChanged(bool authentica
if (m_asyncSetup.contains(homeConnectConnection)) { if (m_asyncSetup.contains(homeConnectConnection)) {
ThingSetupInfo *info = m_asyncSetup.take(homeConnectConnection); ThingSetupInfo *info = m_asyncSetup.take(homeConnectConnection);
if (authenticated) { if (authenticated) {
m_homeConnectConnections.insert(info->thing(), homeConnectConnection);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
} else { } else {
homeConnectConnection->deleteLater();
info->finish(Thing::ThingErrorHardwareFailure); info->finish(Thing::ThingErrorHardwareFailure);
} }
} else { } else {
@ -267,3 +307,67 @@ void IntegrationPluginHomeConnect::onRequestExecuted(QUuid requestId, bool succe
} }
} }
} }
void IntegrationPluginHomeConnect::onReceivedHomeAppliances(QList<HomeConnect::HomeAppliance> appliances)
{
HomeConnect *homeConnectConnection = static_cast<HomeConnect *>(sender());
Thing *parentThing = m_homeConnectConnections.key(homeConnectConnection);
if (!parentThing)
return;
ThingDescriptors desciptors;
Q_FOREACH(HomeConnect::HomeAppliance appliance, appliances) {
ThingClassId thingClassId;
/*Oven,
Dishwasher,
Washer,
Dryer,
WasherDryer,
FridgeFreezer,
Refrigerator,
Freezer,
WineCooler,
CoffeeMaker,
Hood,
CleaningRobot,
CookProcessor*/
if (appliance.type.contains("Oven", Qt::CaseInsensitive)) {
thingClassId = ovenThingClassId;
} else if (appliance.type.contains("Dishwasher", Qt::CaseInsensitive)) {
thingClassId = dishwasherThingClassId;
} else if (appliance.type.contains("Washer", Qt::CaseInsensitive)) {
thingClassId = washerThingClassId;
} else if (appliance.type.contains("FidgeFreezer", Qt::CaseInsensitive)) {
thingClassId = fridgeThingClassId;
} else if (appliance.type.contains("Refrigerator", Qt::CaseInsensitive)) {
thingClassId = fridgeThingClassId;
} else if (appliance.type.contains("Freezer", Qt::CaseInsensitive)) {
thingClassId = fridgeThingClassId;
} else if (appliance.type.contains("WineCooler", Qt::CaseInsensitive)) {
thingClassId = fridgeThingClassId;
} else if (appliance.type.contains("CoffeMaker", Qt::CaseInsensitive)) {
thingClassId = coffeMakerThingClassId;
} else if (appliance.type.contains("Dryer", Qt::CaseInsensitive)) {
thingClassId = dryerThingClassId;
} else {
qCWarning(dcHomeConnect()) << "Unknown thing type" << appliance.type;
continue;
}
if (!myThings().filterByParam(m_idParamTypeIds.value(thingClassId), appliance.homeApplianceId).isEmpty()) {
Thing * existingThing = myThings().filterByParam(m_idParamTypeIds.value(thingClassId), appliance.homeApplianceId).first();
existingThing->setStateValue(m_connectedStateTypeIds.value(thingClassId), appliance.connected);
continue;
}
ThingDescriptor descriptor(thingClassId, appliance.name, appliance.brand+" "+appliance.vib, parentThing->id());
ParamList params;
params << Param(m_idParamTypeIds.value(thingClassId), appliance.homeApplianceId);
descriptor.setParams(params);
desciptors.append(descriptor);
}
if (!desciptors.isEmpty())
emit autoThingsAppeared(desciptors);
}

View File

@ -46,9 +46,6 @@ class IntegrationPluginHomeConnect : public IntegrationPlugin
public: public:
explicit IntegrationPluginHomeConnect(); explicit IntegrationPluginHomeConnect();
~IntegrationPluginHomeConnect() override;
void discoverThings(ThingDiscoveryInfo *info) override;
void startPairing(ThingPairingInfo *info) override; void startPairing(ThingPairingInfo *info) override;
void confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret) override; void confirmPairing(ThingPairingInfo *info, const QString &username, const QString &secret) override;
@ -69,10 +66,15 @@ private:
QHash<QUuid, ThingActionInfo *> m_pendingActions; QHash<QUuid, ThingActionInfo *> m_pendingActions;
QHash<ThingClassId, ParamTypeId> m_idParamTypeIds;
QHash<ThingClassId, StateTypeId> m_connectedStateTypeIds;
HomeConnect *createHomeConnection();
private slots: private slots:
void onConnectionChanged(bool connected); void onConnectionChanged(bool connected);
void onAuthenticationStatusChanged(bool authenticated); void onAuthenticationStatusChanged(bool authenticated);
void onRequestExecuted(QUuid requestId, bool success); void onRequestExecuted(QUuid requestId, bool success);
void onReceivedHomeAppliances(QList<HomeConnect::HomeAppliance> appliances);
}; };
#endif // INTEGRATIONPLUGINHOMECONNECT_H #endif // INTEGRATIONPLUGINHOMECONNECT_H

View File

@ -2,6 +2,15 @@
"id": "109abdc7-5d53-4f63-a4b2-851e97cea8ea", "id": "109abdc7-5d53-4f63-a4b2-851e97cea8ea",
"name": "homeConnect", "name": "homeConnect",
"displayName": "Home Connect", "displayName": "Home Connect",
"paramTypes": [
{
"id": "82a1d671-4774-49df-97dc-ed89398c0dc9",
"name": "simulationMode",
"displayName": "Simulation mode",
"defaultValue": false,
"type": "bool"
}
],
"vendors": [ "vendors": [
{ {
"id": "43cfb7a4-402f-4315-86b5-ce095697fd13", "id": "43cfb7a4-402f-4315-86b5-ce095697fd13",
@ -52,6 +61,13 @@
"interfaces": ["connectable"], "interfaces": ["connectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{
"id": "661c1603-356e-4a78-baf4-7ea0bc9da316",
"name": "id",
"displayName": "ID",
"defaultValue": "-",
"type": "QString"
}
], ],
"stateTypes": [ "stateTypes": [
{ {
@ -72,6 +88,13 @@
"interfaces": ["connectable"], "interfaces": ["connectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{
"id": "f6b86d1b-481a-4496-975e-055f5ecc2bdb",
"name": "id",
"displayName": "ID",
"defaultValue": "-",
"type": "QString"
}
], ],
"stateTypes": [ "stateTypes": [
{ {
@ -87,11 +110,18 @@
}, },
{ {
"id": "f6b39ce2-8276-4db7-b2a3-4a04cafacbb9", "id": "f6b39ce2-8276-4db7-b2a3-4a04cafacbb9",
"name": "coffeMachine", "name": "coffeMaker",
"displayName": "Coffe Machine", "displayName": "Coffe Maker",
"interfaces": ["connectable"], "interfaces": ["connectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{
"id": "85d48203-e97b-45cc-a899-494c375389a5",
"name": "id",
"displayName": "ID",
"defaultValue": "-",
"type": "QString"
}
], ],
"stateTypes": [ "stateTypes": [
{ {
@ -112,6 +142,13 @@
"interfaces": ["connectable"], "interfaces": ["connectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{
"id": "cad8a1f9-6313-48dd-bb1d-b285006c760b",
"name": "id",
"displayName": "ID",
"defaultValue": "-",
"type": "QString"
}
], ],
"stateTypes": [ "stateTypes": [
{ {
@ -132,6 +169,13 @@
"interfaces": ["connectable"], "interfaces": ["connectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{
"id": "ba36e6a0-cb88-42d2-bdb9-9d7d106dec83",
"name": "id",
"displayName": "ID",
"defaultValue": "-",
"type": "QString"
}
], ],
"stateTypes": [ "stateTypes": [
{ {
@ -152,6 +196,13 @@
"interfaces": ["connectable"], "interfaces": ["connectable"],
"createMethods": ["auto"], "createMethods": ["auto"],
"paramTypes": [ "paramTypes": [
{
"id": "be4a1dcf-a0ce-44bb-a374-65f875e53c94",
"name": "id",
"displayName": "ID",
"defaultValue": "-",
"type": "QString"
}
], ],
"stateTypes": [ "stateTypes": [
{ {