Commit 3ea62ff66be6ecd4e378ec921069f7d73f9653de
Committed by
Moritz Wirger
1 parent
314d2dbd
Fix problem with wrong datatypes and str.find(), add todo, add function for dele…
…ting lights, add function for setting the HttpHandler, cleanup
Showing
2 changed files
with
33 additions
and
8 deletions
hueplusplus/Hue.cpp
| ... | ... | @@ -49,12 +49,12 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const |
| 49 | 49 | std::vector<HueIdentification> foundBridges; |
| 50 | 50 | for (const std::pair<std::string, std::string> &p : foundDevices) |
| 51 | 51 | { |
| 52 | - unsigned int found = p.second.find("IpBridge"); | |
| 52 | + size_t found = p.second.find("IpBridge"); | |
| 53 | 53 | if (found != std::string::npos) |
| 54 | 54 | { |
| 55 | 55 | HueIdentification bridge; |
| 56 | - unsigned int start = p.first.find("//") + 2; | |
| 57 | - unsigned int length = p.first.find(":", start) - start; | |
| 56 | + size_t start = p.first.find("//") + 2; | |
| 57 | + size_t length = p.first.find(":", start) - start; | |
| 58 | 58 | bridge.ip = p.first.substr(start, length); |
| 59 | 59 | std::string desc = http_handler->GETString("/description.xml", "application/xml", "", bridge.ip); |
| 60 | 60 | std::smatch matchResult; |
| ... | ... | @@ -238,6 +238,7 @@ HueLight& Hue::getLight(int id) |
| 238 | 238 | else if (type == "LST001" || type == "LLC006" || type == "LLC007" || type == "LLC010" || type == "LLC011" || type == "LLC012" || type == "LLC013") |
| 239 | 239 | { |
| 240 | 240 | // HueColorLight Gamut A |
| 241 | + //! \todo Check whether extended strategies are needed, because it is not clear, if these lights really have ct mode | |
| 241 | 242 | HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, simpleColorTemperatureStrategy, simpleColorHueStrategy, http_handler); |
| 242 | 243 | light.colorType = ColorType::GAMUT_A; |
| 243 | 244 | lights.emplace(id, light); |
| ... | ... | @@ -263,6 +264,17 @@ HueLight& Hue::getLight(int id) |
| 263 | 264 | throw(std::runtime_error("Could not determine HueLight type!")); |
| 264 | 265 | } |
| 265 | 266 | |
| 267 | +bool Hue::removeLight(int id) | |
| 268 | +{ | |
| 269 | + Json::Value result = http_handler->DELETEJson("/api/"+username+"/lights/"+std::to_string(id), Json::objectValue, ip); | |
| 270 | + bool success = result.isArray() && !result[0].isNull() && result[0].isMember("success") && result[0]["success"] == "/lights/" + std::to_string(id) + " deleted"; | |
| 271 | + if (success && lights.count(id) != 0) | |
| 272 | + { | |
| 273 | + lights.erase(id); | |
| 274 | + } | |
| 275 | + return success; | |
| 276 | +} | |
| 277 | + | |
| 266 | 278 | std::vector<std::reference_wrapper<HueLight>> Hue::getAllLights() |
| 267 | 279 | { |
| 268 | 280 | refreshState(); | ... | ... |
hueplusplus/include/Hue.h
| ... | ... | @@ -135,6 +135,13 @@ public: |
| 135 | 135 | //! \return \ref HueLight that can be controlled |
| 136 | 136 | HueLight& getLight(int id); |
| 137 | 137 | |
| 138 | + //! \brief Function to remove a light from the bridge | |
| 139 | + //! | |
| 140 | + //! \attention Any use of the light after it was successfully removed results in undefined behavior | |
| 141 | + //! \param id Id of the light to remove | |
| 142 | + //! \return Bool that is true on success | |
| 143 | + bool removeLight(int id); | |
| 144 | + | |
| 138 | 145 | //! \brief Function that returns all light types that are associated with this bridge |
| 139 | 146 | //! |
| 140 | 147 | //! \return A map mapping light id's to light types for every light |
| ... | ... | @@ -145,22 +152,28 @@ public: |
| 145 | 152 | //! \return A vector containing references to every HueLight |
| 146 | 153 | std::vector<std::reference_wrapper<HueLight>> getAllLights(); |
| 147 | 154 | |
| 155 | + //! \brief Function that sets the HttpHandler. | |
| 156 | + //! | |
| 157 | + //! The HttpHandler defines how specific commands that deal with bridge communication are executed | |
| 158 | + //! \param handler a HttpHandler of type \ref IHttpHandler | |
| 159 | + void setHttpHandler(std::shared_ptr<const IHttpHandler> handler) { http_handler = std::move(handler); }; | |
| 160 | + | |
| 148 | 161 | private: |
| 149 | 162 | //! \brief Function that refreshes the local \ref state of the Hue bridge |
| 150 | 163 | void refreshState(); |
| 151 | 164 | |
| 152 | 165 | private: |
| 153 | - std::string ip; //!< IP-Address of the hue bridge in dotted decimal notation like "192.168.2.1" | |
| 166 | + std::string ip; //!< IP-Address of the hue bridge in dotted decimal notation like "192.168.2.1" | |
| 154 | 167 | std::string username; //!< Username that is ussed to access the hue bridge |
| 155 | 168 | Json::Value state; //!< The state of the hue bridge as it is returned from it |
| 156 | 169 | std::map< uint8_t, HueLight > lights; //!< Maps ids to HueLights that are controlled by this bridge |
| 157 | 170 | |
| 158 | - std::shared_ptr<BrightnessStrategy> simpleBrightnessStrategy; //!< Strategy that is used for controlling the brightness of lights | |
| 159 | - std::shared_ptr<ColorHueStrategy> simpleColorHueStrategy; //!< Strategy that is used for controlling the color of lights | |
| 160 | - std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy; //!< Strategy that is used for controlling the color of lights | |
| 171 | + std::shared_ptr<BrightnessStrategy> simpleBrightnessStrategy; //!< Strategy that is used for controlling the brightness of lights | |
| 172 | + std::shared_ptr<ColorHueStrategy> simpleColorHueStrategy; //!< Strategy that is used for controlling the color of lights | |
| 173 | + std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy; //!< Strategy that is used for controlling the color of lights | |
| 161 | 174 | std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy; //!< Strategy that is used for controlling the color temperature of lights |
| 162 | 175 | std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy; //!< Strategy that is used for controlling the color temperature of lights |
| 163 | - std::shared_ptr<const IHttpHandler> http_handler; | |
| 176 | + std::shared_ptr<const IHttpHandler> http_handler; //!< A IHttpHandler that is used to communicate with the bridge | |
| 164 | 177 | }; |
| 165 | 178 | |
| 166 | 179 | #endif | ... | ... |