From eaf4e87c7a8b38d0efc3198de7521a1b64f4b121 Mon Sep 17 00:00:00 2001 From: "bernhard.trinnes" Date: Thu, 23 Jul 2020 10:38:57 +0200 Subject: [PATCH] fixed event parsing --- homeconnect/homeconnect.cpp | 100 ++++++---- homeconnect/homeconnect.h | 1 + homeconnect/integrationpluginhomeconnect.cpp | 94 +++++----- homeconnect/integrationpluginhomeconnect.h | 5 +- homeconnect/integrationpluginhomeconnect.json | 174 ++++++++++++++++-- 5 files changed, 277 insertions(+), 97 deletions(-) diff --git a/homeconnect/homeconnect.cpp b/homeconnect/homeconnect.cpp index 358143ff..bd18ca81 100644 --- a/homeconnect/homeconnect.cpp +++ b/homeconnect/homeconnect.cpp @@ -485,6 +485,27 @@ QUuid HomeConnect::startProgram(const QString &haId, const QString &programKey, return commandId; } +QUuid HomeConnect::stopProgram(const QString &haId) +{ + QUuid commandId = QUuid::createUuid(); + QUrl url = QUrl(m_baseControlUrl+"/api/homeappliances/"+haId+"/programs/active"); + + QNetworkRequest request(url); + request.setRawHeader("Authorization", "Bearer "+m_accessToken); + request.setRawHeader("Accept-Language", "en-US"); + request.setRawHeader("accept", "application/vnd.bsh.sdk.v1+json"); + + QNetworkReply *reply = m_networkManager->deleteResource(request); + connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater); + connect(reply, &QNetworkReply::finished, this, [this, commandId, reply]{ + + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + QByteArray rawData = reply->readAll(); + emit commandExecuted(commandId, checkStatusCode(status, rawData)); + }); + return commandId; +} + void HomeConnect::getStatus(const QString &haid) { QUrl url = QUrl(m_baseControlUrl+"/api/homeappliances/"+haid+"/status"); @@ -566,19 +587,16 @@ void HomeConnect::connectEventStream() }); connect(reply, &QNetworkReply::readyRead, this, [this, reply]{ - int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - QByteArrayList rawData = reply->readAll().split('\n'); - QJsonDocument data; - QString haId; - EventType eventType; - Q_FOREACH(QByteArray line, rawData) { - if (line.startsWith("data")) { - if (checkStatusCode(status, line.remove(0,6))) - data = QJsonDocument::fromJson(line); - } else if (line.startsWith("id")) { - haId = line.split(':').last().trimmed(); - } else if (line.startsWith("event")) { - QString eventString = line.split(':').last().trimmed(); + while (reply->canReadLine()) { + QJsonDocument data; + QString haId; + EventType eventType; + + QByteArray eventTypeLine = reply->readLine(); + if (eventTypeLine == "\n") + continue; + if (eventTypeLine.startsWith("event")) { + QString eventString = eventTypeLine.split(':').last().trimmed(); if (eventString == "KEEP-ALIVE") { eventType = EventTypeKeepAlive; } else if (eventString == "STATUS") { @@ -599,27 +617,45 @@ void HomeConnect::connectEventStream() qCWarning(dcHomeConnect()) << "Unhandled event type" << eventString; return; } - } - } + QByteArray dataLine = reply->readLine(); + if (dataLine.startsWith("data")) { + data = QJsonDocument::fromJson(dataLine.remove(0,6)); - if (data.toVariant().toMap().contains("items")) { - QList events; - QVariantList itemsList = data.toVariant().toMap().value("items").toList(); - Q_FOREACH(QVariant item, itemsList) { - QVariantMap map = item.toMap(); - Event event; - event.key = map["key"].toString(); - event.uri = map["uri"].toString(); - event.name = map["uri"].toString(); - event.value = map["value"]; - event.unit = map["unit"].toString(); - event.timestamp = map["timestamp"].toInt(); - events.append(event); + QByteArray idLine = reply->readLine(); + if (idLine.startsWith("id")) { + haId = idLine.split(':').last().trimmed(); + } else { + qCWarning(dcHomeConnect()) << "Id line: Unexpected line" << eventTypeLine; + continue; + } + } else { + qCWarning(dcHomeConnect()) << "Data Line: Unexpected line" << eventTypeLine; + continue; + } + } else { + qCWarning(dcHomeConnect()) << "Event type: Unexpected line" << eventTypeLine; + continue; + } + + if (data.toVariant().toMap().contains("items")) { + QList events; + QVariantList itemsList = data.toVariant().toMap().value("items").toList(); + Q_FOREACH(QVariant item, itemsList) { + QVariantMap map = item.toMap(); + Event event; + event.key = map["key"].toString(); + event.uri = map["uri"].toString(); + event.name = map["uri"].toString(); + event.value = map["value"]; + event.unit = map["unit"].toString(); + event.timestamp = map["timestamp"].toInt(); + events.append(event); + } + if (!events.isEmpty()) + emit receivedEvents(eventType, haId, events); + } else if (data.toVariant().toMap().contains("error")) { + qCWarning(dcHomeConnect()) << "Event stream error" << data.toVariant().toMap().value("error"); } - if (!events.isEmpty()) - emit receivedEvents(eventType, haId, events); - } else if (data.toVariant().toMap().contains("error")) { - qCWarning(dcHomeConnect()) << "Event stream error" << data.toVariant().toMap().value("error"); } }); } diff --git a/homeconnect/homeconnect.h b/homeconnect/homeconnect.h index 922457cb..f81c9071 100644 --- a/homeconnect/homeconnect.h +++ b/homeconnect/homeconnect.h @@ -130,6 +130,7 @@ public: void getProgramsSelected(const QString &haId); //Get the program which is currently selected void getProgramsActiveOption(const QString &haId, const QString &optionKey); QUuid startProgram(const QString &haId, const QString &programKey, QList