Commit c34b3fa154c30ed0a56c3ee2038603f81783c065

Authored by Moritz W
Committed by Moritz Wirger
1 parent 3ea62ff6

Add new function for checking if a light is on, add function for changing a ligh…

…ts name, fix documentation typo, add function for getting the lights brightness, modify SendPutRequest so a subPath can be set
hueplusplus/HueLight.cpp
@@ -39,12 +39,28 @@ bool HueLight::Off(uint8_t transition) @@ -39,12 +39,28 @@ bool HueLight::Off(uint8_t transition)
39 return OffNoRefresh(transition); 39 return OffNoRefresh(transition);
40 } 40 }
41 41
  42 +bool HueLight::IsOn()
  43 +{
  44 + refreshState();
  45 + return state["state"]["on"].asBool();
  46 +}
  47 +
42 std::string HueLight::getName() 48 std::string HueLight::getName()
43 { 49 {
44 refreshState(); 50 refreshState();
45 return state["name"].asString(); 51 return state["name"].asString();
46 } 52 }
47 53
  54 +bool HueLight::setName(const std::string& name)
  55 +{
  56 + Json::Value request(Json::objectValue);
  57 + request["name"] = name;
  58 + Json::Value reply = SendPutRequest(request, "/name");
  59 +
  60 + //Check whether request was successful
  61 + return !reply[0].isNull() && reply[0].isMember("success") && reply[0]["success"]["/lights/" + std::to_string(id) + "/name"] == name;
  62 +}
  63 +
48 ColorType HueLight::getColorType() 64 ColorType HueLight::getColorType()
49 { 65 {
50 return colorType; 66 return colorType;
@@ -67,7 +83,7 @@ bool HueLight::alert() @@ -67,7 +83,7 @@ bool HueLight::alert()
67 Json::Value request; 83 Json::Value request;
68 request["alert"] = "select"; 84 request["alert"] = "select";
69 85
70 - Json::Value reply = SendPutRequest(request); 86 + Json::Value reply = SendPutRequest(request, "/state");
71 87
72 if (reply[0]["success"]["/lights/" + std::to_string(id) + "/state/alert"].asString() == "select") 88 if (reply[0]["success"]["/lights/" + std::to_string(id) + "/state/alert"].asString() == "select")
73 { 89 {
@@ -78,7 +94,7 @@ bool HueLight::alert() @@ -78,7 +94,7 @@ bool HueLight::alert()
78 } 94 }
79 95
80 HueLight::HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr<const IHttpHandler> handler) 96 HueLight::HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr<const IHttpHandler> handler)
81 - : HueLight(ip, username, id, nullptr, nullptr, nullptr, handler) 97 + : HueLight(ip, username, id, nullptr, nullptr, nullptr, handler)
82 {} 98 {}
83 99
84 HueLight::HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr<const BrightnessStrategy> brightnessStrategy, std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy, std::shared_ptr<const ColorHueStrategy> colorHueStrategy, std::shared_ptr<const IHttpHandler> handler) 100 HueLight::HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr<const BrightnessStrategy> brightnessStrategy, std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy, std::shared_ptr<const ColorHueStrategy> colorHueStrategy, std::shared_ptr<const IHttpHandler> handler)
@@ -89,6 +105,7 @@ HueLight::HueLight(const std::string&amp; ip, const std::string&amp; username, int id, s @@ -89,6 +105,7 @@ HueLight::HueLight(const std::string&amp; ip, const std::string&amp; username, int id, s
89 colorTemperatureStrategy(std::move(colorTempStrategy)), 105 colorTemperatureStrategy(std::move(colorTempStrategy)),
90 colorHueStrategy(std::move(colorHueStrategy)), 106 colorHueStrategy(std::move(colorHueStrategy)),
91 http_handler(std::move(handler)) 107 http_handler(std::move(handler))
  108 +
92 { 109 {
93 refreshState(); 110 refreshState();
94 } 111 }
@@ -112,7 +129,7 @@ bool HueLight::OnNoRefresh(uint8_t transition) @@ -112,7 +129,7 @@ bool HueLight::OnNoRefresh(uint8_t transition)
112 return true; 129 return true;
113 } 130 }
114 131
115 - Json::Value reply = SendPutRequest(request); 132 + Json::Value reply = SendPutRequest(request, "/state");
116 133
117 //Check whether request was successful 134 //Check whether request was successful
118 std::string path = "/lights/" + std::to_string(id) + "/state/"; 135 std::string path = "/lights/" + std::to_string(id) + "/state/";
@@ -151,7 +168,7 @@ bool HueLight::OffNoRefresh(uint8_t transition) @@ -151,7 +168,7 @@ bool HueLight::OffNoRefresh(uint8_t transition)
151 return true; 168 return true;
152 } 169 }
153 170
154 - Json::Value reply = SendPutRequest(request); 171 + Json::Value reply = SendPutRequest(request, "/state");
155 172
156 //Check whether request was successful 173 //Check whether request was successful
157 std::string path = "/lights/" + std::to_string(id) + "/state/"; 174 std::string path = "/lights/" + std::to_string(id) + "/state/";
@@ -171,9 +188,9 @@ bool HueLight::OffNoRefresh(uint8_t transition) @@ -171,9 +188,9 @@ bool HueLight::OffNoRefresh(uint8_t transition)
171 return success; 188 return success;
172 } 189 }
173 190
174 -Json::Value HueLight::SendPutRequest(const Json::Value& request) 191 +Json::Value HueLight::SendPutRequest(const Json::Value& request, const std::string& subPath)
175 { 192 {
176 - return http_handler->PUTJson("/api/"+username+"/lights/"+std::to_string(id)+"/state", request, ip); 193 + return http_handler->PUTJson("/api/"+username+"/lights/"+std::to_string(id)+subPath, request, ip);
177 } 194 }
178 195
179 void HueLight::refreshState() 196 void HueLight::refreshState()
hueplusplus/include/HueLight.h
@@ -113,11 +113,21 @@ public: @@ -113,11 +113,21 @@ public:
113 //! \return Bool that is true on success 113 //! \return Bool that is true on success
114 bool Off(uint8_t transition = 4); 114 bool Off(uint8_t transition = 4);
115 115
  116 + //! \brief Function to check whether a light is on or off
  117 + //!
  118 + //! \return Bool that is true, when the light is on and false, when off
  119 + bool IsOn();
  120 +
116 //! \brief Function that returns the name of the light. 121 //! \brief Function that returns the name of the light.
117 //! 122 //!
118 //! \return String containig the name of the light 123 //! \return String containig the name of the light
119 std::string getName(); 124 std::string getName();
120 125
  126 + //! \brief Function that sets the name of the light
  127 + //!
  128 + //! \return Bool that is true on success
  129 + bool setName(const std::string& name);
  130 +
121 //! \brief Function that returns the color type of the light. 131 //! \brief Function that returns the color type of the light.
122 //! 132 //!
123 //! \return ColorType containig the color type of the light 133 //! \return ColorType containig the color type of the light
@@ -138,7 +148,7 @@ public: @@ -138,7 +148,7 @@ public:
138 //! \brief Function that sets the brightness of this light. 148 //! \brief Function that sets the brightness of this light.
139 //! 149 //!
140 //! Notice the brightness will only be set if the light has a reference to a specific \ref BrightnessStrategy. 150 //! Notice the brightness will only be set if the light has a reference to a specific \ref BrightnessStrategy.
141 - //! The brightness can range from 0 = off to 255 = fully lit. 151 + //! The brightness can range from 0 = off to 254 = fully lit.
142 //! \param bri Unsigned int that specifies the brightness 152 //! \param bri Unsigned int that specifies the brightness
143 //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms 153 //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms
144 //! \return Bool that is true on success 154 //! \return Bool that is true on success
@@ -151,6 +161,20 @@ public: @@ -151,6 +161,20 @@ public:
151 return false; 161 return false;
152 }; 162 };
153 163
  164 + //! \brief Function that returns the brightness of this light.
  165 + //!
  166 + //! Notice the brightness will only be returned if the light has a reference to a specific \ref BrightnessStrategy.
  167 + //! The brightness can range from 0 = off to 254 = fully lit.
  168 + //! \return Unsigned int that is 0 when function failed
  169 + unsigned int getBrightness()
  170 + {
  171 + if (brightnessStrategy)
  172 + {
  173 + return brightnessStrategy->getBrightness(*this);
  174 + }
  175 + return 0;
  176 + };
  177 +
154 //! \brief Fucntion that sets the color temperature of this light in mired. 178 //! \brief Fucntion that sets the color temperature of this light in mired.
155 //! 179 //!
156 //! Notice the color temperature will only be set if the light has a reference to a specific \ref ColorTemperatureStrategy. 180 //! Notice the color temperature will only be set if the light has a reference to a specific \ref ColorTemperatureStrategy.
@@ -398,8 +422,10 @@ protected: @@ -398,8 +422,10 @@ protected:
398 //! \brief Utility function to send a put request to the light. 422 //! \brief Utility function to send a put request to the light.
399 //! 423 //!
400 //! \throws std::runtime_error if the reply could not be parsed 424 //! \throws std::runtime_error if the reply could not be parsed
  425 + //! \param request A Json::Value aka the request to send
  426 + //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/")
401 //! \return The parsed reply 427 //! \return The parsed reply
402 - Json::Value SendPutRequest(const Json::Value& request); 428 + Json::Value SendPutRequest(const Json::Value& request, const std::string& subPath);
403 429
404 //! \brief Virtual function that refreshes the \ref state of the light. 430 //! \brief Virtual function that refreshes the \ref state of the light.
405 virtual void refreshState(); 431 virtual void refreshState();
@@ -414,7 +440,7 @@ protected: @@ -414,7 +440,7 @@ protected:
414 std::shared_ptr<const BrightnessStrategy> brightnessStrategy; //!< holds a reference to the strategy that handles brightness commands 440 std::shared_ptr<const BrightnessStrategy> brightnessStrategy; //!< holds a reference to the strategy that handles brightness commands
415 std::shared_ptr<const ColorTemperatureStrategy> colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands 441 std::shared_ptr<const ColorTemperatureStrategy> colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands
416 std::shared_ptr<const ColorHueStrategy> colorHueStrategy; //!< holds a reference to the strategy that handles all color commands 442 std::shared_ptr<const ColorHueStrategy> colorHueStrategy; //!< holds a reference to the strategy that handles all color commands
417 - std::shared_ptr<const IHttpHandler> http_handler; 443 + std::shared_ptr<const IHttpHandler> http_handler; //!< A IHttpHandler that is used to communicate with the bridge
418 }; 444 };
419 445
420 #endif 446 #endif