Merge pull request #104 from ntim/support_for_philips_hue

Support for philips hue

Former-commit-id: ff2c0b1071cb6a94eb792e590c74fc6eee98c0d9
This commit is contained in:
tvdzwan 2014-05-24 20:51:10 +02:00
commit 4ff400000e
3 changed files with 47 additions and 9 deletions

View File

@ -15,6 +15,7 @@ include_directories(
SET(Leddevice_QT_HEADERS
${CURRENT_SOURCE_DIR}/LedRs232Device.h
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.h
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
)
SET(Leddevice_HEADERS
@ -28,7 +29,6 @@ SET(Leddevice_HEADERS
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
)
SET(Leddevice_SOURCES

View File

@ -13,6 +13,9 @@
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
host(output.c_str()), username("newdeveloper") {
http = new QHttp(host);
timer.setInterval(3000);
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
}
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
@ -23,6 +26,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
// Save light states if not done before.
if (!statesSaved()) {
saveStates(ledValues.size());
switchOn(ledValues.size());
}
// Iterate through colors and set light states.
unsigned int lightId = 1;
@ -37,10 +41,12 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
// Next light id.
lightId++;
}
timer.start();
return 0;
}
int LedDevicePhilipsHue::switchOff() {
timer.stop();
// If light states have been saved before, ...
if (statesSaved()) {
// ... restore them.
@ -54,9 +60,15 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
QHttpRequestHeader header("PUT", url);
header.setValue("Host", host);
header.setValue("Accept-Encoding", "identity");
header.setValue("Connection", "keep-alive");
header.setValue("Content-Length", QString("%1").arg(content.size()));
http->setHost(host);
QEventLoop loop;
// Connect requestFinished signal to quit slot of the loop.
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
// Perfrom request
http->request(header, content.toAscii());
// Go into the loop until the request is finished.
loop.exec();
}
QByteArray LedDevicePhilipsHue::get(QString route) {
@ -92,13 +104,26 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
// Read the response.
QByteArray response = get(getRoute(i + 1));
// Parse JSON.
Json::Value state;
if (!reader.parse(QString(response).toStdString(), state)) {
Json::Value json;
if (!reader.parse(QString(response).toStdString(), json)) {
// Error occured, break loop.
break;
}
// Save state object values which are subject to change.
Json::Value state(Json::objectValue);
state["on"] = json["state"]["on"];
if (json["state"]["on"] == true) {
state["xy"] = json["state"]["xy"];
state["bri"] = json["state"]["bri"];
}
// Save state object.
states.push_back(QString(writer.write(state["state"]).c_str()));
states.push_back(QString(writer.write(state).c_str()).trimmed());
}
}
void LedDevicePhilipsHue::switchOn(unsigned int nLights) {
for (unsigned int i = 0; i < nLights; i++) {
put(getStateRoute(i + 1), "{\"on\": true}");
}
}

View File

@ -4,8 +4,10 @@
#include <string>
// Qt includes
#include <QObject>
#include <QString>
#include <QHttp>
#include <QTimer>
// Leddevice includes
#include <leddevice/LedDevice.h>
@ -20,7 +22,8 @@
*
* @author ntim (github)
*/
class LedDevicePhilipsHue: public LedDevice {
class LedDevicePhilipsHue: public QObject, public LedDevice {
Q_OBJECT
public:
///
/// Constructs the device.
@ -43,9 +46,13 @@ public:
///
virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off
/// Restores the original state of the leds.
virtual int switchOff();
private slots:
/// Restores the status of all lights.
void restoreStates();
private:
/// Array to save the light states.
std::vector<QString> states;
@ -55,6 +62,8 @@ private:
QString username;
/// Qhttp object for sending requests.
QHttp* http;
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
QTimer timer;
///
/// Sends a HTTP GET request (blocking).
@ -95,8 +104,12 @@ private:
///
void saveStates(unsigned int nLights);
/// Restores the status of all lights.
void restoreStates();
///
/// Switches the leds on.
///
/// @param nLights the number of lights
///
void switchOn(unsigned int nLights);
///
/// @return true if light states have been saved.