13
13
LedDevicePhilipsHue::LedDevicePhilipsHue (const std::string& output) :
14
14
host(output.c_str()), username(" newdeveloper" ) {
15
15
http = new QHttp (host);
16
+ timer.setInterval (3000 );
17
+ timer.setSingleShot (true );
18
+ connect (&timer, SIGNAL (timeout ()), this , SLOT (restoreStates ()));
16
19
}
17
20
18
21
LedDevicePhilipsHue::~LedDevicePhilipsHue () {
@@ -23,6 +26,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
23
26
// Save light states if not done before.
24
27
if (!statesSaved ()) {
25
28
saveStates (ledValues.size ());
29
+ switchOn (ledValues.size ());
26
30
}
27
31
// Iterate through colors and set light states.
28
32
unsigned int lightId = 1 ;
@@ -37,10 +41,12 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
37
41
// Next light id.
38
42
lightId++;
39
43
}
44
+ timer.start ();
40
45
return 0 ;
41
46
}
42
47
43
48
int LedDevicePhilipsHue::switchOff () {
49
+ timer.stop ();
44
50
// If light states have been saved before, ...
45
51
if (statesSaved ()) {
46
52
// ... restore them.
@@ -54,9 +60,15 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
54
60
QHttpRequestHeader header (" PUT" , url);
55
61
header.setValue (" Host" , host);
56
62
header.setValue (" Accept-Encoding" , " identity" );
63
+ header.setValue (" Connection" , " keep-alive" );
57
64
header.setValue (" Content-Length" , QString (" %1" ).arg (content.size ()));
58
- http->setHost (host);
65
+ QEventLoop loop;
66
+ // Connect requestFinished signal to quit slot of the loop.
67
+ loop.connect (http, SIGNAL (requestFinished (int , bool )), SLOT (quit ()));
68
+ // Perfrom request
59
69
http->request (header, content.toAscii ());
70
+ // Go into the loop until the request is finished.
71
+ loop.exec ();
60
72
}
61
73
62
74
QByteArray LedDevicePhilipsHue::get (QString route) {
@@ -92,13 +104,26 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
92
104
// Read the response.
93
105
QByteArray response = get (getRoute (i + 1 ));
94
106
// Parse JSON.
95
- Json::Value state ;
96
- if (!reader.parse (QString (response).toStdString (), state )) {
107
+ Json::Value json ;
108
+ if (!reader.parse (QString (response).toStdString (), json )) {
97
109
// Error occured, break loop.
98
110
break ;
99
111
}
112
+ // Save state object values which are subject to change.
113
+ Json::Value state (Json::objectValue);
114
+ state[" on" ] = json[" state" ][" on" ];
115
+ if (json[" state" ][" on" ] == true ) {
116
+ state[" xy" ] = json[" state" ][" xy" ];
117
+ state[" bri" ] = json[" state" ][" bri" ];
118
+ }
100
119
// Save state object.
101
- states.push_back (QString (writer.write (state[" state" ]).c_str ()));
120
+ states.push_back (QString (writer.write (state).c_str ()).trimmed ());
121
+ }
122
+ }
123
+
124
+ void LedDevicePhilipsHue::switchOn (unsigned int nLights) {
125
+ for (unsigned int i = 0 ; i < nLights; i++) {
126
+ put (getStateRoute (i + 1 ), " {\" on\" : true}" );
102
127
}
103
128
}
104
129
0 commit comments