Blocking PUT requests. Only save the brightness and [x,y] state of the lights.

Former-commit-id: 5887fdf1b350172dcdd52c46bd8da74dab521444
This commit is contained in:
ntim 2014-05-01 00:02:10 +02:00
parent ab5e17e105
commit 9220c346bb
1 changed files with 14 additions and 4 deletions

View File

@ -54,9 +54,15 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
QHttpRequestHeader header("PUT", url);
header.setValue("Host", host);
header.setValue("Accept-Encoding", "identity");
header.setValue("Connection", "keep-alive");
header.setValue("Content-Length", QString("%1").arg(content.size()));
http->setHost(host);
QEventLoop loop;
// Connect requestFinished signal to quit slot of the loop.
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
// Perfrom request
http->request(header, content.toAscii());
// Go into the loop until the request is finished.
loop.exec();
}
QByteArray LedDevicePhilipsHue::get(QString route) {
@ -92,13 +98,17 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
// Read the response.
QByteArray response = get(getRoute(i + 1));
// Parse JSON.
Json::Value state;
if (!reader.parse(QString(response).toStdString(), state)) {
Json::Value json;
if (!reader.parse(QString(response).toStdString(), json)) {
// Error occured, break loop.
break;
}
// Save state object values which are subject to change.
Json::Value state(Json::objectValue);
state["xy"] = json["state"]["xy"];
state["bri"] = json["state"]["bri"];
// Save state object.
states.push_back(QString(writer.write(state["state"]).c_str()));
states.push_back(QString(writer.write(state).c_str()).trimmed());
}
}