Connected to events stream and emit deviceStateReceived when a new state

is identified in the events.
This commit is contained in:
Paul Bot 2021-09-28 11:40:01 +03:00
parent adbd78796a
commit 6d3d8f243e
3 changed files with 23 additions and 8 deletions

View File

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

View File

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

View File

@ -128,7 +128,7 @@ public:
QUuid setStartTime(const QString &deviceId, int seconds);
// EVENTS
void getAllEvents();
void connectEventStream();
private:
QString m_language;
QByteArray m_clientId;