@@ -54,9 +54,15 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
54
54
QHttpRequestHeader header (" PUT" , url);
55
55
header.setValue (" Host" , host);
56
56
header.setValue (" Accept-Encoding" , " identity" );
57
+ header.setValue (" Connection" , " keep-alive" );
57
58
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
59
63
http->request (header, content.toAscii ());
64
+ // Go into the loop until the request is finished.
65
+ loop.exec ();
60
66
}
61
67
62
68
QByteArray LedDevicePhilipsHue::get (QString route) {
@@ -92,13 +98,17 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
92
98
// Read the response.
93
99
QByteArray response = get (getRoute (i + 1 ));
94
100
// 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 )) {
97
103
// Error occured, break loop.
98
104
break ;
99
105
}
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" ];
100
110
// 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 ( ));
102
112
}
103
113
}
104
114
0 commit comments