mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
State of all lights is saved and restored on switchOff().
Former-commit-id: 1ee26e8c01d90456424c1b5ea3f113dfd0ff6525
This commit is contained in:
parent
ebb22cdc87
commit
ab5e17e105
@ -1,16 +1,18 @@
|
||||
// Local-Hyperion includes
|
||||
#include "LedDevicePhilipsHue.h"
|
||||
|
||||
#include <iostream>
|
||||
// jsoncpp includes
|
||||
#include <json/json.h>
|
||||
|
||||
// qt includes
|
||||
#include <QtCore/qmath.h>
|
||||
#include <QUrl>
|
||||
#include <QHttpRequestHeader>
|
||||
#include <QThread>
|
||||
#include <QEventLoop>
|
||||
|
||||
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
|
||||
host(output.c_str()), username("newdeveloper") {
|
||||
http = new QHttp(host, 80);
|
||||
http = new QHttp(host);
|
||||
}
|
||||
|
||||
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
|
||||
@ -18,15 +20,20 @@ LedDevicePhilipsHue::~LedDevicePhilipsHue() {
|
||||
}
|
||||
|
||||
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
|
||||
// Save light states if not done before.
|
||||
if (!statesSaved()) {
|
||||
saveStates(ledValues.size());
|
||||
}
|
||||
// Iterate through colors and set light states.
|
||||
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.
|
||||
put(getRoute(lightId), QString("{\"xy\": [%1, %2]}").arg(x).arg(y));
|
||||
put(getStateRoute(lightId), QString("{\"xy\": [%1, %2]}").arg(x).arg(y));
|
||||
// Send brightness color command in JSON format.
|
||||
put(getRoute(lightId), QString("{\"bri\": %1}").arg(qRound(b * 255.0f)));
|
||||
put(getStateRoute(lightId), QString("{\"bri\": %1}").arg(qRound(b * 255.0f)));
|
||||
// Next light id.
|
||||
lightId++;
|
||||
}
|
||||
@ -34,6 +41,11 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
|
||||
}
|
||||
|
||||
int LedDevicePhilipsHue::switchOff() {
|
||||
// If light states have been saved before, ...
|
||||
if (statesSaved()) {
|
||||
// ... restore them.
|
||||
restoreStates();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -47,10 +59,63 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
|
||||
http->request(header, content.toAscii());
|
||||
}
|
||||
|
||||
QString LedDevicePhilipsHue::getRoute(unsigned int lightId) {
|
||||
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) {
|
||||
return QString("lights/%1/state").arg(lightId);
|
||||
}
|
||||
|
||||
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.
|
||||
Json::Value state;
|
||||
if (!reader.parse(QString(response).toStdString(), state)) {
|
||||
// Error occured, break loop.
|
||||
break;
|
||||
}
|
||||
// Save state object.
|
||||
states.push_back(QString(writer.write(state["state"]).c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -18,8 +18,7 @@
|
||||
* 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)
|
||||
*/
|
||||
class LedDevicePhilipsHue : public LedDevice
|
||||
{
|
||||
class LedDevicePhilipsHue: public LedDevice {
|
||||
public:
|
||||
///
|
||||
/// Constructs the device.
|
||||
@ -46,14 +45,26 @@ public:
|
||||
virtual int switchOff();
|
||||
|
||||
private:
|
||||
/// Array to save the light states.
|
||||
std::vector<QString> states;
|
||||
/// Ip address of the bridge
|
||||
QString host;
|
||||
/// User name for the API ("newdeveloper")
|
||||
QString username;
|
||||
/// Qhttp object for sending requests.
|
||||
QHttp* http;
|
||||
|
||||
///
|
||||
/// Sends a HTTP PUT request
|
||||
/// Sends a HTTP GET request (blocking).
|
||||
///
|
||||
/// @param route the URI of the request
|
||||
///
|
||||
/// @return response of the request
|
||||
///
|
||||
QByteArray get(QString route);
|
||||
|
||||
///
|
||||
/// Sends a HTTP PUT request (non-blocking).
|
||||
///
|
||||
/// @param route the URI of the request
|
||||
///
|
||||
@ -64,10 +75,32 @@ private:
|
||||
///
|
||||
/// @param lightId the id of the hue light (starting from 1)
|
||||
///
|
||||
/// @return the URI of the light
|
||||
/// @return the URI of the light state for PUT requests.
|
||||
///
|
||||
QString getStateRoute(unsigned int lightId);
|
||||
|
||||
///
|
||||
/// @param lightId the id of the hue light (starting from 1)
|
||||
///
|
||||
/// @return the URI of the light for GET requests.
|
||||
///
|
||||
QString getRoute(unsigned int lightId);
|
||||
|
||||
///
|
||||
/// Queries the status of all lights and saves it.
|
||||
///
|
||||
/// @param nLights the number of lights
|
||||
///
|
||||
void saveStates(unsigned int nLights);
|
||||
|
||||
/// Restores the status of all lights.
|
||||
void restoreStates();
|
||||
|
||||
///
|
||||
/// @return true if light states have been saved.
|
||||
///
|
||||
bool statesSaved();
|
||||
|
||||
///
|
||||
/// Converts an RGB color to the Hue xy color space and brightness
|
||||
/// https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md
|
||||
@ -84,7 +117,6 @@ private:
|
||||
///
|
||||
/// @param brightness converted brightness component
|
||||
///
|
||||
void rgbToXYBrightness(float red, float green, float blue,
|
||||
float& x, float& y, float& brightness);
|
||||
void rgbToXYBrightness(float red, float green, float blue, float& x, float& y, float& brightness);
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user