From 4f76a64dfd05d305cf62a9c33e6cba56dd1bdd8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 20 Oct 2015 18:43:58 +0200 Subject: [PATCH] first working netatmo version --- libguh/network/oauth2.cpp | 1 + .../netatmo/devicepluginnetatmo.cpp | 47 ++++++++++++------- .../netatmo/devicepluginnetatmo.json | 11 ++++- .../netatmo/netatmooutdoormodule.cpp | 10 +++- .../netatmo/netatmooutdoormodule.h | 4 +- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/libguh/network/oauth2.cpp b/libguh/network/oauth2.cpp index dca3a241..8feb976c 100644 --- a/libguh/network/oauth2.cpp +++ b/libguh/network/oauth2.cpp @@ -143,6 +143,7 @@ void OAuth2::setAuthenticated(const bool &authenticated) if (authenticated) { qCDebug(dcOAuth2) << "Authenticated successfully" << m_username; } else { + m_timer->stop(); qCWarning(dcOAuth2) << "Authentication failed" << m_username; } m_authenticated = authenticated; diff --git a/plugins/deviceplugins/netatmo/devicepluginnetatmo.cpp b/plugins/deviceplugins/netatmo/devicepluginnetatmo.cpp index bdf6ad1a..246f2d35 100644 --- a/plugins/deviceplugins/netatmo/devicepluginnetatmo.cpp +++ b/plugins/deviceplugins/netatmo/devicepluginnetatmo.cpp @@ -82,8 +82,9 @@ DeviceManager::DeviceSetupStatus DevicePluginNetatmo::setupDevice(Device *device qCDebug(dcNetatmo) << "Setup netatmo indoor base station" << device->params(); NetatmoBaseStation *indoor = new NetatmoBaseStation(device->paramValue("name").toString(), device->paramValue("mac address").toString(), - device->paramValue("connection id").toString(), this); + device->paramValue("connection").toString(), this); + device->setParentId(DeviceId(indoor->connectionId())); m_indoorDevices.insert(indoor, device); connect(indoor, SIGNAL(statesChanged()), this, SLOT(onIndoorStatesChanged())); @@ -91,9 +92,11 @@ DeviceManager::DeviceSetupStatus DevicePluginNetatmo::setupDevice(Device *device } else if (device->deviceClassId() == outdoorDeviceClassId) { qCDebug(dcNetatmo) << "Setup netatmo outdoor module" << device->params(); NetatmoOutdoorModule *outdoor = new NetatmoOutdoorModule(device->paramValue("name").toString(), - device->paramValue("mac address").toString(), - device->paramValue("connection id").toString(), this); + device->paramValue("mac address").toString(), + device->paramValue("connection").toString(), + device->paramValue("base station").toString(),this); + device->setParentId(DeviceId(outdoor->connectionId())); m_outdoorDevices.insert(outdoor, device); connect(outdoor, SIGNAL(statesChanged()), this, SLOT(onOutdoorStatesChanged())); @@ -108,6 +111,14 @@ void DevicePluginNetatmo::deviceRemoved(Device *device) OAuth2 * authentication = m_authentications.key(device); m_authentications.remove(authentication); authentication->deleteLater(); + } else if (device->deviceClassId() == indoorDeviceClassId) { + NetatmoBaseStation *indoor = m_indoorDevices.key(device); + m_indoorDevices.remove(indoor); + indoor->deleteLater(); + } else if (device->deviceClassId() == outdoorDeviceClassId) { + NetatmoOutdoorModule *outdoor = m_outdoorDevices.key(device); + m_outdoorDevices.remove(outdoor); + outdoor->deleteLater(); } } @@ -122,7 +133,7 @@ void DevicePluginNetatmo::networkManagerReplyReady(QNetworkReply *reply) // check HTTP status code if (status != 200) { qCWarning(dcNetatmo) << "Device list reply HTTP error:" << status << reply->errorString(); - emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure); + device->setStateValue(availableStateTypeId, false); reply->deleteLater(); return; } @@ -132,7 +143,6 @@ void DevicePluginNetatmo::networkManagerReplyReady(QNetworkReply *reply) QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &error); if (error.error != QJsonParseError::NoError) { qCWarning(dcNetatmo) << "Device list reply JSON error:" << error.errorString(); - emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure); reply->deleteLater(); return; } @@ -146,13 +156,11 @@ void DevicePluginNetatmo::networkManagerReplyReady(QNetworkReply *reply) void DevicePluginNetatmo::guhTimer() { - foreach (Device *device, myDevices()) { - if (device->deviceClassId() == connectionDeviceClassId) { - OAuth2 *authentication = m_authentications.key(device); - // TODO: check if authenticated - if (authentication->authenticated()) { - refreshData(device, authentication->token()); - } + foreach (OAuth2 *authentication, m_authentications.keys()) { + if (authentication->authenticated()) { + refreshData(m_authentications.value(authentication), authentication->token()); + } else { + authentication->startAuthentication(); } } } @@ -196,7 +204,7 @@ void DevicePluginNetatmo::processRefreshData(const QVariantMap &data, const QStr ParamList params; params.append(Param("name", deviceMap.value("station_name").toString())); params.append(Param("mac address", deviceMap.value("_id").toString())); - params.append(Param("connection id", connectionId)); + params.append(Param("connection", connectionId)); descriptor.setParams(params); emit autoDevicesAppeared(indoorDeviceClassId, QList() << descriptor); } else { @@ -223,7 +231,8 @@ void DevicePluginNetatmo::processRefreshData(const QVariantMap &data, const QStr ParamList params; params.append(Param("name", moduleMap.value("module_name").toString())); params.append(Param("mac address", moduleMap.value("_id").toString())); - params.append(Param("connection id", connectionId)); + params.append(Param("connection", connectionId)); + params.append(Param("base station", moduleMap.value("main_device").toString())); descriptor.setParams(params); emit autoDevicesAppeared(outdoorDeviceClassId, QList() << descriptor); } else { @@ -266,16 +275,20 @@ void DevicePluginNetatmo::onAuthenticationChanged() OAuth2 *authentication = static_cast(sender()); Device *device = m_authentications.value(authentication); + if (!device) + return; + + // set the available state + device->setStateValue(availableStateTypeId, authentication->authenticated()); + // check if this is was a setup athentication if (m_asyncSetups.contains(device)) { - m_asyncSetups.removeAll(device); if (authentication->authenticated()) { + m_asyncSetups.removeAll(device); emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess); refreshData(device, authentication->token()); } else { emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure); - m_authentications.remove(authentication); - authentication->deleteLater(); } } } diff --git a/plugins/deviceplugins/netatmo/devicepluginnetatmo.json b/plugins/deviceplugins/netatmo/devicepluginnetatmo.json index 7201aa4b..965489ae 100644 --- a/plugins/deviceplugins/netatmo/devicepluginnetatmo.json +++ b/plugins/deviceplugins/netatmo/devicepluginnetatmo.json @@ -59,7 +59,7 @@ "readOnly": true }, { - "name": "connection id", + "name": "connection", "type": "QString", "inputType": "TextLine", "readOnly": true @@ -158,8 +158,15 @@ "inputType": "TextLine", "readOnly": true }, + { - "name": "basestation id", + "name": "connection", + "type": "QString", + "inputType": "TextLine", + "readOnly": true + }, + { + "name": "base station", "type": "QString", "inputType": "TextLine", "readOnly": true diff --git a/plugins/deviceplugins/netatmo/netatmooutdoormodule.cpp b/plugins/deviceplugins/netatmo/netatmooutdoormodule.cpp index 70fd7882..256357d9 100644 --- a/plugins/deviceplugins/netatmo/netatmooutdoormodule.cpp +++ b/plugins/deviceplugins/netatmo/netatmooutdoormodule.cpp @@ -22,11 +22,12 @@ #include -NetatmoOutdoorModule::NetatmoOutdoorModule(const QString &name, const QString &macAddress, const QString &connectionId, QObject *parent) : +NetatmoOutdoorModule::NetatmoOutdoorModule(const QString &name, const QString &macAddress, const QString &connectionId, const QString &baseStation, QObject *parent) : QObject(parent), m_name(name), m_macAddress(macAddress), - m_connectionId(connectionId) + m_connectionId(connectionId), + m_baseStation(baseStation) { } @@ -45,6 +46,11 @@ QString NetatmoOutdoorModule::connectionId() const return m_connectionId; } +QString NetatmoOutdoorModule::baseStation() const +{ + return m_baseStation; +} + int NetatmoOutdoorModule::lastUpdate() const { return m_lastUpdate; diff --git a/plugins/deviceplugins/netatmo/netatmooutdoormodule.h b/plugins/deviceplugins/netatmo/netatmooutdoormodule.h index 18b3b981..31dcc9b4 100644 --- a/plugins/deviceplugins/netatmo/netatmooutdoormodule.h +++ b/plugins/deviceplugins/netatmo/netatmooutdoormodule.h @@ -27,12 +27,13 @@ class NetatmoOutdoorModule : public QObject { Q_OBJECT public: - explicit NetatmoOutdoorModule(const QString &name, const QString &macAddress, const QString &connectionId, QObject *parent = 0); + explicit NetatmoOutdoorModule(const QString &name, const QString &macAddress, const QString &connectionId, const QString &baseStation, QObject *parent = 0); // Params QString name() const; QString macAddress() const; QString connectionId() const; + QString baseStation() const; // States int lastUpdate() const; @@ -50,6 +51,7 @@ private: QString m_name; QString m_macAddress; QString m_connectionId; + QString m_baseStation; // States int m_lastUpdate;