mirror of
https://github.com/nymea/nymea-plugins.git
synced 2026-08-02 03:07:33 +02:00
Connected to events stream and emit deviceStateReceived when a new state
is identified in the events.
This commit is contained in:
parent
adbd78796a
commit
6d3d8f243e
@ -204,7 +204,7 @@ void IntegrationPluginMiele::postSetupThing(Thing *thing)
|
|||||||
}
|
}
|
||||||
miele->getDevicesShort();
|
miele->getDevicesShort();
|
||||||
Q_FOREACH (Thing *childThing, myThings().filterByParentId(thing->id())) {
|
Q_FOREACH (Thing *childThing, myThings().filterByParentId(thing->id())) {
|
||||||
QString deviceId = childThing->paramValue(m_idParamTypeIds.value(childThing->thingClassId())).toString();
|
QString deviceId = childThing->paramValue(m_idParamTypeIds.value(childThing->thingClassId())).toString();
|
||||||
miele->getDeviceState(deviceId);
|
miele->getDeviceState(deviceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -214,9 +214,9 @@ void IntegrationPluginMiele::postSetupThing(Thing *thing)
|
|||||||
if (thing->thingClassId() == mieleAccountThingClassId) {
|
if (thing->thingClassId() == mieleAccountThingClassId) {
|
||||||
Miele *miele = m_mieleConnections.value(thing);
|
Miele *miele = m_mieleConnections.value(thing);
|
||||||
miele->getDevicesShort();
|
miele->getDevicesShort();
|
||||||
//miele->connectEventStream();
|
miele->connectEventStream();
|
||||||
thing->setStateValue(mieleAccountConnectedStateTypeId, true);
|
thing->setStateValue(mieleAccountConnectedStateTypeId, true);
|
||||||
thing->setStateValue(mieleAccountLoggedInStateTypeId, true);
|
thing->setStateValue(mieleAccountLoggedInStateTypeId, true);
|
||||||
} else if (m_idParamTypeIds.contains(thing->thingClassId())) {
|
} else if (m_idParamTypeIds.contains(thing->thingClassId())) {
|
||||||
Thing *parentThing = myThings().findById(thing->parentId());
|
Thing *parentThing = myThings().findById(thing->parentId());
|
||||||
if (!parentThing)
|
if (!parentThing)
|
||||||
|
|||||||
@ -423,24 +423,39 @@ QUuid Miele::setStartTime(const QString &deviceId, int seconds)
|
|||||||
return putAction(deviceId, doc);
|
return putAction(deviceId, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Miele::getAllEvents()
|
void Miele::connectEventStream()
|
||||||
{
|
{
|
||||||
QUrl url = m_apiUrl;
|
QUrl url = m_apiUrl;
|
||||||
url.setPath("/v1/devices/all/events");
|
url.setPath("/v1/devices/all/events");
|
||||||
|
|
||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
request.setRawHeader("Authorization", "Bearer "+m_accessToken);
|
request.setRawHeader("Authorization", "Bearer "+m_accessToken);
|
||||||
request.setRawHeader("accept", "*/*");
|
request.setRawHeader("accept", "text/event-stream");
|
||||||
|
|
||||||
QNetworkReply *reply = m_networkManager->get(request);
|
QNetworkReply *reply = m_networkManager->get(request);
|
||||||
connect(reply, &QNetworkReply::finished, [reply, this] {
|
connect(reply, &QNetworkReply::finished, [reply, this] {
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
QTimer::singleShot(5000, this, [this] {getAllEvents();}); //try to reconnect every 5 seconds
|
QTimer::singleShot(5000, this, [this] {connectEventStream();}); //try to reconnect every 5 seconds
|
||||||
});
|
});
|
||||||
connect(reply, &QNetworkReply::readyRead, this, [this, reply]{
|
connect(reply, &QNetworkReply::readyRead, this, [this, reply]{
|
||||||
|
|
||||||
while (reply->canReadLine()) {
|
while (reply->canReadLine()) {
|
||||||
|
QByteArray rawData = reply->readAll();
|
||||||
|
qCDebug(dcMiele()) << "Raw events data: " << rawData;
|
||||||
|
QJsonDocument data = QJsonDocument::fromJson(rawData);
|
||||||
|
qCDebug(dcMiele()) << "Got events data: " << data.toJson();
|
||||||
|
QVariantMap eventsData = data.toVariant().toMap();
|
||||||
|
foreach (QVariant eventEntry, eventsData.values()) {
|
||||||
|
QVariantMap eventEntryMap = eventEntry.toMap();
|
||||||
|
if (!eventEntryMap.contains("ident")) {
|
||||||
|
qCWarning(dcMiele()) << "Got event but no device ident available";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QVariantMap deviceIdent = eventEntryMap.value("ident").toMap().value("deviceIdentLabel").toMap();
|
||||||
|
QString deviceId = deviceIdent.value("fabNumber").toString();
|
||||||
|
QVariantMap state = eventEntryMap.value("state").toMap();
|
||||||
|
emit deviceStateReceived(deviceId, state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -128,7 +128,7 @@ public:
|
|||||||
QUuid setStartTime(const QString &deviceId, int seconds);
|
QUuid setStartTime(const QString &deviceId, int seconds);
|
||||||
|
|
||||||
// EVENTS
|
// EVENTS
|
||||||
void getAllEvents();
|
void connectEventStream();
|
||||||
private:
|
private:
|
||||||
QString m_language;
|
QString m_language;
|
||||||
QByteArray m_clientId;
|
QByteArray m_clientId;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user