From b6d5d07cf15e2d8bd2b5fea3ea66e9af124adba7 Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Fri, 10 Apr 2020 11:49:48 +0200 Subject: [PATCH] Move everything into namespace hueplusplus. --- CMakeLists.txt | 11 ++++++++++- README.md | 42 ++++++++++++++++++++++-------------------- include/hueplusplus/BaseHttpHandler.h | 279 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------ include/hueplusplus/BrightnessStrategy.h | 61 ++++++++++++++++++++++++++++++++----------------------------- include/hueplusplus/ColorHueStrategy.h | 252 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------- include/hueplusplus/ColorTemperatureStrategy.h | 83 +++++++++++++++++++++++++++++++++++++++++++---------------------------------------- include/hueplusplus/ExtendedColorHueStrategy.h | 67 +++++++++++++++++++++++++++++++++++-------------------------------- include/hueplusplus/ExtendedColorTemperatureStrategy.h | 45 ++++++++++++++++++++++++--------------------- include/hueplusplus/Hue.h | 471 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ include/hueplusplus/HueCommandAPI.h | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------- include/hueplusplus/HueConfig.h | 7 +++++-- include/hueplusplus/HueDeviceTypes.h | 23 ++++++++++++----------- include/hueplusplus/HueException.h | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------ include/hueplusplus/HueExceptionMacro.h | 2 +- include/hueplusplus/HueLight.h | 1440 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- include/hueplusplus/IHttpHandler.h | 325 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------- include/hueplusplus/LinHttpHandler.h | 53 ++++++++++++++++++++++++++++------------------------- include/hueplusplus/SimpleBrightnessStrategy.h | 51 +++++++++++++++++++++++++++------------------------ include/hueplusplus/SimpleColorHueStrategy.h | 245 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------- include/hueplusplus/SimpleColorTemperatureStrategy.h | 73 ++++++++++++++++++++++++++++++++++++++----------------------------------- include/hueplusplus/UPnP.h | 27 +++++++++++++++------------ include/hueplusplus/Units.h | 67 +++++++++++++++++++++++++++++++++++-------------------------------- include/hueplusplus/Utils.h | 87 +++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ include/hueplusplus/WinHttpHandler.h | 63 +++++++++++++++++++++++++++++++++------------------------------ src/BaseHttpHandler.cpp | 20 ++++++++++---------- src/ExtendedColorHueStrategy.cpp | 6 +++--- src/ExtendedColorTemperatureStrategy.cpp | 4 ++-- src/Hue.cpp | 44 ++++++++++++++++++++++---------------------- src/HueCommandAPI.cpp | 20 ++++++++++---------- src/HueDeviceTypes.cpp | 88 ++++++++++++++++++++++++++++++++++++++++------------------------------------------------ src/HueException.cpp | 22 +++++++++++----------- src/HueLight.cpp | 52 ++++++++++++++++++++++++++-------------------------- src/LinHttpHandler.cpp | 4 ++-- src/SimpleBrightnessStrategy.cpp | 6 +++--- src/SimpleColorHueStrategy.cpp | 26 +++++++++++++------------- src/SimpleColorTemperatureStrategy.cpp | 8 ++++---- src/UPnP.cpp | 2 +- src/Utils.cpp | 82 +++++++++++++++++++++++++++++++++++++++------------------------------------------- src/WinHttpHandler.cpp | 8 ++++---- test/mocks/mock_BaseHttpHandler.h | 2 +- test/mocks/mock_HttpHandler.h | 2 +- test/mocks/mock_HueLight.h | 10 +++++----- test/test_BaseHttpHandler.cpp | 2 ++ test/test_ExtendedColorHueStrategy.cpp | 2 ++ test/test_ExtendedColorTemperatureStrategy.cpp | 2 ++ test/test_Hue.cpp | 2 ++ test/test_HueCommandAPI.cpp | 2 ++ test/test_HueLight.cpp | 2 ++ test/test_SimpleBrightnessStrategy.cpp | 2 ++ test/test_SimpleColorHueStrategy.cpp | 2 ++ test/test_SimpleColorTemperatureStrategy.cpp | 2 ++ test/test_UPnP.cpp | 2 ++ 52 files changed, 2314 insertions(+), 2233 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6be1cb7..5120b99 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,15 @@ set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) project(hueplusplus LANGUAGES CXX) +# check whether hueplusplus is compiled directly or included as a subdirectory +if(NOT DEFINED hueplusplus_master_project) + if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + set(hueplusplus_master_project ON) + else() + set(hueplusplus_master_project OFF) + endif() +endif() + # options to set option(hueplusplus_TESTS "Build tests" OFF) @@ -27,7 +36,7 @@ endif() # Set default build type if none was specified set(default_build_type "Release") -if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) +if(hueplusplus_master_project AND (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)) message(STATUS "Setting build type to '${default_build_type}' as none was specified") set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build." FORCE) # Set possible values for cmake-gui diff --git a/README.md b/README.md index 4c2b34c..b2cc2f9 100755 --- a/README.md +++ b/README.md @@ -26,16 +26,17 @@ To start searching for a Hue Bridge you will need to choose an IHttpHandler and Then create a HueFinder object with the handler. The handler is needed, because it tells the finder which functions to use to communicate with a bridge or your local network. After that you can call FindBridges(), which will return a vector containing the ip and mac address of all found Bridges. -If no Bridges were found the vector is empty, so make sure that in that case you provide an ip and mac address. ```C++ -// For windows use std::make_shared(); -handler = std::make_shared(); -HueFinder finder(handler); -std::vector bridges = finder.FindBridges(); +// For windows use std::make_shared(); +handler = std::make_shared(); +hueplusplus::HueFinder finder(handler); +std::vector bridges = finder.FindBridges(); if (bridges.empty()) { - bridges.push_back({ "", "" }); + std::cerr << "No bridges found\n"; + return; } + ``` ### Authenticate Bridges @@ -43,19 +44,19 @@ If you have found the Bridge you were looking for, you can then move on with the To get a new username from the Bridge (for now) you simply call GetBridge(bridges[\]), where index is your preferred Bridge from the part [Searching for Bridges](#searchingBridges). ```C++ -Hue bridge = finder.GetBridge(bridges[0]); +hueplusplus::Hue bridge = finder.GetBridge(bridges[0]); ``` If you on the other hand already have a username you can add your bridge like so ```C++ finder.AddUsername(bridges[0].mac, ""); -Hue bridge = finder.GetBridge(bridges[0]); +hueplusplus::Hue bridge = finder.GetBridge(bridges[0]); ``` If you do not want to use the HueFinder or you already know the ip and username of your bridge you have the option to create your own Hue object. Here you will need to provide the ip address, the port number, a username and an HttpHandler ```C++ -// For windows use std::make_shared(); -handler = std::make_shared(); -Hue bridge("192.168.2.102", 80, "", handler); +// For windows use std::make_shared(); +handler = std::make_shared(); +hueplusplus::Hue bridge("192.168.2.102", 80, "", handler); ``` ### Controlling lights @@ -63,11 +64,12 @@ If you have your Bridge all set up, you can now control its lights. For that create a new HueLight object and call getLight(\) on your bridge object to get a reference to a specific light, where id is the id of the light set internally by the Hue Bridge. ```C++ -HueLight light1 = bridge.getLight(1); +hueplusplus::HueLight light1 = bridge.getLight(1); ``` -If you don't know the id of a specific light or want to get an overview over all lights that are controlled by your bridge, you can get a vector containing them by calling getAllLights() on your bridge object. If no lights are found the vector will be empty. +If you don't know the id of a specific light or want to get an overview over all lights that are controlled by your bridge, +you can get a vector containing them by calling getAllLights() on your bridge object. If no lights are found the vector will be empty. ```C++ -std::vector> lights = bridge.getAllLights(); +std::vector> lights = bridge.getAllLights(); ``` If you now want to control a light, call a specific function of it. ```C++ @@ -84,7 +86,7 @@ specific function, but nothing will happen. For that you might want to check wha of a light you are controlling. For that you can call the function getColorType(), which will return a ColorType. ```C++ -ColorType type1 = light1.getColorType(); +hueplusplus::ColorType type1 = light1.getColorType(); ``` There's also a new way to check whether specific functions of a light are available: ```C++ @@ -126,21 +128,21 @@ If you have a project that already uses CMake you probably want to add the huepl For that the best way is to use find_package(). When cmake finds the hueplusplus library you can then link against either the shared or static version of the library. ```cmake -find_package(hueplusplus) +find_package(hueplusplus REQUIRED) -target_link_libraries( hueplusplusstatic) +target_link_libraries( PUBLIC hueplusplusstatic) ``` But this will only work if the hueplusplus library is already installed. To get around this problem there is a pretty awesome way. If you have the hueplusplus repository included in your project repository (as a submodule) or know where the folder lives you can do the following: ```cmake -find_package(hueplusplus) +find_package(hueplusplus QUIET) if(NOT hueplusplus_FOUND) message(STATUS "-- hueplusplus not found, building it") - add_subdirectory("${CMAKE_SOURCE_DIR}//hueplusplus" "${CMAKE_BINARY_DIR}/hueplusplus") + add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}//hueplusplus" "${CMAKE_CURRENT_BINARY_DIR}/hueplusplus") endif() -target_link_libraries( hueplusplusstatic) +target_link_libraries( PUBLIC hueplusplusstatic) ``` This will check if the hueplusplus library was found by find_package() and if not it will use the specified path to the library source and compile it during the build process. diff --git a/include/hueplusplus/BaseHttpHandler.h b/include/hueplusplus/BaseHttpHandler.h index db0fe55..836e952 100644 --- a/include/hueplusplus/BaseHttpHandler.h +++ b/include/hueplusplus/BaseHttpHandler.h @@ -32,144 +32,147 @@ #include "json/json.hpp" -//! Base class for classes that handle http requests and multicast requests -class BaseHttpHandler : public IHttpHandler +namespace hueplusplus { -public: - //! \brief Virtual dtor - virtual ~BaseHttpHandler() = default; - - //! \brief Send a message to a specified host and return the body of the response. - //! - //! \param msg The message that should sent to the specified address - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return The body of the response of the host as a string - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP request with the given method to the specified host and return the body of the response. - //! - //! \param method HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - std::string sendHTTPRequest(const std::string& method, const std::string& uri, const std::string& contentType, - const std::string& body, const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP GET request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - std::string GETString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP POST request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - std::string POSTString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP PUT request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - std::string PUTString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP DELETE request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - std::string DELETEString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP GET request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - nlohmann::json GETJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP POST request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - nlohmann::json POSTJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP PUT request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - nlohmann::json PUTJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; - - //! \brief Send a HTTP DELETE request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - nlohmann::json DELETEJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; -}; + //! Base class for classes that handle http requests and multicast requests + class BaseHttpHandler : public IHttpHandler + { + public: + //! \brief Virtual dtor + virtual ~BaseHttpHandler() = default; + + //! \brief Send a message to a specified host and return the body of the response. + //! + //! \param msg The message that should sent to the specified address + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return The body of the response of the host as a string + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP request with the given method to the specified host and return the body of the response. + //! + //! \param method HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + std::string sendHTTPRequest(const std::string& method, const std::string& uri, const std::string& contentType, + const std::string& body, const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP GET request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + std::string GETString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP POST request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + std::string POSTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP PUT request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + std::string PUTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + std::string DELETEString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP GET request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + nlohmann::json GETJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP POST request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + nlohmann::json POSTJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP PUT request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + nlohmann::json PUTJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; + + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + nlohmann::json DELETEJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const override; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/BrightnessStrategy.h b/include/hueplusplus/BrightnessStrategy.h index 1c12704..c080486 100644 --- a/include/hueplusplus/BrightnessStrategy.h +++ b/include/hueplusplus/BrightnessStrategy.h @@ -23,36 +23,39 @@ #ifndef _BRIGHTNESS_STRATEGY_H #define _BRIGHTNESS_STRATEGY_H -#include +#include -class HueLight; - -//! Virtual base class for all BrightnessStrategies -class BrightnessStrategy +namespace hueplusplus { -public: - //! \brief Virtual function for changing a lights brightness with a specified - //! transition. - //! - //! \param bri The brightness raning from 0 = off to 255 = fully lit - //! \param transition The time it takes to fade to the new brightness in - //! multiples of 100ms, 4 = 400ms and should be seen as the default \param - //! light A reference of the light - virtual bool setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const = 0; - //! \brief Virtual function that returns the current brightnessof the light - //! - //! Should update the lights state by calling refreshState() - //! \param light A reference of the light - //! \return Unsigned int representing the brightness - virtual unsigned int getBrightness(HueLight& light) const = 0; - //! \brief Virtual function that returns the current brightness of the light - //! - //! \note This should not update the lights state - //! \param light A const reference of the light - //! \return Unsigned int representing the brightness - virtual unsigned int getBrightness(const HueLight& light) const = 0; - //! \brief Virtual dtor - virtual ~BrightnessStrategy() = default; -}; + class HueLight; + + //! Virtual base class for all BrightnessStrategies + class BrightnessStrategy + { + public: + //! \brief Virtual function for changing a lights brightness with a specified + //! transition. + //! + //! \param bri The brightness raning from 0 = off to 255 = fully lit + //! \param transition The time it takes to fade to the new brightness in + //! multiples of 100ms, 4 = 400ms and should be seen as the default \param + //! light A reference of the light + virtual bool setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const = 0; + //! \brief Virtual function that returns the current brightnessof the light + //! + //! Should update the lights state by calling refreshState() + //! \param light A reference of the light + //! \return Unsigned int representing the brightness + virtual unsigned int getBrightness(HueLight& light) const = 0; + //! \brief Virtual function that returns the current brightness of the light + //! + //! \note This should not update the lights state + //! \param light A const reference of the light + //! \return Unsigned int representing the brightness + virtual unsigned int getBrightness(const HueLight& light) const = 0; + //! \brief Virtual dtor + virtual ~BrightnessStrategy() = default; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/ColorHueStrategy.h b/include/hueplusplus/ColorHueStrategy.h index 7d40d8a..76c6ee2 100644 --- a/include/hueplusplus/ColorHueStrategy.h +++ b/include/hueplusplus/ColorHueStrategy.h @@ -23,133 +23,135 @@ #ifndef _COLOR_HUE_STRATEGY_H #define _COLOR_HUE_STRATEGY_H +#include #include -#include - -class HueLight; - -//! Virtual base class for all ColorHueStrategies -class ColorHueStrategy +namespace hueplusplus { -public: - //! \brief Virtual function for changing a lights color in hue with a - //! specified transition. - //! - //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is - //! green and 46920 is blue. \param hue The hue of the color \param transition - //! The time it takes to fade to the new color in multiples of 100ms, 4 = - //! 400ms and should be seen as the default \param light A reference of the - //! light - virtual bool setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const = 0; - //! \brief Virtual function for changing a lights color in saturation with a - //! specified transition. - //! - //! The saturation ranges from 0 to 254, whereas 0 is least saturated (white) - //! and 254 is most saturated (vibrant). \param sat The saturation of the - //! color \param transition The time it takes to fade to the new color in - //! multiples of 100ms, 4 = 400ms and should be seen as the default \param - //! light A reference of the light - virtual bool setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const = 0; - //! \brief Virtual function for changing a lights color in hue and saturation - //! format with a specified transition. - //! - //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is - //! green and 46920 is blue. The saturation ranges from 0 to 254, whereas 0 is - //! least saturated (white) and 254 is most saturated (vibrant). \param hue - //! The hue of the color \param sat The saturation of the color \param - //! transition The time it takes to fade to the new color in multiples of - //! 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const = 0; - //! \brief Virtual function for changing a lights color in CIE format with a - //! specified transition. - //! - //! \param x The x coordinate in CIE, ranging from 0 to 1 - //! \param y The y coordinate in CIE, ranging from 0 to 1 - //! \param transition The time it takes to fade to the new color in multiples - //! of 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - virtual bool setColorXY(float x, float y, uint8_t transition, HueLight& light) const = 0; - //! \brief Virtual function for changing a lights color in rgb format with a - //! specified transition. - //! - //! Red, green and blue are ranging from 0 to 255. - //! \param r The red portion of the color - //! \param g The green portion of the color - //! \param b The blue portion of the color - //! \param transition The time it takes to fade to the new color in multiples - //! of 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const = 0; - //! \brief Virtual function for turning on/off the color loop feature of a - //! light. - //! - //! Can be theoretically set for any light, but it only works for lights that - //! support this feature. When this feature is activated the light will fade - //! through every color on the current hue and saturation settings. Notice - //! that none of the setter functions check whether this feature is enabled - //! and the colorloop can only be disabled with this function or by simply - //! calling Off()/OffNoRefresh() and then On()/OnNoRefresh(), so you could - //! alternatively call Off() and then use any of the setter functions. \param - //! on Boolean to turn this feature on or off, true/1 for on and false/0 for - //! off \param light A reference of the light - virtual bool setColorLoop(bool on, HueLight& light) const = 0; - //! \brief Virtual function that lets the light perform one breath cycle in - //! the specified color. - //! - //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is - //! green and 46920 is blue. The saturation ranges from 0 to 254, whereas 0 is - //! least saturated (white) and 254 is most saturated (vibrant). \param hue - //! The hue of the color \param sat The saturation of the color \param light A - //! reference of the light - virtual bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const = 0; - //! \brief Virtual function that lets the light perform one breath cycle in - //! the specified color. - //! - //! \param x The x coordinate in CIE, ranging from 0 to 1 - //! \param y The y coordinate in CIE, ranging from 0 to 1 - //! \param light A reference of the light - virtual bool alertXY(float x, float y, HueLight& light) const = 0; - //! \brief Virtual function that lets the light perform one breath cycle in - //! the specified color. - //! - //! Red, green and blue are ranging from 0 to 255. - //! \param r The red portion of the color - //! \param g The green portion of the color - //! \param b The blue portion of the color - //! \param light A reference of the light - virtual bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const = 0; - //! \brief Virtual function that returns the current color of the light as hue - //! and saturation - //! - //! Should update the lights state by calling refreshState() - //! \param light A reference of the light - //! \return Pair containing the hue as first value and saturation as second - //! value - virtual std::pair getColorHueSaturation(HueLight& light) const = 0; - //! \brief Virtual function that returns the current color of the light as hue - //! and saturation - //! - //! \note This should not update the lights state - //! \param light A const reference of the light - //! \return Pair containing the hue as first value and saturation as second - //! value - virtual std::pair getColorHueSaturation(const HueLight& light) const = 0; - //! \brief Virtual function that returns the current color of the light as xy - //! - //! Should update the lights state by calling refreshState() - //! \param light A reference of the light - //! \return Pair containing the x as first value and y as second value - virtual std::pair getColorXY(HueLight& light) const = 0; - //! \brief Virtual function that returns the current color of the light as xy - //! - //! \note This should not update the lights state - //! \param light A const reference of the light - //! \return Pair containing the x as first value and y as second value - virtual std::pair getColorXY(const HueLight& light) const = 0; - //! \brief Virtual dtor - virtual ~ColorHueStrategy() = default; -}; + class HueLight; + + //! Virtual base class for all ColorHueStrategies + class ColorHueStrategy + { + public: + //! \brief Virtual function for changing a lights color in hue with a + //! specified transition. + //! + //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is + //! green and 46920 is blue. \param hue The hue of the color \param transition + //! The time it takes to fade to the new color in multiples of 100ms, 4 = + //! 400ms and should be seen as the default \param light A reference of the + //! light + virtual bool setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const = 0; + //! \brief Virtual function for changing a lights color in saturation with a + //! specified transition. + //! + //! The saturation ranges from 0 to 254, whereas 0 is least saturated (white) + //! and 254 is most saturated (vibrant). \param sat The saturation of the + //! color \param transition The time it takes to fade to the new color in + //! multiples of 100ms, 4 = 400ms and should be seen as the default \param + //! light A reference of the light + virtual bool setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const = 0; + //! \brief Virtual function for changing a lights color in hue and saturation + //! format with a specified transition. + //! + //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is + //! green and 46920 is blue. The saturation ranges from 0 to 254, whereas 0 is + //! least saturated (white) and 254 is most saturated (vibrant). \param hue + //! The hue of the color \param sat The saturation of the color \param + //! transition The time it takes to fade to the new color in multiples of + //! 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const = 0; + //! \brief Virtual function for changing a lights color in CIE format with a + //! specified transition. + //! + //! \param x The x coordinate in CIE, ranging from 0 to 1 + //! \param y The y coordinate in CIE, ranging from 0 to 1 + //! \param transition The time it takes to fade to the new color in multiples + //! of 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + virtual bool setColorXY(float x, float y, uint8_t transition, HueLight& light) const = 0; + //! \brief Virtual function for changing a lights color in rgb format with a + //! specified transition. + //! + //! Red, green and blue are ranging from 0 to 255. + //! \param r The red portion of the color + //! \param g The green portion of the color + //! \param b The blue portion of the color + //! \param transition The time it takes to fade to the new color in multiples + //! of 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const = 0; + //! \brief Virtual function for turning on/off the color loop feature of a + //! light. + //! + //! Can be theoretically set for any light, but it only works for lights that + //! support this feature. When this feature is activated the light will fade + //! through every color on the current hue and saturation settings. Notice + //! that none of the setter functions check whether this feature is enabled + //! and the colorloop can only be disabled with this function or by simply + //! calling Off()/OffNoRefresh() and then On()/OnNoRefresh(), so you could + //! alternatively call Off() and then use any of the setter functions. \param + //! on Boolean to turn this feature on or off, true/1 for on and false/0 for + //! off \param light A reference of the light + virtual bool setColorLoop(bool on, HueLight& light) const = 0; + //! \brief Virtual function that lets the light perform one breath cycle in + //! the specified color. + //! + //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is + //! green and 46920 is blue. The saturation ranges from 0 to 254, whereas 0 is + //! least saturated (white) and 254 is most saturated (vibrant). \param hue + //! The hue of the color \param sat The saturation of the color \param light A + //! reference of the light + virtual bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const = 0; + //! \brief Virtual function that lets the light perform one breath cycle in + //! the specified color. + //! + //! \param x The x coordinate in CIE, ranging from 0 to 1 + //! \param y The y coordinate in CIE, ranging from 0 to 1 + //! \param light A reference of the light + virtual bool alertXY(float x, float y, HueLight& light) const = 0; + //! \brief Virtual function that lets the light perform one breath cycle in + //! the specified color. + //! + //! Red, green and blue are ranging from 0 to 255. + //! \param r The red portion of the color + //! \param g The green portion of the color + //! \param b The blue portion of the color + //! \param light A reference of the light + virtual bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const = 0; + //! \brief Virtual function that returns the current color of the light as hue + //! and saturation + //! + //! Should update the lights state by calling refreshState() + //! \param light A reference of the light + //! \return Pair containing the hue as first value and saturation as second + //! value + virtual std::pair getColorHueSaturation(HueLight& light) const = 0; + //! \brief Virtual function that returns the current color of the light as hue + //! and saturation + //! + //! \note This should not update the lights state + //! \param light A const reference of the light + //! \return Pair containing the hue as first value and saturation as second + //! value + virtual std::pair getColorHueSaturation(const HueLight& light) const = 0; + //! \brief Virtual function that returns the current color of the light as xy + //! + //! Should update the lights state by calling refreshState() + //! \param light A reference of the light + //! \return Pair containing the x as first value and y as second value + virtual std::pair getColorXY(HueLight& light) const = 0; + //! \brief Virtual function that returns the current color of the light as xy + //! + //! \note This should not update the lights state + //! \param light A const reference of the light + //! \return Pair containing the x as first value and y as second value + virtual std::pair getColorXY(const HueLight& light) const = 0; + //! \brief Virtual dtor + virtual ~ColorHueStrategy() = default; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/ColorTemperatureStrategy.h b/include/hueplusplus/ColorTemperatureStrategy.h index 37be4fe..948d693 100644 --- a/include/hueplusplus/ColorTemperatureStrategy.h +++ b/include/hueplusplus/ColorTemperatureStrategy.h @@ -25,46 +25,49 @@ #include -class HueLight; - -//! Virtual base class for all ColorTemperatureStrategies -class ColorTemperatureStrategy +namespace hueplusplus { -public: - //! \brief Virtual function for changing a lights color temperature in mired - //! with a specified transition. - //! - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \param mired The color temperature in mired \param - //! transition The time it takes to fade to the new color in multiples of - //! 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - virtual bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const = 0; - //! \brief Virtual function that lets the light perform one breath cycle in - //! the specified color. - //! - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \param mired The color temperature in mired \param light - //! A reference of the light - virtual bool alertTemperature(unsigned int mired, HueLight& light) const = 0; - //! \brief Virtual function that returns the current color temperature of the - //! light - //! - //! Should update the lights state by calling refreshState() - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \param light A reference of the light \return Unsigned - //! int representing the color temperature in mired - virtual unsigned int getColorTemperature(HueLight& light) const = 0; - //! \brief Virtual function that returns the current color temperature of the - //! light - //! - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \note This should not update the lights state \param - //! light A const reference of the light \return Unsigned int representing the - //! color temperature in mired - virtual unsigned int getColorTemperature(const HueLight& light) const = 0; - //! \brief Virtual dtor - virtual ~ColorTemperatureStrategy() = default; -}; + class HueLight; + + //! Virtual base class for all ColorTemperatureStrategies + class ColorTemperatureStrategy + { + public: + //! \brief Virtual function for changing a lights color temperature in mired + //! with a specified transition. + //! + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \param mired The color temperature in mired \param + //! transition The time it takes to fade to the new color in multiples of + //! 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + virtual bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const = 0; + //! \brief Virtual function that lets the light perform one breath cycle in + //! the specified color. + //! + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \param mired The color temperature in mired \param light + //! A reference of the light + virtual bool alertTemperature(unsigned int mired, HueLight& light) const = 0; + //! \brief Virtual function that returns the current color temperature of the + //! light + //! + //! Should update the lights state by calling refreshState() + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \param light A reference of the light \return Unsigned + //! int representing the color temperature in mired + virtual unsigned int getColorTemperature(HueLight& light) const = 0; + //! \brief Virtual function that returns the current color temperature of the + //! light + //! + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \note This should not update the lights state \param + //! light A const reference of the light \return Unsigned int representing the + //! color temperature in mired + virtual unsigned int getColorTemperature(const HueLight& light) const = 0; + //! \brief Virtual dtor + virtual ~ColorTemperatureStrategy() = default; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/ExtendedColorHueStrategy.h b/include/hueplusplus/ExtendedColorHueStrategy.h index 000418f..28ad1d3 100644 --- a/include/hueplusplus/ExtendedColorHueStrategy.h +++ b/include/hueplusplus/ExtendedColorHueStrategy.h @@ -26,38 +26,41 @@ #include "HueLight.h" #include "SimpleColorHueStrategy.h" -//! Class extending the implementation of SimpleColorHueStrategy -class ExtendedColorHueStrategy : public SimpleColorHueStrategy +namespace hueplusplus { -public: - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs The hue ranges from 0 to 65535, whereas 65535 and - //! 0 are red, 25500 is green and 46920 is blue. The saturation ranges from 0 - //! to 254, whereas 0 is least saturated (white) and 254 is most saturated - //! (vibrant). \param hue The hue of the color \param sat The saturation of - //! the color \param light A reference of the light - bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const override; - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs \param x The x coordinate in CIE, ranging from 0 - //! to 1 \param y The y coordinate in CIE, ranging from 0 to 1 \param light A - //! reference of the light - bool alertXY(float x, float y, HueLight& light) const override; - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs Red, green and blue are ranging from 0 to 255. - //! \param r The red portion of the color - //! \param g The green portion of the color - //! \param b The blue portion of the color - //! \param light A reference of the light - bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const override; -}; + //! Class extending the implementation of SimpleColorHueStrategy + class ExtendedColorHueStrategy : public SimpleColorHueStrategy + { + public: + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs The hue ranges from 0 to 65535, whereas 65535 and + //! 0 are red, 25500 is green and 46920 is blue. The saturation ranges from 0 + //! to 254, whereas 0 is least saturated (white) and 254 is most saturated + //! (vibrant). \param hue The hue of the color \param sat The saturation of + //! the color \param light A reference of the light + bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const override; + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs \param x The x coordinate in CIE, ranging from 0 + //! to 1 \param y The y coordinate in CIE, ranging from 0 to 1 \param light A + //! reference of the light + bool alertXY(float x, float y, HueLight& light) const override; + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs Red, green and blue are ranging from 0 to 255. + //! \param r The red portion of the color + //! \param g The green portion of the color + //! \param b The blue portion of the color + //! \param light A reference of the light + bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const override; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/ExtendedColorTemperatureStrategy.h b/include/hueplusplus/ExtendedColorTemperatureStrategy.h index e24137c..a4afff4 100644 --- a/include/hueplusplus/ExtendedColorTemperatureStrategy.h +++ b/include/hueplusplus/ExtendedColorTemperatureStrategy.h @@ -26,27 +26,30 @@ #include "HueLight.h" #include "SimpleColorTemperatureStrategy.h" -//! Class implementing the functions of ColorTemperatureStrategy -class ExtendedColorTemperatureStrategy : public SimpleColorTemperatureStrategy +namespace hueplusplus { -public: - //! \brief Function for changing a lights color temperature in mired with a - //! specified transition. - //! - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \param mired The color temperature in mired \param - //! transition The time it takes to fade to the new color in multiples of - //! 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const override; - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs The color temperature in mired ranges from 153 to - //! 500 whereas 153 is cold and 500 is warm. \param mired The color - //! temperature in mired \param light A reference of the light - bool alertTemperature(unsigned int mired, HueLight& light) const override; -}; + //! Class implementing the functions of ColorTemperatureStrategy + class ExtendedColorTemperatureStrategy : public SimpleColorTemperatureStrategy + { + public: + //! \brief Function for changing a lights color temperature in mired with a + //! specified transition. + //! + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \param mired The color temperature in mired \param + //! transition The time it takes to fade to the new color in multiples of + //! 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const override; + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs The color temperature in mired ranges from 153 to + //! 500 whereas 153 is cold and 500 is warm. \param mired The color + //! temperature in mired \param light A reference of the light + bool alertTemperature(unsigned int mired, HueLight& light) const override; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/Hue.h b/include/hueplusplus/Hue.h index 1aa8360..3134ca7 100644 --- a/include/hueplusplus/Hue.h +++ b/include/hueplusplus/Hue.h @@ -38,245 +38,248 @@ #include "json/json.hpp" -// forward declarations -class Hue; - -//! -//! Class to find all Hue bridges on the network and create usernames for them. -//! -class HueFinder +namespace hueplusplus { -public: - struct HueIdentification - { - std::string ip; - int port = 80; - std::string mac; - }; + // forward declarations + class Hue; -public: - //! \brief Constructor of HueFinder class //! - //! \param handler HttpHandler of type \ref IHttpHandler for communication with the bridge - HueFinder(std::shared_ptr handler); - - //! \brief Finds all bridges in the network and returns them. - //! - //! The user should be given the opportunity to select the correct one based on the mac address. - //! \return vector containing ip and mac of all found bridges - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - std::vector FindBridges() const; - - //! \brief Gets a \ref Hue bridge based on its identification + //! Class to find all Hue bridges on the network and create usernames for them. //! - //! \param identification \ref HueIdentification that specifies a bridge - //! \return \ref Hue class object - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body or username could not be requested - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - Hue GetBridge(const HueIdentification& identification); - - //! \brief Function that adds a username to the usernames map - //! - //! \param mac MAC address of Hue bridge - //! \param username Username that is used to control the Hue bridge - void AddUsername(const std::string& mac, const std::string& username); - - //! \brief Function that returns a map of mac addresses and usernames. - //! - //! Note these should be saved at the end and re-loaded with \ref AddUsername - //! next time, so only one username is generated per bridge. \returns A map - //! mapping mac address to username for every bridge - const std::map& GetAllUsernames() const; - - //! \brief Normalizes mac address to plain hex number. - //! \returns \p input without separators and whitespace, in lower case. - static std::string NormalizeMac(std::string input); - -private: - //! \brief Parses mac address from description.xml - //! - //! \param description Content of description.xml file as returned by GET request. - //! \returns Content of xml element \c serialNumber if description matches a Hue bridge, otherwise an empty string. - static std::string ParseDescription(const std::string& description); - - std::map usernames; //!< Maps all macs to usernames added by \ref - //!< HueFinder::AddUsername - std::shared_ptr http_handler; -}; - -//! Hue class -class Hue -{ - friend class HueFinder; - -public: - //! \brief Constructor of Hue class - //! - //! \param ip IP address in dotted decimal notation like "192.168.2.1" - //! \param port Port of the hue bridge - //! \param username String that specifies the username that is used to control - //! the bridge. This needs to be acquired in \ref requestUsername - //! \param handler HttpHandler for communication with the bridge - Hue(const std::string& ip, const int port, const std::string& username, - std::shared_ptr handler); - - //! \brief Function to get the ip address of the hue bridge - //! - //! \return string containing ip - std::string getBridgeIP(); - - //! \brief Function to get the port of the hue bridge - //! - //! \return integer containing port - int getBridgePort(); - - //! \brief Send a username request to the Hue bridge. - //! - //! Blocks for about 30 seconds and 5 seconds to prepare. - //! It automatically sets the username variable according to the username received and returns the username - //! received. This function should only be called once to acquire a username to control the bridge and the username - //! should be saved for future use. - //! \return username for API usage - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error except link button not pressed. - //! \throws nlohmann::json::parse_error when response could not be parsed - std::string requestUsername(); - - //! \brief Function that returns the username - //! - //! \return The username used for API access - std::string getUsername(); - - //! \brief Function to set the ip address of this class representing a bridge - //! - //! \param ip String that specifies the ip in dotted decimal notation like "192.168.2.1" - void setIP(const std::string& ip); - - //! \brief Function to set the port of this class representing a bridge - //! - //! \param port Integer that specifies the port of an address like - //! "192.168.2.1:8080" - void setPort(const int port); - - //! \brief Function that returns a \ref HueLight of specified id - //! - //! \param id Integer that specifies the ID of a Hue light - //! \return \ref HueLight that can be controlled - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when id does not exist or type is unknown - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - HueLight& getLight(int id); - - //! \brief Function to remove a light from the bridge - //! - //! \attention Any use of the light after it was successfully removed results in undefined behavior - //! \param id Id of the light to remove - //! \return true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contains no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - bool removeLight(int id); - - //! \brief Function that returns all light types that are associated with this bridge - //! - //! \return A map mapping light id's to light types for every light - // const std::map& getAllLightTypes(); - - //! \brief Function that returns all lights that are associated with this - //! bridge - //! - //! \return A vector containing references to every HueLight - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contains no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - std::vector> getAllLights(); - - //! \brief Function that tells whether a given light id represents an existing light - //! - //! Calls refreshState to update the local bridge state - //! \param id Id of a light to check for existance - //! \return Bool that is true when a light with the given id exists and false when not - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contains no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - bool lightExists(int id); - - //! \brief Const function that tells whether a given light id represents an - //! existing light - //! - //! \note This will not update the local state of the bridge - //! \param id Id of a light to check for existance - //! \return Bool that is true when a light with the given id exists and false - //! when not - bool lightExists(int id) const; + class HueFinder + { + public: + struct HueIdentification + { + std::string ip; + int port = 80; + std::string mac; + }; + + public: + //! \brief Constructor of HueFinder class + //! + //! \param handler HttpHandler of type \ref IHttpHandler for communication with the bridge + HueFinder(std::shared_ptr handler); + + //! \brief Finds all bridges in the network and returns them. + //! + //! The user should be given the opportunity to select the correct one based on the mac address. + //! \return vector containing ip and mac of all found bridges + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + std::vector FindBridges() const; + + //! \brief Gets a \ref Hue bridge based on its identification + //! + //! \param identification \ref HueIdentification that specifies a bridge + //! \return \ref Hue class object + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body or username could not be requested + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + Hue GetBridge(const HueIdentification& identification); + + //! \brief Function that adds a username to the usernames map + //! + //! \param mac MAC address of Hue bridge + //! \param username Username that is used to control the Hue bridge + void AddUsername(const std::string& mac, const std::string& username); + + //! \brief Function that returns a map of mac addresses and usernames. + //! + //! Note these should be saved at the end and re-loaded with \ref AddUsername + //! next time, so only one username is generated per bridge. \returns A map + //! mapping mac address to username for every bridge + const std::map& GetAllUsernames() const; + + //! \brief Normalizes mac address to plain hex number. + //! \returns \p input without separators and whitespace, in lower case. + static std::string NormalizeMac(std::string input); + + private: + //! \brief Parses mac address from description.xml + //! + //! \param description Content of description.xml file as returned by GET request. + //! \returns Content of xml element \c serialNumber if description matches a Hue bridge, otherwise an empty + //! string. + static std::string ParseDescription(const std::string& description); + + std::map usernames; //!< Maps all macs to usernames added by \ref + //!< HueFinder::AddUsername + std::shared_ptr http_handler; + }; - //! \brief Const function that returns the picture name of a given light id - //! - //! \note This will not update the local state of the bridge. - //! \note This function will only return the filename without extension, - //! because Philips provides different file types. \param id Id of a light to - //! get the picture of \return String that either contains the filename of the - //! picture of the light or if it was not found an empty string - std::string getPictureOfLight(int id) const; - - //! \brief Const function that returns the picture name of a given model id - //! - //! \note This will not update the local state of the bridge. - //! \note This function will only return the filename without extension, - //! because Philips provides different file types. \param model_id Model Id of - //! a device to get the picture of \return String that either contains the - //! filename of the picture of the device or if it was not found an empty - //! string - std::string getPictureOfModel(const std::string& model_id) const; - - //! \brief Function that sets the HttpHandler and updates the HueCommandAPI. - //! - //! The HttpHandler and HueCommandAPI are used for bridge communication - //! \param handler a HttpHandler of type \ref IHttpHandler - void setHttpHandler(std::shared_ptr handler) + //! Hue class + class Hue { - http_handler = std::move(handler); - commands = HueCommandAPI(ip, port, username, http_handler); - } - -private: - //! \brief Function that refreshes the local \ref state of the Hue bridge - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - void refreshState(); - -private: - std::string ip; //!< IP-Address of the hue bridge in dotted decimal notation - //!< like "192.168.2.1" - std::string username; //!< Username that is ussed to access the hue bridge - int port; - nlohmann::json state; //!< The state of the hue bridge as it is returned from it - std::map lights; //!< Maps ids to HueLights that are controlled by this bridge - - std::shared_ptr simpleBrightnessStrategy; //!< Strategy that is used for controlling the - //!< brightness of lights - std::shared_ptr simpleColorHueStrategy; //!< Strategy that is used for controlling the - //!< color of lights - std::shared_ptr extendedColorHueStrategy; //!< Strategy that is used for controlling the - //!< color of lights - std::shared_ptr simpleColorTemperatureStrategy; //!< Strategy that is used for controlling - //!< the color temperature of lights - std::shared_ptr extendedColorTemperatureStrategy; //!< Strategy that is used for - //!< controlling the color temperature - //!< of lights - std::shared_ptr http_handler; //!< A IHttpHandler that is used to communicate with the - //!< bridge - HueCommandAPI commands; //!< A HueCommandAPI that is used to communicate with the bridge -}; + friend class HueFinder; + + public: + //! \brief Constructor of Hue class + //! + //! \param ip IP address in dotted decimal notation like "192.168.2.1" + //! \param port Port of the hue bridge + //! \param username String that specifies the username that is used to control + //! the bridge. This needs to be acquired in \ref requestUsername + //! \param handler HttpHandler for communication with the bridge + Hue(const std::string& ip, const int port, const std::string& username, + std::shared_ptr handler); + + //! \brief Function to get the ip address of the hue bridge + //! + //! \return string containing ip + std::string getBridgeIP(); + + //! \brief Function to get the port of the hue bridge + //! + //! \return integer containing port + int getBridgePort(); + + //! \brief Send a username request to the Hue bridge. + //! + //! Blocks for about 30 seconds and 5 seconds to prepare. + //! It automatically sets the username variable according to the username received and returns the username + //! received. This function should only be called once to acquire a username to control the bridge and the + //! username should be saved for future use. \return username for API usage \throws std::system_error when + //! system or socket operations fail \throws HueException when response contained no body \throws + //! HueAPIResponseException when response contains an error except link button not pressed. \throws + //! nlohmann::json::parse_error when response could not be parsed + std::string requestUsername(); + + //! \brief Function that returns the username + //! + //! \return The username used for API access + std::string getUsername(); + + //! \brief Function to set the ip address of this class representing a bridge + //! + //! \param ip String that specifies the ip in dotted decimal notation like "192.168.2.1" + void setIP(const std::string& ip); + + //! \brief Function to set the port of this class representing a bridge + //! + //! \param port Integer that specifies the port of an address like + //! "192.168.2.1:8080" + void setPort(const int port); + + //! \brief Function that returns a \ref HueLight of specified id + //! + //! \param id Integer that specifies the ID of a Hue light + //! \return \ref HueLight that can be controlled + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when id does not exist or type is unknown + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + HueLight& getLight(int id); + + //! \brief Function to remove a light from the bridge + //! + //! \attention Any use of the light after it was successfully removed results in undefined behavior + //! \param id Id of the light to remove + //! \return true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contains no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + bool removeLight(int id); + + //! \brief Function that returns all light types that are associated with this bridge + //! + //! \return A map mapping light id's to light types for every light + // const std::map& getAllLightTypes(); + + //! \brief Function that returns all lights that are associated with this + //! bridge + //! + //! \return A vector containing references to every HueLight + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contains no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + std::vector> getAllLights(); + + //! \brief Function that tells whether a given light id represents an existing light + //! + //! Calls refreshState to update the local bridge state + //! \param id Id of a light to check for existance + //! \return Bool that is true when a light with the given id exists and false when not + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contains no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + bool lightExists(int id); + + //! \brief Const function that tells whether a given light id represents an + //! existing light + //! + //! \note This will not update the local state of the bridge + //! \param id Id of a light to check for existance + //! \return Bool that is true when a light with the given id exists and false + //! when not + bool lightExists(int id) const; + + //! \brief Const function that returns the picture name of a given light id + //! + //! \note This will not update the local state of the bridge. + //! \note This function will only return the filename without extension, + //! because Philips provides different file types. \param id Id of a light to + //! get the picture of \return String that either contains the filename of the + //! picture of the light or if it was not found an empty string + std::string getPictureOfLight(int id) const; + + //! \brief Const function that returns the picture name of a given model id + //! + //! \note This will not update the local state of the bridge. + //! \note This function will only return the filename without extension, + //! because Philips provides different file types. \param model_id Model Id of + //! a device to get the picture of \return String that either contains the + //! filename of the picture of the device or if it was not found an empty + //! string + std::string getPictureOfModel(const std::string& model_id) const; + + //! \brief Function that sets the HttpHandler and updates the HueCommandAPI. + //! + //! The HttpHandler and HueCommandAPI are used for bridge communication + //! \param handler a HttpHandler of type \ref IHttpHandler + void setHttpHandler(std::shared_ptr handler) + { + http_handler = std::move(handler); + commands = HueCommandAPI(ip, port, username, http_handler); + } + + private: + //! \brief Function that refreshes the local \ref state of the Hue bridge + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + void refreshState(); + + private: + std::string ip; //!< IP-Address of the hue bridge in dotted decimal notation + //!< like "192.168.2.1" + std::string username; //!< Username that is ussed to access the hue bridge + int port; + nlohmann::json state; //!< The state of the hue bridge as it is returned from it + std::map lights; //!< Maps ids to HueLights that are controlled by this bridge + + std::shared_ptr simpleBrightnessStrategy; //!< Strategy that is used for controlling the + //!< brightness of lights + std::shared_ptr simpleColorHueStrategy; //!< Strategy that is used for controlling the + //!< color of lights + std::shared_ptr extendedColorHueStrategy; //!< Strategy that is used for controlling the + //!< color of lights + std::shared_ptr + simpleColorTemperatureStrategy; //!< Strategy that is used for controlling + //!< the color temperature of lights + std::shared_ptr extendedColorTemperatureStrategy; //!< Strategy that is used for + //!< controlling the color + //!< temperature of lights + std::shared_ptr http_handler; //!< A IHttpHandler that is used to communicate with the + //!< bridge + HueCommandAPI commands; //!< A HueCommandAPI that is used to communicate with the bridge + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/HueCommandAPI.h b/include/hueplusplus/HueCommandAPI.h index 7bd9895..4d978dc 100644 --- a/include/hueplusplus/HueCommandAPI.h +++ b/include/hueplusplus/HueCommandAPI.h @@ -30,94 +30,98 @@ #include "HueException.h" #include "IHttpHandler.h" -//! Handles communication to the bridge via IHttpHandler and enforces a timeout -//! between each request -class HueCommandAPI +namespace hueplusplus { -public: - //! \brief Construct from ip, username and HttpHandler - //! - //! \param ip ip address of the Hue bridge in dotted decimal notation like "192.168.2.1" - //! \param port of the hue bridge - //! \param username username that is used to control the bridge - //! \param httpHandler HttpHandler for communication with the bridge - HueCommandAPI( - const std::string& ip, int port, const std::string& username, std::shared_ptr httpHandler); - - //! \brief Copy construct from other HueCommandAPI - //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed - HueCommandAPI(const HueCommandAPI&) = default; - //! \brief Move construct from other HueCommandAPI - //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed - HueCommandAPI(HueCommandAPI&&) = default; - - //! \brief Copy assign from other HueCommandAPI - //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed - HueCommandAPI& operator=(const HueCommandAPI&) = default; - //! \brief Move assign from other HueCommandAPI - //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed - HueCommandAPI& operator=(HueCommandAPI&&) = default; - - //! \brief Sends a HTTP PUT request to the bridge and returns the response - //! - //! This function will block until at least \ref minDelay has passed to any previous request - //! \param path API request path (appended after /api/{username}) - //! \param request Request to the api, may be empty - //! \returns The return value of the underlying \ref IHttpHandler::PUTJson call - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contains no body - //! \throws HueAPIResponseException when response contains an error - nlohmann::json PUTRequest(const std::string& path, const nlohmann::json& request) const; - nlohmann::json PUTRequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const; - - //! \brief Sends a HTTP GET request to the bridge and returns the response - //! - //! This function will block until at least \ref minDelay has passed to any previous request - //! \param path API request path (appended after /api/{username}) - //! \param request Request to the api, may be empty - //! \returns The return value of the underlying \ref IHttpHandler::GETJson call - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contains no body - //! \throws HueAPIResponseException when response contains an error - nlohmann::json GETRequest(const std::string& path, const nlohmann::json& request) const; - nlohmann::json GETRequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const; - - //! \brief Sends a HTTP DELETE request to the bridge and returns the response - //! - //! This function will block until at least \ref minDelay has passed to any previous request - //! \param path API request path (appended after /api/{username}) - //! \param request Request to the api, may be empty - //! \returns The return value of the underlying \ref IHttpHandler::DELETEJson call - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contains no body - //! \throws HueAPIResponseException when response contains an error - nlohmann::json DELETERequest(const std::string& path, const nlohmann::json& request) const; - nlohmann::json DELETERequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const; - -private: - struct TimeoutData + //! Handles communication to the bridge via IHttpHandler and enforces a timeout + //! between each request + class HueCommandAPI { - std::chrono::steady_clock::time_point timeout; - std::mutex mutex; + public: + //! \brief Construct from ip, username and HttpHandler + //! + //! \param ip ip address of the Hue bridge in dotted decimal notation like "192.168.2.1" + //! \param port of the hue bridge + //! \param username username that is used to control the bridge + //! \param httpHandler HttpHandler for communication with the bridge + HueCommandAPI(const std::string& ip, int port, const std::string& username, + std::shared_ptr httpHandler); + + //! \brief Copy construct from other HueCommandAPI + //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed + HueCommandAPI(const HueCommandAPI&) = default; + //! \brief Move construct from other HueCommandAPI + //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed + HueCommandAPI(HueCommandAPI&&) = default; + + //! \brief Copy assign from other HueCommandAPI + //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed + HueCommandAPI& operator=(const HueCommandAPI&) = default; + //! \brief Move assign from other HueCommandAPI + //! \note All copies refer to the same timeout data, so even calls from different objects will be delayed + HueCommandAPI& operator=(HueCommandAPI&&) = default; + + //! \brief Sends a HTTP PUT request to the bridge and returns the response + //! + //! This function will block until at least \ref minDelay has passed to any previous request + //! \param path API request path (appended after /api/{username}) + //! \param request Request to the api, may be empty + //! \returns The return value of the underlying \ref IHttpHandler::PUTJson call + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contains no body + //! \throws HueAPIResponseException when response contains an error + nlohmann::json PUTRequest(const std::string& path, const nlohmann::json& request) const; + nlohmann::json PUTRequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const; + + //! \brief Sends a HTTP GET request to the bridge and returns the response + //! + //! This function will block until at least \ref minDelay has passed to any previous request + //! \param path API request path (appended after /api/{username}) + //! \param request Request to the api, may be empty + //! \returns The return value of the underlying \ref IHttpHandler::GETJson call + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contains no body + //! \throws HueAPIResponseException when response contains an error + nlohmann::json GETRequest(const std::string& path, const nlohmann::json& request) const; + nlohmann::json GETRequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const; + + //! \brief Sends a HTTP DELETE request to the bridge and returns the response + //! + //! This function will block until at least \ref minDelay has passed to any previous request + //! \param path API request path (appended after /api/{username}) + //! \param request Request to the api, may be empty + //! \returns The return value of the underlying \ref IHttpHandler::DELETEJson call + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contains no body + //! \throws HueAPIResponseException when response contains an error + nlohmann::json DELETERequest(const std::string& path, const nlohmann::json& request) const; + nlohmann::json DELETERequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const; + + private: + struct TimeoutData + { + std::chrono::steady_clock::time_point timeout; + std::mutex mutex; + }; + + //! \brief Throws an exception if response contains an error, passes though value + //! \throws HueAPIResponseException when response contains an error + //! \returns \ref response if there is no error + nlohmann::json HandleError(FileInfo fileInfo, const nlohmann::json& response) const; + + //! \brief Combines path with api prefix and username + //! \returns "/api//" + std::string CombinedPath(const std::string& path) const; + + public: + static constexpr std::chrono::steady_clock::duration minDelay = std::chrono::milliseconds(100); + + private: + std::string ip; + int port; + std::string username; + std::shared_ptr httpHandler; + std::shared_ptr timeout; }; - - //! \brief Throws an exception if response contains an error, passes though value - //! \throws HueAPIResponseException when response contains an error - //! \returns \ref response if there is no error - nlohmann::json HandleError(FileInfo fileInfo, const nlohmann::json& response) const; - - //! \brief Combines path with api prefix and username - //! \returns "/api//" - std::string CombinedPath(const std::string& path) const; - -public: - static constexpr std::chrono::steady_clock::duration minDelay = std::chrono::milliseconds(100); -private: - std::string ip; - int port; - std::string username; - std::shared_ptr httpHandler; - std::shared_ptr timeout; -}; +} // namespace hueplusplus #endif \ No newline at end of file diff --git a/include/hueplusplus/HueConfig.h b/include/hueplusplus/HueConfig.h index de5c069..a64adb3 100644 --- a/include/hueplusplus/HueConfig.h +++ b/include/hueplusplus/HueConfig.h @@ -23,7 +23,10 @@ #ifndef _HUE_CONFIG_H #define _HUE_CONFIG_H -const uint16_t c_PRE_ALERT_DELAY = 120; //!< Delay for advanced alerts before the actual alert -const uint16_t c_POST_ALERT_DELAY = 1600; //!< Delay for advanced alerts after the actual alert +namespace hueplusplus +{ + constexpr uint16_t c_PRE_ALERT_DELAY = 120; //!< Delay for advanced alerts before the actual alert + constexpr uint16_t c_POST_ALERT_DELAY = 1600; //!< Delay for advanced alerts after the actual alert +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/HueDeviceTypes.h b/include/hueplusplus/HueDeviceTypes.h index 0e963ad..5e6ced7 100644 --- a/include/hueplusplus/HueDeviceTypes.h +++ b/include/hueplusplus/HueDeviceTypes.h @@ -20,7 +20,6 @@ along with hueplusplus. If not, see . **/ - #ifndef _HUEDEVICETYPES_H #define _HUEDEVICETYPES_H @@ -29,15 +28,17 @@ #include "HueLight.h" -struct MakeHueLight { - auto operator()(std::string type, int id, HueCommandAPI commands, - std::shared_ptr simpleBrightnessStrategy, - std::shared_ptr extendedColorTemperatureStrategy, - std::shared_ptr simpleColorTemperatureStrategy, - std::shared_ptr extendedColorHueStrategy, - std::shared_ptr simpleColorHueStrategy) -> HueLight; -}; - +namespace hueplusplus +{ + struct MakeHueLight + { + auto operator()(std::string type, int id, HueCommandAPI commands, + std::shared_ptr simpleBrightnessStrategy, + std::shared_ptr extendedColorTemperatureStrategy, + std::shared_ptr simpleColorTemperatureStrategy, + std::shared_ptr extendedColorHueStrategy, + std::shared_ptr simpleColorHueStrategy) -> HueLight; + }; +} // namespace hueplusplus #endif - diff --git a/include/hueplusplus/HueException.h b/include/hueplusplus/HueException.h index fafb7cc..ba3ceb4 100644 --- a/include/hueplusplus/HueException.h +++ b/include/hueplusplus/HueException.h @@ -28,91 +28,94 @@ #include "json/json.hpp" -//! \brief Contains information about error location, use CURRENT_FILE_INFO to create -struct FileInfo +namespace hueplusplus { - //! \brief Current file name from __FILE__. Empty if unknown - std::string filename; - //! \brief Current line number from __LINE__. -1 if unknown - int line = -1; - //! \brief Current function from __func__. Empty if unknown - std::string func; - - //! \brief String representation of func, file and line. - //! \returns " in :" or "Unknown file" if unknown. - std::string ToString() const; -}; - -//! \brief Exception class with file information. Base class of all custom exception classes -class HueException : public std::exception -{ -public: - //! \brief Creates HueException with information about the error and source - //! \param fileInfo Source of the error. Must not always be the throw location, - //! can also be a calling function which matches the cause better. - //! \param message Human readable error message. - HueException(FileInfo fileInfo, const std::string& message); - - //! \brief What message of the exception - //! \returns exception name, file info and constructor message as char* into member string - const char* what() const noexcept override; - - //! \brief Filename and line where the exception was thrown or caused by - const FileInfo& GetFile() const noexcept; - -protected: - //! \brief Creates HueException with child class name - //! - //! Should be used by subclasses which can append additional information to the end of whatMessage. - //! \param exceptionName class name of the subclass - //! \param fileInfo Source of the error. Must not always be the throw location, - //! can also be a calling function which matches the cause better. - //! \param message Human readable error message - HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message); - -private: - std::string whatMessage; - FileInfo fileInfo; -}; - -//! \brief Exception caused by a Hue API "error" response with additional information -//! -//! Refer to Hue developer documentation for more detail on specific error codes. -class HueAPIResponseException : public HueException -{ -public: - //! \brief Create exception with info from Hue API error - //! \param fileInfo Source of the error. Must not always be the throw location, - //! can also be a calling function which matches the cause better. - //! \param error Hue API error code from error response. - //! \param address URI the API call referred to from error response. - //! \param description Error description from response. - HueAPIResponseException(FileInfo fileInfo, int error, std::string address, std::string description); - - //! \brief Error number from Hue API error response. + //! \brief Contains information about error location, use CURRENT_FILE_INFO to create + struct FileInfo + { + //! \brief Current file name from __FILE__. Empty if unknown + std::string filename; + //! \brief Current line number from __LINE__. -1 if unknown + int line = -1; + //! \brief Current function from __func__. Empty if unknown + std::string func; + + //! \brief String representation of func, file and line. + //! \returns " in :" or "Unknown file" if unknown. + std::string ToString() const; + }; + + //! \brief Exception class with file information. Base class of all custom exception classes + class HueException : public std::exception + { + public: + //! \brief Creates HueException with information about the error and source + //! \param fileInfo Source of the error. Must not always be the throw location, + //! can also be a calling function which matches the cause better. + //! \param message Human readable error message. + HueException(FileInfo fileInfo, const std::string& message); + + //! \brief What message of the exception + //! \returns exception name, file info and constructor message as char* into member string + const char* what() const noexcept override; + + //! \brief Filename and line where the exception was thrown or caused by + const FileInfo& GetFile() const noexcept; + + protected: + //! \brief Creates HueException with child class name + //! + //! Should be used by subclasses which can append additional information to the end of whatMessage. + //! \param exceptionName class name of the subclass + //! \param fileInfo Source of the error. Must not always be the throw location, + //! can also be a calling function which matches the cause better. + //! \param message Human readable error message + HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message); + + private: + std::string whatMessage; + FileInfo fileInfo; + }; + + //! \brief Exception caused by a Hue API "error" response with additional information //! - //! Refer to Hue developer documentation for meaning of error codes. - int GetErrorNumber() const noexcept; - //! \brief Address the API call tried to access. - const std::string& GetAddress() const noexcept; - //! \brief Error description - const std::string& GetDescription() const noexcept; - - //! \brief Creates exception from API response. - //! \param fileInfo Location of the cause - //! \param response Hue API response. Must contain a member "error" with "type", "address" and "description". - //! \returns HueAPIResponseException with info from the response. - //! If response does not contain the required members, they are defaulted to -1 or "". - static HueAPIResponseException Create(FileInfo fileInfo, const nlohmann::json& response); - -private: - //! \brief Creates exception message containing the given information - static std::string GetMessage(int error, const std::string& addr, const std::string& description); - -private: - int error; - std::string address; - std::string description; -}; + //! Refer to Hue developer documentation for more detail on specific error codes. + class HueAPIResponseException : public HueException + { + public: + //! \brief Create exception with info from Hue API error + //! \param fileInfo Source of the error. Must not always be the throw location, + //! can also be a calling function which matches the cause better. + //! \param error Hue API error code from error response. + //! \param address URI the API call referred to from error response. + //! \param description Error description from response. + HueAPIResponseException(FileInfo fileInfo, int error, std::string address, std::string description); + + //! \brief Error number from Hue API error response. + //! + //! Refer to Hue developer documentation for meaning of error codes. + int GetErrorNumber() const noexcept; + //! \brief Address the API call tried to access. + const std::string& GetAddress() const noexcept; + //! \brief Error description + const std::string& GetDescription() const noexcept; + + //! \brief Creates exception from API response. + //! \param fileInfo Location of the cause + //! \param response Hue API response. Must contain a member "error" with "type", "address" and "description". + //! \returns HueAPIResponseException with info from the response. + //! If response does not contain the required members, they are defaulted to -1 or "". + static HueAPIResponseException Create(FileInfo fileInfo, const nlohmann::json& response); + + private: + //! \brief Creates exception message containing the given information + static std::string GetMessage(int error, const std::string& addr, const std::string& description); + + private: + int error; + std::string address; + std::string description; + }; +} // namespace hueplusplus #endif \ No newline at end of file diff --git a/include/hueplusplus/HueExceptionMacro.h b/include/hueplusplus/HueExceptionMacro.h index 80ffaeb..843b651 100644 --- a/include/hueplusplus/HueExceptionMacro.h +++ b/include/hueplusplus/HueExceptionMacro.h @@ -23,5 +23,5 @@ #include "HueException.h" #ifndef CURRENT_FILE_INFO -#define CURRENT_FILE_INFO (FileInfo{__FILE__, __LINE__, __func__}) +#define CURRENT_FILE_INFO (::hueplusplus::FileInfo{__FILE__, __LINE__, __func__}) #endif \ No newline at end of file diff --git a/include/hueplusplus/HueLight.h b/include/hueplusplus/HueLight.h index 0bd20ca..1e981cd 100644 --- a/include/hueplusplus/HueLight.h +++ b/include/hueplusplus/HueLight.h @@ -32,61 +32,63 @@ #include "json/json.hpp" -/*enum ModelType +namespace hueplusplus { -UNDEFINED, // undefined model -LCT001, // Hue bulb A19, Color Gamut B, ECL -LCT007, // Hue bulb A19, Color Gamut B, ECL -LCT010, // Hue bulb A19, Color Gamut C, ECL -LCT014, // Hue bulb A19, Color Gamut C, ECL - -LCT002, // Hue Spot BR30, Color Gamut B, ECL -LCT003, // Hue Spot GU10, Color Gamut B, ECL - -LCT011, // Hue BR30, Color Gamut C, ECL - -LST001, // Hue LightStrips, Color Gamut A, CL -LST002, // Hue LightStrips Plus, Color Gamut C, ECL - -LLC010, // Hue Living Colors Iris, Color Gamut A, CL -LLC011, // Hue Living Colors Bloom, Color Gamut A, CL -LLC012, // Hue Living Colors Bloom, Color Gamut A, CL -LLC006, // Living Colors Gen3 Iris, Color Gamut A, CL, NO HUE FRIEND -LLC007, // Living Colors Gen3 Bloom, Aura, Color Gamut A, CL, NO HUE FRIEND -LLC013, // Disney Living Colors, Color Gamut A, CL - -LWB004, // Hue A19 Lux, Color Gamut -, DL -LWB006, // Hue A19 Lux, Color Gamut -, DL -LWB007, // Hue A19 Lux, Color Gamut -, DL -LWB010, // Hue A19 Lux, Color Gamut -, DL -LWB014, // Hue A19 Lux, Color Gamut -, DL - -LLM001, // Color Light Module, Color Gamut B, ECL -LLM010, // Color Temperature Module, Color Gamut 2200K-6500K, CTL -LLM011, // Color Temperature Module, Color Gamut 2200K-6500K, CTL -LLM012, // Color Temperature Module, Color Gamut 2200K-6500K, CTL - -LTW001, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL -LTW004, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL -LTW013, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL -LTW014, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL - -LLC020 // Hue Go, Color Gamut C, ECL -};*/ - -//! enum that specifies the color type of all HueLights -enum ColorType -{ - UNDEFINED, //!< ColorType for this light is unknown or undefined - NONE, //!< light has no specific ColorType - GAMUT_A, - GAMUT_B, - GAMUT_C, - TEMPERATURE, - GAMUT_A_TEMPERATURE, - GAMUT_B_TEMPERATURE, - GAMUT_C_TEMPERATURE -}; + /*enum ModelType + { + UNDEFINED, // undefined model + LCT001, // Hue bulb A19, Color Gamut B, ECL + LCT007, // Hue bulb A19, Color Gamut B, ECL + LCT010, // Hue bulb A19, Color Gamut C, ECL + LCT014, // Hue bulb A19, Color Gamut C, ECL + + LCT002, // Hue Spot BR30, Color Gamut B, ECL + LCT003, // Hue Spot GU10, Color Gamut B, ECL + + LCT011, // Hue BR30, Color Gamut C, ECL + + LST001, // Hue LightStrips, Color Gamut A, CL + LST002, // Hue LightStrips Plus, Color Gamut C, ECL + + LLC010, // Hue Living Colors Iris, Color Gamut A, CL + LLC011, // Hue Living Colors Bloom, Color Gamut A, CL + LLC012, // Hue Living Colors Bloom, Color Gamut A, CL + LLC006, // Living Colors Gen3 Iris, Color Gamut A, CL, NO HUE FRIEND + LLC007, // Living Colors Gen3 Bloom, Aura, Color Gamut A, CL, NO HUE FRIEND + LLC013, // Disney Living Colors, Color Gamut A, CL + + LWB004, // Hue A19 Lux, Color Gamut -, DL + LWB006, // Hue A19 Lux, Color Gamut -, DL + LWB007, // Hue A19 Lux, Color Gamut -, DL + LWB010, // Hue A19 Lux, Color Gamut -, DL + LWB014, // Hue A19 Lux, Color Gamut -, DL + + LLM001, // Color Light Module, Color Gamut B, ECL + LLM010, // Color Temperature Module, Color Gamut 2200K-6500K, CTL + LLM011, // Color Temperature Module, Color Gamut 2200K-6500K, CTL + LLM012, // Color Temperature Module, Color Gamut 2200K-6500K, CTL + + LTW001, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL + LTW004, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL + LTW013, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL + LTW014, // Hue Spot BR30, Color Gamut 2200K-6500K, CTL + + LLC020 // Hue Go, Color Gamut C, ECL + };*/ + + //! enum that specifies the color type of all HueLights + enum ColorType + { + UNDEFINED, //!< ColorType for this light is unknown or undefined + NONE, //!< light has no specific ColorType + GAMUT_A, + GAMUT_B, + GAMUT_C, + TEMPERATURE, + GAMUT_A_TEMPERATURE, + GAMUT_B_TEMPERATURE, + GAMUT_C_TEMPERATURE + }; //! //! Class for Hue Light fixtures @@ -101,690 +103,692 @@ class HueLight friend class SimpleColorTemperatureStrategy; friend class ExtendedColorTemperatureStrategy; -public: - //! \brief std dtor - ~HueLight() = default; - - //! \brief Function that turns the light on. - //! - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms - //! \return true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool On(uint8_t transition = 4); - - //! \brief Function that turns the light off. - //! - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual 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 - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool isOn(); - - //! \brief Const function to check whether a light is on or off - //! - //! \note This will not refresh the light state - //! \return Bool that is true, when the light is on and false, when off - virtual bool isOn() const; - - //! \brief Const function that returns the id of this light - //! - //! \return integer representing the light id - virtual int getId() const; - - //! \brief Const function that returns the light type - //! - //! \return String containing the type - virtual std::string getType() const; - - //! \brief Function that returns the name of the light. - //! - //! \return String containig the name of the light - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual std::string getName(); - - //! \brief Const function that returns the name of the light. - //! - //! \note This will not refresh the light state - //! \return String containig the name of the light - virtual std::string getName() const; - - //! \brief Const function that returns the modelid of the light - //! - //! \return String conatining the modelid - virtual std::string getModelId() const; - - //! \brief Const function that returns the uniqueid of the light - //! - //! \note Only working on bridges with versions starting at 1.4 - //! \return String containing the uniqueid or an empty string when the function is not supported - virtual std::string getUId() const; - - //! \brief Const function that returns the manufacturername of the light - //! - //! \note Only working on bridges with versions starting at 1.7 - //! \return String containing the manufacturername or an empty string when the function is not supported - virtual std::string getManufacturername() const; - - //! \brief Const function that returns the productname of the light - //! - //! \note Only working on bridges with versions starting at 1.24 - //! \return String containing the productname or an empty string when the function is not supported - virtual std::string getProductname() const; - - //! \brief Const function that returns the luminaireuniqueid of the light - //! - //! \note Only working on bridges with versions starting at 1.9 - //! \return String containing the luminaireuniqueid or an empty string when the function is not supported - virtual std::string getLuminaireUId() const; - - //! \brief Function that returns the software version of the light - //! - //! \return String containing the software version - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual std::string getSwVersion(); - - //! \brief Const function that returns the software version of the light - //! - //! \note This will not refresh the light state - //! \return String containing the software version - virtual std::string getSwVersion() const; - - //! \brief Function that sets the name of the light - //! - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setName(const std::string& name); - - //! \brief Const function that returns the color type of the light. - //! - //! \return ColorType containig the color type of the light - virtual ColorType getColorType() const; - - //! \brief Const function to check whether this light has brightness control - //! - //! \return Bool that is true when the light has specified abilities and false - //! when not - virtual bool hasBrightnessControl() const { return brightnessStrategy != nullptr; }; - - //! \brief Const function to check whether this light has color temperature - //! control - //! - //! \return Bool that is true when the light has specified abilities and false - //! when not - virtual bool hasTemperatureControl() const { return colorTemperatureStrategy != nullptr; }; - - //! \brief Connst function to check whether this light has full color control - //! - //! \return Bool that is true when the light has specified abilities and false - //! when not - virtual bool hasColorControl() const { return colorHueStrategy != nullptr; }; - - //! \brief Const function that converts Kelvin to Mired. - //! - //! \param kelvin Unsigned integer value in Kelvin - //! \return Unsigned integer value in Mired - unsigned int KelvinToMired(unsigned int kelvin) const; - - //! \brief Const function that converts Mired to Kelvin. - //! - //! \param mired Unsigned integer value in Mired - //! \return Unsigned integer value in Kelvin - unsigned int MiredToKelvin(unsigned int mired) const; - - //! \brief Function that sets the brightness of this light. - //! - //! \note 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 - //! 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 - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setBrightness(unsigned int bri, uint8_t transition = 4) - { - if (brightnessStrategy) + public: + //! \brief std dtor + ~HueLight() = default; + + //! \brief Function that turns the light on. + //! + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool On(uint8_t transition = 4); + + //! \brief Function that turns the light off. + //! + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual 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 + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool isOn(); + + //! \brief Const function to check whether a light is on or off + //! + //! \note This will not refresh the light state + //! \return Bool that is true, when the light is on and false, when off + virtual bool isOn() const; + + //! \brief Const function that returns the id of this light + //! + //! \return integer representing the light id + virtual int getId() const; + + //! \brief Const function that returns the light type + //! + //! \return String containing the type + virtual std::string getType() const; + + //! \brief Function that returns the name of the light. + //! + //! \return String containig the name of the light + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual std::string getName(); + + //! \brief Const function that returns the name of the light. + //! + //! \note This will not refresh the light state + //! \return String containig the name of the light + virtual std::string getName() const; + + //! \brief Const function that returns the modelid of the light + //! + //! \return String conatining the modelid + virtual std::string getModelId() const; + + //! \brief Const function that returns the uniqueid of the light + //! + //! \note Only working on bridges with versions starting at 1.4 + //! \return String containing the uniqueid or an empty string when the function is not supported + virtual std::string getUId() const; + + //! \brief Const function that returns the manufacturername of the light + //! + //! \note Only working on bridges with versions starting at 1.7 + //! \return String containing the manufacturername or an empty string when the function is not supported + virtual std::string getManufacturername() const; + + //! \brief Const function that returns the productname of the light + //! + //! \note Only working on bridges with versions starting at 1.24 + //! \return String containing the productname or an empty string when the function is not supported + virtual std::string getProductname() const; + + //! \brief Const function that returns the luminaireuniqueid of the light + //! + //! \note Only working on bridges with versions starting at 1.9 + //! \return String containing the luminaireuniqueid or an empty string when the function is not supported + virtual std::string getLuminaireUId() const; + + //! \brief Function that returns the software version of the light + //! + //! \return String containing the software version + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual std::string getSwVersion(); + + //! \brief Const function that returns the software version of the light + //! + //! \note This will not refresh the light state + //! \return String containing the software version + virtual std::string getSwVersion() const; + + //! \brief Function that sets the name of the light + //! + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setName(const std::string& name); + + //! \brief Const function that returns the color type of the light. + //! + //! \return ColorType containig the color type of the light + virtual ColorType getColorType() const; + + //! \brief Const function to check whether this light has brightness control + //! + //! \return Bool that is true when the light has specified abilities and false + //! when not + virtual bool hasBrightnessControl() const { return brightnessStrategy != nullptr; }; + + //! \brief Const function to check whether this light has color temperature + //! control + //! + //! \return Bool that is true when the light has specified abilities and false + //! when not + virtual bool hasTemperatureControl() const { return colorTemperatureStrategy != nullptr; }; + + //! \brief Connst function to check whether this light has full color control + //! + //! \return Bool that is true when the light has specified abilities and false + //! when not + virtual bool hasColorControl() const { return colorHueStrategy != nullptr; }; + + //! \brief Const function that converts Kelvin to Mired. + //! + //! \param kelvin Unsigned integer value in Kelvin + //! \return Unsigned integer value in Mired + unsigned int KelvinToMired(unsigned int kelvin) const; + + //! \brief Const function that converts Mired to Kelvin. + //! + //! \param mired Unsigned integer value in Mired + //! \return Unsigned integer value in Kelvin + unsigned int MiredToKelvin(unsigned int mired) const; + + //! \brief Function that sets the brightness of this light. + //! + //! \note 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 + //! 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 + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setBrightness(unsigned int bri, uint8_t transition = 4) { - return brightnessStrategy->setBrightness(bri, transition, *this); - } - return false; - }; - - //! \brief Const function that returns the brightness of this light. - //! - //! \note The brightness will only be returned if the light has a reference to - //! a specific \ref BrightnessStrategy. \note This will not refresh the light - //! state The brightness can range from 0 = off to 254 = fully lit. \return - //! Unsigned int that is 0 when function failed - virtual unsigned int getBrightness() const - { - if (brightnessStrategy) + if (brightnessStrategy) + { + return brightnessStrategy->setBrightness(bri, transition, *this); + } + return false; + }; + + //! \brief Const function that returns the brightness of this light. + //! + //! \note The brightness will only be returned if the light has a reference to + //! a specific \ref BrightnessStrategy. \note This will not refresh the light + //! state The brightness can range from 0 = off to 254 = fully lit. \return + //! Unsigned int that is 0 when function failed + virtual unsigned int getBrightness() const { - return brightnessStrategy->getBrightness(*this); - } - return 0; - }; - - //! \brief Function that returns the brightness of this light. - //! - //! \note 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 - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual unsigned int getBrightness() - { - if (brightnessStrategy) + if (brightnessStrategy) + { + return brightnessStrategy->getBrightness(*this); + } + return 0; + }; + + //! \brief Function that returns the brightness of this light. + //! + //! \note 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 + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual unsigned int getBrightness() { - return brightnessStrategy->getBrightness(*this); - } - return 0; - }; - - //! \brief Fucntion that sets the color temperature of this light in mired. - //! - //! \note The color temperature will only be set if the light has a reference - //! to a specific \ref ColorTemperatureStrategy. The color temperature can - //! range from 153 to 500. - //! \param mired Unsigned int that specifies the color temperature in Mired - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setColorTemperature(unsigned int mired, uint8_t transition = 4) - { - if (colorTemperatureStrategy) + if (brightnessStrategy) + { + return brightnessStrategy->getBrightness(*this); + } + return 0; + }; + + //! \brief Fucntion that sets the color temperature of this light in mired. + //! + //! \note The color temperature will only be set if the light has a reference + //! to a specific \ref ColorTemperatureStrategy. The color temperature can + //! range from 153 to 500. + //! \param mired Unsigned int that specifies the color temperature in Mired + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setColorTemperature(unsigned int mired, uint8_t transition = 4) { - return colorTemperatureStrategy->setColorTemperature(mired, transition, *this); - } - return false; - }; - - //! \brief Const function that returns the current color temperature of the - //! light - //! - //! \note The color temperature will only be returned when the light has a - //! reference to a specific \ref ColorTemperatureStrategy. - //! \note This will not refresh the light state - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. - //! \return Unsigned int representing the color temperature in mired or 0 when failed - virtual unsigned int getColorTemperature() const - { - if (colorTemperatureStrategy) + if (colorTemperatureStrategy) + { + return colorTemperatureStrategy->setColorTemperature(mired, transition, *this); + } + return false; + }; + + //! \brief Const function that returns the current color temperature of the + //! light + //! + //! \note The color temperature will only be returned when the light has a + //! reference to a specific \ref ColorTemperatureStrategy. + //! \note This will not refresh the light state + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. + //! \return Unsigned int representing the color temperature in mired or 0 when failed + virtual unsigned int getColorTemperature() const { - return colorTemperatureStrategy->getColorTemperature(*this); - } - return 0; - }; - - //! \brief Function that returns the current color temperature of the light - //! - //! \note The color temperature will only be returned when the light has a - //! reference to a specific \ref ColorTemperatureStrategy. - //! Updates the lights state by calling refreshState() - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. - //! \return Unsigned int representing the color temperature in mired or 0 when failed - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual unsigned int getColorTemperature() - { - if (colorTemperatureStrategy) + if (colorTemperatureStrategy) + { + return colorTemperatureStrategy->getColorTemperature(*this); + } + return 0; + }; + + //! \brief Function that returns the current color temperature of the light + //! + //! \note The color temperature will only be returned when the light has a + //! reference to a specific \ref ColorTemperatureStrategy. + //! Updates the lights state by calling refreshState() + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. + //! \return Unsigned int representing the color temperature in mired or 0 when failed + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual unsigned int getColorTemperature() { - return colorTemperatureStrategy->getColorTemperature(*this); - } - return 0; - }; - - //! \brief Function to set the color of this light with specified hue. - //! - //! \note The color will only be set if the light has a reference to a - //! specific \ref ColorHueStrategy. The hue can range from 0 to 65535, whereas - //! 65535 and 0 are red, 25500 is green and 46920 is blue. - //! \param hue uint16_t that specifies the hue - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setColorHue(uint16_t hue, uint8_t transition = 4) - { - if (colorHueStrategy) + if (colorTemperatureStrategy) + { + return colorTemperatureStrategy->getColorTemperature(*this); + } + return 0; + }; + + //! \brief Function to set the color of this light with specified hue. + //! + //! \note The color will only be set if the light has a reference to a + //! specific \ref ColorHueStrategy. The hue can range from 0 to 65535, whereas + //! 65535 and 0 are red, 25500 is green and 46920 is blue. + //! \param hue uint16_t that specifies the hue + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setColorHue(uint16_t hue, uint8_t transition = 4) { - return colorHueStrategy->setColorHue(hue, transition, *this); - } - return false; - }; - - //! \brief Function to set the color of this light with specified saturation. - //! - //! \note The color will only be set if the light has a reference to a - //! specific \ref ColorHueStrategy. The saturation can range from 0 to 254, - //! whereas 0 is least saturated (white) and 254 is most saturated. - //! \param sat uint8_t that specifies the saturation - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setColorSaturation(uint8_t sat, uint8_t transition = 4) - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->setColorHue(hue, transition, *this); + } + return false; + }; + + //! \brief Function to set the color of this light with specified saturation. + //! + //! \note The color will only be set if the light has a reference to a + //! specific \ref ColorHueStrategy. The saturation can range from 0 to 254, + //! whereas 0 is least saturated (white) and 254 is most saturated. + //! \param sat uint8_t that specifies the saturation + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setColorSaturation(uint8_t sat, uint8_t transition = 4) { - return colorHueStrategy->setColorSaturation(sat, transition, *this); - } - return false; - }; - - //! \brief Function to set the color of this light with specified hue and - //! saturation. - //! - //! \note The color will only be set if the light has a reference to a - //! specific \ref ColorHueStrategy. - //! \param hue uint16_t that specifies the hue - //! \param sat uint8_t that specifies the saturation - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms. - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4) - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->setColorSaturation(sat, transition, *this); + } + return false; + }; + + //! \brief Function to set the color of this light with specified hue and + //! saturation. + //! + //! \note The color will only be set if the light has a reference to a + //! specific \ref ColorHueStrategy. + //! \param hue uint16_t that specifies the hue + //! \param sat uint8_t that specifies the saturation + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms. + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4) { - return colorHueStrategy->setColorHueSaturation(hue, sat, transition, *this); - } - return false; - }; - - //! \brief Const function that returns the current color of the light as hue - //! and saturation - //! - //! \note The color hue and saturation will only be returned when the light - //! has a reference to a specific \ref ColorHueStrategy. - //! \note This will not refresh the light state - //! \return Pair containing the hue as first value and saturation as second value or an empty one when failed - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual std::pair getColorHueSaturation() const - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->setColorHueSaturation(hue, sat, transition, *this); + } + return false; + }; + + //! \brief Const function that returns the current color of the light as hue + //! and saturation + //! + //! \note The color hue and saturation will only be returned when the light + //! has a reference to a specific \ref ColorHueStrategy. + //! \note This will not refresh the light state + //! \return Pair containing the hue as first value and saturation as second value or an empty one when failed + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual std::pair getColorHueSaturation() const { - return colorHueStrategy->getColorHueSaturation(*this); - } - return {}; - }; - - //! \brief Function that returns the current color of the light as hue and - //! saturation - //! - //! \note The color hue and saturation will only be returned when the light - //! has a reference to a specific \ref ColorHueStrategy. Updates the lights - //! state by calling refreshState() - //! \return Pair containing the hue as first value and saturation as second value or an empty one when failed - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual std::pair getColorHueSaturation() - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->getColorHueSaturation(*this); + } + return {}; + }; + + //! \brief Function that returns the current color of the light as hue and + //! saturation + //! + //! \note The color hue and saturation will only be returned when the light + //! has a reference to a specific \ref ColorHueStrategy. Updates the lights + //! state by calling refreshState() + //! \return Pair containing the hue as first value and saturation as second value or an empty one when failed + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual std::pair getColorHueSaturation() { - return colorHueStrategy->getColorHueSaturation(*this); - } - return {}; - }; - - //! \brief Function to set the color of this light in CIE with specified x y. - //! - //! \note The color will only be set if the light has a reference to a - //! specific \ref ColorHueStrategy. The values of x and y are ranging from 0 to 1. - //! \param x float that specifies the x coordinate in CIE - //! \param y float that specifies the y coordinate in CIE - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setColorXY(float x, float y, uint8_t transition = 4) - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->getColorHueSaturation(*this); + } + return {}; + }; + + //! \brief Function to set the color of this light in CIE with specified x y. + //! + //! \note The color will only be set if the light has a reference to a + //! specific \ref ColorHueStrategy. The values of x and y are ranging from 0 to 1. + //! \param x float that specifies the x coordinate in CIE + //! \param y float that specifies the y coordinate in CIE + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setColorXY(float x, float y, uint8_t transition = 4) { - return colorHueStrategy->setColorXY(x, y, transition, *this); - } - return false; - }; - - //! \brief Const function that returns the current color of the light as xy - //! - //! \note The color x and y will only be returned when the light has a - //! reference to a specific \ref ColorHueStrategy. - //! \note This does not update the lights state - //! \return Pair containing the x as first value and y as second value or an - //! empty one when failed - virtual std::pair getColorXY() const - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->setColorXY(x, y, transition, *this); + } + return false; + }; + + //! \brief Const function that returns the current color of the light as xy + //! + //! \note The color x and y will only be returned when the light has a + //! reference to a specific \ref ColorHueStrategy. + //! \note This does not update the lights state + //! \return Pair containing the x as first value and y as second value or an + //! empty one when failed + virtual std::pair getColorXY() const { - return colorHueStrategy->getColorXY(*this); - } - return {}; - }; - - //! \brief Function that returns the current color of the light as xy - //! - //! \note The color x and y will only be returned when the light has a - //! reference to a specific \ref ColorHueStrategy. - //! Updates the lights state by calling refreshState() - //! \return Pair containing the x as first value and y as second value or an empty one when failed - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual std::pair getColorXY() - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->getColorXY(*this); + } + return {}; + }; + + //! \brief Function that returns the current color of the light as xy + //! + //! \note The color x and y will only be returned when the light has a + //! reference to a specific \ref ColorHueStrategy. + //! Updates the lights state by calling refreshState() + //! \return Pair containing the x as first value and y as second value or an empty one when failed + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual std::pair getColorXY() { - return colorHueStrategy->getColorXY(*this); - } - return {}; - }; - - //! \brief Function to set the color of this light with red green and blue - //! values. - //! - //! \note The color will only be set if the light has a reference to a - //! specific \ref ColorHueStrategy. The values of red, green and blue are - //! ranging from 0 to 255. - //! \param r uint8_t that specifies the red color value - //! \param g uint8_t that specifies the green color value - //! \param b uint8_t that specifies the blue color value - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition = 4) - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->getColorXY(*this); + } + return {}; + }; + + //! \brief Function to set the color of this light with red green and blue + //! values. + //! + //! \note The color will only be set if the light has a reference to a + //! specific \ref ColorHueStrategy. The values of red, green and blue are + //! ranging from 0 to 255. + //! \param r uint8_t that specifies the red color value + //! \param g uint8_t that specifies the green color value + //! \param b uint8_t that specifies the blue color value + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition = 4) { - return colorHueStrategy->setColorRGB(r, g, b, transition, *this); - } - return false; - }; - - //! \brief Function that lets the light perform one breath cycle. - //! - //! Can be used for locating a light. - //! \return bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool alert(); - - //! \brief Function that lets the light perform one breath cycle in specified - //! color temperature. - //! - //! \note The breath cylce will only be performed if the light has a reference - //! to a specific \ref ColorTemperatureStrategy. - //! \param mired Color temperature in mired - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool alertTemperature(unsigned int mired) - { - if (colorTemperatureStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->setColorRGB(r, g, b, transition, *this); + } + return false; + }; + + //! \brief Function that lets the light perform one breath cycle. + //! + //! Can be used for locating a light. + //! \return bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool alert(); + + //! \brief Function that lets the light perform one breath cycle in specified + //! color temperature. + //! + //! \note The breath cylce will only be performed if the light has a reference + //! to a specific \ref ColorTemperatureStrategy. + //! \param mired Color temperature in mired + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool alertTemperature(unsigned int mired) { - return colorTemperatureStrategy->alertTemperature(mired, *this); - } - return false; - }; - - //! \brief Function that lets the light perform one breath cycle in specified - //! color. - //! - //! \note The breath cylce will only be performed if the light has a reference - //! to a specific \ref ColorHueStrategy. - //! \param hue uint16_t that specifies the hue - //! \param sat uint8_t that specifies the saturation - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool alertHueSaturation(uint16_t hue, uint8_t sat) - { - if (colorHueStrategy) + if (colorTemperatureStrategy) + { + return colorTemperatureStrategy->alertTemperature(mired, *this); + } + return false; + }; + + //! \brief Function that lets the light perform one breath cycle in specified + //! color. + //! + //! \note The breath cylce will only be performed if the light has a reference + //! to a specific \ref ColorHueStrategy. + //! \param hue uint16_t that specifies the hue + //! \param sat uint8_t that specifies the saturation + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool alertHueSaturation(uint16_t hue, uint8_t sat) { - return colorHueStrategy->alertHueSaturation(hue, sat, *this); - } - return false; - }; - - //! \brief Function that lets the light perform one breath cycle in specified - //! color. - //! - //! \note The breath cylce will only be performed if the light has a reference - //! to a specific \ref ColorHueStrategy. The values of x and y are ranging - //! from 0 to 1. - //! \param x float that specifies the x coordinate in CIE - //! \param y float that specifies the y coordinate in CIE - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool alertXY(float x, float y) - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->alertHueSaturation(hue, sat, *this); + } + return false; + }; + + //! \brief Function that lets the light perform one breath cycle in specified + //! color. + //! + //! \note The breath cylce will only be performed if the light has a reference + //! to a specific \ref ColorHueStrategy. The values of x and y are ranging + //! from 0 to 1. + //! \param x float that specifies the x coordinate in CIE + //! \param y float that specifies the y coordinate in CIE + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool alertXY(float x, float y) { - return colorHueStrategy->alertXY(x, y, *this); - } - return false; - }; - - //! \brief Function that lets the light perform one breath cycle in specified - //! color. - //! - //! \note The breath cylce will only be performed if the light has a reference - //! to a specific \ref ColorHueStrategy. The values of red, green and blue are - //! ranging from 0 to 255. - //! \param r uint8_t that specifies the red color value - //! \param g uint8_t that specifies the green color value - //! \param b uint8_t that specifies the blue color value - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool alertRGB(uint8_t r, uint8_t g, uint8_t b) - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->alertXY(x, y, *this); + } + return false; + }; + + //! \brief Function that lets the light perform one breath cycle in specified + //! color. + //! + //! \note The breath cylce will only be performed if the light has a reference + //! to a specific \ref ColorHueStrategy. The values of red, green and blue are + //! ranging from 0 to 255. + //! \param r uint8_t that specifies the red color value + //! \param g uint8_t that specifies the green color value + //! \param b uint8_t that specifies the blue color value + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool alertRGB(uint8_t r, uint8_t g, uint8_t b) { - return colorHueStrategy->alertRGB(r, g, b, *this); - } - return false; - }; - - //! \brief Function to turn colorloop effect on/off. - //! - //! Notice this function will only be performed light has a reference to a - //! specific \ref ColorHueStrategy. The colorloop effect will loop through all - //! colors on current hue and saturation levels. Notice that none of the - //! setter functions check whether this feature is enabled and the colorloop - //! can only be disabled with this function or by simply calling - //! Off()/OffNoRefresh() and then On()/OnNoRefresh(), so you could - //! alternatively call Off() and then use any of the setter functions. - //! \param on bool that enables this feature when true and disables it when false - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool setColorLoop(bool on) - { - if (colorHueStrategy) + if (colorHueStrategy) + { + return colorHueStrategy->alertRGB(r, g, b, *this); + } + return false; + }; + + //! \brief Function to turn colorloop effect on/off. + //! + //! Notice this function will only be performed light has a reference to a + //! specific \ref ColorHueStrategy. The colorloop effect will loop through all + //! colors on current hue and saturation levels. Notice that none of the + //! setter functions check whether this feature is enabled and the colorloop + //! can only be disabled with this function or by simply calling + //! Off()/OffNoRefresh() and then On()/OnNoRefresh(), so you could + //! alternatively call Off() and then use any of the setter functions. + //! \param on bool that enables this feature when true and disables it when false + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool setColorLoop(bool on) { - return colorHueStrategy->setColorLoop(on, *this); - } - return false; - }; - -protected: - //! \brief Protected ctor that is used by \ref Hue class. - //! - //! \param id Integer that specifies the id of this light - //! \param commands HueCommandAPI for communication with the bridge - //! - //! leaves strategies unset - HueLight(int id, const HueCommandAPI& commands); - - //! \brief Protected ctor that is used by \ref Hue class, also sets - //! strategies. - //! - //! \param id Integer that specifies the id of this light - //! \param commands HueCommandAPI for communication with the bridge - //! \param brightnessStrategy Strategy for brightness. May be nullptr. - //! \param colorTempStrategy Strategy for color temperature. May be nullptr. - //! \param colorHueStrategy Strategy for color hue/saturation. May be nullptr. - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - HueLight(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, - std::shared_ptr colorTempStrategy, - std::shared_ptr colorHueStrategy); - - //! \brief Protected function that sets the brightness strategy. - //! - //! The strategy defines how specific commands that deal with brightness - //! control are executed \param strat a strategy of type \ref - //! BrightnessStrategy - virtual void setBrightnessStrategy(std::shared_ptr strat) - { - brightnessStrategy = std::move(strat); - }; - - //! \brief Protected function that sets the colorTemperature strategy. - //! - //! The strategy defines how specific commands that deal with colortemperature - //! control are executed \param strat a strategy of type \ref - //! ColorTemperatureStrategy - virtual void setColorTemperatureStrategy(std::shared_ptr strat) - { - colorTemperatureStrategy = std::move(strat); - }; - - //! \brief Protected function that sets the colorHue strategy. - //! - //! The strategy defines how specific commands that deal with color control - //! are executed \param strat a strategy of type \ref ColorHueStrategy - virtual void setColorHueStrategy(std::shared_ptr strat) - { - colorHueStrategy = std::move(strat); + if (colorHueStrategy) + { + return colorHueStrategy->setColorLoop(on, *this); + } + return false; + }; + + protected: + //! \brief Protected ctor that is used by \ref Hue class. + //! + //! \param id Integer that specifies the id of this light + //! \param commands HueCommandAPI for communication with the bridge + //! + //! leaves strategies unset + HueLight(int id, const HueCommandAPI& commands); + + //! \brief Protected ctor that is used by \ref Hue class, also sets + //! strategies. + //! + //! \param id Integer that specifies the id of this light + //! \param commands HueCommandAPI for communication with the bridge + //! \param brightnessStrategy Strategy for brightness. May be nullptr. + //! \param colorTempStrategy Strategy for color temperature. May be nullptr. + //! \param colorHueStrategy Strategy for color hue/saturation. May be nullptr. + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + HueLight(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, + std::shared_ptr colorTempStrategy, + std::shared_ptr colorHueStrategy); + + //! \brief Protected function that sets the brightness strategy. + //! + //! The strategy defines how specific commands that deal with brightness + //! control are executed \param strat a strategy of type \ref + //! BrightnessStrategy + virtual void setBrightnessStrategy(std::shared_ptr strat) + { + brightnessStrategy = std::move(strat); + }; + + //! \brief Protected function that sets the colorTemperature strategy. + //! + //! The strategy defines how specific commands that deal with colortemperature + //! control are executed \param strat a strategy of type \ref + //! ColorTemperatureStrategy + virtual void setColorTemperatureStrategy(std::shared_ptr strat) + { + colorTemperatureStrategy = std::move(strat); + }; + + //! \brief Protected function that sets the colorHue strategy. + //! + //! The strategy defines how specific commands that deal with color control + //! are executed \param strat a strategy of type \ref ColorHueStrategy + virtual void setColorHueStrategy(std::shared_ptr strat) + { + colorHueStrategy = std::move(strat); + }; + + //! \brief Protected function that sets the HueCommandAPI. + //! + //! The HueCommandAPI is used for bridge communication + //! \param commandAPI the new HueCommandAPI + virtual void setCommandAPI(const HueCommandAPI& commandAPI) { commands = commandAPI; }; + + //! \brief Function that turns the light on without refreshing its state. + //! + //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool OnNoRefresh(uint8_t transition = 4); + + //! \brief Function that turns the light off without refreshing its state. + //! + //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual bool OffNoRefresh(uint8_t transition = 4); + + //! \brief Utility function to send a put request to the light. + //! + //! \throws nlohmann::json::parse_error if the reply could not be parsed + //! \param request A nlohmann::json aka the request to send + //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") + //! \param fileInfo FileInfo from calling function for exception details. + //! \return The parsed reply + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual nlohmann::json SendPutRequest( + const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); + + //! \brief Virtual function that refreshes the \ref state of the light. + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws HueAPIResponseException when response contains an error + //! \throws nlohmann::json::parse_error when response could not be parsed + virtual void refreshState(); + + protected: + int id; //!< holds the id of the light + nlohmann::json state; //!< holds the current state of the light updated by \ref refreshState + ColorType colorType; //!< holds the \ref ColorType of the light + + 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 + HueCommandAPI commands; //!< A IHttpHandler that is used to communicate with the bridge }; - - //! \brief Protected function that sets the HueCommandAPI. - //! - //! The HueCommandAPI is used for bridge communication - //! \param commandAPI the new HueCommandAPI - virtual void setCommandAPI(const HueCommandAPI& commandAPI) { commands = commandAPI; }; - - //! \brief Function that turns the light on without refreshing its state. - //! - //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool OnNoRefresh(uint8_t transition = 4); - - //! \brief Function that turns the light off without refreshing its state. - //! - //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual bool OffNoRefresh(uint8_t transition = 4); - - //! \brief Utility function to send a put request to the light. - //! - //! \throws nlohmann::json::parse_error if the reply could not be parsed - //! \param request A nlohmann::json aka the request to send - //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") - //! \param fileInfo FileInfo from calling function for exception details. - //! \return The parsed reply - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual nlohmann::json SendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); - - //! \brief Virtual function that refreshes the \ref state of the light. - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws HueAPIResponseException when response contains an error - //! \throws nlohmann::json::parse_error when response could not be parsed - virtual void refreshState(); - -protected: - int id; //!< holds the id of the light - nlohmann::json state; //!< holds the current state of the light updated by \ref refreshState - ColorType colorType; //!< holds the \ref ColorType of the light - - 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 - HueCommandAPI commands; //!< A IHttpHandler that is used to communicate with the bridge -}; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/IHttpHandler.h b/include/hueplusplus/IHttpHandler.h index edcb6be..67156a1 100644 --- a/include/hueplusplus/IHttpHandler.h +++ b/include/hueplusplus/IHttpHandler.h @@ -30,167 +30,170 @@ #include "json/json.hpp" -//! Abstract class for classes that handle http requests and multicast requests -class IHttpHandler +namespace hueplusplus { -public: - //! \brief Virtual dtor - virtual ~IHttpHandler() = default; - - //! \brief Send a message to a specified host and return the response. - //! - //! \param msg The message that should be sent to the specified address - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return The response of the host as a string - //! \throws std::system_error when system or socket operations fail - virtual std::string send(const std::string& msg, const std::string& adr, int port = 80) const = 0; - - //! \brief Send a message to a specified host and return the body of the response. - //! - //! \param msg The message that should sent to the specified address - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return The body of the response of the host as a string - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - virtual std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const = 0; - - //! \brief Send a multicast request with a specified message. - //! - //! \param msg The message that should sent to the specified multicast address - //! \param adr Optional ip or hostname in dotted decimal notation, default is "239.255.255.250" - //! \param port Optional port the request is sent to, default is 1900 - //! \param timeout Optional time to wait for responses in seconds, default is 5 - //! - //! Blocks for the duration of the timeout. - //! - //! \return vector of strings containing each received answer - //! \throws std::system_error when system or socket operations fail - virtual std::vector sendMulticast( - const std::string& msg, const std::string& adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0; - - //! \brief Send a HTTP request with the given method to the specified host and return the body of the response. - //! - //! \param method HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - virtual std::string sendHTTPRequest(const std::string& method, const std::string& uri, - const std::string& contentType, const std::string& body, const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP GET request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - virtual std::string GETString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP POST request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - virtual std::string POSTString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP PUT request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - virtual std::string PUTString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP DELETE request to the specified host and return the body of the response. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! that specifies the port to which the request is sent to. Default is 80 - //! \return Body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - virtual std::string DELETEString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP GET request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - virtual nlohmann::json GETJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP POST request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - virtual nlohmann::json POSTJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP PUT request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - virtual nlohmann::json PUTJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; - - //! \brief Send a HTTP DELETE request to the specified host and return the body of the response parsed as JSON. - //! - //! \param uri Uniform Resource Identifier in the request - //! \param body Request body, may be empty - //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" - //! \param port Optional port the request is sent to, default is 80 - //! \return Parsed body of the response of the host - //! \throws std::system_error when system or socket operations fail - //! \throws HueException when response contained no body - //! \throws nlohmann::json::parse_error when the body could not be parsed - virtual nlohmann::json DELETEJson( - const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; -}; + //! Abstract class for classes that handle http requests and multicast requests + class IHttpHandler + { + public: + //! \brief Virtual dtor + virtual ~IHttpHandler() = default; + + //! \brief Send a message to a specified host and return the response. + //! + //! \param msg The message that should be sent to the specified address + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return The response of the host as a string + //! \throws std::system_error when system or socket operations fail + virtual std::string send(const std::string& msg, const std::string& adr, int port = 80) const = 0; + + //! \brief Send a message to a specified host and return the body of the response. + //! + //! \param msg The message that should sent to the specified address + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return The body of the response of the host as a string + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string sendGetHTTPBody(const std::string& msg, const std::string& adr, int port = 80) const = 0; + + //! \brief Send a multicast request with a specified message. + //! + //! \param msg The message that should sent to the specified multicast address + //! \param adr Optional ip or hostname in dotted decimal notation, default is "239.255.255.250" + //! \param port Optional port the request is sent to, default is 1900 + //! \param timeout Optional time to wait for responses in seconds, default is 5 + //! + //! Blocks for the duration of the timeout. + //! + //! \return vector of strings containing each received answer + //! \throws std::system_error when system or socket operations fail + virtual std::vector sendMulticast(const std::string& msg, + const std::string& adr = "239.255.255.250", int port = 1900, int timeout = 5) const = 0; + + //! \brief Send a HTTP request with the given method to the specified host and return the body of the response. + //! + //! \param method HTTP method type e.g. GET, HEAD, POST, PUT, DELETE, ... + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string sendHTTPRequest(const std::string& method, const std::string& uri, + const std::string& contentType, const std::string& body, const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP GET request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string GETString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP POST request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string POSTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP PUT request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string PUTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param contentType MIME type of the body data e.g. "text/html", "application/json", ... + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! that specifies the port to which the request is sent to. Default is 80 + //! \return Body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + virtual std::string DELETEString(const std::string& uri, const std::string& contentType, + const std::string& body, const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP GET request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + virtual nlohmann::json GETJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP POST request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + virtual nlohmann::json POSTJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP PUT request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + virtual nlohmann::json PUTJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; + + //! \brief Send a HTTP DELETE request to the specified host and return the body of the response parsed as JSON. + //! + //! \param uri Uniform Resource Identifier in the request + //! \param body Request body, may be empty + //! \param adr Ip or hostname in dotted decimal notation like "192.168.2.1" + //! \param port Optional port the request is sent to, default is 80 + //! \return Parsed body of the response of the host + //! \throws std::system_error when system or socket operations fail + //! \throws HueException when response contained no body + //! \throws nlohmann::json::parse_error when the body could not be parsed + virtual nlohmann::json DELETEJson( + const std::string& uri, const nlohmann::json& body, const std::string& adr, int port = 80) const = 0; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/LinHttpHandler.h b/include/hueplusplus/LinHttpHandler.h index 5717d4c..ed56c00 100644 --- a/include/hueplusplus/LinHttpHandler.h +++ b/include/hueplusplus/LinHttpHandler.h @@ -30,31 +30,34 @@ #include "json/json.hpp" -//! Class to handle http requests and multicast requests on linux systems -class LinHttpHandler : public BaseHttpHandler +namespace hueplusplus { -public: - //! \brief Function that sends a given message to the specified host and - //! returns the response. - //! - //! \param msg String that contains the message that is sent to the specified - //! address \param adr String that contains an ip or hostname in dotted - //! decimal notation like "192.168.2.1" \param port Optional integer that - //! specifies the port to which the request is sent to. Default is 80 \return - //! String containing the response of the host - virtual std::string send(const std::string& msg, const std::string& adr, int port = 80) const; - - //! \brief Function that sends a multicast request with the specified message. - //! - //! \param msg String that contains the request that is sent to the specified - //! address \param adr Optional String that contains an ip or hostname in - //! dotted decimal notation, default is "239.255.255.250" \param port Optional - //! integer that specifies the port to which the request is sent. Default is - //! 1900 \param timeout Optional Integer that specifies the timeout of the - //! request in seconds. Default is 5 \return Vector containing strings of each - //! answer received - virtual std::vector sendMulticast( - const std::string& msg, const std::string& adr = "239.255.255.250", int port = 1900, int timeout = 5) const; -}; + //! Class to handle http requests and multicast requests on linux systems + class LinHttpHandler : public BaseHttpHandler + { + public: + //! \brief Function that sends a given message to the specified host and + //! returns the response. + //! + //! \param msg String that contains the message that is sent to the specified + //! address \param adr String that contains an ip or hostname in dotted + //! decimal notation like "192.168.2.1" \param port Optional integer that + //! specifies the port to which the request is sent to. Default is 80 \return + //! String containing the response of the host + virtual std::string send(const std::string& msg, const std::string& adr, int port = 80) const; + + //! \brief Function that sends a multicast request with the specified message. + //! + //! \param msg String that contains the request that is sent to the specified + //! address \param adr Optional String that contains an ip or hostname in + //! dotted decimal notation, default is "239.255.255.250" \param port Optional + //! integer that specifies the port to which the request is sent. Default is + //! 1900 \param timeout Optional Integer that specifies the timeout of the + //! request in seconds. Default is 5 \return Vector containing strings of each + //! answer received + virtual std::vector sendMulticast( + const std::string& msg, const std::string& adr = "239.255.255.250", int port = 1900, int timeout = 5) const; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/SimpleBrightnessStrategy.h b/include/hueplusplus/SimpleBrightnessStrategy.h index 59df71a..7c2cdce 100644 --- a/include/hueplusplus/SimpleBrightnessStrategy.h +++ b/include/hueplusplus/SimpleBrightnessStrategy.h @@ -26,30 +26,33 @@ #include "BrightnessStrategy.h" #include "HueLight.h" -//! Class implementing the functions of BrightnessStrategy -class SimpleBrightnessStrategy : public BrightnessStrategy +namespace hueplusplus { -public: - //! \brief Function for changing a lights brightness with a specified - //! transition. - //! - //! \param bri The brightness raning from 0 = off to 255 = fully lit - //! \param transition The time it takes to fade to the new brightness in - //! multiples of 100ms, 4 = 400ms and should be seen as the default \param - //! light A reference of the light - bool setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const override; - //! \brief Function that returns the current brightness of the light - //! - //! Updates the lights state by calling refreshState() - //! \param light A reference of the light - //! \return Unsigned int representing the brightness - unsigned int getBrightness(HueLight& light) const override; - //! \brief Function that returns the current brightness of the light - //! - //! \note This does not update the lights state - //! \param light A const reference of the light - //! \return Unsigned int representing the brightness - unsigned int getBrightness(const HueLight& light) const override; -}; + //! Class implementing the functions of BrightnessStrategy + class SimpleBrightnessStrategy : public BrightnessStrategy + { + public: + //! \brief Function for changing a lights brightness with a specified + //! transition. + //! + //! \param bri The brightness raning from 0 = off to 255 = fully lit + //! \param transition The time it takes to fade to the new brightness in + //! multiples of 100ms, 4 = 400ms and should be seen as the default \param + //! light A reference of the light + bool setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const override; + //! \brief Function that returns the current brightness of the light + //! + //! Updates the lights state by calling refreshState() + //! \param light A reference of the light + //! \return Unsigned int representing the brightness + unsigned int getBrightness(HueLight& light) const override; + //! \brief Function that returns the current brightness of the light + //! + //! \note This does not update the lights state + //! \param light A const reference of the light + //! \return Unsigned int representing the brightness + unsigned int getBrightness(const HueLight& light) const override; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/SimpleColorHueStrategy.h b/include/hueplusplus/SimpleColorHueStrategy.h index c963ef2..ae7c378 100644 --- a/include/hueplusplus/SimpleColorHueStrategy.h +++ b/include/hueplusplus/SimpleColorHueStrategy.h @@ -26,127 +26,130 @@ #include "ColorHueStrategy.h" #include "HueLight.h" -//! Class implementing the functions of ColorHueStrategy -class SimpleColorHueStrategy : public ColorHueStrategy +namespace hueplusplus { -public: - //! \brief Function for changing a lights color in hue with a specified - //! transition. - //! - //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is - //! green and 46920 is blue. \param hue The hue of the color \param transition - //! The time it takes to fade to the new color in multiples of 100ms, 4 = - //! 400ms and should be seen as the default \param light A reference of the - //! light - bool setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const override; - //! \brief Function for changing a lights color in saturation with a specified - //! transition. - //! - //! The saturation ranges from 0 to 254, whereas 0 is least saturated (white) - //! and 254 is most saturated (vibrant). \param sat The saturation of the - //! color \param transition The time it takes to fade to the new color in - //! multiples of 100ms, 4 = 400ms and should be seen as the default \param - //! light A reference of the light - bool setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const override; - //! \brief Function for changing a lights color in hue and saturation format - //! with a specified transition. - //! - //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is - //! green and 46920 is blue. The saturation ranges from 0 to 254, whereas 0 is - //! least saturated (white) and 254 is most saturated (vibrant). \param hue - //! The hue of the color \param sat The saturation of the color \param - //! transition The time it takes to fade to the new color in multiples of - //! 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const override; - //! \brief Function for changing a lights color in CIE format with a specified - //! transition. - //! - //! \param x The x coordinate in CIE, ranging from 0 to 1 - //! \param y The y coordinate in CIE, ranging from 0 to 1 - //! \param transition The time it takes to fade to the new color in multiples - //! of 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - bool setColorXY(float x, float y, uint8_t transition, HueLight& light) const override; - //! \brief Function for changing a lights color in rgb format with a specified - //! transition. - //! - //! Red, green and blue are ranging from 0 to 255. - //! \param r The red portion of the color - //! \param g The green portion of the color - //! \param b The blue portion of the color - //! \param transition The time it takes to fade to the new color in multiples - //! of 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const override; - //! \brief Function for turning on/off the color loop feature of a light. - //! - //! Can be theoretically set for any light, but it only works for lights that - //! support this feature. When this feature is activated the light will fade - //! through every color on the current hue and saturation settings. Notice - //! that none of the setter functions check whether this feature is enabled - //! and the colorloop can only be disabled with this function or by simply - //! calling Off()/OffNoRefresh() and then On()/OnNoRefresh(), so you could - //! alternatively call Off() and then use any of the setter functions. - //! \param on Boolean to turn this feature on or off, true/1 for on and - //! false/0 for off \param light A reference of the light - bool setColorLoop(bool on, HueLight& light) const override; - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! \note It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs The hue ranges from 0 to 65535, whereas 65535 and - //! 0 are red, 25500 is green and 46920 is blue. The saturation ranges from 0 - //! to 254, whereas 0 is least saturated (white) and 254 is most saturated - //! (vibrant). \param hue The hue of the color \param sat The saturation of - //! the color \param light A reference of the light - bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const override; - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! \note It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs \param x The x coordinate in CIE, ranging from 0 - //! to 1 \param y The y coordinate in CIE, ranging from 0 to 1 \param light A - //! reference of the light - bool alertXY(float x, float y, HueLight& light) const override; - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! \note It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs Red, green and blue are ranging from 0 to 255. - //! \param r The red portion of the color - //! \param g The green portion of the color - //! \param b The blue portion of the color - //! \param light A reference of the light - bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const override; - //! \brief Function that returns the current color of the light as hue and - //! saturation - //! - //! Updates the lights state by calling refreshState() - //! \param light A reference of the light - //! \return Pair containing the hue as first value and saturation as second - //! value - std::pair getColorHueSaturation(HueLight& light) const override; - //! \brief Function that returns the current color of the light as hue and - //! saturation - //! - //! \note This does not update the lights state - //! \param light A const reference of the light - //! \return Pair containing the hue as first value and saturation as second - //! value - std::pair getColorHueSaturation(const HueLight& light) const override; - //! \brief Function that returns the current color of the light as xy - //! - //! Updates the lights state by calling refreshState() - //! \param light A reference of the light - //! \return Pair containing the x as first value and y as second value - std::pair getColorXY(HueLight& light) const override; - //! \brief Function that returns the current color of the light as xy - //! - //! \note This does not update the lights state - //! \param light A const reference of the light - //! \return Pair containing the x as first value and y as second value - std::pair getColorXY(const HueLight& light) const override; -}; + //! Class implementing the functions of ColorHueStrategy + class SimpleColorHueStrategy : public ColorHueStrategy + { + public: + //! \brief Function for changing a lights color in hue with a specified + //! transition. + //! + //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is + //! green and 46920 is blue. \param hue The hue of the color \param transition + //! The time it takes to fade to the new color in multiples of 100ms, 4 = + //! 400ms and should be seen as the default \param light A reference of the + //! light + bool setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const override; + //! \brief Function for changing a lights color in saturation with a specified + //! transition. + //! + //! The saturation ranges from 0 to 254, whereas 0 is least saturated (white) + //! and 254 is most saturated (vibrant). \param sat The saturation of the + //! color \param transition The time it takes to fade to the new color in + //! multiples of 100ms, 4 = 400ms and should be seen as the default \param + //! light A reference of the light + bool setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const override; + //! \brief Function for changing a lights color in hue and saturation format + //! with a specified transition. + //! + //! The hue ranges from 0 to 65535, whereas 65535 and 0 are red, 25500 is + //! green and 46920 is blue. The saturation ranges from 0 to 254, whereas 0 is + //! least saturated (white) and 254 is most saturated (vibrant). \param hue + //! The hue of the color \param sat The saturation of the color \param + //! transition The time it takes to fade to the new color in multiples of + //! 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const override; + //! \brief Function for changing a lights color in CIE format with a specified + //! transition. + //! + //! \param x The x coordinate in CIE, ranging from 0 to 1 + //! \param y The y coordinate in CIE, ranging from 0 to 1 + //! \param transition The time it takes to fade to the new color in multiples + //! of 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + bool setColorXY(float x, float y, uint8_t transition, HueLight& light) const override; + //! \brief Function for changing a lights color in rgb format with a specified + //! transition. + //! + //! Red, green and blue are ranging from 0 to 255. + //! \param r The red portion of the color + //! \param g The green portion of the color + //! \param b The blue portion of the color + //! \param transition The time it takes to fade to the new color in multiples + //! of 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const override; + //! \brief Function for turning on/off the color loop feature of a light. + //! + //! Can be theoretically set for any light, but it only works for lights that + //! support this feature. When this feature is activated the light will fade + //! through every color on the current hue and saturation settings. Notice + //! that none of the setter functions check whether this feature is enabled + //! and the colorloop can only be disabled with this function or by simply + //! calling Off()/OffNoRefresh() and then On()/OnNoRefresh(), so you could + //! alternatively call Off() and then use any of the setter functions. + //! \param on Boolean to turn this feature on or off, true/1 for on and + //! false/0 for off \param light A reference of the light + bool setColorLoop(bool on, HueLight& light) const override; + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! \note It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs The hue ranges from 0 to 65535, whereas 65535 and + //! 0 are red, 25500 is green and 46920 is blue. The saturation ranges from 0 + //! to 254, whereas 0 is least saturated (white) and 254 is most saturated + //! (vibrant). \param hue The hue of the color \param sat The saturation of + //! the color \param light A reference of the light + bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const override; + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! \note It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs \param x The x coordinate in CIE, ranging from 0 + //! to 1 \param y The y coordinate in CIE, ranging from 0 to 1 \param light A + //! reference of the light + bool alertXY(float x, float y, HueLight& light) const override; + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! \note It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs Red, green and blue are ranging from 0 to 255. + //! \param r The red portion of the color + //! \param g The green portion of the color + //! \param b The blue portion of the color + //! \param light A reference of the light + bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const override; + //! \brief Function that returns the current color of the light as hue and + //! saturation + //! + //! Updates the lights state by calling refreshState() + //! \param light A reference of the light + //! \return Pair containing the hue as first value and saturation as second + //! value + std::pair getColorHueSaturation(HueLight& light) const override; + //! \brief Function that returns the current color of the light as hue and + //! saturation + //! + //! \note This does not update the lights state + //! \param light A const reference of the light + //! \return Pair containing the hue as first value and saturation as second + //! value + std::pair getColorHueSaturation(const HueLight& light) const override; + //! \brief Function that returns the current color of the light as xy + //! + //! Updates the lights state by calling refreshState() + //! \param light A reference of the light + //! \return Pair containing the x as first value and y as second value + std::pair getColorXY(HueLight& light) const override; + //! \brief Function that returns the current color of the light as xy + //! + //! \note This does not update the lights state + //! \param light A const reference of the light + //! \return Pair containing the x as first value and y as second value + std::pair getColorXY(const HueLight& light) const override; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/SimpleColorTemperatureStrategy.h b/include/hueplusplus/SimpleColorTemperatureStrategy.h index 484c081..40b40e1 100644 --- a/include/hueplusplus/SimpleColorTemperatureStrategy.h +++ b/include/hueplusplus/SimpleColorTemperatureStrategy.h @@ -26,41 +26,44 @@ #include "ColorTemperatureStrategy.h" #include "HueLight.h" -//! Class implementing the functions of ColorTemperatureStrategy -class SimpleColorTemperatureStrategy : public ColorTemperatureStrategy +namespace hueplusplus { -public: - //! \brief Function for changing a lights color temperature in mired with a - //! specified transition. - //! - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \param mired The color temperature in mired \param - //! transition The time it takes to fade to the new color in multiples of - //! 100ms, 4 = 400ms and should be seen as the default \param light A - //! reference of the light - bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const override; - //! \brief Function that lets the light perform one breath cycle in the - //! specified color. - //! - //! It uses this_thread::sleep_for to accomodate for the time an \ref - //! HueLight::alert() needs The color temperature in mired ranges from 153 to - //! 500 whereas 153 is cold and 500 is warm. \param mired The color - //! temperature in mired \param light A reference of the light - bool alertTemperature(unsigned int mired, HueLight& light) const override; - //! \brief Function that returns the current color temperature of the light - //! - //! Updates the lights state by calling refreshState() - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \param light A reference of the light \return Unsigned - //! int representing the color temperature in mired - unsigned int getColorTemperature(HueLight& light) const override; - //! \brief Function that returns the current color temperature of the light - //! - //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold - //! and 500 is warm. \note This does not update the lights state \param light - //! A const reference of the light \return Unsigned int representing the color - //! temperature in mired - unsigned int getColorTemperature(const HueLight& light) const override; -}; + //! Class implementing the functions of ColorTemperatureStrategy + class SimpleColorTemperatureStrategy : public ColorTemperatureStrategy + { + public: + //! \brief Function for changing a lights color temperature in mired with a + //! specified transition. + //! + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \param mired The color temperature in mired \param + //! transition The time it takes to fade to the new color in multiples of + //! 100ms, 4 = 400ms and should be seen as the default \param light A + //! reference of the light + bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const override; + //! \brief Function that lets the light perform one breath cycle in the + //! specified color. + //! + //! It uses this_thread::sleep_for to accomodate for the time an \ref + //! HueLight::alert() needs The color temperature in mired ranges from 153 to + //! 500 whereas 153 is cold and 500 is warm. \param mired The color + //! temperature in mired \param light A reference of the light + bool alertTemperature(unsigned int mired, HueLight& light) const override; + //! \brief Function that returns the current color temperature of the light + //! + //! Updates the lights state by calling refreshState() + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \param light A reference of the light \return Unsigned + //! int representing the color temperature in mired + unsigned int getColorTemperature(HueLight& light) const override; + //! \brief Function that returns the current color temperature of the light + //! + //! The color temperature in mired ranges from 153 to 500 whereas 153 is cold + //! and 500 is warm. \note This does not update the lights state \param light + //! A const reference of the light \return Unsigned int representing the color + //! temperature in mired + unsigned int getColorTemperature(const HueLight& light) const override; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/UPnP.h b/include/hueplusplus/UPnP.h index 01e75a9..9754f7e 100644 --- a/include/hueplusplus/UPnP.h +++ b/include/hueplusplus/UPnP.h @@ -29,18 +29,21 @@ #include "IHttpHandler.h" -//! Class that looks for UPnP devices using an m-search package -class UPnP +namespace hueplusplus { -public: - //! \brief Searches for UPnP devices and returns all found ones. - //! - //! It does it by sending an m-search packet and waits for all responses. - //! Since responses can be received multiple times this function conveniently removes all duplicates. - //! \param handler HttpHandler for communication - //! \return A vector containing pairs of address and name of all found devices - //! \throws std::system_error when system or socket operations fail - std::vector> getDevices(std::shared_ptr handler); -}; + //! Class that looks for UPnP devices using an m-search package + class UPnP + { + public: + //! \brief Searches for UPnP devices and returns all found ones. + //! + //! It does it by sending an m-search packet and waits for all responses. + //! Since responses can be received multiple times this function conveniently removes all duplicates. + //! \param handler HttpHandler for communication + //! \return A vector containing pairs of address and name of all found devices + //! \throws std::system_error when system or socket operations fail + std::vector> getDevices(std::shared_ptr handler); + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/Units.h b/include/hueplusplus/Units.h index ff24330..4095fa3 100644 --- a/include/hueplusplus/Units.h +++ b/include/hueplusplus/Units.h @@ -23,38 +23,41 @@ #ifndef _UNITS_H #define _UNITS_H -struct Kelvin +namespace hueplusplus { - int value; -}; - -struct Mired -{ - int value; -}; - -struct Brightness -{ - int value; -}; - -struct HueSaturation -{ - int hue; - int saturation; -}; - -struct XY -{ - float x; - float y; -}; - -struct RGB -{ - uint8_t r; - uint8_t g; - uint8_t b; -}; + struct Kelvin + { + int value; + }; + + struct Mired + { + int value; + }; + + struct Brightness + { + int value; + }; + + struct HueSaturation + { + int hue; + int saturation; + }; + + struct XY + { + float x; + float y; + }; + + struct RGB + { + uint8_t r; + uint8_t g; + uint8_t b; + }; +} // namespace hueplusplus #endif diff --git a/include/hueplusplus/Utils.h b/include/hueplusplus/Utils.h index 96cff7c..51eb85a 100644 --- a/include/hueplusplus/Utils.h +++ b/include/hueplusplus/Utils.h @@ -25,59 +25,62 @@ #include "json/json.hpp" -namespace utils +namespace hueplusplus { - namespace detail + namespace utils { - // Forward declaration - template - nlohmann::json safeGetMemberHelper(const nlohmann::json& json, std::size_t index, Paths&&... otherPaths); + namespace detail + { + // Forward declaration + template + nlohmann::json safeGetMemberHelper(const nlohmann::json& json, std::size_t index, Paths&&... otherPaths); - inline nlohmann::json safeGetMemberHelper(const nlohmann::json& json) { return json; } + inline nlohmann::json safeGetMemberHelper(const nlohmann::json& json) { return json; } - template >::value>* = nullptr> - nlohmann::json safeGetMemberHelper(const nlohmann::json& json, KeyT&& key, Paths&&... otherPaths) - { - auto memberIt = json.find(std::forward(key)); - if (memberIt == json.end()) + template >::value>* = nullptr> + nlohmann::json safeGetMemberHelper(const nlohmann::json& json, KeyT&& key, Paths&&... otherPaths) { - return nullptr; + auto memberIt = json.find(std::forward(key)); + if (memberIt == json.end()) + { + return nullptr; + } + return safeGetMemberHelper(*memberIt, std::forward(otherPaths)...); } - return safeGetMemberHelper(*memberIt, std::forward(otherPaths)...); - } - // Needs to be after the other safeGetMemberHelper, otherwise another forward declaration is needed - template - nlohmann::json safeGetMemberHelper(const nlohmann::json& json, std::size_t index, Paths&&... otherPaths) - { - if (!json.is_array() || json.size() <= index) + // Needs to be after the other safeGetMemberHelper, otherwise another forward declaration is needed + template + nlohmann::json safeGetMemberHelper(const nlohmann::json& json, std::size_t index, Paths&&... otherPaths) { - return nullptr; + if (!json.is_array() || json.size() <= index) + { + return nullptr; + } + return safeGetMemberHelper(json[index], std::forward(otherPaths)...); } - return safeGetMemberHelper(json[index], std::forward(otherPaths)...); - } - } // namespace detail + } // namespace detail - //! \brief Function for validating that a request was executed correctly - //! - //! \param request The request that was sent initially - //! \param reply The reply that was received - //! \param lightId The identifier of the light - //! \return True if request was executed correctly - bool validateReplyForLight(const nlohmann::json& request, const nlohmann::json& reply, int lightId); + //! \brief Function for validating that a request was executed correctly + //! + //! \param request The request that was sent initially + //! \param reply The reply that was received + //! \param lightId The identifier of the light + //! \return True if request was executed correctly + bool validateReplyForLight(const nlohmann::json& request, const nlohmann::json& reply, int lightId); - //! \brief Returns the object/array member or null if it does not exist - //! - //! \param json The base json value - //! \param paths Any number of child accesses (e.g. 0, "key" would access json[0]["key"]) - //! \returns The specified member or null if any intermediate object does not contain the specified child. - template - nlohmann::json safeGetMember(const nlohmann::json& json, Paths&&... paths) - { - return detail::safeGetMemberHelper(json, std::forward(paths)...); - } + //! \brief Returns the object/array member or null if it does not exist + //! + //! \param json The base json value + //! \param paths Any number of child accesses (e.g. 0, "key" would access json[0]["key"]) + //! \returns The specified member or null if any intermediate object does not contain the specified child. + template + nlohmann::json safeGetMember(const nlohmann::json& json, Paths&&... paths) + { + return detail::safeGetMemberHelper(json, std::forward(paths)...); + } -} // namespace utils + } // namespace utils +} // namespace hueplusplus #endif \ No newline at end of file diff --git a/include/hueplusplus/WinHttpHandler.h b/include/hueplusplus/WinHttpHandler.h index e5c92dc..12719b3 100644 --- a/include/hueplusplus/WinHttpHandler.h +++ b/include/hueplusplus/WinHttpHandler.h @@ -30,40 +30,43 @@ #include "BaseHttpHandler.h" -//! Class to handle http requests and multicast requests on windows systems -class WinHttpHandler : public BaseHttpHandler +namespace hueplusplus { -public: - //! \brief Ctor needed for initializing wsaData - WinHttpHandler(); + //! Class to handle http requests and multicast requests on windows systems + class WinHttpHandler : public BaseHttpHandler + { + public: + //! \brief Ctor needed for initializing wsaData + WinHttpHandler(); - //! \brief Dtor needed for wsaData cleanup - ~WinHttpHandler(); + //! \brief Dtor needed for wsaData cleanup + ~WinHttpHandler(); - //! \brief Function that sends a given message to the specified host and - //! returns the response. - //! - //! \param msg String that contains the message that is sent to the specified - //! address \param adr String that contains an ip or hostname in dotted - //! decimal notation like "192.168.2.1" \param port Optional integer that - //! specifies the port to which the request is sent to. Default is 80 \return - //! String containing the response of the host - std::string send(const std::string& msg, const std::string& adr, int port = 80) const override; + //! \brief Function that sends a given message to the specified host and + //! returns the response. + //! + //! \param msg String that contains the message that is sent to the specified + //! address \param adr String that contains an ip or hostname in dotted + //! decimal notation like "192.168.2.1" \param port Optional integer that + //! specifies the port to which the request is sent to. Default is 80 \return + //! String containing the response of the host + std::string send(const std::string& msg, const std::string& adr, int port = 80) const override; - //! \brief Function that sends a multicast request with the specified message. - //! - //! \param msg String that contains the request that is sent to the specified - //! address \param adr Optional String that contains an ip or hostname in - //! dotted decimal notation, default is "239.255.255.250" \param port Optional - //! integer that specifies the port to which the request is sent. Default is - //! 1900 \param timeout Optional Integer that specifies the timeout of the - //! request in seconds. Default is 5 \return Vector containing strings of each - //! answer received - std::vector sendMulticast(const std::string& msg, const std::string& adr = "239.255.255.250", - int port = 1900, int timeout = 5) const override; + //! \brief Function that sends a multicast request with the specified message. + //! + //! \param msg String that contains the request that is sent to the specified + //! address \param adr Optional String that contains an ip or hostname in + //! dotted decimal notation, default is "239.255.255.250" \param port Optional + //! integer that specifies the port to which the request is sent. Default is + //! 1900 \param timeout Optional Integer that specifies the timeout of the + //! request in seconds. Default is 5 \return Vector containing strings of each + //! answer received + std::vector sendMulticast(const std::string& msg, const std::string& adr = "239.255.255.250", + int port = 1900, int timeout = 5) const override; -private: - WSADATA wsaData; -}; + private: + WSADATA wsaData; + }; +} // namespace hueplusplus #endif diff --git a/src/BaseHttpHandler.cpp b/src/BaseHttpHandler.cpp index 8a3b6bc..e374a46 100644 --- a/src/BaseHttpHandler.cpp +++ b/src/BaseHttpHandler.cpp @@ -24,7 +24,7 @@ #include "hueplusplus/HueExceptionMacro.h" -std::string BaseHttpHandler::sendGetHTTPBody(const std::string& msg, const std::string& adr, int port) const +std::string hueplusplus::BaseHttpHandler::sendGetHTTPBody(const std::string& msg, const std::string& adr, int port) const { std::string response = send(msg, adr, port); size_t start = response.find("\r\n\r\n"); @@ -41,7 +41,7 @@ std::string BaseHttpHandler::sendGetHTTPBody(const std::string& msg, const std:: return response; } -std::string BaseHttpHandler::sendHTTPRequest(const std::string& method, const std::string& uri, +std::string hueplusplus::BaseHttpHandler::sendHTTPRequest(const std::string& method, const std::string& uri, const std::string& contentType, const std::string& body, const std::string& adr, int port) const { std::string request; @@ -68,49 +68,49 @@ std::string BaseHttpHandler::sendHTTPRequest(const std::string& method, const st return sendGetHTTPBody(request.c_str(), adr, port); } -std::string BaseHttpHandler::GETString(const std::string& uri, const std::string& contentType, const std::string& body, +std::string hueplusplus::BaseHttpHandler::GETString(const std::string& uri, const std::string& contentType, const std::string& body, const std::string& adr, int port) const { return sendHTTPRequest("GET", uri, contentType, body, adr, port); } -std::string BaseHttpHandler::POSTString(const std::string& uri, const std::string& contentType, const std::string& body, +std::string hueplusplus::BaseHttpHandler::POSTString(const std::string& uri, const std::string& contentType, const std::string& body, const std::string& adr, int port) const { return sendHTTPRequest("POST", uri, contentType, body, adr, port); } -std::string BaseHttpHandler::PUTString(const std::string& uri, const std::string& contentType, const std::string& body, +std::string hueplusplus::BaseHttpHandler::PUTString(const std::string& uri, const std::string& contentType, const std::string& body, const std::string& adr, int port) const { return sendHTTPRequest("PUT", uri, contentType, body, adr, port); } -std::string BaseHttpHandler::DELETEString(const std::string& uri, const std::string& contentType, +std::string hueplusplus::BaseHttpHandler::DELETEString(const std::string& uri, const std::string& contentType, const std::string& body, const std::string& adr, int port) const { return sendHTTPRequest("DELETE", uri, contentType, body, adr, port); } -nlohmann::json BaseHttpHandler::GETJson( +nlohmann::json hueplusplus::BaseHttpHandler::GETJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(GETString(uri, "application/json", body.dump(), adr, port)); } -nlohmann::json BaseHttpHandler::POSTJson( +nlohmann::json hueplusplus::BaseHttpHandler::POSTJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(POSTString(uri, "application/json", body.dump(), adr, port)); } -nlohmann::json BaseHttpHandler::PUTJson( +nlohmann::json hueplusplus::BaseHttpHandler::PUTJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(PUTString(uri, "application/json", body.dump(), adr, port)); } -nlohmann::json BaseHttpHandler::DELETEJson( +nlohmann::json hueplusplus::BaseHttpHandler::DELETEJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(DELETEString(uri, "application/json", body.dump(), adr, port)); diff --git a/src/ExtendedColorHueStrategy.cpp b/src/ExtendedColorHueStrategy.cpp index 2805af5..632ddd7 100644 --- a/src/ExtendedColorHueStrategy.cpp +++ b/src/ExtendedColorHueStrategy.cpp @@ -28,7 +28,7 @@ #include "hueplusplus/HueConfig.h" -bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const +bool hueplusplus::ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -110,7 +110,7 @@ bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, Hue } } -bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const +bool hueplusplus::ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -192,7 +192,7 @@ bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const } } -bool ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const +bool hueplusplus::ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); diff --git a/src/ExtendedColorTemperatureStrategy.cpp b/src/ExtendedColorTemperatureStrategy.cpp index 88b948b..fbecacb 100644 --- a/src/ExtendedColorTemperatureStrategy.cpp +++ b/src/ExtendedColorTemperatureStrategy.cpp @@ -30,7 +30,7 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool ExtendedColorTemperatureStrategy::setColorTemperature( +bool hueplusplus::ExtendedColorTemperatureStrategy::setColorTemperature( unsigned int mired, uint8_t transition, HueLight& light) const { light.refreshState(); @@ -68,7 +68,7 @@ bool ExtendedColorTemperatureStrategy::setColorTemperature( return utils::validateReplyForLight(request, reply, light.id); } -bool ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const +bool hueplusplus::ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); diff --git a/src/Hue.cpp b/src/Hue.cpp index 92071b3..fecd25a 100644 --- a/src/Hue.cpp +++ b/src/Hue.cpp @@ -41,9 +41,9 @@ #include "hueplusplus/UPnP.h" #include "hueplusplus/Utils.h" -HueFinder::HueFinder(std::shared_ptr handler) : http_handler(std::move(handler)) {} +hueplusplus::HueFinder::HueFinder(std::shared_ptr handler) : http_handler(std::move(handler)) {} -std::vector HueFinder::FindBridges() const +std::vector hueplusplus::HueFinder::FindBridges() const { UPnP uplug; std::vector> foundDevices = uplug.getDevices(http_handler); @@ -71,7 +71,7 @@ std::vector HueFinder::FindBridges() const return foundBridges; } -Hue HueFinder::GetBridge(const HueIdentification& identification) +hueplusplus::Hue hueplusplus::HueFinder::GetBridge(const HueIdentification& identification) { std::string normalizedMac = NormalizeMac(identification.mac); auto pos = usernames.find(normalizedMac); @@ -91,17 +91,17 @@ Hue HueFinder::GetBridge(const HueIdentification& identification) return bridge; } -void HueFinder::AddUsername(const std::string& mac, const std::string& username) +void hueplusplus::HueFinder::AddUsername(const std::string& mac, const std::string& username) { usernames[NormalizeMac(mac)] = username; } -const std::map& HueFinder::GetAllUsernames() const +const std::map& hueplusplus::HueFinder::GetAllUsernames() const { return usernames; } -std::string HueFinder::NormalizeMac(std::string input) +std::string hueplusplus::HueFinder::NormalizeMac(std::string input) { // Remove any non alphanumeric characters (e.g. ':' and whitespace) input.erase(std::remove_if(input.begin(), input.end(), [](char c) { return !std::isalnum(c, std::locale()); }), @@ -111,7 +111,7 @@ std::string HueFinder::NormalizeMac(std::string input) return input; } -std::string HueFinder::ParseDescription(const std::string& description) +std::string hueplusplus::HueFinder::ParseDescription(const std::string& description) { const char* model = "Philips hue bridge"; const char* serialBegin = ""; @@ -133,7 +133,7 @@ std::string HueFinder::ParseDescription(const std::string& description) return std::string(); } -Hue::Hue( +hueplusplus::Hue::Hue( const std::string& ip, const int port, const std::string& username, std::shared_ptr handler) : ip(ip), port(port), @@ -147,17 +147,17 @@ Hue::Hue( commands(ip, port, username, http_handler) {} -std::string Hue::getBridgeIP() +std::string hueplusplus::Hue::getBridgeIP() { return ip; } -int Hue::getBridgePort() +int hueplusplus::Hue::getBridgePort() { return port; } -std::string Hue::requestUsername() +std::string hueplusplus::Hue::requestUsername() { std::cout << "Please press the link Button! You've got 35 secs!\n"; // when the link // button was @@ -206,22 +206,22 @@ std::string Hue::requestUsername() return username; } -std::string Hue::getUsername() +std::string hueplusplus::Hue::getUsername() { return username; } -void Hue::setIP(const std::string& ip) +void hueplusplus::Hue::setIP(const std::string& ip) { this->ip = ip; } -void Hue::setPort(const int port) +void hueplusplus::Hue::setPort(const int port) { this->port = port; } -HueLight& Hue::getLight(int id) +hueplusplus::HueLight& hueplusplus::Hue::getLight(int id) { auto pos = lights.find(id); if (pos != lights.end()) @@ -245,7 +245,7 @@ HueLight& Hue::getLight(int id) return lights.find(id)->second; } -bool Hue::removeLight(int id) +bool hueplusplus::Hue::removeLight(int id) { nlohmann::json result = commands.DELETERequest("/lights/" + std::to_string(id), nlohmann::json::object(), CURRENT_FILE_INFO); @@ -257,7 +257,7 @@ bool Hue::removeLight(int id) return success; } -std::vector> Hue::getAllLights() +std::vector> hueplusplus::Hue::getAllLights() { refreshState(); nlohmann::json lightsState = state["lights"]; @@ -273,7 +273,7 @@ std::vector> Hue::getAllLights() return result; } -bool Hue::lightExists(int id) +bool hueplusplus::Hue::lightExists(int id) { refreshState(); auto pos = lights.find(id); @@ -288,7 +288,7 @@ bool Hue::lightExists(int id) return false; } -bool Hue::lightExists(int id) const +bool hueplusplus::Hue::lightExists(int id) const { auto pos = lights.find(id); if (pos != lights.end()) @@ -302,7 +302,7 @@ bool Hue::lightExists(int id) const return false; } -std::string Hue::getPictureOfLight(int id) const +std::string hueplusplus::Hue::getPictureOfLight(int id) const { std::string ret = ""; auto pos = lights.find(id); @@ -313,7 +313,7 @@ std::string Hue::getPictureOfLight(int id) const return ret; } -std::string Hue::getPictureOfModel(const std::string& model_id) const +std::string hueplusplus::Hue::getPictureOfModel(const std::string& model_id) const { std::string ret = ""; if (model_id == "LCT001" || model_id == "LCT007" || model_id == "LCT010" || model_id == "LCT014" @@ -437,7 +437,7 @@ std::string Hue::getPictureOfModel(const std::string& model_id) const return ret; } -void Hue::refreshState() +void hueplusplus::Hue::refreshState() { if (username.empty()) { diff --git a/src/HueCommandAPI.cpp b/src/HueCommandAPI.cpp index 929ba2f..e4ac202 100644 --- a/src/HueCommandAPI.cpp +++ b/src/HueCommandAPI.cpp @@ -26,7 +26,7 @@ #include "hueplusplus/HueExceptionMacro.h" -constexpr std::chrono::steady_clock::duration HueCommandAPI::minDelay; +constexpr std::chrono::steady_clock::duration hueplusplus::HueCommandAPI::minDelay; namespace { @@ -63,7 +63,7 @@ namespace } } // namespace -HueCommandAPI::HueCommandAPI( +hueplusplus::HueCommandAPI::HueCommandAPI( const std::string& ip, const int port, const std::string& username, std::shared_ptr httpHandler) : ip(ip), port(port), @@ -72,43 +72,43 @@ HueCommandAPI::HueCommandAPI( timeout(new TimeoutData{std::chrono::steady_clock::now(), {}}) {} -nlohmann::json HueCommandAPI::PUTRequest(const std::string& path, const nlohmann::json& request) const +nlohmann::json hueplusplus::HueCommandAPI::PUTRequest(const std::string& path, const nlohmann::json& request) const { return PUTRequest(path, request, CURRENT_FILE_INFO); } -nlohmann::json HueCommandAPI::PUTRequest( +nlohmann::json hueplusplus::HueCommandAPI::PUTRequest( const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const { return HandleError(fileInfo, RunWithTimeout(timeout, minDelay, [&]() { return httpHandler->PUTJson(CombinedPath(path), request, ip); })); } -nlohmann::json HueCommandAPI::GETRequest(const std::string& path, const nlohmann::json& request) const +nlohmann::json hueplusplus::HueCommandAPI::GETRequest(const std::string& path, const nlohmann::json& request) const { return GETRequest(path, request, CURRENT_FILE_INFO); } -nlohmann::json HueCommandAPI::GETRequest( +nlohmann::json hueplusplus::HueCommandAPI::GETRequest( const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const { return HandleError(fileInfo, RunWithTimeout(timeout, minDelay, [&]() { return httpHandler->GETJson(CombinedPath(path), request, ip); })); } -nlohmann::json HueCommandAPI::DELETERequest(const std::string& path, const nlohmann::json& request) const +nlohmann::json hueplusplus::HueCommandAPI::DELETERequest(const std::string& path, const nlohmann::json& request) const { return DELETERequest(path, request, CURRENT_FILE_INFO); } -nlohmann::json HueCommandAPI::DELETERequest( +nlohmann::json hueplusplus::HueCommandAPI::DELETERequest( const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const { return HandleError(fileInfo, RunWithTimeout(timeout, minDelay, [&]() { return httpHandler->DELETEJson(CombinedPath(path), request, ip); })); } -nlohmann::json HueCommandAPI::HandleError(FileInfo fileInfo, const nlohmann::json& response) const +nlohmann::json hueplusplus::HueCommandAPI::HandleError(FileInfo fileInfo, const nlohmann::json& response) const { if (response.count("error")) { @@ -121,7 +121,7 @@ nlohmann::json HueCommandAPI::HandleError(FileInfo fileInfo, const nlohmann::jso return response; } -std::string HueCommandAPI::CombinedPath(const std::string& path) const +std::string hueplusplus::HueCommandAPI::CombinedPath(const std::string& path) const { std::string result = "/api/"; result.append(username); diff --git a/src/HueDeviceTypes.cpp b/src/HueDeviceTypes.cpp index d1f64db..4f9f688 100644 --- a/src/HueDeviceTypes.cpp +++ b/src/HueDeviceTypes.cpp @@ -20,63 +20,53 @@ along with hueplusplus. If not, see . **/ +#include "hueplusplus/HueDeviceTypes.h" + #include -#include "hueplusplus/HueDeviceTypes.h" #include "hueplusplus/HueExceptionMacro.h" -const std::set getGamutBTypes() +namespace { - static const std::set c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES = { - "LCT001", "LCT002", "LCT003", "LCT007", "LLM001" + const std::set getGamutBTypes() + { + static const std::set c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES + = {"LCT001", "LCT002", "LCT003", "LCT007", "LLM001"}; + return c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES; }; - return c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES; -}; + const std::set getGamutCTypes() + { + static const std::set c_EXTENDEDCOLORLIGHT_GAMUTC_TYPES + = {"LCT010", "LCT011", "LCT012", "LCT014", "LCT015", "LCT016", "LLC020", "LST002"}; + return c_EXTENDEDCOLORLIGHT_GAMUTC_TYPES; + } -const std::set getGamutCTypes() -{ - static const std::set c_EXTENDEDCOLORLIGHT_GAMUTC_TYPES = { - "LCT010", "LCT011", "LCT012", "LCT014", - "LCT015", "LCT016", "LLC020", "LST002" - }; - return c_EXTENDEDCOLORLIGHT_GAMUTC_TYPES; -} - -const std::set getGamutATypes() -{ - static const std::set c_EXTENDEDCOLORLIGHT_GAMUTA_TYPES = { - "LST001", "LLC005" , "LLC006", "LLC007", - "LLC010", "LLC011", "LLC012", "LLC013", - "LLC014" - }; - return c_EXTENDEDCOLORLIGHT_GAMUTA_TYPES; -} + const std::set getGamutATypes() + { + static const std::set c_EXTENDEDCOLORLIGHT_GAMUTA_TYPES + = {"LST001", "LLC005", "LLC006", "LLC007", "LLC010", "LLC011", "LLC012", "LLC013", "LLC014"}; + return c_EXTENDEDCOLORLIGHT_GAMUTA_TYPES; + } -const std::set getNoColorTypes() -{ - static const std::set c_DIMMABLELIGHT_NO_COLOR_TYPES = { - "LWB004", "LWB006", "LWB007", "LWB010", - "LWB014", "LDF001", "LDF002", "LDD001", - "LDD002", "MWM001" - }; - return c_DIMMABLELIGHT_NO_COLOR_TYPES; -} + const std::set getNoColorTypes() + { + static const std::set c_DIMMABLELIGHT_NO_COLOR_TYPES + = {"LWB004", "LWB006", "LWB007", "LWB010", "LWB014", "LDF001", "LDF002", "LDD001", "LDD002", "MWM001"}; + return c_DIMMABLELIGHT_NO_COLOR_TYPES; + } -const std::set getTemperatureLightTypes() -{ - static const std::set c_TEMPERATURELIGHT_TYPES = { - "LLM010", "LLM011", "LLM012", "LTW001", "LTW004", - "LTW010", "LTW011", "LTW012", "LTW013", "LTW014", - "LTW015", "LTP001", "LTP002", "LTP003", "LTP004", - "LTP005", "LTD003", "LTF001", "LTF002", "LTC001", - "LTC002", "LTC003", "LTC004", "LTC011", "LTC012", - "LTD001", "LTD002", "LFF001", "LTT001", "LDT001" - }; - return c_TEMPERATURELIGHT_TYPES; -} + const std::set getTemperatureLightTypes() + { + static const std::set c_TEMPERATURELIGHT_TYPES + = {"LLM010", "LLM011", "LLM012", "LTW001", "LTW004", "LTW010", "LTW011", "LTW012", "LTW013", "LTW014", + "LTW015", "LTP001", "LTP002", "LTP003", "LTP004", "LTP005", "LTD003", "LTF001", "LTF002", "LTC001", + "LTC002", "LTC003", "LTC004", "LTC011", "LTC012", "LTD001", "LTD002", "LFF001", "LTT001", "LDT001"}; + return c_TEMPERATURELIGHT_TYPES; + } +} // namespace -auto MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands, +auto hueplusplus::MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands, std::shared_ptr simpleBrightnessStrategy, std::shared_ptr extendedColorTemperatureStrategy, std::shared_ptr simpleColorTemperatureStrategy, @@ -85,14 +75,16 @@ auto MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands, { if (getGamutBTypes().count(type)) { - auto light = HueLight(id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); + auto light = HueLight( + id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); light.colorType = ColorType::GAMUT_B; return light; } else if (getGamutCTypes().count(type)) { - auto light = HueLight(id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); + auto light = HueLight( + id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); light.colorType = ColorType::GAMUT_C; return light; } diff --git a/src/HueException.cpp b/src/HueException.cpp index 268023c..788c611 100644 --- a/src/HueException.cpp +++ b/src/HueException.cpp @@ -22,21 +22,21 @@ #include "hueplusplus/HueException.h" -HueException::HueException(FileInfo fileInfo, const std::string& message) +hueplusplus::HueException::HueException(FileInfo fileInfo, const std::string& message) : HueException("HueException", std::move(fileInfo), message) {} -const char* HueException::what() const noexcept +const char* hueplusplus::HueException::what() const noexcept { return whatMessage.c_str(); } -const FileInfo& HueException::GetFile() const noexcept +const hueplusplus::FileInfo& hueplusplus::HueException::GetFile() const noexcept { return fileInfo; } -HueException::HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message) +hueplusplus::HueException::HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message) : fileInfo(std::move(fileInfo)) { whatMessage = exceptionName; @@ -46,7 +46,7 @@ HueException::HueException(const char* exceptionName, FileInfo fileInfo, const s whatMessage.append(message); } -HueAPIResponseException::HueAPIResponseException( +hueplusplus::HueAPIResponseException::HueAPIResponseException( FileInfo fileInfo, int error, std::string address, std::string description) : HueException("HueApiResponseException", std::move(fileInfo), GetMessage(error, address, description)), error(error), @@ -54,22 +54,22 @@ HueAPIResponseException::HueAPIResponseException( description(std::move(description)) {} -int HueAPIResponseException::GetErrorNumber() const noexcept +int hueplusplus::HueAPIResponseException::GetErrorNumber() const noexcept { return error; } -const std::string& HueAPIResponseException::GetAddress() const noexcept +const std::string& hueplusplus::HueAPIResponseException::GetAddress() const noexcept { return address; } -const std::string& HueAPIResponseException::GetDescription() const noexcept +const std::string& hueplusplus::HueAPIResponseException::GetDescription() const noexcept { return description; } -HueAPIResponseException HueAPIResponseException::Create(FileInfo fileInfo, const nlohmann::json& response) +hueplusplus::HueAPIResponseException hueplusplus::HueAPIResponseException::Create(FileInfo fileInfo, const nlohmann::json& response) { const nlohmann::json error = response.at("error"); int errorCode = error.value("type", -1); @@ -78,7 +78,7 @@ HueAPIResponseException HueAPIResponseException::Create(FileInfo fileInfo, const return HueAPIResponseException(std::move(fileInfo), errorCode, std::move(address), std::move(description)); } -std::string HueAPIResponseException::GetMessage(int error, const std::string& addr, const std::string& description) +std::string hueplusplus::HueAPIResponseException::GetMessage(int error, const std::string& addr, const std::string& description) { std::string result = std::to_string(error); result.append(" "); @@ -88,7 +88,7 @@ std::string HueAPIResponseException::GetMessage(int error, const std::string& ad return result; } -std::string FileInfo::ToString() const +std::string hueplusplus::FileInfo::ToString() const { if (filename.empty() || line < 0) { diff --git a/src/HueLight.cpp b/src/HueLight.cpp index 36d1467..e57fbf3 100644 --- a/src/HueLight.cpp +++ b/src/HueLight.cpp @@ -30,56 +30,56 @@ #include "hueplusplus/Utils.h" #include "json/json.hpp" -bool HueLight::On(uint8_t transition) +bool hueplusplus::HueLight::On(uint8_t transition) { refreshState(); return OnNoRefresh(transition); } -bool HueLight::Off(uint8_t transition) +bool hueplusplus::HueLight::Off(uint8_t transition) { refreshState(); return OffNoRefresh(transition); } -bool HueLight::isOn() +bool hueplusplus::HueLight::isOn() { refreshState(); return state["state"]["on"].get(); } -bool HueLight::isOn() const +bool hueplusplus::HueLight::isOn() const { return state["state"]["on"].get(); } -int HueLight::getId() const +int hueplusplus::HueLight::getId() const { return id; } -std::string HueLight::getType() const +std::string hueplusplus::HueLight::getType() const { return state["type"].get(); } -std::string HueLight::getName() +std::string hueplusplus::HueLight::getName() { refreshState(); return state["name"].get(); } -std::string HueLight::getName() const +std::string hueplusplus::HueLight::getName() const { return state["name"].get(); } -std::string HueLight::getModelId() const +std::string hueplusplus::HueLight::getModelId() const { return state["modelid"].get(); } -std::string HueLight::getUId() const +std::string hueplusplus::HueLight::getUId() const { if (state.count("uniqueid")) { @@ -88,7 +88,7 @@ std::string HueLight::getUId() const return std::string(); } -std::string HueLight::getManufacturername() const +std::string hueplusplus::HueLight::getManufacturername() const { if (state.count("manufacturername")) { @@ -97,7 +97,7 @@ std::string HueLight::getManufacturername() const return std::string(); } -std::string HueLight::getProductname() const +std::string hueplusplus::HueLight::getProductname() const { if (state.count("productname")) { @@ -106,7 +106,7 @@ std::string HueLight::getProductname() const return std::string(); } -std::string HueLight::getLuminaireUId() const +std::string hueplusplus::HueLight::getLuminaireUId() const { if (state.count("luminaireuniqueid")) { @@ -115,18 +115,18 @@ std::string HueLight::getLuminaireUId() const return std::string(); } -std::string HueLight::getSwVersion() +std::string hueplusplus::HueLight::getSwVersion() { refreshState(); return state["swversion"].get(); } -std::string HueLight::getSwVersion() const +std::string hueplusplus::HueLight::getSwVersion() const { return state["swversion"].get(); } -bool HueLight::setName(const std::string& name) +bool hueplusplus::HueLight::setName(const std::string& name) { nlohmann::json request = nlohmann::json::object(); request["name"] = name; @@ -136,22 +136,22 @@ bool HueLight::setName(const std::string& name) return utils::safeGetMember(reply, 0, "success", "/lights/" + std::to_string(id) + "/name") == name; } -ColorType HueLight::getColorType() const +hueplusplus::ColorType hueplusplus::HueLight::getColorType() const { return colorType; } -unsigned int HueLight::KelvinToMired(unsigned int kelvin) const +unsigned int hueplusplus::HueLight::KelvinToMired(unsigned int kelvin) const { return int(0.5f + (1000000 / kelvin)); } -unsigned int HueLight::MiredToKelvin(unsigned int mired) const +unsigned int hueplusplus::HueLight::MiredToKelvin(unsigned int mired) const { return int(0.5f + (1000000 / mired)); } -bool HueLight::alert() +bool hueplusplus::HueLight::alert() { nlohmann::json request; request["alert"] = "select"; @@ -161,9 +161,9 @@ bool HueLight::alert() return utils::validateReplyForLight(request, reply, id); } -HueLight::HueLight(int id, const HueCommandAPI& commands) : HueLight(id, commands, nullptr, nullptr, nullptr) {} +hueplusplus::HueLight::HueLight(int id, const HueCommandAPI& commands) : HueLight(id, commands, nullptr, nullptr, nullptr) {} -HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, +hueplusplus::HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, std::shared_ptr colorTempStrategy, std::shared_ptr colorHueStrategy) : id(id), @@ -176,7 +176,7 @@ HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr LinHttpHandler::sendMulticast( +std::vector hueplusplus::LinHttpHandler::sendMulticast( const std::string& msg, const std::string& adr, int port, int timeout) const { hostent* server; // host information diff --git a/src/SimpleBrightnessStrategy.cpp b/src/SimpleBrightnessStrategy.cpp index 0bb5a3b..745fb91 100644 --- a/src/SimpleBrightnessStrategy.cpp +++ b/src/SimpleBrightnessStrategy.cpp @@ -29,7 +29,7 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const +bool hueplusplus::SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const { light.refreshState(); if (bri == 0) @@ -76,13 +76,13 @@ bool SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint8_t transitio } } -unsigned int SimpleBrightnessStrategy::getBrightness(HueLight& light) const +unsigned int hueplusplus::SimpleBrightnessStrategy::getBrightness(HueLight& light) const { light.refreshState(); return light.state["state"]["bri"].get(); } -unsigned int SimpleBrightnessStrategy::getBrightness(const HueLight& light) const +unsigned int hueplusplus::SimpleBrightnessStrategy::getBrightness(const HueLight& light) const { return light.state["state"]["bri"].get(); } diff --git a/src/SimpleColorHueStrategy.cpp b/src/SimpleColorHueStrategy.cpp index b97cb60..776353e 100644 --- a/src/SimpleColorHueStrategy.cpp +++ b/src/SimpleColorHueStrategy.cpp @@ -30,7 +30,7 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool SimpleColorHueStrategy::setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -60,7 +60,7 @@ bool SimpleColorHueStrategy::setColorHue(uint16_t hue, uint8_t transition, HueLi return utils::validateReplyForLight(request, reply, light.id); } -bool SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -93,7 +93,7 @@ bool SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_t transition, return utils::validateReplyForLight(request, reply, light.id); } -bool SimpleColorHueStrategy::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -132,7 +132,7 @@ bool SimpleColorHueStrategy::setColorHueSaturation(uint16_t hue, uint8_t sat, ui return utils::validateReplyForLight(request, reply, light.id); } -bool SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t transition, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -165,7 +165,7 @@ bool SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t transition, Hu return utils::validateReplyForLight(request, reply, light.id); } -bool SimpleColorHueStrategy::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const { if ((r == 0) && (g == 0) && (b == 0)) { @@ -191,7 +191,7 @@ bool SimpleColorHueStrategy::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_ return light.setColorXY(x, y, transition); } -bool SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) const { // colorloop light.refreshState(); @@ -218,7 +218,7 @@ bool SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) const return utils::validateReplyForLight(request, reply, light.id); } -bool SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -277,7 +277,7 @@ bool SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLi } } -bool SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -336,7 +336,7 @@ bool SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const } } -bool SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const +bool hueplusplus::SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -395,24 +395,24 @@ bool SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& } } -std::pair SimpleColorHueStrategy::getColorHueSaturation(HueLight& light) const +std::pair hueplusplus::SimpleColorHueStrategy::getColorHueSaturation(HueLight& light) const { light.refreshState(); return std::make_pair(light.state["state"]["hue"].get(), light.state["state"]["sat"].get()); } -std::pair SimpleColorHueStrategy::getColorHueSaturation(const HueLight& light) const +std::pair hueplusplus::SimpleColorHueStrategy::getColorHueSaturation(const HueLight& light) const { return std::make_pair(light.state["state"]["hue"].get(), light.state["state"]["sat"].get()); } -std::pair SimpleColorHueStrategy::getColorXY(HueLight& light) const +std::pair hueplusplus::SimpleColorHueStrategy::getColorXY(HueLight& light) const { light.refreshState(); return std::make_pair(light.state["state"]["xy"][0].get(), light.state["state"]["xy"][1].get()); } -std::pair SimpleColorHueStrategy::getColorXY(const HueLight& light) const +std::pair hueplusplus::SimpleColorHueStrategy::getColorXY(const HueLight& light) const { return std::make_pair(light.state["state"]["xy"][0].get(), light.state["state"]["xy"][1].get()); } diff --git a/src/SimpleColorTemperatureStrategy.cpp b/src/SimpleColorTemperatureStrategy.cpp index ccb8bec..681e0dc 100644 --- a/src/SimpleColorTemperatureStrategy.cpp +++ b/src/SimpleColorTemperatureStrategy.cpp @@ -30,7 +30,7 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool SimpleColorTemperatureStrategy::setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const +bool hueplusplus::SimpleColorTemperatureStrategy::setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -67,7 +67,7 @@ bool SimpleColorTemperatureStrategy::setColorTemperature(unsigned int mired, uin return utils::validateReplyForLight(request, reply, light.id); } -bool SimpleColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const +bool hueplusplus::SimpleColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -101,13 +101,13 @@ bool SimpleColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLig } } -unsigned int SimpleColorTemperatureStrategy::getColorTemperature(HueLight& light) const +unsigned int hueplusplus::SimpleColorTemperatureStrategy::getColorTemperature(HueLight& light) const { light.refreshState(); return light.state["state"]["ct"].get(); } -unsigned int SimpleColorTemperatureStrategy::getColorTemperature(const HueLight& light) const +unsigned int hueplusplus::SimpleColorTemperatureStrategy::getColorTemperature(const HueLight& light) const { return light.state["state"]["ct"].get(); } diff --git a/src/UPnP.cpp b/src/UPnP.cpp index f75303c..5df18b0 100644 --- a/src/UPnP.cpp +++ b/src/UPnP.cpp @@ -25,7 +25,7 @@ #include #include -std::vector> UPnP::getDevices(std::shared_ptr handler) +std::vector> hueplusplus::UPnP::getDevices(std::shared_ptr handler) { // send UPnP M-Search request std::vector foundDevices diff --git a/src/Utils.cpp b/src/Utils.cpp index 2500671..56b17ae 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -24,59 +24,55 @@ #include -namespace utils +bool hueplusplus::utils::validateReplyForLight(const nlohmann::json& request, const nlohmann::json& reply, int lightId) { - bool validateReplyForLight(const nlohmann::json& request, const nlohmann::json& reply, int lightId) + bool success = false; + std::string path = "/lights/" + std::to_string(lightId) + "/state/"; + for (auto it = reply.begin(); it != reply.end(); ++it) { - bool success = false; - std::string path = "/lights/" + std::to_string(lightId) + "/state/"; - for (auto it = reply.begin(); it != reply.end(); ++it) + success = it.value().count("success"); + if (success) { - success = it.value().count("success"); - if (success) + // Traverse through first object + nlohmann::json successObject = it.value()["success"]; + for (auto successIt = successObject.begin(); successIt != successObject.end(); ++successIt) { - // Traverse through first object - nlohmann::json successObject = it.value()["success"]; - for (auto successIt = successObject.begin(); successIt != successObject.end(); ++successIt) + const std::string successPath = successIt.key(); + if (successPath.find(path) == 0) { - const std::string successPath = successIt.key(); - if (successPath.find(path) == 0) + const std::string valueKey = successPath.substr(path.size()); + auto requestIt = request.find(valueKey); + success = requestIt != request.end(); + if (success) { - const std::string valueKey = successPath.substr(path.size()); - auto requestIt = request.find(valueKey); - success = requestIt != request.end(); - if (success) + if (valueKey == "xy") { - if (valueKey == "xy") - { - success - = std::abs(requestIt.value()[0].get() - successIt.value()[0].get()) - <= 1E-4f - && std::abs(requestIt.value()[1].get() - successIt.value()[1].get()) - <= 1E-4f; - } - else - { - success = requestIt.value() == successIt.value(); - } - if (!success) - { - std::cout << "Value " << requestIt.value() << " does not match reply " - << successIt.value() << std::endl; - } + success = std::abs(requestIt.value()[0].get() - successIt.value()[0].get()) + <= 1E-4f + && std::abs(requestIt.value()[1].get() - successIt.value()[1].get()) + <= 1E-4f; + } + else + { + success = requestIt.value() == successIt.value(); + } + if (!success) + { + std::cout << "Value " << requestIt.value() << " does not match reply " << successIt.value() + << std::endl; } - } - else - { - success = false; } } + else + { + success = false; + } } - if (!success) // Fail fast - { - break; - } } - return success; + if (!success) // Fail fast + { + break; + } } -} // namespace utils \ No newline at end of file + return success; +} \ No newline at end of file diff --git a/src/WinHttpHandler.cpp b/src/WinHttpHandler.cpp index 14332e8..6cbb07c 100644 --- a/src/WinHttpHandler.cpp +++ b/src/WinHttpHandler.cpp @@ -54,7 +54,7 @@ namespace }; } // namespace -WinHttpHandler::WinHttpHandler() +hueplusplus::WinHttpHandler::WinHttpHandler() { // Initialize Winsock int return_code = WSAStartup(MAKEWORD(2, 2), &wsaData); @@ -65,12 +65,12 @@ WinHttpHandler::WinHttpHandler() } } -WinHttpHandler::~WinHttpHandler() +hueplusplus::WinHttpHandler::~WinHttpHandler() { WSACleanup(); } -std::string WinHttpHandler::send(const std::string& msg, const std::string& adr, int port) const +std::string hueplusplus::WinHttpHandler::send(const std::string& msg, const std::string& adr, int port) const { struct addrinfo hints = {}; hints.ai_family = AF_INET; @@ -171,7 +171,7 @@ std::string WinHttpHandler::send(const std::string& msg, const std::string& adr, return response; } -std::vector WinHttpHandler::sendMulticast( +std::vector hueplusplus::WinHttpHandler::sendMulticast( const std::string& msg, const std::string& adr, int port, int timeout) const { struct addrinfo hints = {}; diff --git a/test/mocks/mock_BaseHttpHandler.h b/test/mocks/mock_BaseHttpHandler.h index b712cd6..871c628 100644 --- a/test/mocks/mock_BaseHttpHandler.h +++ b/test/mocks/mock_BaseHttpHandler.h @@ -32,7 +32,7 @@ #include "json/json.hpp" //! Mock Class -class MockBaseHttpHandler : public BaseHttpHandler +class MockBaseHttpHandler : public hueplusplus::BaseHttpHandler { public: MOCK_CONST_METHOD3(send, std::string(const std::string& msg, const std::string& adr, int port)); diff --git a/test/mocks/mock_HttpHandler.h b/test/mocks/mock_HttpHandler.h index d94e8d4..df76420 100644 --- a/test/mocks/mock_HttpHandler.h +++ b/test/mocks/mock_HttpHandler.h @@ -32,7 +32,7 @@ #include "json/json.hpp" //! Mock Class -class MockHttpHandler : public IHttpHandler +class MockHttpHandler : public hueplusplus::IHttpHandler { public: MOCK_CONST_METHOD3(send, std::string(const std::string& msg, const std::string& adr, int port)); diff --git a/test/mocks/mock_HueLight.h b/test/mocks/mock_HueLight.h index 6ef130d..3c24bc5 100644 --- a/test/mocks/mock_HueLight.h +++ b/test/mocks/mock_HueLight.h @@ -33,11 +33,11 @@ #include "../testhelper.h" //! Mock Class -class MockHueLight : public HueLight +class MockHueLight : public hueplusplus::HueLight { public: - MockHueLight(std::shared_ptr handler) - : HueLight(1, HueCommandAPI(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler)) {}; + MockHueLight(std::shared_ptr handler) + : HueLight(1, hueplusplus::HueCommandAPI(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler)) {}; nlohmann::json& getState() { return state; }; @@ -71,7 +71,7 @@ public: MOCK_METHOD1(setName, bool(std::string& name)); - MOCK_CONST_METHOD0(getColorType, ColorType()); + MOCK_CONST_METHOD0(getColorType, hueplusplus::ColorType()); MOCK_CONST_METHOD0(hasBrightnessControl, bool()); @@ -126,7 +126,7 @@ public: MOCK_METHOD1(OffNoRefresh, bool(uint8_t transition)); MOCK_METHOD3( - SendPutRequest, nlohmann::json(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo)); + SendPutRequest, nlohmann::json(const nlohmann::json& request, const std::string& subPath, hueplusplus::FileInfo fileInfo)); MOCK_METHOD0(refreshState, void()); }; diff --git a/test/test_BaseHttpHandler.cpp b/test/test_BaseHttpHandler.cpp index ea4501e..fbafb85 100644 --- a/test/test_BaseHttpHandler.cpp +++ b/test/test_BaseHttpHandler.cpp @@ -32,6 +32,8 @@ #include "mocks/mock_BaseHttpHandler.h" #include "hueplusplus/HueException.h" +using namespace hueplusplus; + TEST(BaseHttpHandler, sendGetHTTPBody) { using namespace ::testing; diff --git a/test/test_ExtendedColorHueStrategy.cpp b/test/test_ExtendedColorHueStrategy.cpp index 4c66eac..97fac0f 100644 --- a/test/test_ExtendedColorHueStrategy.cpp +++ b/test/test_ExtendedColorHueStrategy.cpp @@ -33,6 +33,8 @@ #include "mocks/mock_HttpHandler.h" #include "mocks/mock_HueLight.h" +using namespace hueplusplus; + TEST(ExtendedColorHueStrategy, alertHueSaturation) { using namespace ::testing; diff --git a/test/test_ExtendedColorTemperatureStrategy.cpp b/test/test_ExtendedColorTemperatureStrategy.cpp index 488fe7b..4379b4c 100644 --- a/test/test_ExtendedColorTemperatureStrategy.cpp +++ b/test/test_ExtendedColorTemperatureStrategy.cpp @@ -33,6 +33,8 @@ #include "mocks/mock_HttpHandler.h" #include "mocks/mock_HueLight.h" +using namespace hueplusplus; + TEST(ExtendedColorTemperatureStrategy, setColorTemperature) { using namespace ::testing; diff --git a/test/test_Hue.cpp b/test/test_Hue.cpp index 3af5a3d..a671192 100644 --- a/test/test_Hue.cpp +++ b/test/test_Hue.cpp @@ -34,6 +34,8 @@ #include "json/json.hpp" #include "mocks/mock_HttpHandler.h" +using namespace hueplusplus; + class HueFinderTest : public ::testing::Test { protected: diff --git a/test/test_HueCommandAPI.cpp b/test/test_HueCommandAPI.cpp index ba6418e..9049ea9 100644 --- a/test/test_HueCommandAPI.cpp +++ b/test/test_HueCommandAPI.cpp @@ -29,6 +29,8 @@ #include "json/json.hpp" #include "mocks/mock_HttpHandler.h" +using namespace hueplusplus; + TEST(HueCommandAPI, PUTRequest) { using namespace ::testing; diff --git a/test/test_HueLight.cpp b/test/test_HueLight.cpp index dbfeb66..eceba76 100644 --- a/test/test_HueLight.cpp +++ b/test/test_HueLight.cpp @@ -30,6 +30,8 @@ #include "json/json.hpp" #include "mocks/mock_HttpHandler.h" +using namespace hueplusplus; + class HueLightTest : public ::testing::Test { protected: diff --git a/test/test_SimpleBrightnessStrategy.cpp b/test/test_SimpleBrightnessStrategy.cpp index a3e6487..9e08a9b 100644 --- a/test/test_SimpleBrightnessStrategy.cpp +++ b/test/test_SimpleBrightnessStrategy.cpp @@ -34,6 +34,8 @@ #include "mocks/mock_HttpHandler.h" #include "mocks/mock_HueLight.h" +using namespace hueplusplus; + TEST(SimpleBrightnessStrategy, setBrightness) { using namespace ::testing; diff --git a/test/test_SimpleColorHueStrategy.cpp b/test/test_SimpleColorHueStrategy.cpp index b5714b6..ea0c36e 100644 --- a/test/test_SimpleColorHueStrategy.cpp +++ b/test/test_SimpleColorHueStrategy.cpp @@ -33,6 +33,8 @@ #include "mocks/mock_HttpHandler.h" #include "mocks/mock_HueLight.h" +using namespace hueplusplus; + TEST(SimpleColorHueStrategy, setColorHue) { using namespace ::testing; diff --git a/test/test_SimpleColorTemperatureStrategy.cpp b/test/test_SimpleColorTemperatureStrategy.cpp index aadde88..b00803d 100644 --- a/test/test_SimpleColorTemperatureStrategy.cpp +++ b/test/test_SimpleColorTemperatureStrategy.cpp @@ -34,6 +34,8 @@ #include "mocks/mock_HttpHandler.h" #include "mocks/mock_HueLight.h" +using namespace hueplusplus; + TEST(SimpleColorTemperatureStrategy, setColorTemperature) { using namespace ::testing; diff --git a/test/test_UPnP.cpp b/test/test_UPnP.cpp index ad24266..f001d80 100644 --- a/test/test_UPnP.cpp +++ b/test/test_UPnP.cpp @@ -30,6 +30,8 @@ #include "json/json.hpp" #include "mocks/mock_HttpHandler.h" +using namespace hueplusplus; + const std::vector> expected_uplug_dev = {{"http://192.168.2.1:1900/gatedesc.xml", "Linux/2.6.36, UPnP/1.0, Portable SDK for UPnP devices/1.6.19"}, {"http://192.168.2.116:80/description.xml", "Linux/3.14.0 UPnP/1.0 IpBridge/1.21.0"}}; -- libgit2 0.21.4