2016-06-03 16:46:45 +02:00
|
|
|
// Local-Hyperion includes
|
|
|
|
#include "LedDevicePhilipsHue.h"
|
|
|
|
|
|
|
|
// qt includes
|
|
|
|
#include <QtCore/qmath.h>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
bool operator ==(CiColor p1, CiColor p2)
|
|
|
|
{
|
2016-06-03 16:46:45 +02:00
|
|
|
return (p1.x == p2.x) && (p1.y == p2.y) && (p1.bri == p2.bri);
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
bool operator !=(CiColor p1, CiColor p2)
|
|
|
|
{
|
2016-06-03 16:46:45 +02:00
|
|
|
return !(p1 == p2);
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
CiColor CiColor::rgbToCiColor(float red, float green, float blue, CiColorTriangle colorSpace)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +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.664511f + g * 0.154324f + b * 0.162028f;
|
|
|
|
float Y = r * 0.283881f + g * 0.668433f + b * 0.047685f;
|
|
|
|
float Z = r * 0.000088f + g * 0.072310f + b * 0.986039f;
|
|
|
|
// Convert to x,y space.
|
|
|
|
float cx = X / (X + Y + Z);
|
|
|
|
float cy = Y / (X + Y + Z);
|
|
|
|
if (std::isnan(cx))
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +02:00
|
|
|
cx = 0.0f;
|
2016-08-14 10:46:44 +02:00
|
|
|
}
|
2017-03-31 10:17:14 +02:00
|
|
|
if (std::isnan(cy))
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +02:00
|
|
|
cy = 0.0f;
|
2016-08-14 10:46:44 +02:00
|
|
|
}
|
2019-02-07 11:37:53 +01:00
|
|
|
// RGB to HSV/B Conversion after gamma correction use V for brightness, not Y from XYZ Space.
|
|
|
|
float bri = fmax(fmax(r, g), b);
|
2017-03-31 10:17:14 +02:00
|
|
|
CiColor xy =
|
2019-02-07 11:37:53 +01:00
|
|
|
{ cx, cy, bri };
|
2017-03-31 10:17:14 +02:00
|
|
|
// Check if the given XY value is within the color reach of our lamps.
|
|
|
|
if (!isPointInLampsReach(xy, colorSpace))
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +02:00
|
|
|
// 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;
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
2017-03-31 10:17:14 +02:00
|
|
|
return xy;
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
float CiColor::crossProduct(CiColor p1, CiColor p2)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2016-06-03 16:46:45 +02:00
|
|
|
return p1.x * p2.y - p1.y * p2.x;
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
bool CiColor::isPointInLampsReach(CiColor p, CiColorTriangle colorSpace)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +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 };
|
2016-06-03 16:46:45 +02:00
|
|
|
float s = crossProduct(q, v2) / crossProduct(v1, v2);
|
|
|
|
float t = crossProduct(v1, q) / crossProduct(v1, v2);
|
2016-08-14 10:46:44 +02:00
|
|
|
if ((s >= 0.0f) && (t >= 0.0f) && (s + t <= 1.0f))
|
|
|
|
{
|
2016-06-03 16:46:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
CiColor CiColor::getClosestPointToPoint(CiColor a, CiColor b, CiColor p)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +02:00
|
|
|
CiColor AP =
|
|
|
|
{ p.x - a.x, p.y - a.y };
|
|
|
|
CiColor AB =
|
|
|
|
{ b.x - a.x, b.y - a.y };
|
2016-06-03 16:46:45 +02:00
|
|
|
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;
|
2016-08-14 10:46:44 +02:00
|
|
|
if (t < 0.0f)
|
|
|
|
{
|
2016-06-03 16:46:45 +02:00
|
|
|
t = 0.0f;
|
2016-08-14 10:46:44 +02:00
|
|
|
}
|
|
|
|
else if (t > 1.0f)
|
|
|
|
{
|
2016-06-03 16:46:45 +02:00
|
|
|
t = 1.0f;
|
|
|
|
}
|
2017-03-31 10:17:14 +02:00
|
|
|
return
|
|
|
|
{ a.x + AB.x * t, a.y + AB.y * t};
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
float CiColor::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2016-06-03 16:46:45 +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);
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
PhilipsHueBridge::PhilipsHueBridge(Logger* log, QString host, QString username)
|
|
|
|
: QObject()
|
|
|
|
, log(log)
|
|
|
|
, host(host)
|
|
|
|
, username(username)
|
|
|
|
{
|
|
|
|
// setup reconnection timer
|
|
|
|
bTimer.setInterval(5000);
|
|
|
|
bTimer.setSingleShot(true);
|
|
|
|
|
|
|
|
connect(&bTimer, &QTimer::timeout, this, &PhilipsHueBridge::bConnect);
|
|
|
|
connect(&manager, &QNetworkAccessManager::finished, this, &PhilipsHueBridge::resolveReply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhilipsHueBridge::bConnect(void)
|
|
|
|
{
|
|
|
|
if(username.isEmpty() || host.isEmpty())
|
|
|
|
{
|
|
|
|
Error(log,"Username or IP Address is empty!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QString url = QString("http://%1/api/%2").arg(host).arg(username);
|
|
|
|
Debug(log, "Connect to bridge %s", QSTRING_CSTR(url));
|
|
|
|
|
|
|
|
QNetworkRequest request(url);
|
|
|
|
manager.get(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void PhilipsHueBridge::resolveReply(QNetworkReply* reply)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
// TODO use put request also for network error checking with decent threshold
|
|
|
|
if(reply->operation() == QNetworkAccessManager::GetOperation)
|
|
|
|
{
|
|
|
|
if(reply->error() == QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
QByteArray response = reply->readAll();
|
|
|
|
QJsonParseError error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(response, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
|
|
{
|
|
|
|
Error(log, "Got invalid response from bridge");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// check for authorization
|
|
|
|
if(doc.isArray())
|
|
|
|
{
|
|
|
|
Error(log, "Authorization failed, username invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonObject obj = doc.object()["lights"].toObject();
|
|
|
|
|
|
|
|
if(obj.isEmpty())
|
|
|
|
{
|
|
|
|
Error(log, "Bridge has no registered bulbs/stripes");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all available light ids and their values
|
|
|
|
QStringList keys = obj.keys();
|
|
|
|
QMap<quint16,QJsonObject> map;
|
|
|
|
for (int i = 0; i < keys.size(); ++i)
|
|
|
|
{
|
|
|
|
map.insert(keys.at(i).toInt(), obj.take(keys.at(i)).toObject());
|
|
|
|
}
|
|
|
|
emit newLights(map);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(log,"Network Error: %s", QSTRING_CSTR(reply->errorString()));
|
|
|
|
bTimer.start();
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 10:17:14 +02:00
|
|
|
reply->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhilipsHueBridge::post(QString route, QString content)
|
|
|
|
{
|
2019-01-01 19:47:07 +01:00
|
|
|
//Debug(log, "Post %s: %s", QSTRING_CSTR(QString("http://IP/api/USR/%1").arg(route)), QSTRING_CSTR(content));
|
2017-09-16 00:18:17 +02:00
|
|
|
|
|
|
|
QNetworkRequest request(QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route));
|
|
|
|
manager.put(request, content.toLatin1());
|
2017-03-31 10:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::set<QString> PhilipsHueLight::GAMUT_A_MODEL_IDS =
|
|
|
|
{ "LLC001", "LLC005", "LLC006", "LLC007", "LLC010", "LLC011", "LLC012", "LLC013", "LLC014", "LST001" };
|
|
|
|
const std::set<QString> PhilipsHueLight::GAMUT_B_MODEL_IDS =
|
|
|
|
{ "LCT001", "LCT002", "LCT003", "LCT007", "LLM001" };
|
|
|
|
const std::set<QString> PhilipsHueLight::GAMUT_C_MODEL_IDS =
|
2019-02-07 11:37:53 +01:00
|
|
|
{ "LLC020", "LST002", "LCT011", "LCT012", "LCT010", "LCT014", "LCT015", "LCT016", "LCT024" };
|
2017-03-31 10:17:14 +02:00
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge* bridge, unsigned int id, QJsonObject values)
|
2017-09-16 00:18:17 +02:00
|
|
|
: log(log)
|
|
|
|
, bridge(bridge)
|
|
|
|
, id(id)
|
2017-03-31 10:17:14 +02:00
|
|
|
{
|
|
|
|
// Get state object values which are subject to change.
|
2017-09-16 00:18:17 +02:00
|
|
|
if (!values["state"].toObject().contains("on"))
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Error(log, "Got invalid state object from light ID %d", id);
|
2017-03-31 10:17:14 +02:00
|
|
|
}
|
|
|
|
QJsonObject state;
|
2017-09-16 00:18:17 +02:00
|
|
|
state["on"] = values["state"].toObject()["on"];
|
2017-03-31 10:17:14 +02:00
|
|
|
on = false;
|
2017-09-16 00:18:17 +02:00
|
|
|
if (values["state"].toObject()["on"].toBool())
|
2017-03-31 10:17:14 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
state["xy"] = values["state"].toObject()["xy"];
|
|
|
|
state["bri"] = values["state"].toObject()["bri"];
|
2017-03-31 10:17:14 +02:00
|
|
|
on = true;
|
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
color = {
|
|
|
|
(float) state["xy"].toArray()[0].toDouble(),
|
|
|
|
(float) state["xy"].toArray()[1].toDouble(),
|
|
|
|
(float) state["bri"].toDouble() / 255.0f
|
|
|
|
};
|
|
|
|
transitionTime = values["state"].toObject()["transitiontime"].toInt();
|
2017-03-31 10:17:14 +02:00
|
|
|
}
|
|
|
|
// Determine the model id.
|
2017-09-16 00:18:17 +02:00
|
|
|
modelId = values["modelid"].toString().trimmed().replace("\"", "");
|
2017-03-31 10:17:14 +02:00
|
|
|
// Determine the original state.
|
|
|
|
originalState = QJsonDocument(state).toJson(QJsonDocument::JsonFormat::Compact).trimmed();
|
|
|
|
// Find id in the sets and set the appropriate color space.
|
|
|
|
if (GAMUT_A_MODEL_IDS.find(modelId) != GAMUT_A_MODEL_IDS.end())
|
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Debug(log, "Recognized model id %s of light ID %d as gamut A", modelId.toStdString().c_str(), id);
|
2017-03-31 10:17:14 +02:00
|
|
|
colorSpace.red =
|
2019-02-07 11:37:53 +01:00
|
|
|
{ 0.704f, 0.296f};
|
2017-03-31 10:17:14 +02:00
|
|
|
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())
|
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Debug(log, "Recognized model id %s of light ID %d as gamut B", modelId.toStdString().c_str(), id);
|
2017-03-31 10:17:14 +02:00
|
|
|
colorSpace.red =
|
|
|
|
{ 0.675f, 0.322f};
|
|
|
|
colorSpace.green =
|
2019-02-07 11:37:53 +01:00
|
|
|
{ 0.409f, 0.518f};
|
2017-03-31 10:17:14 +02:00
|
|
|
colorSpace.blue =
|
|
|
|
{ 0.167f, 0.04f};
|
|
|
|
}
|
|
|
|
else if (GAMUT_C_MODEL_IDS.find(modelId) != GAMUT_C_MODEL_IDS.end())
|
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Debug(log, "Recognized model id %s of light ID %d as gamut C", modelId.toStdString().c_str(), id);
|
2017-03-31 10:17:14 +02:00
|
|
|
colorSpace.red =
|
2019-02-07 11:37:53 +01:00
|
|
|
{ 0.6915f, 0.3083f};
|
2017-03-31 10:17:14 +02:00
|
|
|
colorSpace.green =
|
2019-02-07 11:37:53 +01:00
|
|
|
{ 0.17f, 0.7f};
|
2017-03-31 10:17:14 +02:00
|
|
|
colorSpace.blue =
|
2019-02-07 11:37:53 +01:00
|
|
|
{ 0.1532f, 0.0475f};
|
2017-03-31 10:17:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Warning(log, "Did not recognize model id %s of light ID %d", modelId.toStdString().c_str(), id);
|
2017-03-31 10:17:14 +02:00
|
|
|
colorSpace.red =
|
|
|
|
{ 1.0f, 0.0f};
|
|
|
|
colorSpace.green =
|
|
|
|
{ 0.0f, 1.0f};
|
|
|
|
colorSpace.blue =
|
|
|
|
{ 0.0f, 0.0f};
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
2017-09-16 00:18:17 +02:00
|
|
|
|
|
|
|
Info(log,"Light ID %d created", id);
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
PhilipsHueLight::~PhilipsHueLight()
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +02:00
|
|
|
// Restore the original state.
|
|
|
|
set(originalState);
|
|
|
|
}
|
2016-08-23 20:07:12 +02:00
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
void PhilipsHueLight::set(QString state)
|
|
|
|
{
|
2019-01-01 19:47:07 +01:00
|
|
|
bridge->post(QString("lights/%1/state").arg(id), state);
|
2017-03-31 10:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhilipsHueLight::setOn(bool on)
|
|
|
|
{
|
|
|
|
if (this->on != on)
|
|
|
|
{
|
|
|
|
QString arg = on ? "true" : "false";
|
|
|
|
set(QString("{ \"on\": %1 }").arg(arg));
|
|
|
|
}
|
|
|
|
this->on = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhilipsHueLight::setTransitionTime(unsigned int transitionTime)
|
|
|
|
{
|
|
|
|
if (this->transitionTime != transitionTime)
|
|
|
|
{
|
|
|
|
set(QString("{ \"transitiontime\": %1 }").arg(transitionTime));
|
|
|
|
}
|
|
|
|
this->transitionTime = transitionTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhilipsHueLight::setColor(CiColor color, float brightnessFactor)
|
|
|
|
{
|
|
|
|
if (this->color != color)
|
|
|
|
{
|
|
|
|
const int bri = qRound(qMin(254.0f, brightnessFactor * qMax(1.0f, color.bri * 254.0f)));
|
|
|
|
set(QString("{ \"xy\": [%1, %2], \"bri\": %3 }").arg(color.x, 0, 'f', 4).arg(color.y, 0, 'f', 4).arg(bri));
|
|
|
|
}
|
|
|
|
this->color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
CiColor PhilipsHueLight::getColor() const
|
|
|
|
{
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
CiColorTriangle PhilipsHueLight::getColorSpace() const
|
|
|
|
{
|
|
|
|
return colorSpace;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
LedDevice* LedDevicePhilipsHue::construct(const QJsonObject &deviceConfig)
|
|
|
|
{
|
|
|
|
return new LedDevicePhilipsHue(deviceConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
LedDevicePhilipsHue::LedDevicePhilipsHue(const QJsonObject& deviceConfig)
|
2019-01-01 19:47:07 +01:00
|
|
|
: LedDevice(deviceConfig)
|
|
|
|
, _bridge(nullptr)
|
2017-03-31 10:17:14 +02:00
|
|
|
{
|
|
|
|
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
LedDevicePhilipsHue::~LedDevicePhilipsHue()
|
|
|
|
{
|
2017-03-31 10:17:14 +02:00
|
|
|
switchOff();
|
2019-01-01 19:47:07 +01:00
|
|
|
delete _bridge;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LedDevicePhilipsHue::start()
|
|
|
|
{
|
|
|
|
_bridge = new PhilipsHueBridge(_log, _devConfig["output"].toString(), _devConfig["username"].toString());
|
|
|
|
_deviceReady = init(_devConfig);
|
|
|
|
|
|
|
|
connect(_bridge, &PhilipsHueBridge::newLights, this, &LedDevicePhilipsHue::newLights);
|
|
|
|
connect(this, &LedDevice::enableStateChanged, this, &LedDevicePhilipsHue::stateChanged);
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:59:58 +02:00
|
|
|
bool LedDevicePhilipsHue::init(const QJsonObject &deviceConfig)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2016-10-13 21:59:58 +02:00
|
|
|
switchOffOnBlack = deviceConfig["switchOffOnBlack"].toBool(true);
|
2017-03-31 11:11:28 +02:00
|
|
|
brightnessFactor = (float) deviceConfig["brightnessFactor"].toDouble(1.0);
|
2017-03-31 10:17:14 +02:00
|
|
|
transitionTime = deviceConfig["transitiontime"].toInt(1);
|
2016-10-13 21:59:58 +02:00
|
|
|
QJsonArray lArray = deviceConfig["lightIds"].toArray();
|
2017-09-16 00:18:17 +02:00
|
|
|
|
|
|
|
QJsonObject newDC = deviceConfig;
|
|
|
|
if(!lArray.empty())
|
|
|
|
{
|
|
|
|
for(const auto i : lArray)
|
|
|
|
{
|
|
|
|
lightIds.push_back(i.toInt());
|
|
|
|
}
|
|
|
|
// get light info from bridge
|
2019-01-01 19:47:07 +01:00
|
|
|
_bridge->bConnect();
|
2017-09-16 00:18:17 +02:00
|
|
|
|
|
|
|
// adapt latchTime to count of user lightIds (bridge 10Hz max overall)
|
|
|
|
newDC.insert("latchTime",QJsonValue(100*(int)lightIds.size()));
|
|
|
|
}
|
|
|
|
else
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Error(_log,"No light ID provided, abort");
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
LedDevice::init(newDC);
|
|
|
|
|
2016-08-23 20:07:12 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
void LedDevicePhilipsHue::newLights(QMap<quint16, QJsonObject> map)
|
2016-08-23 20:07:12 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
if(!lightIds.empty())
|
|
|
|
{
|
|
|
|
// search user lightid inside map and create light if found
|
|
|
|
lights.clear();
|
|
|
|
for(const auto id : lightIds)
|
|
|
|
{
|
|
|
|
if (map.contains(id))
|
|
|
|
{
|
2019-01-01 19:47:07 +01:00
|
|
|
lights.push_back(PhilipsHueLight(_log, _bridge, id, map.value(id)));
|
2017-09-16 00:18:17 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(_log,"Light id %d isn't used on this bridge", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 20:07:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-14 10:46:44 +02:00
|
|
|
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues)
|
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
// lights will be empty sometimes
|
|
|
|
if(lights.empty()) return -1;
|
|
|
|
|
|
|
|
// more lights then leds, stop always
|
|
|
|
if(ledValues.size() < lights.size())
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Error(_log,"More LightIDs configured than leds, each LightID requires one led!");
|
|
|
|
return -1;
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
2017-09-16 00:18:17 +02:00
|
|
|
|
|
|
|
// Iterate through lights and set colors.
|
2016-06-03 16:46:45 +02:00
|
|
|
unsigned int idx = 0;
|
2017-09-16 00:18:17 +02:00
|
|
|
for (PhilipsHueLight& light : lights)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
// Get color.
|
|
|
|
ColorRgb color = ledValues.at(idx);
|
2016-06-03 16:46:45 +02:00
|
|
|
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
|
2017-03-31 10:17:14 +02:00
|
|
|
CiColor xy = CiColor::rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f,
|
|
|
|
light.getColorSpace());
|
2017-09-16 00:18:17 +02:00
|
|
|
|
|
|
|
if (switchOffOnBlack && xy.bri == 0)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-03-31 10:17:14 +02:00
|
|
|
light.setOn(false);
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|
2017-03-31 10:17:14 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
light.setOn(true);
|
|
|
|
}
|
2017-09-16 00:18:17 +02:00
|
|
|
// Write color if color has been changed.
|
2017-03-31 10:17:14 +02:00
|
|
|
light.setTransitionTime(transitionTime);
|
|
|
|
light.setColor(xy, brightnessFactor);
|
2017-09-16 00:18:17 +02:00
|
|
|
|
2016-06-03 16:46:45 +02:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
void LedDevicePhilipsHue::stateChanged(bool newState)
|
2016-08-14 10:46:44 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
if(newState)
|
2019-01-01 19:47:07 +01:00
|
|
|
_bridge->bConnect();
|
2017-09-16 00:18:17 +02:00
|
|
|
else
|
|
|
|
lights.clear();
|
2016-06-03 16:46:45 +02:00
|
|
|
}
|