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

Commit 601616b

Browse files
author
Tim Niggemann
committed
Added light ids to device config.
1 parent 1b6a204 commit 601616b

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
196196
const std::string username = deviceConfig.get("username", "newdeveloper").asString();
197197
const bool switchOffOnBlack = deviceConfig.get("switchOffOnBlack", true).asBool();
198198
const int transitiontime = deviceConfig.get("transitiontime", 1).asInt();
199-
device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime);
199+
std::vector<unsigned int> lightIds;
200+
for (size_t i = 0; i < deviceConfig["lightIds"].size(); i++) {
201+
lightIds.push_back(deviceConfig["lightIds"][i].asInt());
202+
}
203+
device = new LedDevicePhilipsHue(output, username, switchOffOnBlack, transitiontime, lightIds);
200204
}
201205
else if (type == "test")
202206
{

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ CiColor PhilipsHueLamp::rgbToCiColor(float red, float green, float blue) {
133133
}
134134

135135
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::string& username, bool switchOffOnBlack,
136-
int transitiontime) :
136+
int transitiontime, std::vector<unsigned int> lightIds) :
137137
host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
138-
transitiontime) {
138+
transitiontime), lightIds(lightIds) {
139139
http = new QHttp(host);
140140
timer.setInterval(3000);
141141
timer.setSingleShot(true);
@@ -251,10 +251,17 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
251251
// Use json parser to parse reponse.
252252
Json::Reader reader;
253253
Json::FastWriter writer;
254+
// Create light ids if none supplied by the user.
255+
if (lightIds.size() != nLights) {
256+
lightIds.clear();
257+
for (unsigned int i = 0; i < nLights; i++) {
258+
lightIds.push_back(i + 1);
259+
}
260+
}
254261
// Iterate lights.
255262
for (unsigned int i = 0; i < nLights; i++) {
256263
// Read the response.
257-
QByteArray response = get(getRoute(i + 1));
264+
QByteArray response = get(getRoute(lightIds.at(i)));
258265
// Parse JSON.
259266
Json::Value json;
260267
if (!reader.parse(QString(response).toStdString(), json)) {
@@ -272,7 +279,7 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
272279
QString modelId = QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", "");
273280
QString originalState = QString(writer.write(state).c_str()).trimmed();
274281
// Save state object.
275-
lamps.push_back(PhilipsHueLamp(i + 1, originalState, modelId));
282+
lamps.push_back(PhilipsHueLamp(lightIds.at(i), originalState, modelId));
276283
}
277284
}
278285

libsrc/leddevice/LedDevicePhilipsHue.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ Q_OBJECT
131131
///
132132
/// @param transitiontime the time duration a light change takes in multiples of 100 ms (default: 400 ms).
133133
///
134+
/// @param lightIds light ids of the lights to control if not starting at one in ascending order.
135+
///
134136
LedDevicePhilipsHue(const std::string& output, const std::string& username = "newdeveloper", bool switchOffOnBlack =
135-
false, int transitiontime = 1);
137+
false, int transitiontime = 1, std::vector<unsigned int> lightIds = {});
136138

137139
///
138140
/// Destructor of this device
@@ -171,6 +173,8 @@ private slots:
171173
/// Transition time in multiples of 100 ms.
172174
/// The default of the Hue lights will be 400 ms, but we want to have it snapier
173175
int transitiontime;
176+
/// Array of the light ids.
177+
std::vector<unsigned int> lightIds;
174178

175179
///
176180
/// Sends a HTTP GET request (blocking).

0 commit comments

Comments
 (0)