mirror of
https://github.com/nymea/nymea-plugins.git
synced 2026-07-18 00:43:48 +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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -214,9 +214,9 @@ void IntegrationPluginMiele::postSetupThing(Thing *thing)
|
||||
if (thing->thingClassId() == mieleAccountThingClassId) {
|
||||
Miele *miele = m_mieleConnections.value(thing);
|
||||
miele->getDevicesShort();
|
||||
//miele->connectEventStream();
|
||||
miele->connectEventStream();
|
||||
thing->setStateValue(mieleAccountConnectedStateTypeId, true);
|
||||
thing->setStateValue(mieleAccountLoggedInStateTypeId, true);
|
||||
thing->setStateValue(mieleAccountLoggedInStateTypeId, true);
|
||||
} else if (m_idParamTypeIds.contains(thing->thingClassId())) {
|
||||
Thing *parentThing = myThings().findById(thing->parentId());
|
||||
if (!parentThing)
|
||||
|
||||
@ -423,24 +423,39 @@ QUuid Miele::setStartTime(const QString &deviceId, int seconds)
|
||||
return putAction(deviceId, doc);
|
||||
}
|
||||
|
||||
void Miele::getAllEvents()
|
||||
void Miele::connectEventStream()
|
||||
{
|
||||
QUrl url = m_apiUrl;
|
||||
url.setPath("/v1/devices/all/events");
|
||||
|
||||
QNetworkRequest request(url);
|
||||
request.setRawHeader("Authorization", "Bearer "+m_accessToken);
|
||||
request.setRawHeader("accept", "*/*");
|
||||
request.setRawHeader("accept", "text/event-stream");
|
||||
|
||||
QNetworkReply *reply = m_networkManager->get(request);
|
||||
connect(reply, &QNetworkReply::finished, [reply, this] {
|
||||
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]{
|
||||
|
||||
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);
|
||||
|
||||
// EVENTS
|
||||
void getAllEvents();
|
||||
void connectEventStream();
|
||||
private:
|
||||
QString m_language;
|
||||
QByteArray m_clientId;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user