first working netatmo version

This commit is contained in:
Simon Stürz 2015-10-20 18:43:58 +02:00 committed by Michael Zanetti
parent 5f66f0cbe3
commit 4f76a64dfd
5 changed files with 51 additions and 22 deletions

View File

@ -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;

View File

@ -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<DeviceDescriptor>() << 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<DeviceDescriptor>() << descriptor);
} else {
@ -266,16 +275,20 @@ void DevicePluginNetatmo::onAuthenticationChanged()
OAuth2 *authentication = static_cast<OAuth2 *>(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();
}
}
}

View File

@ -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

View File

@ -22,11 +22,12 @@
#include <QVariantMap>
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;

View File

@ -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;