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 39 return OffNoRefresh(transition);
40 40 }
41 41  
  42 +bool HueLight::IsOn()
  43 +{
  44 + refreshState();
  45 + return state["state"]["on"].asBool();
  46 +}
  47 +
42 48 std::string HueLight::getName()
43 49 {
44 50 refreshState();
45 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 64 ColorType HueLight::getColorType()
49 65 {
50 66 return colorType;
... ... @@ -67,7 +83,7 @@ bool HueLight::alert()
67 83 Json::Value request;
68 84 request["alert"] = "select";
69 85  
70   - Json::Value reply = SendPutRequest(request);
  86 + Json::Value reply = SendPutRequest(request, "/state");
71 87  
72 88 if (reply[0]["success"]["/lights/" + std::to_string(id) + "/state/alert"].asString() == "select")
73 89 {
... ... @@ -78,7 +94,7 @@ bool HueLight::alert()
78 94 }
79 95  
80 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 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 105 colorTemperatureStrategy(std::move(colorTempStrategy)),
90 106 colorHueStrategy(std::move(colorHueStrategy)),
91 107 http_handler(std::move(handler))
  108 +
92 109 {
93 110 refreshState();
94 111 }
... ... @@ -112,7 +129,7 @@ bool HueLight::OnNoRefresh(uint8_t transition)
112 129 return true;
113 130 }
114 131  
115   - Json::Value reply = SendPutRequest(request);
  132 + Json::Value reply = SendPutRequest(request, "/state");
116 133  
117 134 //Check whether request was successful
118 135 std::string path = "/lights/" + std::to_string(id) + "/state/";
... ... @@ -151,7 +168,7 @@ bool HueLight::OffNoRefresh(uint8_t transition)
151 168 return true;
152 169 }
153 170  
154   - Json::Value reply = SendPutRequest(request);
  171 + Json::Value reply = SendPutRequest(request, "/state");
155 172  
156 173 //Check whether request was successful
157 174 std::string path = "/lights/" + std::to_string(id) + "/state/";
... ... @@ -171,9 +188,9 @@ bool HueLight::OffNoRefresh(uint8_t transition)
171 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 196 void HueLight::refreshState()
... ...
hueplusplus/include/HueLight.h
... ... @@ -113,11 +113,21 @@ public:
113 113 //! \return Bool that is true on success
114 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 121 //! \brief Function that returns the name of the light.
117 122 //!
118 123 //! \return String containig the name of the light
119 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 131 //! \brief Function that returns the color type of the light.
122 132 //!
123 133 //! \return ColorType containig the color type of the light
... ... @@ -138,7 +148,7 @@ public:
138 148 //! \brief Function that sets the brightness of this light.
139 149 //!
140 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 152 //! \param bri Unsigned int that specifies the brightness
143 153 //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms
144 154 //! \return Bool that is true on success
... ... @@ -151,6 +161,20 @@ public:
151 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 178 //! \brief Fucntion that sets the color temperature of this light in mired.
155 179 //!
156 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 422 //! \brief Utility function to send a put request to the light.
399 423 //!
400 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 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 430 //! \brief Virtual function that refreshes the \ref state of the light.
405 431 virtual void refreshState();
... ... @@ -414,7 +440,7 @@ protected:
414 440 std::shared_ptr<const BrightnessStrategy> brightnessStrategy; //!< holds a reference to the strategy that handles brightness commands
415 441 std::shared_ptr<const ColorTemperatureStrategy> colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands
416 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 446 #endif
... ...