diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 20c347f8..e638d5ab 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -216,7 +216,7 @@ int LedDevicePhilipsHue::switchOff() { } void LedDevicePhilipsHue::put(QString route, QString content) { - QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); + QString url = getUrl(route); // Perfrom request QNetworkRequest request(url); QNetworkReply* reply = manager->put(request, content.toLatin1()); @@ -228,7 +228,7 @@ void LedDevicePhilipsHue::put(QString route, QString content) { } QByteArray LedDevicePhilipsHue::get(QString route) { - QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); + QString url = getUrl(route); // Perfrom request QNetworkRequest request(url); QNetworkReply* reply = manager->get(request); @@ -249,6 +249,10 @@ QString LedDevicePhilipsHue::getRoute(unsigned int lightId) { return QString("lights/%1").arg(lightId); } +QString LedDevicePhilipsHue::getUrl(QString route) { + return QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route); +} + void LedDevicePhilipsHue::saveStates(unsigned int nLights) { // Clear saved lamps. lights.clear(); @@ -257,10 +261,12 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) { Json::FastWriter writer; // Read light ids if none have been supplied by the user. if (lightIds.size() != nLights) { + lightIds.clear(); + // QByteArray response = get("lights"); Json::Value json; if (!reader.parse(QString(response).toStdString(), json)) { - throw std::runtime_error("No lights found"); + throw std::runtime_error(("No lights found at " + getUrl("lights")).toStdString()); } // Loop over all children. for (Json::ValueIterator it = json.begin(); it != json.end() && lightIds.size() < nLights; it++) { @@ -269,6 +275,10 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) { std::cout << "LedDevicePhilipsHue::saveStates(nLights=" << nLights << "): found light with id " << lightId << "." << std::endl; } + // Check if we found enough lights. + if (lightIds.size() != nLights) { + throw std::runtime_error(("Not enough lights found at " + getUrl("lights")).toStdString()); + } } // Iterate lights. for (unsigned int i = 0; i < nLights; i++) { @@ -278,12 +288,22 @@ 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; + std::cerr << "LedDevicePhilipsHue::saveStates(nLights=" << nLights << "): got invalid response from light " + << getUrl(getRoute(lightIds.at(i))).toStdString() << "." << std::endl; break; } // Get state object values which are subject to change. Json::Value state(Json::objectValue); + if (!json.isMember("state")) { + std::cerr << "LedDevicePhilipsHue::saveStates(nLights=" << nLights << "): got no state for light from " + << getUrl(getRoute(lightIds.at(i))).toStdString() << std::endl; + break; + } + if (!json["state"].isMember("on")) { + std::cerr << "LedDevicePhilipsHue::saveStates(nLights=" << nLights << "): got no valid state from light " + << getUrl(getRoute(lightIds.at(i))).toStdString() << std::endl; + break; + } state["on"] = json["state"]["on"]; if (json["state"]["on"] == true) { state["xy"] = json["state"]["xy"]; diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index 61047062..5e5409b6 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -207,6 +207,13 @@ private: /// QString getRoute(unsigned int lightId); + /// + /// @param route + /// + /// @return the full URL of the request. + /// + QString getUrl(QString route); + /// /// Queries the status of all lights and saves it. ///