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
|
|
|
|
2014-07-15 08:51:07 +02:00
|
|
|
#include <set>
|
|
|
|
|
2014-07-16 20:22:37 +02:00
|
|
|
bool operator ==(CiColor p1, CiColor p2) {
|
|
|
|
return (p1.x == p2.x) && (p1.y == p2.y) && (p1.bri == p2.bri);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator !=(CiColor p1, CiColor p2) {
|
|
|
|
return !(p1 == p2);
|
|
|
|
}
|
|
|
|
|
2015-11-24 19:37:52 +01:00
|
|
|
PhilipsHueLight::PhilipsHueLight(unsigned int id, QString originalState, QString modelId) :
|
|
|
|
id(id), originalState(originalState) {
|
2015-11-20 18:47:00 +01:00
|
|
|
// Hue system model ids (http://www.developers.meethue.com/documentation/supported-lights).
|
|
|
|
// Light strips, color iris, ...
|
2015-11-24 19:37:52 +01:00
|
|
|
const std::set<QString> GAMUT_A_MODEL_IDS = { "LLC001", "LLC005", "LLC006", "LLC007", "LLC010", "LLC011", "LLC012",
|
2015-11-20 18:47:00 +01:00
|
|
|
"LLC013", "LLC014", "LST001" };
|
2015-11-24 19:37:52 +01:00
|
|
|
// Hue bulbs, spots, ...
|
|
|
|
const std::set<QString> GAMUT_B_MODEL_IDS = { "LCT001", "LCT002", "LCT003", "LCT007", "LLM001" };
|
|
|
|
// Hue Lightstrip plus, go ...
|
|
|
|
const std::set<QString> 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};
|
|
|
|
}
|
2014-07-16 20:22:37 +02:00
|
|
|
// Initialize black color.
|
|
|
|
black = rgbToCiColor(0.0f, 0.0f, 0.0f);
|
|
|
|
// Initialize color with black
|
|
|
|
color = {black.x, black.y, black.bri};
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:46:53 +01:00
|
|
|
float PhilipsHueLight::crossProduct(CiColor p1, CiColor p2) {
|
2014-07-16 20:22:37 +02:00
|
|
|
return p1.x * p2.y - p1.y * p2.x;
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:46:53 +01:00
|
|
|
bool PhilipsHueLight::isPointInLampsReach(CiColor p) {
|
2014-07-16 20:22:37 +02:00
|
|
|
CiColor v1 = { colorSpace.green.x - colorSpace.red.x, colorSpace.green.y - colorSpace.red.y };
|
|
|
|
CiColor v2 = { colorSpace.blue.x - colorSpace.red.x, colorSpace.blue.y - colorSpace.red.y };
|
|
|
|
CiColor q = { p.x - colorSpace.red.x, p.y - colorSpace.red.y };
|
|
|
|
float s = crossProduct(q, v2) / crossProduct(v1, v2);
|
|
|
|
float t = crossProduct(v1, q) / crossProduct(v1, v2);
|
|
|
|
if ((s >= 0.0f) && (t >= 0.0f) && (s + t <= 1.0f)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:46:53 +01:00
|
|
|
CiColor PhilipsHueLight::getClosestPointToPoint(CiColor a, CiColor b, CiColor p) {
|
2014-07-16 20:22:37 +02:00
|
|
|
CiColor AP = { p.x - a.x, p.y - a.y };
|
|
|
|
CiColor AB = { b.x - a.x, b.y - a.y };
|
|
|
|
float ab2 = AB.x * AB.x + AB.y * AB.y;
|
|
|
|
float ap_ab = AP.x * AB.x + AP.y * AB.y;
|
|
|
|
float t = ap_ab / ab2;
|
|
|
|
if (t < 0.0f) {
|
|
|
|
t = 0.0f;
|
|
|
|
} else if (t > 1.0f) {
|
|
|
|
t = 1.0f;
|
|
|
|
}
|
|
|
|
return {a.x + AB.x * t, a.y + AB.y * t};
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:46:53 +01:00
|
|
|
float PhilipsHueLight::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2) {
|
2014-07-16 20:22:37 +02:00
|
|
|
// Horizontal difference.
|
|
|
|
float dx = p1.x - p2.x;
|
|
|
|
// Vertical difference.
|
|
|
|
float dy = p1.y - p2.y;
|
|
|
|
// Absolute value.
|
|
|
|
return sqrt(dx * dx + dy * dy);
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:46:53 +01:00
|
|
|
CiColor PhilipsHueLight::rgbToCiColor(float red, float green, float blue) {
|
2014-07-16 20:22:37 +02:00
|
|
|
// Apply gamma correction.
|
|
|
|
float r = (red > 0.04045f) ? powf((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
|
|
|
|
float g = (green > 0.04045f) ? powf((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
|
|
|
|
float b = (blue > 0.04045f) ? powf((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
|
|
|
|
// Convert to XYZ space.
|
|
|
|
float X = r * 0.649926f + g * 0.103455f + b * 0.197109f;
|
|
|
|
float Y = r * 0.234327f + g * 0.743075f + b * 0.022598f;
|
|
|
|
float Z = r * 0.0000000f + g * 0.053077f + b * 1.035763f;
|
|
|
|
// Convert to x,y space.
|
|
|
|
float cx = X / (X + Y + Z);
|
|
|
|
float cy = Y / (X + Y + Z);
|
|
|
|
if (isnan(cx)) {
|
|
|
|
cx = 0.0f;
|
|
|
|
}
|
|
|
|
if (isnan(cy)) {
|
|
|
|
cy = 0.0f;
|
|
|
|
}
|
|
|
|
// Brightness is simply Y in the XYZ space.
|
|
|
|
CiColor xy = { cx, cy, Y };
|
|
|
|
// Check if the given XY value is within the color reach of our lamps.
|
|
|
|
if (!isPointInLampsReach(xy)) {
|
|
|
|
// It seems the color is out of reach let's find the closes color we can produce with our lamp and send this XY value out.
|
|
|
|
CiColor pAB = getClosestPointToPoint(colorSpace.red, colorSpace.green, xy);
|
|
|
|
CiColor pAC = getClosestPointToPoint(colorSpace.blue, colorSpace.red, xy);
|
|
|
|
CiColor pBC = getClosestPointToPoint(colorSpace.green, colorSpace.blue, xy);
|
|
|
|
// Get the distances per point and see which point is closer to our Point.
|
|
|
|
float dAB = getDistanceBetweenTwoPoints(xy, pAB);
|
|
|
|
float dAC = getDistanceBetweenTwoPoints(xy, pAC);
|
|
|
|
float dBC = getDistanceBetweenTwoPoints(xy, pBC);
|
|
|
|
float lowest = dAB;
|
|
|
|
CiColor closestPoint = pAB;
|
|
|
|
if (dAC < lowest) {
|
|
|
|
lowest = dAC;
|
|
|
|
closestPoint = pAC;
|
|
|
|
}
|
|
|
|
if (dBC < lowest) {
|
|
|
|
lowest = dBC;
|
|
|
|
closestPoint = pBC;
|
|
|
|
}
|
|
|
|
// Change the xy value to a value which is within the reach of the lamp.
|
|
|
|
xy.x = closestPoint.x;
|
|
|
|
xy.y = closestPoint.y;
|
|
|
|
}
|
|
|
|
return xy;
|
|
|
|
}
|
2014-07-16 11:49:34 +02:00
|
|
|
|
2015-02-01 15:29:40 +01:00
|
|
|
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::string& username, bool switchOffOnBlack,
|
2015-03-03 15:45:09 +01:00
|
|
|
int transitiontime, std::vector<unsigned int> lightIds) :
|
2015-02-01 15:29:40 +01:00
|
|
|
host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
|
2015-03-03 15:45:09 +01:00
|
|
|
transitiontime), lightIds(lightIds) {
|
2014-04-28 14:32:37 +02:00
|
|
|
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.
|
2014-07-15 08:51:07 +02:00
|
|
|
if (!areStatesSaved()) {
|
|
|
|
saveStates((unsigned int) ledValues.size());
|
|
|
|
switchOn((unsigned int) ledValues.size());
|
2014-04-28 14:32:37 +02:00
|
|
|
}
|
2014-07-16 16:45:45 +02:00
|
|
|
// If there are less states saved than colors given, then maybe something went wrong before.
|
2015-03-03 15:46:53 +01:00
|
|
|
if (lights.size() != ledValues.size()) {
|
2014-07-16 16:45:45 +02:00
|
|
|
restoreStates();
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-28 14:32:37 +02:00
|
|
|
// Iterate through colors and set light states.
|
2014-07-15 09:59:01 +02:00
|
|
|
unsigned int idx = 0;
|
2014-04-27 12:59:44 +02:00
|
|
|
for (const ColorRgb& color : ledValues) {
|
2014-07-15 09:55:58 +02:00
|
|
|
// Get lamp.
|
2015-03-03 15:46:53 +01:00
|
|
|
PhilipsHueLight& lamp = lights.at(idx);
|
2014-04-27 12:59:44 +02:00
|
|
|
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
|
2014-07-16 20:22:37 +02:00
|
|
|
CiColor xy = lamp.rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f);
|
2014-07-15 09:55:58 +02:00
|
|
|
// Write color if color has been changed.
|
2014-07-16 20:22:37 +02:00
|
|
|
if (xy != lamp.color) {
|
2015-03-02 13:51:50 +01:00
|
|
|
// Switch on if the lamp has been previously switched off.
|
|
|
|
if (switchOffOnBlack && lamp.color == lamp.black) {
|
|
|
|
put(getStateRoute(lamp.id), QString("{\"on\": true}"));
|
|
|
|
}
|
2014-07-16 11:49:34 +02:00
|
|
|
// Send adjust color and brightness command in JSON format.
|
2015-02-01 15:29:40 +01:00
|
|
|
// We have to set the transition time each time.
|
2014-07-16 16:21:11 +02:00
|
|
|
put(getStateRoute(lamp.id),
|
2015-02-01 15:29:40 +01:00
|
|
|
QString("{\"xy\": [%1, %2], \"bri\": %3, \"transitiontime\": %4}").arg(xy.x).arg(xy.y).arg(
|
|
|
|
qRound(xy.bri * 255.0f)).arg(transitiontime));
|
2014-07-16 20:22:37 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
// Switch lamp off if switchOffOnBlack is enabled and the lamp is currently on.
|
|
|
|
if (switchOffOnBlack) {
|
|
|
|
// From black to a color.
|
|
|
|
if (lamp.color == lamp.black && xy != lamp.black) {
|
|
|
|
put(getStateRoute(lamp.id), QString("{\"on\": true}"));
|
|
|
|
}
|
|
|
|
// From a color to black.
|
|
|
|
else if (lamp.color != lamp.black && xy == lamp.black) {
|
|
|
|
put(getStateRoute(lamp.id), QString("{\"on\": false}"));
|
|
|
|
}
|
2014-07-15 09:55:58 +02:00
|
|
|
}
|
2014-07-16 20:22:37 +02:00
|
|
|
// Remember last color.
|
|
|
|
lamp.color = xy;
|
2014-04-27 12:59:44 +02:00
|
|
|
// Next light id.
|
2014-07-15 09:59:01 +02:00
|
|
|
idx++;
|
2014-04-27 12:59:44 +02:00
|
|
|
}
|
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, ...
|
2014-07-15 08:51:07 +02:00
|
|
|
if (areStatesSaved()) {
|
2014-04-28 14:32:37 +02:00
|
|
|
// ... 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) {
|
2014-07-15 09:55:58 +02:00
|
|
|
// Clear saved lamps.
|
2015-03-03 15:46:53 +01:00
|
|
|
lights.clear();
|
2014-04-28 14:32:37 +02:00
|
|
|
// Use json parser to parse reponse.
|
|
|
|
Json::Reader reader;
|
|
|
|
Json::FastWriter writer;
|
2015-03-03 15:45:09 +01:00
|
|
|
// Create light ids if none supplied by the user.
|
|
|
|
if (lightIds.size() != nLights) {
|
|
|
|
lightIds.clear();
|
|
|
|
for (unsigned int i = 0; i < nLights; i++) {
|
|
|
|
lightIds.push_back(i + 1);
|
|
|
|
}
|
|
|
|
}
|
2014-04-28 14:32:37 +02:00
|
|
|
// Iterate lights.
|
|
|
|
for (unsigned int i = 0; i < nLights; i++) {
|
|
|
|
// Read the response.
|
2015-03-03 15:45:09 +01:00
|
|
|
QByteArray response = get(getRoute(lightIds.at(i)));
|
2014-04-28 14:32:37 +02:00
|
|
|
// 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-07-15 09:55:58 +02:00
|
|
|
// Get state object values which are subject to change.
|
2014-05-01 00:02:10 +02:00
|
|
|
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-07-15 09:55:58 +02:00
|
|
|
// Determine the model id.
|
2014-07-15 08:54:40 +02:00
|
|
|
QString modelId = QString(writer.write(json["modelid"]).c_str()).trimmed().replace("\"", "");
|
2014-07-15 09:55:58 +02:00
|
|
|
QString originalState = QString(writer.write(state).c_str()).trimmed();
|
|
|
|
// Save state object.
|
2015-03-03 15:46:53 +01:00
|
|
|
lights.push_back(PhilipsHueLight(lightIds.at(i), originalState, modelId));
|
2014-04-28 14:32:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:22:13 +02:00
|
|
|
void LedDevicePhilipsHue::switchOn(unsigned int nLights) {
|
2015-03-03 15:46:53 +01:00
|
|
|
for (PhilipsHueLight light : lights) {
|
|
|
|
put(getStateRoute(light.id), "{\"on\": true}");
|
2014-05-07 15:22:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-28 14:32:37 +02:00
|
|
|
void LedDevicePhilipsHue::restoreStates() {
|
2015-03-03 15:46:53 +01:00
|
|
|
for (PhilipsHueLight light : lights) {
|
|
|
|
put(getStateRoute(light.id), light.originalState);
|
2014-04-28 14:32:37 +02:00
|
|
|
}
|
|
|
|
// Clear saved light states.
|
2015-03-03 15:46:53 +01:00
|
|
|
lights.clear();
|
2014-04-28 14:32:37 +02:00
|
|
|
}
|
|
|
|
|
2014-07-15 08:51:07 +02:00
|
|
|
bool LedDevicePhilipsHue::areStatesSaved() {
|
2015-03-03 15:46:53 +01:00
|
|
|
return !lights.empty();
|
2014-04-28 14:32:37 +02:00
|
|
|
}
|