Former-commit-id: deb8e613728d62804d72716bcebaf87b660f5290
This commit is contained in:
T. van der Zwan 2014-05-24 21:11:31 +02:00
commit d37960c609
4 changed files with 48 additions and 10 deletions

View File

@ -1 +1 @@
bfe399d8f3299c6110179fddb9f2b06b475d6a53 e6c826638971667596af0fb18e819514d8390286

View File

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

View File

@ -13,6 +13,9 @@
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) : LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
host(output.c_str()), username("newdeveloper") { host(output.c_str()), username("newdeveloper") {
http = new QHttp(host); http = new QHttp(host);
timer.setInterval(3000);
timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
} }
LedDevicePhilipsHue::~LedDevicePhilipsHue() { LedDevicePhilipsHue::~LedDevicePhilipsHue() {
@ -23,6 +26,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
// Save light states if not done before. // Save light states if not done before.
if (!statesSaved()) { if (!statesSaved()) {
saveStates(ledValues.size()); saveStates(ledValues.size());
switchOn(ledValues.size());
} }
// Iterate through colors and set light states. // Iterate through colors and set light states.
unsigned int lightId = 1; unsigned int lightId = 1;
@ -37,10 +41,12 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
// Next light id. // Next light id.
lightId++; lightId++;
} }
timer.start();
return 0; return 0;
} }
int LedDevicePhilipsHue::switchOff() { int LedDevicePhilipsHue::switchOff() {
timer.stop();
// If light states have been saved before, ... // If light states have been saved before, ...
if (statesSaved()) { if (statesSaved()) {
// ... restore them. // ... restore them.
@ -54,9 +60,15 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
QHttpRequestHeader header("PUT", url); QHttpRequestHeader header("PUT", url);
header.setValue("Host", host); header.setValue("Host", host);
header.setValue("Accept-Encoding", "identity"); header.setValue("Accept-Encoding", "identity");
header.setValue("Connection", "keep-alive");
header.setValue("Content-Length", QString("%1").arg(content.size())); 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()); http->request(header, content.toAscii());
// Go into the loop until the request is finished.
loop.exec();
} }
QByteArray LedDevicePhilipsHue::get(QString route) { QByteArray LedDevicePhilipsHue::get(QString route) {
@ -92,13 +104,26 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
// Read the response. // Read the response.
QByteArray response = get(getRoute(i + 1)); QByteArray response = get(getRoute(i + 1));
// Parse JSON. // Parse JSON.
Json::Value state; Json::Value json;
if (!reader.parse(QString(response).toStdString(), state)) { if (!reader.parse(QString(response).toStdString(), json)) {
// Error occured, break loop. // Error occured, break loop.
break; 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. // 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> #include <string>
// Qt includes // Qt includes
#include <QObject>
#include <QString> #include <QString>
#include <QHttp> #include <QHttp>
#include <QTimer>
// Leddevice includes // Leddevice includes
#include <leddevice/LedDevice.h> #include <leddevice/LedDevice.h>
@ -20,7 +22,8 @@
* *
* @author ntim (github) * @author ntim (github)
*/ */
class LedDevicePhilipsHue: public LedDevice { class LedDevicePhilipsHue: public QObject, public LedDevice {
Q_OBJECT
public: public:
/// ///
/// Constructs the device. /// Constructs the device.
@ -43,9 +46,13 @@ public:
/// ///
virtual int write(const std::vector<ColorRgb> & ledValues); virtual int write(const std::vector<ColorRgb> & ledValues);
/// Switch the leds off /// Restores the original state of the leds.
virtual int switchOff(); virtual int switchOff();
private slots:
/// Restores the status of all lights.
void restoreStates();
private: private:
/// Array to save the light states. /// Array to save the light states.
std::vector<QString> states; std::vector<QString> states;
@ -55,6 +62,8 @@ private:
QString username; QString username;
/// Qhttp object for sending requests. /// Qhttp object for sending requests.
QHttp* http; QHttp* http;
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
QTimer timer;
/// ///
/// Sends a HTTP GET request (blocking). /// Sends a HTTP GET request (blocking).
@ -95,8 +104,12 @@ private:
/// ///
void saveStates(unsigned int nLights); 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. /// @return true if light states have been saved.