Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit ff2c0b1

Browse files
committed
Merge pull request #104 from ntim/support_for_philips_hue
Support for philips hue
2 parents d1dc28d + 04959ea commit ff2c0b1

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

libsrc/leddevice/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ include_directories(
1515
SET(Leddevice_QT_HEADERS
1616
${CURRENT_SOURCE_DIR}/LedRs232Device.h
1717
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.h
18+
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
1819
)
1920

2021
SET(Leddevice_HEADERS
@@ -28,7 +29,6 @@ SET(Leddevice_HEADERS
2829
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
2930
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
3031
${CURRENT_SOURCE_DIR}/LedDeviceHyperionUsbasp.h
31-
${CURRENT_SOURCE_DIR}/LedDevicePhilipsHue.h
3232
)
3333

3434
SET(Leddevice_SOURCES

libsrc/leddevice/LedDevicePhilipsHue.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) :
1414
host(output.c_str()), username("newdeveloper") {
1515
http = new QHttp(host);
16+
timer.setInterval(3000);
17+
timer.setSingleShot(true);
18+
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
1619
}
1720

1821
LedDevicePhilipsHue::~LedDevicePhilipsHue() {
@@ -23,6 +26,7 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
2326
// Save light states if not done before.
2427
if (!statesSaved()) {
2528
saveStates(ledValues.size());
29+
switchOn(ledValues.size());
2630
}
2731
// Iterate through colors and set light states.
2832
unsigned int lightId = 1;
@@ -37,10 +41,12 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
3741
// Next light id.
3842
lightId++;
3943
}
44+
timer.start();
4045
return 0;
4146
}
4247

4348
int LedDevicePhilipsHue::switchOff() {
49+
timer.stop();
4450
// If light states have been saved before, ...
4551
if (statesSaved()) {
4652
// ... restore them.
@@ -54,9 +60,15 @@ void LedDevicePhilipsHue::put(QString route, QString content) {
5460
QHttpRequestHeader header("PUT", url);
5561
header.setValue("Host", host);
5662
header.setValue("Accept-Encoding", "identity");
63+
header.setValue("Connection", "keep-alive");
5764
header.setValue("Content-Length", QString("%1").arg(content.size()));
58-
http->setHost(host);
65+
QEventLoop loop;
66+
// Connect requestFinished signal to quit slot of the loop.
67+
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
68+
// Perfrom request
5969
http->request(header, content.toAscii());
70+
// Go into the loop until the request is finished.
71+
loop.exec();
6072
}
6173

6274
QByteArray LedDevicePhilipsHue::get(QString route) {
@@ -92,13 +104,26 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) {
92104
// Read the response.
93105
QByteArray response = get(getRoute(i + 1));
94106
// Parse JSON.
95-
Json::Value state;
96-
if (!reader.parse(QString(response).toStdString(), state)) {
107+
Json::Value json;
108+
if (!reader.parse(QString(response).toStdString(), json)) {
97109
// Error occured, break loop.
98110
break;
99111
}
112+
// Save state object values which are subject to change.
113+
Json::Value state(Json::objectValue);
114+
state["on"] = json["state"]["on"];
115+
if (json["state"]["on"] == true) {
116+
state["xy"] = json["state"]["xy"];
117+
state["bri"] = json["state"]["bri"];
118+
}
100119
// Save state object.
101-
states.push_back(QString(writer.write(state["state"]).c_str()));
120+
states.push_back(QString(writer.write(state).c_str()).trimmed());
121+
}
122+
}
123+
124+
void LedDevicePhilipsHue::switchOn(unsigned int nLights) {
125+
for (unsigned int i = 0; i < nLights; i++) {
126+
put(getStateRoute(i + 1), "{\"on\": true}");
102127
}
103128
}
104129

libsrc/leddevice/LedDevicePhilipsHue.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include <string>
55

66
// Qt includes
7+
#include <QObject>
78
#include <QString>
89
#include <QHttp>
10+
#include <QTimer>
911

1012
// Leddevice includes
1113
#include <leddevice/LedDevice.h>
@@ -20,7 +22,8 @@
2022
*
2123
* @author ntim (github)
2224
*/
23-
class LedDevicePhilipsHue: public LedDevice {
25+
class LedDevicePhilipsHue: public QObject, public LedDevice {
26+
Q_OBJECT
2427
public:
2528
///
2629
/// Constructs the device.
@@ -43,9 +46,13 @@ class LedDevicePhilipsHue: public LedDevice {
4346
///
4447
virtual int write(const std::vector<ColorRgb> & ledValues);
4548

46-
/// Switch the leds off
49+
/// Restores the original state of the leds.
4750
virtual int switchOff();
4851

52+
private slots:
53+
/// Restores the status of all lights.
54+
void restoreStates();
55+
4956
private:
5057
/// Array to save the light states.
5158
std::vector<QString> states;
@@ -55,6 +62,8 @@ class LedDevicePhilipsHue: public LedDevice {
5562
QString username;
5663
/// Qhttp object for sending requests.
5764
QHttp* http;
65+
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
66+
QTimer timer;
5867

5968
///
6069
/// Sends a HTTP GET request (blocking).
@@ -95,8 +104,12 @@ class LedDevicePhilipsHue: public LedDevice {
95104
///
96105
void saveStates(unsigned int nLights);
97106

98-
/// Restores the status of all lights.
99-
void restoreStates();
107+
///
108+
/// Switches the leds on.
109+
///
110+
/// @param nLights the number of lights
111+
///
112+
void switchOn(unsigned int nLights);
100113

101114
///
102115
/// @return true if light states have been saved.

0 commit comments

Comments
 (0)