diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index ee019e18..18c167dc 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -5,7 +5,7 @@ #include // qt includes -#include +#include #include #include @@ -20,33 +20,33 @@ bool operator !=(CiColor p1, CiColor p2) { } PhilipsHueLight::PhilipsHueLight(unsigned int id, QString originalState, QString modelId) : - id(id), originalState(originalState) { - // Hue system model ids (http://www.developers.meethue.com/documentation/supported-lights). - // Light strips, color iris, ... - const std::set GAMUT_A_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC010", "LLC011", "LLC012", - "LLC013", "LLC014", "LST001" }; - // Hue bulbs, spots, ... - const std::set GAMUT_B_MODEL_IDS = { "LCT001", "LCT002", "LCT003", "LCT007", "LLM001" }; - // Hue Lightstrip plus, go ... - const std::set GAMUT_C_MODEL_IDS = { "LLC020", "LST002" }; - // Find id in the sets and set the appropiate color space. - if (GAMUT_A_MODEL_IDS.find(modelId) != GAMUT_A_MODEL_IDS.end()) { - colorSpace.red = {0.703f, 0.296f}; - colorSpace.green = {0.2151f, 0.7106f}; - colorSpace.blue = {0.138f, 0.08f}; - } else if (GAMUT_B_MODEL_IDS.find(modelId) != GAMUT_B_MODEL_IDS.end()) { - colorSpace.red = {0.675f, 0.322f}; - colorSpace.green = {0.4091f, 0.518f}; - colorSpace.blue = {0.167f, 0.04f}; - } else if (GAMUT_C_MODEL_IDS.find(modelId) != GAMUT_B_MODEL_IDS.end()) { - colorSpace.red = {0.675f, 0.322f}; - colorSpace.green = {0.2151f, 0.7106f}; - colorSpace.blue = {0.167f, 0.04f}; - } else { - colorSpace.red = {1.0f, 0.0f}; - colorSpace.green = {0.0f, 1.0f}; - colorSpace.blue = {0.0f, 0.0f}; - } + id(id), originalState(originalState) { + // Hue system model ids (http://www.developers.meethue.com/documentation/supported-lights). + // Light strips, color iris, ... + const std::set GAMUT_A_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC010", "LLC011", "LLC012", + "LLC013", "LLC014", "LST001" }; + // Hue bulbs, spots, ... + const std::set GAMUT_B_MODEL_IDS = { "LCT001", "LCT002", "LCT003", "LCT007", "LLM001" }; + // Hue Lightstrip plus, go ... + const std::set GAMUT_C_MODEL_IDS = { "LLC020", "LST002" }; + // Find id in the sets and set the appropiate color space. + if (GAMUT_A_MODEL_IDS.find(modelId) != GAMUT_A_MODEL_IDS.end()) { + colorSpace.red = {0.703f, 0.296f}; + colorSpace.green = {0.2151f, 0.7106f}; + colorSpace.blue = {0.138f, 0.08f}; + } else if (GAMUT_B_MODEL_IDS.find(modelId) != GAMUT_B_MODEL_IDS.end()) { + colorSpace.red = {0.675f, 0.322f}; + colorSpace.green = {0.4091f, 0.518f}; + colorSpace.blue = {0.167f, 0.04f}; + } else if (GAMUT_C_MODEL_IDS.find(modelId) != GAMUT_B_MODEL_IDS.end()) { + colorSpace.red = {0.675f, 0.322f}; + colorSpace.green = {0.2151f, 0.7106f}; + colorSpace.blue = {0.167f, 0.04f}; + } else { + colorSpace.red = {1.0f, 0.0f}; + colorSpace.green = {0.0f, 1.0f}; + colorSpace.blue = {0.0f, 0.0f}; + } // Initialize black color. black = rgbToCiColor(0.0f, 0.0f, 0.0f); // Initialize color with black @@ -214,8 +214,8 @@ int LedDevicePhilipsHue::switchOff() { return 0; } -void LedDevicePhilipsHue::put(QString route, QString content) { - QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); +void LedDevicePhilipsHue::put(QString route, QString content) { + QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); // Perfrom request QNetworkRequest request(url); QNetworkReply* reply = manager->put(request, content.toLatin1()); @@ -226,8 +226,8 @@ void LedDevicePhilipsHue::put(QString route, QString content) { loop.exec(); } -QByteArray LedDevicePhilipsHue::get(QString route) { - QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); +QByteArray LedDevicePhilipsHue::get(QString route) { + QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); // Perfrom request QNetworkRequest request(url); QNetworkReply* reply = manager->get(request); @@ -236,8 +236,8 @@ QByteArray LedDevicePhilipsHue::get(QString route) { loop.connect(reply, SIGNAL(finished()), SLOT(quit())); // Go into the loop until the request is finished. loop.exec(); - // Read all data of the response. - return reply->readAll(); + // Read all data of the response. + return reply->readAll(); } QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) { @@ -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.