11
11
#include < QHttpRequestHeader>
12
12
#include < QEventLoop>
13
13
14
- LedDevicePhilipsHue::LedDevicePhilipsHue (const std::string & output) :
14
+ LedDevicePhilipsHue::LedDevicePhilipsHue (const std::string& output) :
15
15
host(output.c_str()), username(" newdeveloper" ) {
16
16
http = new QHttp (host);
17
17
timer.setInterval (3000 );
@@ -30,23 +30,24 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
30
30
// Iterate through colors and set light states.
31
31
unsigned int lightId = 0 ;
32
32
for (const ColorRgb &color : ledValues) {
33
- lightId++;
34
33
// Send only request to the brigde if color changed (prevents DDOS --> 503)
35
34
if (!oldLedValues.empty ())
36
- if (!hasColorChanged (lightId, &color))
35
+ if (!hasColorChanged (lightId, &color)) {
36
+ lightId++;
37
37
continue ;
38
+ }
38
39
39
40
float r = color.red / 255 .0f ;
40
41
float g = color.green / 255 .0f ;
41
42
float b = color.blue / 255 .0f ;
42
43
43
44
// set color gamut triangle
44
- if (std::find (hueBulbs.begin (), hueBulbs.end (), modelIds[( lightId - 1 ) ]) != hueBulbs.end ()) {
45
+ if (std::find (hueBulbs.begin (), hueBulbs.end (), modelIds[lightId]) != hueBulbs.end ()) {
45
46
Red = {0 .675f , 0 .322f };
46
47
Green = {0 .4091f , 0 .518f };
47
48
Blue = {0 .167f , 0 .04f };
48
49
} else if (std::find (livingColors.begin (),
49
- livingColors.end (), modelIds[( lightId - 1 ) ]) != livingColors.end ()) {
50
+ livingColors.end (), modelIds[lightId]) != livingColors.end ()) {
50
51
Red = {0 .703f , 0 .296f };
51
52
Green = {0 .214f , 0 .709f };
52
53
Blue = {0 .139f , 0 .081f };
@@ -58,10 +59,11 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
58
59
// if color equal black, switch off lamp ...
59
60
if (r == 0 .0f && g == 0 .0f && b == 0 .0f ) {
60
61
switchLampOff (lightId);
62
+ lightId++;
61
63
continue ;
62
64
}
63
65
// ... and if lamp off, switch on
64
- if (!checkOnStatus (states[( lightId - 1 ) ]))
66
+ if (!checkOnStatus (states[lightId]))
65
67
switchLampOn (lightId);
66
68
67
69
float bri;
@@ -71,6 +73,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
71
73
// Send adjust color and brightness command in JSON format.
72
74
put (getStateRoute (lightId),
73
75
QString (" {\" xy\" : [%1, %2], \" bri\" : %3}" ).arg (p.x ).arg (p.y ).arg (qRound (b * 255 .0f )));
76
+ lightId++;
74
77
}
75
78
oldLedValues = ledValues;
76
79
timer.start ();
@@ -79,7 +82,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
79
82
80
83
bool LedDevicePhilipsHue::hasColorChanged (unsigned int lightId, const ColorRgb *color) {
81
84
bool matchFound = true ;
82
- const ColorRgb &tmpOldColor = oldLedValues[( lightId - 1 ) ];
85
+ const ColorRgb &tmpOldColor = oldLedValues[lightId];
83
86
if ((*color).red == tmpOldColor.red )
84
87
matchFound = false ;
85
88
if (!matchFound && (*color).green == tmpOldColor.green )
@@ -139,7 +142,7 @@ QByteArray LedDevicePhilipsHue::get(QString route) {
139
142
}
140
143
141
144
QString LedDevicePhilipsHue::getStateRoute (unsigned int lightId) {
142
- return QString (" lights/%1/state" ).arg (lightId);
145
+ return QString (" lights/%1/state" ).arg (lightId + 1 );
143
146
}
144
147
145
148
QString LedDevicePhilipsHue::getRoute (unsigned int lightId) {
@@ -178,18 +181,20 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
178
181
179
182
void LedDevicePhilipsHue::switchLampOn (unsigned int lightId) {
180
183
put (getStateRoute (lightId), " {\" on\" : true}" );
181
- states[( lightId - 1 ) ].replace (" \" on\" :false" , " \" on\" :true" );
184
+ states[lightId].replace (" \" on\" :false" , " \" on\" :true" );
182
185
}
183
186
184
187
void LedDevicePhilipsHue::switchLampOff (unsigned int lightId) {
185
188
put (getStateRoute (lightId), " {\" on\" : false}" );
186
- states[( lightId - 1 ) ].replace (" \" on\" :true" , " \" on\" :false" );
189
+ states[lightId].replace (" \" on\" :true" , " \" on\" :false" );
187
190
}
188
191
189
192
void LedDevicePhilipsHue::restoreStates () {
190
- unsigned int lightId = 1 ;
193
+ unsigned int lightId = 0 ;
191
194
for (QString state : states) {
192
- put (getStateRoute (lightId), state);
195
+ if (!checkOnStatus (states[lightId]))
196
+ switchLampOn (lightId);
197
+ put (getStateRoute (lightId), states[lightId]);
193
198
lightId++;
194
199
}
195
200
// Clear saved light states.
0 commit comments