diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 690a7b94..2cedc8a5 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -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()); } }