mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
Added more precise outputs on failures as well as some additional checks.
Former-commit-id: a0def586410d90abda002504d9c2ae88abc43088
This commit is contained in:
parent
d75a88eb9f
commit
926f47fc1a
@ -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"];
|
||||
|
@ -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.
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user