mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
Philiphs Hue Improvments (#472)
* patch and improve philiphshue * cleanup * cleanup * cleanup * LedDevice switchOn() * add signal for on/off to ledDevice
This commit is contained in:
parent
81f5f51257
commit
74ff5c7ada
@ -40,9 +40,12 @@ public:
|
|||||||
///
|
///
|
||||||
virtual ~LedDevice() {}
|
virtual ~LedDevice() {}
|
||||||
|
|
||||||
/// Switch the leds off
|
/// Switch the leds off (led hardware disable)
|
||||||
virtual int switchOff();
|
virtual int switchOff();
|
||||||
|
|
||||||
|
/// Switch the leds on (led hardware enable), used if reinitialization is required for the device implementation
|
||||||
|
virtual int switchOn();
|
||||||
|
|
||||||
virtual int setLedValues(const std::vector<ColorRgb>& ledValues);
|
virtual int setLedValues(const std::vector<ColorRgb>& ledValues);
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -66,6 +69,13 @@ public:
|
|||||||
|
|
||||||
inline bool componentState() { return enabled(); };
|
inline bool componentState() { return enabled(); };
|
||||||
|
|
||||||
|
signals:
|
||||||
|
///
|
||||||
|
/// Emits whenever the led device switches between on/off
|
||||||
|
/// @param newState The new state of the device
|
||||||
|
///
|
||||||
|
void enableStateChanged(bool newState);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
///
|
///
|
||||||
/// Writes the RGB-Color values to the leds.
|
/// Writes the RGB-Color values to the leds.
|
||||||
|
@ -44,12 +44,18 @@ int LedDevice::open()
|
|||||||
|
|
||||||
void LedDevice::setEnable(bool enable)
|
void LedDevice::setEnable(bool enable)
|
||||||
{
|
{
|
||||||
|
// emit signal when state changed
|
||||||
|
if (_enabled != enable)
|
||||||
|
{
|
||||||
|
emit enableStateChanged(enable);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set black to leds when they should go off
|
||||||
if ( _enabled && !enable)
|
if ( _enabled && !enable)
|
||||||
{
|
{
|
||||||
switchOff();
|
switchOff();
|
||||||
}
|
}
|
||||||
_enabled = enable;
|
_enabled = enable;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int LedDevice::addToDeviceMap(QString name, LedDeviceCreateFuncType funcPtr)
|
int LedDevice::addToDeviceMap(QString name, LedDeviceCreateFuncType funcPtr)
|
||||||
@ -165,6 +171,10 @@ int LedDevice::switchOff()
|
|||||||
return _deviceReady ? write(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK )) : -1;
|
return _deviceReady ? write(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK )) : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int LedDevice::switchOn()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void LedDevice::setLedCount(int ledCount)
|
void LedDevice::setLedCount(int ledCount)
|
||||||
{
|
{
|
||||||
|
@ -3,12 +3,8 @@
|
|||||||
|
|
||||||
// qt includes
|
// qt includes
|
||||||
#include <QtCore/qmath.h>
|
#include <QtCore/qmath.h>
|
||||||
#include <QEventLoop>
|
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
|
|
||||||
const CiColor CiColor::BLACK =
|
|
||||||
{ 0, 0, 0 };
|
|
||||||
|
|
||||||
bool operator ==(CiColor p1, CiColor p2)
|
bool operator ==(CiColor p1, CiColor p2)
|
||||||
{
|
{
|
||||||
return (p1.x == p2.x) && (p1.y == p2.y) && (p1.bri == p2.bri);
|
return (p1.x == p2.x) && (p1.y == p2.y) && (p1.bri == p2.bri);
|
||||||
@ -126,40 +122,89 @@ float CiColor::getDistanceBetweenTwoPoints(CiColor p1, CiColor p2)
|
|||||||
return sqrt(dx * dx + dy * dy);
|
return sqrt(dx * dx + dy * dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray PhilipsHueBridge::get(QString route)
|
PhilipsHueBridge::PhilipsHueBridge(Logger* log, QString host, QString username)
|
||||||
|
: QObject()
|
||||||
|
, log(log)
|
||||||
|
, host(host)
|
||||||
|
, username(username)
|
||||||
{
|
{
|
||||||
QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route);
|
// setup reconnection timer
|
||||||
Debug(log, "Get %s", url.toStdString().c_str());
|
bTimer.setInterval(5000);
|
||||||
// Perfrom request
|
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);
|
QNetworkRequest request(url);
|
||||||
QNetworkReply* reply = manager->get(request);
|
manager.get(request);
|
||||||
// Connect requestFinished signal to quit slot of the loop.
|
}
|
||||||
QEventLoop loop;
|
}
|
||||||
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
|
void PhilipsHueBridge::resolveReply(QNetworkReply* reply)
|
||||||
// Go into the loop until the request is finished.
|
{
|
||||||
loop.exec();
|
// TODO use put request also for network error checking with decent threshold
|
||||||
// Read all data of the response.
|
if(reply->operation() == QNetworkAccessManager::GetOperation)
|
||||||
|
{
|
||||||
|
if(reply->error() == QNetworkReply::NoError)
|
||||||
|
{
|
||||||
QByteArray response = reply->readAll();
|
QByteArray response = reply->readAll();
|
||||||
// Free space.
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
// Return response;
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhilipsHueBridge::post(QString route, QString content)
|
void PhilipsHueBridge::post(QString route, QString content)
|
||||||
{
|
{
|
||||||
QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route);
|
Debug(log, "Post %s: %s", QSTRING_CSTR(QString("http://IP/api/USR/%1").arg(route)), QSTRING_CSTR(content));
|
||||||
Debug(log, "Post %s: %s", url.toStdString().c_str(), content.toStdString().c_str());
|
|
||||||
// Perfrom request
|
QNetworkRequest request(QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route));
|
||||||
QNetworkRequest request(url);
|
manager.put(request, content.toLatin1());
|
||||||
QNetworkReply* reply = manager->put(request, content.toLatin1());
|
|
||||||
// Connect finished signal to quit slot of the loop.
|
|
||||||
QEventLoop loop;
|
|
||||||
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
|
|
||||||
// Go into the loop until the request is finished.
|
|
||||||
loop.exec();
|
|
||||||
// Free space.
|
|
||||||
reply->deleteLater();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::set<QString> PhilipsHueLight::GAMUT_A_MODEL_IDS =
|
const std::set<QString> PhilipsHueLight::GAMUT_A_MODEL_IDS =
|
||||||
@ -167,54 +212,42 @@ const std::set<QString> PhilipsHueLight::GAMUT_A_MODEL_IDS =
|
|||||||
const std::set<QString> PhilipsHueLight::GAMUT_B_MODEL_IDS =
|
const std::set<QString> PhilipsHueLight::GAMUT_B_MODEL_IDS =
|
||||||
{ "LCT001", "LCT002", "LCT003", "LCT007", "LLM001" };
|
{ "LCT001", "LCT002", "LCT003", "LCT007", "LLM001" };
|
||||||
const std::set<QString> PhilipsHueLight::GAMUT_C_MODEL_IDS =
|
const std::set<QString> PhilipsHueLight::GAMUT_C_MODEL_IDS =
|
||||||
{ "LLC020", "LST002" };
|
{ "LLC020", "LST002", "LCT011", "LCT012", "LCT010", "LCT014" };
|
||||||
|
|
||||||
PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned int id) :
|
PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned int id, QJsonObject values)
|
||||||
log(log), bridge(bridge), id(id)
|
: log(log)
|
||||||
|
, bridge(bridge)
|
||||||
|
, id(id)
|
||||||
{
|
{
|
||||||
|
|
||||||
// Get model id and original state.
|
|
||||||
QByteArray response = bridge.get(QString("lights/%1").arg(id));
|
|
||||||
// Use JSON parser to parse response.
|
|
||||||
QJsonParseError error;
|
|
||||||
QJsonDocument reader = QJsonDocument::fromJson(response, &error);
|
|
||||||
;
|
|
||||||
// Parse response.
|
|
||||||
if (error.error != QJsonParseError::NoError)
|
|
||||||
{
|
|
||||||
Error(log, "Got invalid response from light %d", id);
|
|
||||||
}
|
|
||||||
// Get state object values which are subject to change.
|
// Get state object values which are subject to change.
|
||||||
QJsonObject json = reader.object();
|
if (!values["state"].toObject().contains("on"))
|
||||||
if (!json["state"].toObject().contains("on"))
|
|
||||||
{
|
{
|
||||||
Error(log, "Got no state object from light %d", id);
|
Error(log, "Got invalid state object from light ID %d", id);
|
||||||
}
|
|
||||||
if (!json["state"].toObject().contains("on"))
|
|
||||||
{
|
|
||||||
Error(log, "Got invalid state object from light %d", id);
|
|
||||||
}
|
}
|
||||||
QJsonObject state;
|
QJsonObject state;
|
||||||
state["on"] = json["state"].toObject()["on"];
|
state["on"] = values["state"].toObject()["on"];
|
||||||
on = false;
|
on = false;
|
||||||
if (json["state"].toObject()["on"].toBool() == true)
|
if (values["state"].toObject()["on"].toBool())
|
||||||
{
|
{
|
||||||
state["xy"] = json["state"].toObject()["xy"];
|
state["xy"] = values["state"].toObject()["xy"];
|
||||||
state["bri"] = json["state"].toObject()["bri"];
|
state["bri"] = values["state"].toObject()["bri"];
|
||||||
on = true;
|
on = true;
|
||||||
|
|
||||||
color =
|
color = {
|
||||||
{ (float) state["xy"].toArray()[0].toDouble(),(float) state["xy"].toArray()[1].toDouble(), (float) state["bri"].toDouble() / 255.0f};
|
(float) state["xy"].toArray()[0].toDouble(),
|
||||||
transitionTime = json["state"].toObject()["transitiontime"].toInt();
|
(float) state["xy"].toArray()[1].toDouble(),
|
||||||
|
(float) state["bri"].toDouble() / 255.0f
|
||||||
|
};
|
||||||
|
transitionTime = values["state"].toObject()["transitiontime"].toInt();
|
||||||
}
|
}
|
||||||
// Determine the model id.
|
// Determine the model id.
|
||||||
modelId = json["modelid"].toString().trimmed().replace("\"", "");
|
modelId = values["modelid"].toString().trimmed().replace("\"", "");
|
||||||
// Determine the original state.
|
// Determine the original state.
|
||||||
originalState = QJsonDocument(state).toJson(QJsonDocument::JsonFormat::Compact).trimmed();
|
originalState = QJsonDocument(state).toJson(QJsonDocument::JsonFormat::Compact).trimmed();
|
||||||
// Find id in the sets and set the appropriate color space.
|
// Find id in the sets and set the appropriate color space.
|
||||||
if (GAMUT_A_MODEL_IDS.find(modelId) != GAMUT_A_MODEL_IDS.end())
|
if (GAMUT_A_MODEL_IDS.find(modelId) != GAMUT_A_MODEL_IDS.end())
|
||||||
{
|
{
|
||||||
Debug(log, "Recognized model id %s as gamut A", modelId.toStdString().c_str());
|
Debug(log, "Recognized model id %s of light ID %d as gamut A", modelId.toStdString().c_str(), id);
|
||||||
colorSpace.red =
|
colorSpace.red =
|
||||||
{ 0.703f, 0.296f};
|
{ 0.703f, 0.296f};
|
||||||
colorSpace.green =
|
colorSpace.green =
|
||||||
@ -224,7 +257,7 @@ PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned
|
|||||||
}
|
}
|
||||||
else if (GAMUT_B_MODEL_IDS.find(modelId) != GAMUT_B_MODEL_IDS.end())
|
else if (GAMUT_B_MODEL_IDS.find(modelId) != GAMUT_B_MODEL_IDS.end())
|
||||||
{
|
{
|
||||||
Debug(log, "Recognized model id %s as gamut B", modelId.toStdString().c_str());
|
Debug(log, "Recognized model id %s of light ID %d as gamut B", modelId.toStdString().c_str(), id);
|
||||||
colorSpace.red =
|
colorSpace.red =
|
||||||
{ 0.675f, 0.322f};
|
{ 0.675f, 0.322f};
|
||||||
colorSpace.green =
|
colorSpace.green =
|
||||||
@ -234,7 +267,7 @@ PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned
|
|||||||
}
|
}
|
||||||
else if (GAMUT_C_MODEL_IDS.find(modelId) != GAMUT_C_MODEL_IDS.end())
|
else if (GAMUT_C_MODEL_IDS.find(modelId) != GAMUT_C_MODEL_IDS.end())
|
||||||
{
|
{
|
||||||
Debug(log, "Recognized model id %s as gamut C", modelId.toStdString().c_str());
|
Debug(log, "Recognized model id %s of light ID %d as gamut C", modelId.toStdString().c_str(), id);
|
||||||
colorSpace.red =
|
colorSpace.red =
|
||||||
{ 0.675f, 0.322f};
|
{ 0.675f, 0.322f};
|
||||||
colorSpace.green =
|
colorSpace.green =
|
||||||
@ -244,7 +277,7 @@ PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Warning(log, "Did not recognize model id %s", modelId.toStdString().c_str());
|
Warning(log, "Did not recognize model id %s of light ID %d", modelId.toStdString().c_str(), id);
|
||||||
colorSpace.red =
|
colorSpace.red =
|
||||||
{ 1.0f, 0.0f};
|
{ 1.0f, 0.0f};
|
||||||
colorSpace.green =
|
colorSpace.green =
|
||||||
@ -252,6 +285,8 @@ PhilipsHueLight::PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned
|
|||||||
colorSpace.blue =
|
colorSpace.blue =
|
||||||
{ 0.0f, 0.0f};
|
{ 0.0f, 0.0f};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Info(log,"Light ID %d created", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
PhilipsHueLight::~PhilipsHueLight()
|
PhilipsHueLight::~PhilipsHueLight()
|
||||||
@ -304,154 +339,121 @@ CiColorTriangle PhilipsHueLight::getColorSpace() const
|
|||||||
return colorSpace;
|
return colorSpace;
|
||||||
}
|
}
|
||||||
|
|
||||||
LedDevicePhilipsHue::LedDevicePhilipsHue(const QJsonObject &deviceConfig) :
|
|
||||||
LedDevice()
|
|
||||||
{
|
|
||||||
manager = new QNetworkAccessManager();
|
|
||||||
_deviceReady = init(deviceConfig);
|
|
||||||
|
|
||||||
timer.setInterval(3000);
|
|
||||||
timer.setSingleShot(true);
|
|
||||||
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
|
|
||||||
}
|
|
||||||
|
|
||||||
LedDevicePhilipsHue::~LedDevicePhilipsHue()
|
|
||||||
{
|
|
||||||
// Switch off.
|
|
||||||
switchOff();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LedDevicePhilipsHue::init(const QJsonObject &deviceConfig)
|
|
||||||
{
|
|
||||||
LedDevice::init(deviceConfig);
|
|
||||||
|
|
||||||
bridge =
|
|
||||||
{ _log, manager, deviceConfig["output"].toString(), deviceConfig["username"].toString("newdeveloper")};
|
|
||||||
switchOffOnBlack = deviceConfig["switchOffOnBlack"].toBool(true);
|
|
||||||
brightnessFactor = (float) deviceConfig["brightnessFactor"].toDouble(1.0);
|
|
||||||
transitionTime = deviceConfig["transitiontime"].toInt(1);
|
|
||||||
lightIds.clear();
|
|
||||||
QJsonArray lArray = deviceConfig["lightIds"].toArray();
|
|
||||||
for (int i = 0; i < lArray.size(); i++)
|
|
||||||
{
|
|
||||||
lightIds.push_back(lArray[i].toInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
LedDevice* LedDevicePhilipsHue::construct(const QJsonObject &deviceConfig)
|
LedDevice* LedDevicePhilipsHue::construct(const QJsonObject &deviceConfig)
|
||||||
{
|
{
|
||||||
return new LedDevicePhilipsHue(deviceConfig);
|
return new LedDevicePhilipsHue(deviceConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LedDevicePhilipsHue::LedDevicePhilipsHue(const QJsonObject& deviceConfig)
|
||||||
|
: LedDevice()
|
||||||
|
, bridge(_log, deviceConfig["output"].toString(), deviceConfig["username"].toString())
|
||||||
|
{
|
||||||
|
_deviceReady = init(deviceConfig);
|
||||||
|
|
||||||
|
connect(&bridge, &PhilipsHueBridge::newLights, this, &LedDevicePhilipsHue::newLights);
|
||||||
|
connect(this, &LedDevice::enableStateChanged, this, &LedDevicePhilipsHue::stateChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
LedDevicePhilipsHue::~LedDevicePhilipsHue()
|
||||||
|
{
|
||||||
|
switchOff();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LedDevicePhilipsHue::init(const QJsonObject &deviceConfig)
|
||||||
|
{
|
||||||
|
switchOffOnBlack = deviceConfig["switchOffOnBlack"].toBool(true);
|
||||||
|
brightnessFactor = (float) deviceConfig["brightnessFactor"].toDouble(1.0);
|
||||||
|
transitionTime = deviceConfig["transitiontime"].toInt(1);
|
||||||
|
QJsonArray lArray = deviceConfig["lightIds"].toArray();
|
||||||
|
|
||||||
|
QJsonObject newDC = deviceConfig;
|
||||||
|
if(!lArray.empty())
|
||||||
|
{
|
||||||
|
for(const auto i : lArray)
|
||||||
|
{
|
||||||
|
lightIds.push_back(i.toInt());
|
||||||
|
}
|
||||||
|
// get light info from bridge
|
||||||
|
bridge.bConnect();
|
||||||
|
|
||||||
|
// adapt latchTime to count of user lightIds (bridge 10Hz max overall)
|
||||||
|
newDC.insert("latchTime",QJsonValue(100*(int)lightIds.size()));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Error(_log,"No light ID provided, abort");
|
||||||
|
}
|
||||||
|
|
||||||
|
LedDevice::init(newDC);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LedDevicePhilipsHue::newLights(QMap<quint16, QJsonObject> map)
|
||||||
|
{
|
||||||
|
if(!lightIds.empty())
|
||||||
|
{
|
||||||
|
// search user lightid inside map and create light if found
|
||||||
|
lights.clear();
|
||||||
|
for(const auto id : lightIds)
|
||||||
|
{
|
||||||
|
if (map.contains(id))
|
||||||
|
{
|
||||||
|
lights.push_back(PhilipsHueLight(_log, bridge, id, map.value(id)));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Error(_log,"Light id %d isn't used on this bridge", id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues)
|
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues)
|
||||||
{
|
{
|
||||||
// Save light states if not done before.
|
// lights will be empty sometimes
|
||||||
if (!areStatesSaved())
|
if(lights.empty()) return -1;
|
||||||
|
|
||||||
|
// more lights then leds, stop always
|
||||||
|
if(ledValues.size() < lights.size())
|
||||||
{
|
{
|
||||||
saveStates((unsigned int) ledValues.size());
|
Error(_log,"More LightIDs configured than leds, each LightID requires one led!");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
// If there are less states saved than colors given, then maybe something went wrong before.
|
|
||||||
if (lights.size() != ledValues.size())
|
// Iterate through lights and set colors.
|
||||||
{
|
|
||||||
restoreStates();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// Iterate through colors and set light states.
|
|
||||||
unsigned int idx = 0;
|
unsigned int idx = 0;
|
||||||
for (const ColorRgb& color : ledValues)
|
for (PhilipsHueLight& light : lights)
|
||||||
{
|
{
|
||||||
// Get lamp.
|
// Get color.
|
||||||
PhilipsHueLight& light = lights.at(idx);
|
ColorRgb color = ledValues.at(idx);
|
||||||
|
|
||||||
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
|
// Scale colors from [0, 255] to [0, 1] and convert to xy space.
|
||||||
CiColor xy = CiColor::rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f,
|
CiColor xy = CiColor::rgbToCiColor(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f,
|
||||||
light.getColorSpace());
|
light.getColorSpace());
|
||||||
// Write color if color has been changed.
|
|
||||||
if (switchOffOnBlack && light.getColor() != CiColor::BLACK && xy == CiColor::BLACK)
|
if (switchOffOnBlack && xy.bri == 0)
|
||||||
{
|
{
|
||||||
light.setOn(false);
|
light.setOn(false);
|
||||||
}
|
}
|
||||||
else if (switchOffOnBlack && light.getColor() == CiColor::BLACK && xy != CiColor::BLACK)
|
|
||||||
{
|
|
||||||
light.setOn(true);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
light.setOn(true);
|
light.setOn(true);
|
||||||
}
|
}
|
||||||
|
// Write color if color has been changed.
|
||||||
light.setTransitionTime(transitionTime);
|
light.setTransitionTime(transitionTime);
|
||||||
light.setColor(xy, brightnessFactor);
|
light.setColor(xy, brightnessFactor);
|
||||||
// Next light id.
|
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
// Reset timer.
|
|
||||||
timer.start();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int LedDevicePhilipsHue::switchOff()
|
void LedDevicePhilipsHue::stateChanged(bool newState)
|
||||||
{
|
|
||||||
timer.stop();
|
|
||||||
// If light states have been saved before, ...
|
|
||||||
if (areStatesSaved())
|
|
||||||
{
|
|
||||||
// ... restore them.
|
|
||||||
restoreStates();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LedDevicePhilipsHue::saveStates(unsigned int nLights)
|
|
||||||
{
|
|
||||||
|
|
||||||
// Clear saved lamps.
|
|
||||||
lights.clear();
|
|
||||||
//
|
|
||||||
if (nLights == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Read light ids if none have been supplied by the user.
|
|
||||||
if (lightIds.size() != nLights)
|
|
||||||
{
|
|
||||||
lightIds.clear();
|
|
||||||
// Retrieve lights from bridge.
|
|
||||||
QByteArray response = bridge.get("lights");
|
|
||||||
// Use QJsonDocument to parse reponse.
|
|
||||||
QJsonParseError error;
|
|
||||||
QJsonDocument reader = QJsonDocument::fromJson(response, &error);
|
|
||||||
if (error.error != QJsonParseError::NoError)
|
|
||||||
{
|
|
||||||
Error(_log, "No lights found.");
|
|
||||||
}
|
|
||||||
// Loop over all children.
|
|
||||||
QJsonObject json = reader.object();
|
|
||||||
for (QJsonObject::iterator it = json.begin(); it != json.end() && lightIds.size() < nLights; it++)
|
|
||||||
{
|
|
||||||
int lightId = atoi(it.key().toStdString().c_str());
|
|
||||||
lightIds.push_back(lightId);
|
|
||||||
Debug(_log, "nLights=%d: found light with id %d.", nLights, lightId);
|
|
||||||
}
|
|
||||||
// Check if we found enough lights.
|
|
||||||
if (lightIds.size() != nLights)
|
|
||||||
{
|
|
||||||
Error(_log, "Not enough lights found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Iterate lights.
|
|
||||||
for (unsigned int i = 0; i < nLights; i++)
|
|
||||||
{
|
|
||||||
lights.push_back(PhilipsHueLight(_log, bridge, lightIds.at(i)));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void LedDevicePhilipsHue::restoreStates()
|
|
||||||
{
|
{
|
||||||
|
if(newState)
|
||||||
|
bridge.bConnect();
|
||||||
|
else
|
||||||
lights.clear();
|
lights.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LedDevicePhilipsHue::areStatesSaved()
|
|
||||||
{
|
|
||||||
return !lights.empty();
|
|
||||||
}
|
|
||||||
|
@ -24,8 +24,6 @@ struct CiColor
|
|||||||
float y;
|
float y;
|
||||||
/// The brightness.
|
/// The brightness.
|
||||||
float bri;
|
float bri;
|
||||||
/// Black color constant.
|
|
||||||
static const CiColor BLACK;
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Converts an RGB color to the Hue xy color space and brightness.
|
/// Converts an RGB color to the Hue xy color space and brightness.
|
||||||
@ -89,34 +87,42 @@ struct CiColorTriangle
|
|||||||
CiColor red, green, blue;
|
CiColor red, green, blue;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PhilipsHueBridge
|
class PhilipsHueBridge : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Logger* log;
|
Logger* log;
|
||||||
/// QNetworkAccessManager object for sending requests.
|
/// QNetworkAccessManager for sending requests.
|
||||||
QNetworkAccessManager* manager;
|
QNetworkAccessManager manager;
|
||||||
/// Ip address of the bridge
|
/// Ip address of the bridge
|
||||||
QString host;
|
QString host;
|
||||||
/// User name for the API ("newdeveloper")
|
/// User name for the API ("newdeveloper")
|
||||||
QString username;
|
QString username;
|
||||||
|
/// Timer for bridge reconnect interval
|
||||||
|
QTimer bTimer;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
///
|
||||||
|
/// Receive all replies and check for error, schedule reconnect on issues
|
||||||
|
/// Emits newLights() on success when triggered from connect()
|
||||||
|
///
|
||||||
|
void resolveReply(QNetworkReply* reply);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
///
|
||||||
|
/// Connect to bridge to check availbility and user
|
||||||
|
///
|
||||||
|
void bConnect(void);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
///
|
||||||
|
/// Emits with a QMap of current bridge light/value pairs
|
||||||
|
///
|
||||||
|
void newLights(QMap<quint16,QJsonObject> map);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
PhilipsHueBridge(Logger* log, QNetworkAccessManager* manager, QString host, QString username) :
|
PhilipsHueBridge(Logger* log, QString host, QString username);
|
||||||
log(log), manager(manager), host(host), username(username)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
PhilipsHueBridge()
|
|
||||||
{
|
|
||||||
log = NULL;
|
|
||||||
manager = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
|
||||||
/// @param route the route of the GET request.
|
|
||||||
///
|
|
||||||
/// @return the response of the GET request.
|
|
||||||
///
|
|
||||||
QByteArray get(QString route);
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// @param route the route of the POST request.
|
/// @param route the route of the POST request.
|
||||||
@ -134,6 +140,7 @@ class PhilipsHueLight
|
|||||||
private:
|
private:
|
||||||
Logger* log;
|
Logger* log;
|
||||||
PhilipsHueBridge& bridge;
|
PhilipsHueBridge& bridge;
|
||||||
|
/// light id
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
bool on;
|
bool on;
|
||||||
unsigned int transitionTime;
|
unsigned int transitionTime;
|
||||||
@ -165,7 +172,7 @@ public:
|
|||||||
/// @param bridge the bridge
|
/// @param bridge the bridge
|
||||||
/// @param id the light id
|
/// @param id the light id
|
||||||
///
|
///
|
||||||
PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned int id);
|
PhilipsHueLight(Logger* log, PhilipsHueBridge& bridge, unsigned int id, QJsonObject values);
|
||||||
~PhilipsHueLight();
|
~PhilipsHueLight();
|
||||||
|
|
||||||
///
|
///
|
||||||
@ -196,8 +203,6 @@ public:
|
|||||||
*
|
*
|
||||||
* To use set the device to "philipshue".
|
* To use set the device to "philipshue".
|
||||||
* Uses the official Philips Hue API (http://developers.meethue.com).
|
* Uses the official Philips Hue API (http://developers.meethue.com).
|
||||||
* Framegrabber must be limited to 10 Hz / numer of lights to avoid rate limitation by the hue bridge.
|
|
||||||
* Create a new API user name "newdeveloper" on the bridge (http://developers.meethue.com/gettingstarted.html)
|
|
||||||
*
|
*
|
||||||
* @author ntim (github), bimsarck (github)
|
* @author ntim (github), bimsarck (github)
|
||||||
*/
|
*/
|
||||||
@ -222,12 +227,14 @@ public:
|
|||||||
/// constructs leddevice
|
/// constructs leddevice
|
||||||
static LedDevice* construct(const QJsonObject &deviceConfig);
|
static LedDevice* construct(const QJsonObject &deviceConfig);
|
||||||
|
|
||||||
/// Restores the original state of the leds.
|
|
||||||
virtual int switchOff();
|
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
/// Restores the status of all lights.
|
/// creates new PhilipsHueLight(s) based on user lightid with bridge feedback
|
||||||
void restoreStates();
|
///
|
||||||
|
/// @param map Map of lightid/value pairs of bridge
|
||||||
|
///
|
||||||
|
void newLights(QMap<quint16, QJsonObject> map);
|
||||||
|
|
||||||
|
void stateChanged(bool newState);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
///
|
///
|
||||||
@ -241,10 +248,9 @@ protected:
|
|||||||
bool init(const QJsonObject &deviceConfig);
|
bool init(const QJsonObject &deviceConfig);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QNetworkAccessManager* manager;
|
/// bridge class
|
||||||
PhilipsHueBridge bridge;
|
PhilipsHueBridge bridge;
|
||||||
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
|
|
||||||
QTimer timer;
|
|
||||||
///
|
///
|
||||||
bool switchOffOnBlack;
|
bool switchOffOnBlack;
|
||||||
/// The brightness factor to multiply on color change.
|
/// The brightness factor to multiply on color change.
|
||||||
@ -256,17 +262,4 @@ private:
|
|||||||
std::vector<unsigned int> lightIds;
|
std::vector<unsigned int> lightIds;
|
||||||
/// Array to save the lamps.
|
/// Array to save the lamps.
|
||||||
std::vector<PhilipsHueLight> lights;
|
std::vector<PhilipsHueLight> lights;
|
||||||
|
|
||||||
///
|
|
||||||
/// Queries the status of all lights and saves it.
|
|
||||||
///
|
|
||||||
/// @param nLights the number of lights
|
|
||||||
///
|
|
||||||
void saveStates(unsigned int nLights);
|
|
||||||
|
|
||||||
///
|
|
||||||
/// @return true if light states have been saved.
|
|
||||||
///
|
|
||||||
bool areStatesSaved();
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
"output": {
|
"output": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"title":"edt_dev_spec_targetIp_title",
|
"title":"edt_dev_spec_targetIp_title",
|
||||||
|
"default":"",
|
||||||
"propertyOrder" : 1
|
"propertyOrder" : 1
|
||||||
},
|
},
|
||||||
"username": {
|
"username": {
|
||||||
@ -30,30 +31,21 @@
|
|||||||
"type": "number",
|
"type": "number",
|
||||||
"title":"edt_dev_spec_brightnessFactor_title",
|
"title":"edt_dev_spec_brightnessFactor_title",
|
||||||
"default" : 1.0,
|
"default" : 1.0,
|
||||||
"minimum" : 0.0,
|
"minimum" : 0.5,
|
||||||
"maximum" : 1.0,
|
"maximum" : 10.0,
|
||||||
"propertyOrder" : 5
|
"propertyOrder" : 5
|
||||||
},
|
},
|
||||||
"lightIds": {
|
"lightIds": {
|
||||||
"type": "array",
|
"type": "array",
|
||||||
"title":"edt_dev_spec_lightid_title",
|
"title":"edt_dev_spec_lightid_title",
|
||||||
"minItems": 1,
|
"minItems": 1,
|
||||||
|
"uniqueItems" : true,
|
||||||
"items" : {
|
"items" : {
|
||||||
"type" : "integer",
|
"type" : "integer",
|
||||||
"minimum" : 0,
|
"minimum" : 0,
|
||||||
"title" : "edt_dev_spec_lightid_itemtitle"
|
"title" : "edt_dev_spec_lightid_itemtitle"
|
||||||
},
|
},
|
||||||
"propertyOrder" : 6
|
"propertyOrder" : 6
|
||||||
},
|
|
||||||
"latchTime": {
|
|
||||||
"type": "integer",
|
|
||||||
"title":"edt_dev_spec_latchtime_title",
|
|
||||||
"default": 200,
|
|
||||||
"append" : "edt_append_ms",
|
|
||||||
"minimum": 100,
|
|
||||||
"maximum": 1000,
|
|
||||||
"access" : "expert",
|
|
||||||
"propertyOrder" : 7
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": true
|
"additionalProperties": true
|
||||||
|
Loading…
Reference in New Issue
Block a user