Reads hue light ids in case the user did not supply any.

Former-commit-id: 3323fe83e3bb5f42c4b6c98cbbb021fb929cf54d
This commit is contained in:
ntim 2016-03-10 09:33:16 +01:00
parent 14fc1d9bb6
commit 7849e9f23e

View File

@ -254,11 +254,19 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
// Use json parser to parse reponse.
Json::Reader reader;
Json::FastWriter writer;
// Create light ids if none supplied by the user.
// Read light ids if none have been supplied by the user.
if (lightIds.size() != nLights) {
lightIds.clear();
for (unsigned int i = 0; i < nLights; i++) {
lightIds.push_back(i + 1);
QByteArray response = get("lights");
Json::Value json;
if (!reader.parse(QString(response).toStdString(), json)) {
throw std::runtime_error("No lights found");
}
// Loop over all children.
for (Json::ValueIterator it = json.begin(); it != json.end() && lightIds.size() <= nLights; it++) {
int lightId = atoi(it.key().asCString());
lightIds.push_back(lightId);
std::cout << "LedDevicePhilipsHue::saveStates(nLights=" << nLights << "): found light with id " << lightId
<< "." << std::endl;
}
}
// Iterate lights.
@ -269,6 +277,8 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
Json::Value json;
if (!reader.parse(QString(response).toStdString(), json)) {
// Error occured, break loop.
std::cerr << "LedDevicePhilipsHue::saveStates(nLights=" << nLights
<< "): got invalid response from light with id " << lightIds.at(i) << "." << std::endl;
break;
}
// Get state object values which are subject to change.