Commit 084e06d2d89b8094b481de12aa7110a0fca5766c
1 parent
ee78d8b4
Make usage of refactored HttpHandler, cleanup code
Showing
8 changed files
with
90 additions
and
59 deletions
hueplusplus/HttpHandler.cpp
| @@ -18,18 +18,19 @@ | @@ -18,18 +18,19 @@ | ||
| 18 | **/ | 18 | **/ |
| 19 | 19 | ||
| 20 | #include "include/HttpHandler.h" | 20 | #include "include/HttpHandler.h" |
| 21 | + | ||
| 22 | +#include <chrono> | ||
| 23 | +#include <netinet/in.h> // struct sockaddr_in, struct sockaddr | ||
| 24 | +#include <arpa/inet.h> | ||
| 21 | #include <iostream> | 25 | #include <iostream> |
| 22 | #include <memory> | 26 | #include <memory> |
| 23 | -#include <stdexcept> | ||
| 24 | -#include <stdio.h> // printf, sprintf | ||
| 25 | -#include <stdlib.h> // exit | ||
| 26 | -#include <unistd.h> // read, write, close | 27 | +#include <netdb.h> // struct hostent, gethostbyname |
| 27 | #include <sys/socket.h> // socket, connect | 28 | #include <sys/socket.h> // socket, connect |
| 28 | -#include <netinet/in.h> // struct sockaddr_in, struct sockaddr | ||
| 29 | -#include <arpa/inet.h> | ||
| 30 | -#include <netdb.h> // struct hostent, gethostbyname | ||
| 31 | -#include <string.h> | ||
| 32 | -#include <chrono> | 29 | +#include <stdexcept> |
| 30 | +#include <stdio.h> // printf, sprintf | ||
| 31 | +#include <stdlib.h> // exit | ||
| 32 | +#include <string.h> // functions for C style null-terminated strings | ||
| 33 | +#include <unistd.h> // read, write, close | ||
| 33 | 34 | ||
| 34 | class SocketCloser { | 35 | class SocketCloser { |
| 35 | public: SocketCloser(int sockFd) :s(sockFd) {} | 36 | public: SocketCloser(int sockFd) :s(sockFd) {} |
hueplusplus/Hue.cpp
| @@ -18,28 +18,28 @@ | @@ -18,28 +18,28 @@ | ||
| 18 | **/ | 18 | **/ |
| 19 | 19 | ||
| 20 | #include "include/Hue.h" | 20 | #include "include/Hue.h" |
| 21 | -#include "include/HueLight.h" | ||
| 22 | -#include "include/SimpleBrightnessStrategy.h" | ||
| 23 | -#include "include/SimpleColorHueStrategy.h" | ||
| 24 | -#include "include/ExtendedColorHueStrategy.h" | ||
| 25 | -#include "include/SimpleColorTemperatureStrategy.h" | ||
| 26 | -#include "include/ExtendedColorTemperatureStrategy.h" | ||
| 27 | - | ||
| 28 | -#include "include/HttpHandler.h" | ||
| 29 | -#include "include/UPnP.h" | ||
| 30 | 21 | ||
| 31 | #include <chrono> | 22 | #include <chrono> |
| 32 | #include <iostream> | 23 | #include <iostream> |
| 33 | -#include <memory> | ||
| 34 | #include <regex> | 24 | #include <regex> |
| 35 | #include <stdexcept> | 25 | #include <stdexcept> |
| 36 | #include <thread> | 26 | #include <thread> |
| 37 | 27 | ||
| 28 | +#include "include/ExtendedColorHueStrategy.h" | ||
| 29 | +#include "include/ExtendedColorTemperatureStrategy.h" | ||
| 30 | +#include "include/SimpleBrightnessStrategy.h" | ||
| 31 | +#include "include/SimpleColorHueStrategy.h" | ||
| 32 | +#include "include/SimpleColorTemperatureStrategy.h" | ||
| 33 | + | ||
| 34 | +#include "include/UPnP.h" | ||
| 35 | + | ||
| 36 | +HueFinder::HueFinder(std::shared_ptr<const IHttpHandler> handler) : http_handler(std::move(handler)) | ||
| 37 | +{} | ||
| 38 | 38 | ||
| 39 | std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const | 39 | std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const |
| 40 | { | 40 | { |
| 41 | UPnP uplug; | 41 | UPnP uplug; |
| 42 | - std::vector<std::pair<std::string, std::string>> foundDevices = uplug.getDevices(); | 42 | + std::vector<std::pair<std::string, std::string>> foundDevices = uplug.getDevices(http_handler); |
| 43 | 43 | ||
| 44 | //Does not work | 44 | //Does not work |
| 45 | std::regex manufRegex("<manufacturer>Royal Philips Electronics</manufacturer>"); | 45 | std::regex manufRegex("<manufacturer>Royal Philips Electronics</manufacturer>"); |
| @@ -56,7 +56,7 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const | @@ -56,7 +56,7 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const | ||
| 56 | unsigned int start = p.first.find("//") + 2; | 56 | unsigned int start = p.first.find("//") + 2; |
| 57 | unsigned int length = p.first.find(":", start) - start; | 57 | unsigned int length = p.first.find(":", start) - start; |
| 58 | bridge.ip = p.first.substr(start, length); | 58 | bridge.ip = p.first.substr(start, length); |
| 59 | - std::string desc = HttpHandler().GETString("/description.xml", "application/xml", "", bridge.ip); | 59 | + std::string desc = http_handler->GETString("/description.xml", "application/xml", "", bridge.ip); |
| 60 | std::smatch matchResult; | 60 | std::smatch matchResult; |
| 61 | if (std::regex_search(desc, manufRegex) && std::regex_search(desc, manURLRegex) && std::regex_search(desc, modelRegex) && std::regex_search(desc, matchResult, serialRegex)) | 61 | if (std::regex_search(desc, manufRegex) && std::regex_search(desc, manURLRegex) && std::regex_search(desc, modelRegex) && std::regex_search(desc, matchResult, serialRegex)) |
| 62 | { | 62 | { |
| @@ -93,7 +93,7 @@ Hue HueFinder::GetBridge(const HueIdentification& identification) | @@ -93,7 +93,7 @@ Hue HueFinder::GetBridge(const HueIdentification& identification) | ||
| 93 | AddUsername(identification.mac, username); | 93 | AddUsername(identification.mac, username); |
| 94 | } | 94 | } |
| 95 | } | 95 | } |
| 96 | - return Hue(identification.ip, username); | 96 | + return Hue(identification.ip, username, http_handler); |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | void HueFinder::AddUsername(const std::string& mac, const std::string& username) | 99 | void HueFinder::AddUsername(const std::string& mac, const std::string& username) |
| @@ -122,7 +122,7 @@ std::string HueFinder::RequestUsername(const std::string & ip) const | @@ -122,7 +122,7 @@ std::string HueFinder::RequestUsername(const std::string & ip) const | ||
| 122 | if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1)) | 122 | if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1)) |
| 123 | { | 123 | { |
| 124 | lastCheck = std::chrono::steady_clock::now(); | 124 | lastCheck = std::chrono::steady_clock::now(); |
| 125 | - answer = HttpHandler().GETJson("/api", request, ip); | 125 | + answer = http_handler->GETJson("/api", request, ip); |
| 126 | 126 | ||
| 127 | if (answer[0]["success"] != Json::nullValue) | 127 | if (answer[0]["success"] != Json::nullValue) |
| 128 | { | 128 | { |
| @@ -141,9 +141,10 @@ std::string HueFinder::RequestUsername(const std::string & ip) const | @@ -141,9 +141,10 @@ std::string HueFinder::RequestUsername(const std::string & ip) const | ||
| 141 | } | 141 | } |
| 142 | 142 | ||
| 143 | 143 | ||
| 144 | -Hue::Hue(const std::string& ip, const std::string& username) : | 144 | +Hue::Hue(const std::string& ip, const std::string& username, std::shared_ptr<const IHttpHandler> handler) : |
| 145 | ip(ip), | 145 | ip(ip), |
| 146 | -username(username) | 146 | +username(username), |
| 147 | +http_handler(std::move(handler)) | ||
| 147 | { | 148 | { |
| 148 | simpleBrightnessStrategy = std::make_shared<SimpleBrightnessStrategy>(); | 149 | simpleBrightnessStrategy = std::make_shared<SimpleBrightnessStrategy>(); |
| 149 | simpleColorHueStrategy = std::make_shared<SimpleColorHueStrategy>(); | 150 | simpleColorHueStrategy = std::make_shared<SimpleColorHueStrategy>(); |
| @@ -172,7 +173,8 @@ void Hue::requestUsername(const std::string& ip) | @@ -172,7 +173,8 @@ void Hue::requestUsername(const std::string& ip) | ||
| 172 | if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1)) | 173 | if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1)) |
| 173 | { | 174 | { |
| 174 | lastCheck = std::chrono::steady_clock::now(); | 175 | lastCheck = std::chrono::steady_clock::now(); |
| 175 | - answer = HttpHandler().GETJson("/api", request, ip); | 176 | + answer = http_handler->GETJson("/api", request, ip); |
| 177 | + | ||
| 176 | if (answer[0]["success"] != Json::nullValue) | 178 | if (answer[0]["success"] != Json::nullValue) |
| 177 | { | 179 | { |
| 178 | // [{"success":{"username": "<username>"}}] | 180 | // [{"success":{"username": "<username>"}}] |
| @@ -218,7 +220,7 @@ HueLight& Hue::getLight(int id) | @@ -218,7 +220,7 @@ HueLight& Hue::getLight(int id) | ||
| 218 | if (type == "LCT001" || type == "LCT002" || type == "LCT003" || type == "LCT007" || type == "LLM001") | 220 | if (type == "LCT001" || type == "LCT002" || type == "LCT003" || type == "LCT007" || type == "LLM001") |
| 219 | { | 221 | { |
| 220 | // HueExtendedColorLight Gamut B | 222 | // HueExtendedColorLight Gamut B |
| 221 | - HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); | 223 | + HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy, http_handler); |
| 222 | light.colorType = ColorType::GAMUT_B; | 224 | light.colorType = ColorType::GAMUT_B; |
| 223 | lights.emplace(id, light); | 225 | lights.emplace(id, light); |
| 224 | return lights.find(id)->second; | 226 | return lights.find(id)->second; |
| @@ -226,7 +228,7 @@ HueLight& Hue::getLight(int id) | @@ -226,7 +228,7 @@ HueLight& Hue::getLight(int id) | ||
| 226 | else if (type == "LCT010" || type == "LCT011" || type == "LCT014" || type == "LLC020" || type == "LST002") | 228 | else if (type == "LCT010" || type == "LCT011" || type == "LCT014" || type == "LLC020" || type == "LST002") |
| 227 | { | 229 | { |
| 228 | // HueExtendedColorLight Gamut C | 230 | // HueExtendedColorLight Gamut C |
| 229 | - HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); | 231 | + HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy, http_handler); |
| 230 | light.colorType = ColorType::GAMUT_C; | 232 | light.colorType = ColorType::GAMUT_C; |
| 231 | lights.emplace(id, light); | 233 | lights.emplace(id, light); |
| 232 | return lights.find(id)->second; | 234 | return lights.find(id)->second; |
| @@ -234,7 +236,7 @@ HueLight& Hue::getLight(int id) | @@ -234,7 +236,7 @@ HueLight& Hue::getLight(int id) | ||
| 234 | else if (type == "LST001" || type == "LLC006" || type == "LLC007" || type == "LLC010" || type == "LLC011" || type == "LLC012" || type == "LLC013") | 236 | else if (type == "LST001" || type == "LLC006" || type == "LLC007" || type == "LLC010" || type == "LLC011" || type == "LLC012" || type == "LLC013") |
| 235 | { | 237 | { |
| 236 | // HueColorLight Gamut A | 238 | // HueColorLight Gamut A |
| 237 | - HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, simpleColorTemperatureStrategy, simpleColorHueStrategy); | 239 | + HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, simpleColorTemperatureStrategy, simpleColorHueStrategy, http_handler); |
| 238 | light.colorType = ColorType::GAMUT_A; | 240 | light.colorType = ColorType::GAMUT_A; |
| 239 | lights.emplace(id, light); | 241 | lights.emplace(id, light); |
| 240 | return lights.find(id)->second; | 242 | return lights.find(id)->second; |
| @@ -242,7 +244,7 @@ HueLight& Hue::getLight(int id) | @@ -242,7 +244,7 @@ HueLight& Hue::getLight(int id) | ||
| 242 | else if (type == "LWB004" || type == "LWB006" || type == "LWB007" || type == "LWB010" || type == "LWB014") | 244 | else if (type == "LWB004" || type == "LWB006" || type == "LWB007" || type == "LWB010" || type == "LWB014") |
| 243 | { | 245 | { |
| 244 | // HueDimmableLight No Color Type | 246 | // HueDimmableLight No Color Type |
| 245 | - HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, nullptr, nullptr); | 247 | + HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, nullptr, nullptr, http_handler); |
| 246 | light.colorType = ColorType::NONE; | 248 | light.colorType = ColorType::NONE; |
| 247 | lights.emplace(id, light); | 249 | lights.emplace(id, light); |
| 248 | return lights.find(id)->second; | 250 | return lights.find(id)->second; |
| @@ -250,7 +252,7 @@ HueLight& Hue::getLight(int id) | @@ -250,7 +252,7 @@ HueLight& Hue::getLight(int id) | ||
| 250 | else if (type == "LLM010" || type == "LLM011" || type == "LLM012" || type == "LTW001" || type == "LTW004" || type == "LTW013" || type == "LTW014") | 252 | else if (type == "LLM010" || type == "LLM011" || type == "LLM012" || type == "LTW001" || type == "LTW004" || type == "LTW013" || type == "LTW014") |
| 251 | { | 253 | { |
| 252 | // HueTemperatureLight | 254 | // HueTemperatureLight |
| 253 | - HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, simpleColorTemperatureStrategy, nullptr); | 255 | + HueLight light = HueLight(ip, username, id, simpleBrightnessStrategy, simpleColorTemperatureStrategy, nullptr, http_handler); |
| 254 | light.colorType = ColorType::TEMPERATURE; | 256 | light.colorType = ColorType::TEMPERATURE; |
| 255 | lights.emplace(id, light); | 257 | lights.emplace(id, light); |
| 256 | return lights.find(id)->second; | 258 | return lights.find(id)->second; |
| @@ -284,13 +286,13 @@ void Hue::refreshState() | @@ -284,13 +286,13 @@ void Hue::refreshState() | ||
| 284 | { | 286 | { |
| 285 | return; | 287 | return; |
| 286 | } | 288 | } |
| 287 | - Json::Value answer = HttpHandler().GETJson("/api/"+username, Json::objectValue, ip); | 289 | + Json::Value answer = http_handler->GETJson("/api/"+username, Json::objectValue, ip); |
| 288 | if (answer.isObject() && answer.isMember("lights")) | 290 | if (answer.isObject() && answer.isMember("lights")) |
| 289 | { | 291 | { |
| 290 | state = answer; | 292 | state = answer; |
| 291 | } | 293 | } |
| 292 | else | 294 | else |
| 293 | { | 295 | { |
| 294 | - std::cout << "Answer in Hue::refreshState of HttpHandler().GETJson(...) is not expected!\n"; | 296 | + std::cout << "Answer in Hue::refreshState of http_handler->GETJson(...) is not expected!\n"; |
| 295 | } | 297 | } |
| 296 | } | 298 | } |
hueplusplus/HueLight.cpp
| @@ -19,13 +19,12 @@ | @@ -19,13 +19,12 @@ | ||
| 19 | 19 | ||
| 20 | #include "include/HueLight.h" | 20 | #include "include/HueLight.h" |
| 21 | 21 | ||
| 22 | -#include "include/HttpHandler.h" | ||
| 23 | -#include "include/json/json.h" | ||
| 24 | - | ||
| 25 | #include <cmath> | 22 | #include <cmath> |
| 26 | #include <iostream> | 23 | #include <iostream> |
| 27 | #include <thread> | 24 | #include <thread> |
| 28 | 25 | ||
| 26 | +#include "include/json/json.h" | ||
| 27 | + | ||
| 29 | bool HueLight::On(uint8_t transition) | 28 | bool HueLight::On(uint8_t transition) |
| 30 | { | 29 | { |
| 31 | std::cout << "Turning lamp with id: " << id << " on\n"; | 30 | std::cout << "Turning lamp with id: " << id << " on\n"; |
| @@ -78,17 +77,18 @@ bool HueLight::alert() | @@ -78,17 +77,18 @@ bool HueLight::alert() | ||
| 78 | return false; | 77 | return false; |
| 79 | } | 78 | } |
| 80 | 79 | ||
| 81 | -HueLight::HueLight(const std::string& ip, const std::string& username, int id) | ||
| 82 | - : HueLight(ip, username, id, nullptr, nullptr, nullptr) | 80 | +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) | ||
| 83 | {} | 82 | {} |
| 84 | 83 | ||
| 85 | -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) | 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) |
| 86 | : ip(ip), | 85 | : ip(ip), |
| 87 | username(username), | 86 | username(username), |
| 88 | id(id), | 87 | id(id), |
| 89 | brightnessStrategy(std::move(brightnessStrategy)), | 88 | brightnessStrategy(std::move(brightnessStrategy)), |
| 90 | colorTemperatureStrategy(std::move(colorTempStrategy)), | 89 | colorTemperatureStrategy(std::move(colorTempStrategy)), |
| 91 | - colorHueStrategy(std::move(colorHueStrategy)) | 90 | + colorHueStrategy(std::move(colorHueStrategy)), |
| 91 | + http_handler(std::move(handler)) | ||
| 92 | { | 92 | { |
| 93 | refreshState(); | 93 | refreshState(); |
| 94 | } | 94 | } |
| @@ -173,7 +173,7 @@ bool HueLight::OffNoRefresh(uint8_t transition) | @@ -173,7 +173,7 @@ bool HueLight::OffNoRefresh(uint8_t transition) | ||
| 173 | 173 | ||
| 174 | Json::Value HueLight::SendPutRequest(const Json::Value& request) | 174 | Json::Value HueLight::SendPutRequest(const Json::Value& request) |
| 175 | { | 175 | { |
| 176 | - return HttpHandler().PUTJson("/api/"+username+"/lights/"+std::to_string(id)+"/state", request, ip); | 176 | + return http_handler->PUTJson("/api/"+username+"/lights/"+std::to_string(id)+"/state", request, ip); |
| 177 | } | 177 | } |
| 178 | 178 | ||
| 179 | void HueLight::refreshState() | 179 | void HueLight::refreshState() |
| @@ -181,7 +181,7 @@ void HueLight::refreshState() | @@ -181,7 +181,7 @@ void HueLight::refreshState() | ||
| 181 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | 181 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
| 182 | std::cout << "\tRefreshing lampstate of lamp with id: " << id << ", ip: " << ip << "\n"; | 182 | std::cout << "\tRefreshing lampstate of lamp with id: " << id << ", ip: " << ip << "\n"; |
| 183 | 183 | ||
| 184 | - state = HttpHandler().GETJson("/api/"+username+"/lights/"+std::to_string(id), Json::objectValue, ip); | 184 | + state = http_handler->GETJson("/api/"+username+"/lights/"+std::to_string(id), Json::objectValue, ip); |
| 185 | //! \todo check whether getAnswer is containing right information | 185 | //! \todo check whether getAnswer is containing right information |
| 186 | 186 | ||
| 187 | std::cout << "\tRefresh state took: " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() << "ms" << std::endl; | 187 | std::cout << "\tRefresh state took: " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() << "ms" << std::endl; |
hueplusplus/UPnP.cpp
| @@ -18,14 +18,14 @@ | @@ -18,14 +18,14 @@ | ||
| 18 | **/ | 18 | **/ |
| 19 | 19 | ||
| 20 | #include "include/UPnP.h" | 20 | #include "include/UPnP.h" |
| 21 | -#include "include/HttpHandler.h" | 21 | + |
| 22 | #include <algorithm> | 22 | #include <algorithm> |
| 23 | #include <iostream> | 23 | #include <iostream> |
| 24 | 24 | ||
| 25 | -std::vector<std::pair<std::string, std::string>> UPnP::getDevices() | 25 | +std::vector<std::pair<std::string, std::string>> UPnP::getDevices(std::shared_ptr<const IHttpHandler> handler) |
| 26 | { | 26 | { |
| 27 | // send UPnP M-Search request | 27 | // send UPnP M-Search request |
| 28 | - std::vector<std::string> foundDevices = HttpHandler().sendMulticast("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n", "239.255.255.250", 1900, 5); | 28 | + std::vector<std::string> foundDevices = handler->sendMulticast("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n", "239.255.255.250", 1900, 5); |
| 29 | 29 | ||
| 30 | std::vector<std::pair<std::string, std::string>> devices; | 30 | std::vector<std::pair<std::string, std::string>> devices; |
| 31 | 31 |
hueplusplus/include/HttpHandler.h
| @@ -23,9 +23,10 @@ | @@ -23,9 +23,10 @@ | ||
| 23 | #include <string> | 23 | #include <string> |
| 24 | #include <vector> | 24 | #include <vector> |
| 25 | 25 | ||
| 26 | -#include "json/json.h" | ||
| 27 | #include "IHttpHandler.h" | 26 | #include "IHttpHandler.h" |
| 28 | 27 | ||
| 28 | +#include "json/json.h" | ||
| 29 | + | ||
| 29 | //! Class to handle http requests and multicast requests | 30 | //! Class to handle http requests and multicast requests |
| 30 | class HttpHandler : public IHttpHandler | 31 | class HttpHandler : public IHttpHandler |
| 31 | { | 32 | { |
hueplusplus/include/Hue.h
| @@ -20,19 +20,20 @@ | @@ -20,19 +20,20 @@ | ||
| 20 | #ifndef _HUE_H | 20 | #ifndef _HUE_H |
| 21 | #define _HUE_H | 21 | #define _HUE_H |
| 22 | 22 | ||
| 23 | -#include "HueLight.h" | ||
| 24 | -#include "BrightnessStrategy.h" | ||
| 25 | -#include "ColorHueStrategy.h" | ||
| 26 | -#include "ColorTemperatureStrategy.h" | ||
| 27 | - | ||
| 28 | -#include "json/json.h" | ||
| 29 | - | ||
| 30 | #include <map> | 23 | #include <map> |
| 31 | #include <memory> | 24 | #include <memory> |
| 32 | #include <string> | 25 | #include <string> |
| 33 | #include <utility> | 26 | #include <utility> |
| 34 | #include <vector> | 27 | #include <vector> |
| 35 | 28 | ||
| 29 | +#include "BrightnessStrategy.h" | ||
| 30 | +#include "ColorHueStrategy.h" | ||
| 31 | +#include "ColorTemperatureStrategy.h" | ||
| 32 | +#include "HueLight.h" | ||
| 33 | +#include "IHttpHandler.h" | ||
| 34 | + | ||
| 35 | +#include "json/json.h" | ||
| 36 | + | ||
| 36 | // forward declarations | 37 | // forward declarations |
| 37 | class Hue; | 38 | class Hue; |
| 38 | 39 | ||
| @@ -47,7 +48,14 @@ public: | @@ -47,7 +48,14 @@ public: | ||
| 47 | std::string ip; | 48 | std::string ip; |
| 48 | std::string mac; | 49 | std::string mac; |
| 49 | }; | 50 | }; |
| 51 | + | ||
| 50 | public: | 52 | public: |
| 53 | + | ||
| 54 | + //! \brief Constructor of HueFinder class | ||
| 55 | + //! | ||
| 56 | + //! \param handler HttpHandler of type \ref IHttpHandler for communication with the bridge | ||
| 57 | + HueFinder(std::shared_ptr<const IHttpHandler> handler); | ||
| 58 | + | ||
| 51 | //! \brief Function that finds all bridges in the network and returns them. | 59 | //! \brief Function that finds all bridges in the network and returns them. |
| 52 | //! | 60 | //! |
| 53 | //! The user should be given the opportunity to select the correct one based on the mac address. | 61 | //! The user should be given the opportunity to select the correct one based on the mac address. |
| @@ -68,9 +76,10 @@ public: | @@ -68,9 +76,10 @@ public: | ||
| 68 | 76 | ||
| 69 | //! \brief Function that returns a map of mac addresses and usernames. | 77 | //! \brief Function that returns a map of mac addresses and usernames. |
| 70 | //! | 78 | //! |
| 71 | - //! These should be saved at the end and re-loaded next time, so only one username is generated per bridge. | 79 | + //! Note these should be saved at the end and re-loaded with \ref AddUsername next time, so only one username is generated per bridge. |
| 72 | //! \returns A map mapping mac address to username for every bridge | 80 | //! \returns A map mapping mac address to username for every bridge |
| 73 | const std::map<std::string, std::string>& GetAllUsernames() const; | 81 | const std::map<std::string, std::string>& GetAllUsernames() const; |
| 82 | + | ||
| 74 | private: | 83 | private: |
| 75 | //! \brief Function that sends a username request to the Hue bridge. | 84 | //! \brief Function that sends a username request to the Hue bridge. |
| 76 | //! | 85 | //! |
| @@ -82,6 +91,7 @@ private: | @@ -82,6 +91,7 @@ private: | ||
| 82 | 91 | ||
| 83 | private: | 92 | private: |
| 84 | std::map<std::string, std::string> usernames; //!< Maps all macs to usernames added by \ref HueFinder::AddUsername | 93 | std::map<std::string, std::string> usernames; //!< Maps all macs to usernames added by \ref HueFinder::AddUsername |
| 94 | + std::shared_ptr<const IHttpHandler> http_handler; | ||
| 85 | }; | 95 | }; |
| 86 | 96 | ||
| 87 | //! Hue class | 97 | //! Hue class |
| @@ -92,7 +102,8 @@ public: | @@ -92,7 +102,8 @@ public: | ||
| 92 | //! | 102 | //! |
| 93 | //! \param ip String that specifies the ip address of the Hue bridge in dotted decimal notation like "192.168.2.1" | 103 | //! \param ip String that specifies the ip address of the Hue bridge in dotted decimal notation like "192.168.2.1" |
| 94 | //! \param username String that specifies the username that is used to control the bridge. This needs to be acquired in \ref requestUsername | 104 | //! \param username String that specifies the username that is used to control the bridge. This needs to be acquired in \ref requestUsername |
| 95 | - Hue(const std::string& ip, const std::string& username); | 105 | + //! \param handler HttpHandler of type \ref IHttpHandler for communication with the bridge |
| 106 | + Hue(const std::string& ip, const std::string& username, std::shared_ptr<const IHttpHandler> handler); | ||
| 96 | 107 | ||
| 97 | //! \brief Function to get the ip address of the hue bridge | 108 | //! \brief Function to get the ip address of the hue bridge |
| 98 | //! | 109 | //! |
| @@ -149,6 +160,7 @@ private: | @@ -149,6 +160,7 @@ private: | ||
| 149 | std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy; //!< 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 |
| 150 | std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy; //!< Strategy that is used for controlling the color temperature of lights | 161 | std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy; //!< Strategy that is used for controlling the color temperature of lights |
| 151 | std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy; //!< Strategy that is used for controlling the color temperature of lights | 162 | std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy; //!< Strategy that is used for controlling the color temperature of lights |
| 163 | + std::shared_ptr<const IHttpHandler> http_handler; | ||
| 152 | }; | 164 | }; |
| 153 | 165 | ||
| 154 | #endif | 166 | #endif |
hueplusplus/include/HueLight.h
| @@ -22,10 +22,12 @@ | @@ -22,10 +22,12 @@ | ||
| 22 | 22 | ||
| 23 | #include <memory> | 23 | #include <memory> |
| 24 | 24 | ||
| 25 | -#include "json/json.h" | ||
| 26 | #include "BrightnessStrategy.h" | 25 | #include "BrightnessStrategy.h" |
| 27 | #include "ColorHueStrategy.h" | 26 | #include "ColorHueStrategy.h" |
| 28 | #include "ColorTemperatureStrategy.h" | 27 | #include "ColorTemperatureStrategy.h" |
| 28 | +#include "IHttpHandler.h" | ||
| 29 | + | ||
| 30 | +#include "json/json.h" | ||
| 29 | 31 | ||
| 30 | /*enum ModelType | 32 | /*enum ModelType |
| 31 | { | 33 | { |
| @@ -341,9 +343,10 @@ protected: | @@ -341,9 +343,10 @@ protected: | ||
| 341 | //! \param ip String that specifies the ip of the Hue bridge | 343 | //! \param ip String that specifies the ip of the Hue bridge |
| 342 | //! \param username String that specifies the username used to control the bridge | 344 | //! \param username String that specifies the username used to control the bridge |
| 343 | //! \param id Integer that specifies the id of this light | 345 | //! \param id Integer that specifies the id of this light |
| 346 | + //! \param handler HttpHandler of type \ref IHttpHandler for communication with the bridge | ||
| 344 | //! | 347 | //! |
| 345 | //! leaves strategies unset | 348 | //! leaves strategies unset |
| 346 | - HueLight(const std::string& ip, const std::string& username, int id); | 349 | + HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr<const IHttpHandler> handler); |
| 347 | 350 | ||
| 348 | //! \brief Protected ctor that is used by \ref Hue class, also sets strategies. | 351 | //! \brief Protected ctor that is used by \ref Hue class, also sets strategies. |
| 349 | //! | 352 | //! |
| @@ -353,7 +356,8 @@ protected: | @@ -353,7 +356,8 @@ protected: | ||
| 353 | //! \param brightnessStrategy Strategy for brightness. May be nullptr. | 356 | //! \param brightnessStrategy Strategy for brightness. May be nullptr. |
| 354 | //! \param colorTempStrategy Strategy for color temperature. May be nullptr. | 357 | //! \param colorTempStrategy Strategy for color temperature. May be nullptr. |
| 355 | //! \param colorHueStrategy Strategy for color hue/saturation. May be nullptr. | 358 | //! \param colorHueStrategy Strategy for color hue/saturation. May be nullptr. |
| 356 | - 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); | 359 | + //! \param handler HttpHandler of type \ref IHttpHandler for communication with the bridge |
| 360 | + 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); | ||
| 357 | 361 | ||
| 358 | //! \brief Protected function that sets the brightness strategy. | 362 | //! \brief Protected function that sets the brightness strategy. |
| 359 | //! | 363 | //! |
| @@ -373,6 +377,12 @@ protected: | @@ -373,6 +377,12 @@ protected: | ||
| 373 | //! \param strat a strategy of type \ref ColorHueStrategy | 377 | //! \param strat a strategy of type \ref ColorHueStrategy |
| 374 | void setColorHueStrategy(std::shared_ptr<const ColorHueStrategy> strat) { colorHueStrategy = std::move(strat); }; | 378 | void setColorHueStrategy(std::shared_ptr<const ColorHueStrategy> strat) { colorHueStrategy = std::move(strat); }; |
| 375 | 379 | ||
| 380 | + //! \brief Protected function that sets the HttpHandler. | ||
| 381 | + //! | ||
| 382 | + //! The HttpHandler defines how specific commands that deal with bridge communication are executed | ||
| 383 | + //! \param handler a HttpHandler of type \ref IHttpHandler | ||
| 384 | + void setHttpHandler(std::shared_ptr<const IHttpHandler> handler) { http_handler = std::move(handler); }; | ||
| 385 | + | ||
| 376 | //! \brief Function that turns the light on without refreshing its state. | 386 | //! \brief Function that turns the light on without refreshing its state. |
| 377 | //! | 387 | //! |
| 378 | //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms | 388 | //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms |
| @@ -404,6 +414,7 @@ protected: | @@ -404,6 +414,7 @@ protected: | ||
| 404 | std::shared_ptr<const BrightnessStrategy> brightnessStrategy; //!< holds a reference to the strategy that handles brightness commands | 414 | std::shared_ptr<const BrightnessStrategy> brightnessStrategy; //!< holds a reference to the strategy that handles brightness commands |
| 405 | std::shared_ptr<const ColorTemperatureStrategy> colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands | 415 | std::shared_ptr<const ColorTemperatureStrategy> colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands |
| 406 | std::shared_ptr<const ColorHueStrategy> colorHueStrategy; //!< holds a reference to the strategy that handles all color commands | 416 | 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; | ||
| 407 | }; | 418 | }; |
| 408 | 419 | ||
| 409 | #endif | 420 | #endif |
hueplusplus/include/UPnP.h
| @@ -20,9 +20,12 @@ | @@ -20,9 +20,12 @@ | ||
| 20 | #ifndef _UPNP_H | 20 | #ifndef _UPNP_H |
| 21 | #define _UPNP_H | 21 | #define _UPNP_H |
| 22 | 22 | ||
| 23 | +#include <memory> | ||
| 23 | #include <string> | 24 | #include <string> |
| 24 | #include <vector> | 25 | #include <vector> |
| 25 | 26 | ||
| 27 | +#include "IHttpHandler.h" | ||
| 28 | + | ||
| 26 | //! Class that looks for UPnP devices using an m-search package | 29 | //! Class that looks for UPnP devices using an m-search package |
| 27 | class UPnP | 30 | class UPnP |
| 28 | { | 31 | { |
| @@ -31,8 +34,9 @@ public: | @@ -31,8 +34,9 @@ public: | ||
| 31 | //! | 34 | //! |
| 32 | //! It does it by sending an m-search packet and waits for all responses. | 35 | //! It does it by sending an m-search packet and waits for all responses. |
| 33 | //! Since responses can be received multiple times this function conveniently removes all duplicates. | 36 | //! Since responses can be received multiple times this function conveniently removes all duplicates. |
| 37 | + //! \param handler HttpHandler of type \ref IHttpHandler for communication with the bridge | ||
| 34 | //! \return A vector containing pairs of address and name of all found devices | 38 | //! \return A vector containing pairs of address and name of all found devices |
| 35 | - std::vector<std::pair<std::string, std::string>> getDevices(); | 39 | + std::vector<std::pair<std::string, std::string>> getDevices(std::shared_ptr<const IHttpHandler> handler); |
| 36 | }; | 40 | }; |
| 37 | 41 | ||
| 38 | #endif | 42 | #endif |