Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 5887fdf

Browse files
committed
Blocking PUT requests. Only save the brightness and [x,y] state of the lights.
1 parent 1ee26e8 commit 5887fdf

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,15 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
5454
QHttpRequestHeader header("PUT", url);
5555
header.setValue("Host", host);
5656
header.setValue("Accept-Encoding", "identity");
57+
header.setValue("Connection", "keep-alive");
5758
header.setValue("Content-Length", QString("%1").arg(content.size()));
58-
http->setHost(host);
59+
QEventLoop loop;
60+
// Connect requestFinished signal to quit slot of the loop.
61+
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
62+
// Perfrom request
5963
http->request(header, content.toAscii());
64+
// Go into the loop until the request is finished.
65+
loop.exec();
6066
}
6167

6268
QByteArray LedDevicePhilipsHue::get(QString route) {
@@ -92,13 +98,17 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
9298
// Read the response.
9399
QByteArray response = get(getRoute(i + 1));
94100
// Parse JSON.
95-
Json::Value state;
96-
if (!reader.parse(QString(response).toStdString(), state)) {
101+
Json::Value json;
102+
if (!reader.parse(QString(response).toStdString(), json)) {
97103
// Error occured, break loop.
98104
break;
99105
}
106+
// Save state object values which are subject to change.
107+
Json::Value state(Json::objectValue);
108+
state["xy"] = json["state"]["xy"];
109+
state["bri"] = json["state"]["bri"];
100110
// Save state object.
101-
states.push_back(QString(writer.write(state["state"]).c_str()));
111+
states.push_back(QString(writer.write(state).c_str()).trimmed());
102112
}
103113
}
104114

0 commit comments

Comments
 (0)