Added config switch for turing off the lamps if the color black is written.

Former-commit-id: bb4f4bc74c035c10a8dc678a11052ea276ea0149
This commit is contained in:
Tim Niggemann 2014-07-16 11:49:34 +02:00
parent fcb2ff6667
commit b055578759
3 changed files with 24 additions and 9 deletions

View File

@ -164,7 +164,8 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
else if (type == "philipshue") else if (type == "philipshue")
{ {
const std::string output = deviceConfig["output"].asString(); const std::string output = deviceConfig["output"].asString();
device = new LedDevicePhilipsHue(output); const bool switchOffOnBlack = deviceConfig.get("switch_off_on_black", false).asBool();
device = new LedDevicePhilipsHue(output, switchOffOnBlack);
} }
else if (type == "test") else if (type == "test")
{ {

View File

@ -12,8 +12,10 @@
#include <set> #include <set>
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) : const ColorPoint LedDevicePhilipsHue::BLACK = {0.0f, 0.0f, 0.0f};
host(output.c_str()), username("newdeveloper") {
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, bool switchOffOnBlack) :
host(output.c_str()), username("newdeveloper"), switchOffOnBlack(switchOffOnBlack) {
http = new QHttp(host); http = new QHttp(host);
timer.setInterval(3000); timer.setInterval(3000);
timer.setSingleShot(true); timer.setSingleShot(true);
@ -37,12 +39,19 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
HueLamp& lamp = lamps.at(idx); HueLamp& lamp = lamps.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.
ColorPoint xy = rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp); ColorPoint xy = rgbToXYBrightness(color.red / 255.0f, color.green / 255.0f, color.blue / 255.0f, lamp);
// Switch lamp off if switchOffOnBlack is enabled and the lamp is currently on.
if (switchOffOnBlack && xy == BLACK && lamp.color != BLACK) {
put(getStateRoute(lamp.id), QString("{\"on\": false}"));
}
// Write color if color has been changed. // Write color if color has been changed.
if (xy != lamp.color) { else if (xy != lamp.color) {
// Send adjust color command in JSON format. // Switch on if the lamp has been previously switched off.
put(getStateRoute(lamp.id), QString("{\"xy\": [%1, %2]}").arg(xy.x).arg(xy.y)); if (switchOffOnBlack && lamp.color == BLACK) {
// Send brightness color command in JSON format. put(getStateRoute(lamp.id), QString("{\"on\": true}"));
put(getStateRoute(lamp.id), QString("{\"bri\": %1}").arg(qRound(xy.bri * 255.0f))); }
// Send adjust color and brightness command in JSON format.
put(getStateRoute(lamp.id), QString("{\"xy\": [%1, %2], \"bri\": %1}").arg(xy.x).arg(xy.y)
.arg(qRound(xy.bri * 255.0f)));
// Remember written color. // Remember written color.
lamp.color = xy; lamp.color = xy;
} }

View File

@ -71,7 +71,9 @@ public:
/// ///
/// @param output the ip address of the bridge /// @param output the ip address of the bridge
/// ///
LedDevicePhilipsHue(const std::string& output); /// @param switchOffOnBlack kill lights for black
///
LedDevicePhilipsHue(const std::string& output, bool switchOffOnBlack);
/// ///
/// Destructor of this device /// Destructor of this device
@ -95,6 +97,7 @@ private slots:
void restoreStates(); void restoreStates();
private: private:
const static ColorPoint BLACK;
/// Array to save the lamps. /// Array to save the lamps.
std::vector<HueLamp> lamps; std::vector<HueLamp> lamps;
/// Ip address of the bridge /// Ip address of the bridge
@ -105,6 +108,8 @@ private:
QHttp* http; QHttp* http;
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF". /// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
QTimer timer; QTimer timer;
///
bool switchOffOnBlack;
/// ///
/// Sends a HTTP GET request (blocking). /// Sends a HTTP GET request (blocking).