From 853d00289425d90e71331005d152400a7ae69156 Mon Sep 17 00:00:00 2001 From: ntim Date: Wed, 7 May 2014 15:22:13 +0200 Subject: [PATCH] Timer to restore light state properly implemented. Lights are not switched on after state has been saved. Former-commit-id: 04959ea3731eb7de9f59b80a789f03cfeb2d4ba3 --- libsrc/leddevice/LedDevicePhilipsHue.cpp | 18 ++++++++++++++---- libsrc/leddevice/LedDevicePhilipsHue.h | 9 ++++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/libsrc/leddevice/LedDevicePhilipsHue.cpp b/libsrc/leddevice/LedDevicePhilipsHue.cpp index 35f045f2..f8a9698a 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.cpp +++ b/libsrc/leddevice/LedDevicePhilipsHue.cpp @@ -13,9 +13,9 @@ LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output) : host(output.c_str()), username("newdeveloper") { http = new QHttp(host); - timer.setInterval(1000); + timer.setInterval(3000); timer.setSingleShot(true); - connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()())); + connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates())); } LedDevicePhilipsHue::~LedDevicePhilipsHue() { @@ -26,6 +26,7 @@ int LedDevicePhilipsHue::write(const std::vector & 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; @@ -110,13 +111,22 @@ void LedDevicePhilipsHue::saveStates(unsigned int nLights) { } // Save state object values which are subject to change. Json::Value state(Json::objectValue); - state["xy"] = json["state"]["xy"]; - state["bri"] = json["state"]["bri"]; + 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).c_str()).trimmed()); } } +void LedDevicePhilipsHue::switchOn(unsigned int nLights) { + for (unsigned int i = 0; i < nLights; i++) { + put(getStateRoute(i + 1), "{\"on\": true}"); + } +} + void LedDevicePhilipsHue::restoreStates() { unsigned int lightId = 1; for (QString state : states) { diff --git a/libsrc/leddevice/LedDevicePhilipsHue.h b/libsrc/leddevice/LedDevicePhilipsHue.h index 435fc800..84e7bd35 100755 --- a/libsrc/leddevice/LedDevicePhilipsHue.h +++ b/libsrc/leddevice/LedDevicePhilipsHue.h @@ -46,7 +46,7 @@ public: /// virtual int write(const std::vector & ledValues); - /// Switch the leds off + /// Restores the original state of the leds. virtual int switchOff(); private slots: @@ -104,6 +104,13 @@ private: /// void saveStates(unsigned int nLights); + /// + /// Switches the leds on. + /// + /// @param nLights the number of lights + /// + void switchOn(unsigned int nLights); + /// /// @return true if light states have been saved. ///