Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 8158c77

Browse files
committed
Fixes saveState feature
1 parent b1a175a commit 8158c77

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <QHttpRequestHeader>
1212
#include <QEventLoop>
1313

14-
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string &output) :
14+
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
1515
host(output.c_str()), username("newdeveloper") {
1616
http = new QHttp(host);
1717
timer.setInterval(3000);
@@ -30,23 +30,24 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
3030
// Iterate through colors and set light states.
3131
unsigned int lightId = 0;
3232
for (const ColorRgb &color : ledValues) {
33-
lightId++;
3433
// Send only request to the brigde if color changed (prevents DDOS --> 503)
3534
if (!oldLedValues.empty())
36-
if(!hasColorChanged(lightId, &color))
35+
if(!hasColorChanged(lightId, &color)) {
36+
lightId++;
3737
continue;
38+
}
3839

3940
float r = color.red / 255.0f;
4041
float g = color.green / 255.0f;
4142
float b = color.blue / 255.0f;
4243

4344
//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()) {
4546
Red = {0.675f, 0.322f};
4647
Green = {0.4091f, 0.518f};
4748
Blue = {0.167f, 0.04f};
4849
} else if (std::find(livingColors.begin(),
49-
livingColors.end(), modelIds[(lightId - 1)]) != livingColors.end()) {
50+
livingColors.end(), modelIds[lightId]) != livingColors.end()) {
5051
Red = {0.703f, 0.296f};
5152
Green = {0.214f, 0.709f};
5253
Blue = {0.139f, 0.081f};
@@ -58,10 +59,11 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
5859
// if color equal black, switch off lamp ...
5960
if (r == 0.0f && g == 0.0f && b == 0.0f) {
6061
switchLampOff(lightId);
62+
lightId++;
6163
continue;
6264
}
6365
// ... and if lamp off, switch on
64-
if (!checkOnStatus(states[(lightId - 1)]))
66+
if (!checkOnStatus(states[lightId]))
6567
switchLampOn(lightId);
6668

6769
float bri;
@@ -71,6 +73,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
7173
// Send adjust color and brightness command in JSON format.
7274
put(getStateRoute(lightId),
7375
QString("{\"xy\": [%1, %2], \"bri\": %3}").arg(p.x).arg(p.y).arg(qRound(b * 255.0f)));
76+
lightId++;
7477
}
7578
oldLedValues = ledValues;
7679
timer.start();
@@ -79,7 +82,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
7982

8083
bool LedDevicePhilipsHue::hasColorChanged(unsigned int lightId, const ColorRgb *color) {
8184
bool matchFound = true;
82-
const ColorRgb &tmpOldColor = oldLedValues[(lightId - 1)];
85+
const ColorRgb &tmpOldColor = oldLedValues[lightId];
8386
if ((*color).red == tmpOldColor.red)
8487
matchFound = false;
8588
if (!matchFound && (*color).green == tmpOldColor.green)
@@ -139,7 +142,7 @@ QByteArray LedDevicePhilipsHue::get(QString route) {
139142
}
140143

141144
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {
142-
return QString("lights/%1/state").arg(lightId);
145+
return QString("lights/%1/state").arg(lightId + 1);
143146
}
144147

145148
QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
@@ -178,18 +181,20 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
178181

179182
void LedDevicePhilipsHue::switchLampOn(unsigned int lightId) {
180183
put(getStateRoute(lightId), "{\"on\": true}");
181-
states[(lightId - 1)].replace("\"on\":false", "\"on\":true");
184+
states[lightId].replace("\"on\":false", "\"on\":true");
182185
}
183186

184187
void LedDevicePhilipsHue::switchLampOff(unsigned int lightId) {
185188
put(getStateRoute(lightId), "{\"on\": false}");
186-
states[(lightId - 1)].replace("\"on\":true", "\"on\":false");
189+
states[lightId].replace("\"on\":true", "\"on\":false");
187190
}
188191

189192
void LedDevicePhilipsHue::restoreStates() {
190-
unsigned int lightId = 1;
193+
unsigned int lightId = 0;
191194
for (QString state : states) {
192-
put(getStateRoute(lightId), state);
195+
if (!checkOnStatus(states[lightId]))
196+
switchLampOn(lightId);
197+
put(getStateRoute(lightId), states[lightId]);
193198
lightId++;
194199
}
195200
// Clear saved light states.

0 commit comments

Comments
 (0)