2014-04-27 12:59:44 +02:00
|
|
|
// Local-Hyperion includes
|
|
|
|
#include "LedDevicePhilipsHue.h"
|
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
// jsoncpp includes
|
|
|
|
#include <json/json.h>
|
2014-04-27 12:59:44 +02:00
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
// qt includes
|
2014-04-27 12:59:44 +02:00
|
|
|
#include <QtCore/qmath.h>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QHttpRequestHeader>
|
2014-04-28 14:32:37 +02:00
|
|
|
#include <QEventLoop>
|
2014-04-27 12:59:44 +02:00
|
|
|
|
|
|
|
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
|
2014-04-28 14:32:37 +02:00
|
|
|
host(output.c_str()), username("newdeveloper") {
|
|
|
|
http = new QHttp(host);
|
2014-05-07 15:22:13 +02:00
|
|
|
timer.setInterval(3000);
|
2014-05-03 10:12:41 +02:00
|
|
|
timer.setSingleShot(true);
|
2014-05-07 15:22:13 +02:00
|
|
|
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
|
2014-04-27 12:59:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
|
|
|
|
delete http;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
|
2014-04-28 14:32:37 +02:00
|
|
|
// Save light states if not done before.
|
|
|
|
if (!statesSaved()) {
|
|
|
|
saveStates(ledValues.size());
|
2014-05-07 15:22:13 +02:00
|
|
|
switchOn(ledValues.size());
|
2014-04-28 14:32:37 +02:00
|
|
|
}
|
|
|
|
// Iterate through colors and set light states.
|
2014-04-27 12:59:44 +02:00
|
|
|
unsigned int lightId = 1;
|
|
|
|
for (const ColorRgb& color : ledValues) {
|
|
|
|
float x, y, b;
|
|
|
|
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
|
|
|
|
rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, x, y, b);
|
|
|
|
// Send adjust color command in JSON format.
|
2014-04-28 14:32:37 +02:00
|
|
|
put(getStateRoute(lightId), QString("{\"xy\": [%1, %2]}").arg(x).arg(y));
|
2014-04-27 12:59:44 +02:00
|
|
|
// Send brightness color command in JSON format.
|
2014-04-28 14:32:37 +02:00
|
|
|
put(getStateRoute(lightId), QString("{\"bri\": %1}").arg(qRound(b * 255.0f)));
|
2014-04-27 12:59:44 +02:00
|
|
|
// Next light id.
|
|
|
|
lightId++;
|
|
|
|
}
|
2014-05-03 10:12:41 +02:00
|
|
|
timer.start();
|
2014-04-27 12:59:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDevicePhilipsHue::switchOff() {
|
2014-05-03 10:12:41 +02:00
|
|
|
timer.stop();
|
2014-04-28 14:32:37 +02:00
|
|
|
// If light states have been saved before, ...
|
|
|
|
if (statesSaved()) {
|
|
|
|
// ... restore them.
|
|
|
|
restoreStates();
|
|
|
|
}
|
2014-04-27 12:59:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDevicePhilipsHue::put(QString route, QString content) {
|
|
|
|
QString url = QString("/api/%1/%2").arg(username).arg(route);
|
|
|
|
QHttpRequestHeader header("PUT", url);
|
|
|
|
header.setValue("Host", host);
|
|
|
|
header.setValue("Accept-Encoding", "identity");
|
2014-05-01 00:02:10 +02:00
|
|
|
header.setValue("Connection", "keep-alive");
|
2014-04-27 12:59:44 +02:00
|
|
|
header.setValue("Content-Length", QString("%1").arg(content.size()));
|
2014-05-01 00:02:10 +02:00
|
|
|
QEventLoop loop;
|
|
|
|
// Connect requestFinished signal to quit slot of the loop.
|
|
|
|
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
|
|
|
|
// Perfrom request
|
2014-04-27 12:59:44 +02:00
|
|
|
http->request(header, content.toAscii());
|
2014-05-01 00:02:10 +02:00
|
|
|
// Go into the loop until the request is finished.
|
|
|
|
loop.exec();
|
2014-04-27 12:59:44 +02:00
|
|
|
}
|
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
QByteArray LedDevicePhilipsHue::get(QString route) {
|
|
|
|
QString url = QString("/api/%1/%2").arg(username).arg(route);
|
|
|
|
// Event loop to block until request finished.
|
|
|
|
QEventLoop loop;
|
|
|
|
// Connect requestFinished signal to quit slot of the loop.
|
|
|
|
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
|
|
|
|
// Perfrom request
|
|
|
|
http->get(url);
|
|
|
|
// Go into the loop until the request is finished.
|
|
|
|
loop.exec();
|
|
|
|
// Read all data of the response.
|
|
|
|
return http->readAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {
|
2014-04-27 12:59:44 +02:00
|
|
|
return QString("lights/%1/state").arg(lightId);
|
|
|
|
}
|
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
|
|
|
|
return QString("lights/%1").arg(lightId);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
|
|
|
|
// Clear saved light states.
|
|
|
|
states.clear();
|
|
|
|
// Use json parser to parse reponse.
|
|
|
|
Json::Reader reader;
|
|
|
|
Json::FastWriter writer;
|
|
|
|
// Iterate lights.
|
|
|
|
for (unsigned int i = 0; i < nLights; i++) {
|
|
|
|
// Read the response.
|
|
|
|
QByteArray response = get(getRoute(i + 1));
|
|
|
|
// Parse JSON.
|
2014-05-01 00:02:10 +02:00
|
|
|
Json::Value json;
|
|
|
|
if (!reader.parse(QString(response).toStdString(), json)) {
|
2014-04-28 14:32:37 +02:00
|
|
|
// Error occured, break loop.
|
|
|
|
break;
|
|
|
|
}
|
2014-05-01 00:02:10 +02:00
|
|
|
// Save state object values which are subject to change.
|
|
|
|
Json::Value state(Json::objectValue);
|
2014-05-07 15:22:13 +02:00
|
|
|
state["on"] = json["state"]["on"];
|
|
|
|
if (json["state"]["on"] == true) {
|
|
|
|
state["xy"] = json["state"]["xy"];
|
|
|
|
state["bri"] = json["state"]["bri"];
|
|
|
|
}
|
2014-04-28 14:32:37 +02:00
|
|
|
// Save state object.
|
2014-05-01 00:02:10 +02:00
|
|
|
states.push_back(QString(writer.write(state).c_str()).trimmed());
|
2014-04-28 14:32:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:22:13 +02:00
|
|
|
void LedDevicePhilipsHue::switchOn(unsigned int nLights) {
|
|
|
|
for (unsigned int i = 0; i < nLights; i++) {
|
|
|
|
put(getStateRoute(i + 1), "{\"on\": true}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
void LedDevicePhilipsHue::restoreStates() {
|
|
|
|
unsigned int lightId = 1;
|
|
|
|
for (QString state : states) {
|
|
|
|
put(getStateRoute(lightId), state);
|
|
|
|
lightId++;
|
|
|
|
}
|
|
|
|
// Clear saved light states.
|
|
|
|
states.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LedDevicePhilipsHue::statesSaved() {
|
|
|
|
return !states.empty();
|
|
|
|
}
|
|
|
|
|
2014-04-27 12:59:44 +02:00
|
|
|
void LedDevicePhilipsHue::rgbToXYBrightness(float red, float green, float blue, float& x, float& y, float& brightness) {
|
|
|
|
// Apply gamma correction.
|
|
|
|
red = (red > 0.04045f) ? qPow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
|
2014-04-28 14:32:37 +02:00
|
|
|
green = (green > 0.04045f) ? qPow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
|
|
|
|
blue = (blue > 0.04045f) ? qPow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
|
2014-04-27 12:59:44 +02:00
|
|
|
// Convert to XYZ space.
|
|
|
|
float X = red * 0.649926f + green * 0.103455f + blue * 0.197109f;
|
|
|
|
float Y = red * 0.234327f + green * 0.743075f + blue * 0.022598f;
|
|
|
|
float Z = red * 0.0000000f + green * 0.053077f + blue * 1.035763f;
|
|
|
|
// Convert to x,y space.
|
|
|
|
x = X / (X + Y + Z);
|
|
|
|
y = Y / (X + Y + Z);
|
|
|
|
if (isnan(x)) {
|
2014-04-27 18:42:26 +02:00
|
|
|
x = 0.0f;
|
|
|
|
}
|
|
|
|
if (isnan(y)) {
|
|
|
|
y = 0.0f;
|
|
|
|
}
|
|
|
|
// Brightness is simply Y in the XYZ space.
|
2014-04-27 12:59:44 +02:00
|
|
|
brightness = Y;
|
|
|
|
}
|