From c34b3fa154c30ed0a56c3ee2038603f81783c065 Mon Sep 17 00:00:00 2001 From: Moritz W Date: Thu, 2 Nov 2017 19:16:52 +0100 Subject: [PATCH] Add new function for checking if a light is on, add function for changing a lights name, fix documentation typo, add function for getting the lights brightness, modify SendPutRequest so a subPath can be set --- hueplusplus/HueLight.cpp | 29 +++++++++++++++++++++++------ hueplusplus/include/HueLight.h | 32 +++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/hueplusplus/HueLight.cpp b/hueplusplus/HueLight.cpp index ed50bdc..652c06a 100755 --- a/hueplusplus/HueLight.cpp +++ b/hueplusplus/HueLight.cpp @@ -39,12 +39,28 @@ bool HueLight::Off(uint8_t transition) return OffNoRefresh(transition); } +bool HueLight::IsOn() +{ + refreshState(); + return state["state"]["on"].asBool(); +} + std::string HueLight::getName() { refreshState(); return state["name"].asString(); } +bool HueLight::setName(const std::string& name) +{ + Json::Value request(Json::objectValue); + request["name"] = name; + Json::Value reply = SendPutRequest(request, "/name"); + + //Check whether request was successful + return !reply[0].isNull() && reply[0].isMember("success") && reply[0]["success"]["/lights/" + std::to_string(id) + "/name"] == name; +} + ColorType HueLight::getColorType() { return colorType; @@ -67,7 +83,7 @@ bool HueLight::alert() Json::Value request; request["alert"] = "select"; - Json::Value reply = SendPutRequest(request); + Json::Value reply = SendPutRequest(request, "/state"); if (reply[0]["success"]["/lights/" + std::to_string(id) + "/state/alert"].asString() == "select") { @@ -78,7 +94,7 @@ bool HueLight::alert() } HueLight::HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr handler) - : HueLight(ip, username, id, nullptr, nullptr, nullptr, handler) + : HueLight(ip, username, id, nullptr, nullptr, nullptr, handler) {} HueLight::HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr brightnessStrategy, std::shared_ptr colorTempStrategy, std::shared_ptr colorHueStrategy, std::shared_ptr handler) @@ -89,6 +105,7 @@ HueLight::HueLight(const std::string& ip, const std::string& username, int id, s colorTemperatureStrategy(std::move(colorTempStrategy)), colorHueStrategy(std::move(colorHueStrategy)), http_handler(std::move(handler)) + { refreshState(); } @@ -112,7 +129,7 @@ bool HueLight::OnNoRefresh(uint8_t transition) return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = SendPutRequest(request, "/state"); //Check whether request was successful std::string path = "/lights/" + std::to_string(id) + "/state/"; @@ -151,7 +168,7 @@ bool HueLight::OffNoRefresh(uint8_t transition) return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = SendPutRequest(request, "/state"); //Check whether request was successful std::string path = "/lights/" + std::to_string(id) + "/state/"; @@ -171,9 +188,9 @@ bool HueLight::OffNoRefresh(uint8_t transition) return success; } -Json::Value HueLight::SendPutRequest(const Json::Value& request) +Json::Value HueLight::SendPutRequest(const Json::Value& request, const std::string& subPath) { - return http_handler->PUTJson("/api/"+username+"/lights/"+std::to_string(id)+"/state", request, ip); + return http_handler->PUTJson("/api/"+username+"/lights/"+std::to_string(id)+subPath, request, ip); } void HueLight::refreshState() diff --git a/hueplusplus/include/HueLight.h b/hueplusplus/include/HueLight.h index a22d7ee..5783a1f 100755 --- a/hueplusplus/include/HueLight.h +++ b/hueplusplus/include/HueLight.h @@ -113,11 +113,21 @@ public: //! \return Bool that is true on success bool Off(uint8_t transition = 4); + //! \brief Function to check whether a light is on or off + //! + //! \return Bool that is true, when the light is on and false, when off + bool IsOn(); + //! \brief Function that returns the name of the light. //! //! \return String containig the name of the light std::string getName(); + //! \brief Function that sets the name of the light + //! + //! \return Bool that is true on success + bool setName(const std::string& name); + //! \brief Function that returns the color type of the light. //! //! \return ColorType containig the color type of the light @@ -138,7 +148,7 @@ public: //! \brief Function that sets the brightness of this light. //! //! Notice the brightness will only be set if the light has a reference to a specific \ref BrightnessStrategy. - //! The brightness can range from 0 = off to 255 = fully lit. + //! The brightness can range from 0 = off to 254 = fully lit. //! \param bri Unsigned int that specifies the brightness //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms //! \return Bool that is true on success @@ -151,6 +161,20 @@ public: return false; }; + //! \brief Function that returns the brightness of this light. + //! + //! Notice the brightness will only be returned if the light has a reference to a specific \ref BrightnessStrategy. + //! The brightness can range from 0 = off to 254 = fully lit. + //! \return Unsigned int that is 0 when function failed + unsigned int getBrightness() + { + if (brightnessStrategy) + { + return brightnessStrategy->getBrightness(*this); + } + return 0; + }; + //! \brief Fucntion that sets the color temperature of this light in mired. //! //! Notice the color temperature will only be set if the light has a reference to a specific \ref ColorTemperatureStrategy. @@ -398,8 +422,10 @@ protected: //! \brief Utility function to send a put request to the light. //! //! \throws std::runtime_error if the reply could not be parsed + //! \param request A Json::Value aka the request to send + //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") //! \return The parsed reply - Json::Value SendPutRequest(const Json::Value& request); + Json::Value SendPutRequest(const Json::Value& request, const std::string& subPath); //! \brief Virtual function that refreshes the \ref state of the light. virtual void refreshState(); @@ -414,7 +440,7 @@ protected: std::shared_ptr brightnessStrategy; //!< holds a reference to the strategy that handles brightness commands std::shared_ptr colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands std::shared_ptr colorHueStrategy; //!< holds a reference to the strategy that handles all color commands - std::shared_ptr http_handler; + std::shared_ptr http_handler; //!< A IHttpHandler that is used to communicate with the bridge }; #endif -- libgit2 0.21.4