diff --git a/include/hueplusplus/BaseHttpHandler.h b/include/hueplusplus/BaseHttpHandler.h index e0bf4de..a052e2d 100644 --- a/include/hueplusplus/BaseHttpHandler.h +++ b/include/hueplusplus/BaseHttpHandler.h @@ -34,145 +34,145 @@ namespace hueplusplus { - //! 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; - }; +//! 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 2a365de..9e935ed 100644 --- a/include/hueplusplus/BrightnessStrategy.h +++ b/include/hueplusplus/BrightnessStrategy.h @@ -27,35 +27,35 @@ namespace hueplusplus { - 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; - }; +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 76c6ee2..97374ee 100644 --- a/include/hueplusplus/ColorHueStrategy.h +++ b/include/hueplusplus/ColorHueStrategy.h @@ -28,130 +28,130 @@ namespace hueplusplus { - class HueLight; +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; - }; +//! 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 e0b455f..1a18662 100644 --- a/include/hueplusplus/ColorTemperatureStrategy.h +++ b/include/hueplusplus/ColorTemperatureStrategy.h @@ -27,47 +27,47 @@ namespace hueplusplus { - class HueLight; +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; - }; +//! 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 777bcba..a5fd805 100644 --- a/include/hueplusplus/ExtendedColorHueStrategy.h +++ b/include/hueplusplus/ExtendedColorHueStrategy.h @@ -28,39 +28,39 @@ namespace hueplusplus { - //! 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; - }; +//! 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 04dda25..dda748f 100644 --- a/include/hueplusplus/ExtendedColorTemperatureStrategy.h +++ b/include/hueplusplus/ExtendedColorTemperatureStrategy.h @@ -28,28 +28,28 @@ namespace hueplusplus { - //! 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; - }; +//! 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 ad7323f..57f5e65 100644 --- a/include/hueplusplus/Hue.h +++ b/include/hueplusplus/Hue.h @@ -40,246 +40,245 @@ namespace hueplusplus { - // forward declarations - class Hue; +// forward declarations +class Hue; - //! - //! Class to find all Hue bridges on the network and create usernames for them. - //! - class HueFinder +//! +//! Class to find all Hue bridges on the network and create usernames for them. +//! +class HueFinder +{ +public: + struct HueIdentification { - 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; + std::string ip; + int port = 80; + std::string mac; }; - //! Hue class - 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 + //! + //! \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; + + //! \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) { - 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 - }; + 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 c7c343f..405b6b0 100644 --- a/include/hueplusplus/HueCommandAPI.h +++ b/include/hueplusplus/HueCommandAPI.h @@ -32,96 +32,96 @@ namespace hueplusplus { - //! Handles communication to the bridge via IHttpHandler and enforces a timeout - //! between each request - class HueCommandAPI +//! Handles communication to the bridge via IHttpHandler and enforces a timeout +//! between each request +class HueCommandAPI +{ +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 { - 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; + 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; +}; } // namespace hueplusplus #endif \ No newline at end of file diff --git a/include/hueplusplus/HueConfig.h b/include/hueplusplus/HueConfig.h index 83fcc71..b4820e9 100644 --- a/include/hueplusplus/HueConfig.h +++ b/include/hueplusplus/HueConfig.h @@ -25,8 +25,8 @@ 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 +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 347b5a9..f32cca3 100644 --- a/include/hueplusplus/HueDeviceTypes.h +++ b/include/hueplusplus/HueDeviceTypes.h @@ -30,15 +30,15 @@ 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; - }; +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 74eba48..79e0313 100644 --- a/include/hueplusplus/HueException.h +++ b/include/hueplusplus/HueException.h @@ -30,92 +30,92 @@ namespace hueplusplus { - //! \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 +//! \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 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 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; - }; + //! 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/HueLight.h b/include/hueplusplus/HueLight.h index a61bc02..3fd778d 100644 --- a/include/hueplusplus/HueLight.h +++ b/include/hueplusplus/HueLight.h @@ -34,61 +34,61 @@ namespace hueplusplus { - /*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 - }; +/*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 @@ -103,692 +103,691 @@ 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) - { - 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) - { - 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) - { - 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) +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) { - 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 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 (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 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 (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 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 (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 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 (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 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 (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 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 (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->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->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->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->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->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->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->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->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->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->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 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 (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->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->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->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->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->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->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 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 (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) + 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) { - 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) + 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) { - 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) + 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) { - 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 + 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 +}; } // namespace hueplusplus #endif diff --git a/include/hueplusplus/IHttpHandler.h b/include/hueplusplus/IHttpHandler.h index 8d1cfeb..c45b288 100644 --- a/include/hueplusplus/IHttpHandler.h +++ b/include/hueplusplus/IHttpHandler.h @@ -32,168 +32,168 @@ namespace hueplusplus { - //! 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; - }; +//! 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 2a88380..f284ea2 100644 --- a/include/hueplusplus/LinHttpHandler.h +++ b/include/hueplusplus/LinHttpHandler.h @@ -32,32 +32,32 @@ namespace hueplusplus { - //! 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; - }; +//! 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 85cc65b..d2e2c1b 100644 --- a/include/hueplusplus/SimpleBrightnessStrategy.h +++ b/include/hueplusplus/SimpleBrightnessStrategy.h @@ -28,31 +28,31 @@ namespace hueplusplus { - //! 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; - }; +//! 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 61bd502..e13981e 100644 --- a/include/hueplusplus/SimpleColorHueStrategy.h +++ b/include/hueplusplus/SimpleColorHueStrategy.h @@ -28,128 +28,128 @@ namespace hueplusplus { - //! 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; - }; +//! 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 13651aa..17a8148 100644 --- a/include/hueplusplus/SimpleColorTemperatureStrategy.h +++ b/include/hueplusplus/SimpleColorTemperatureStrategy.h @@ -28,42 +28,42 @@ namespace hueplusplus { - //! 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; - }; +//! 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 fbb796b..fe2d7df 100644 --- a/include/hueplusplus/UPnP.h +++ b/include/hueplusplus/UPnP.h @@ -31,19 +31,19 @@ namespace hueplusplus { - //! 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); - }; +//! 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 af5f5a5..eb32e2f 100644 --- a/include/hueplusplus/Units.h +++ b/include/hueplusplus/Units.h @@ -25,39 +25,39 @@ namespace hueplusplus { - 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; - }; +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 ca31d77..d34e297 100644 --- a/include/hueplusplus/Utils.h +++ b/include/hueplusplus/Utils.h @@ -27,60 +27,63 @@ namespace hueplusplus { - namespace utils - { - namespace detail - { - // Forward declaration - template - nlohmann::json safeGetMemberHelper(const nlohmann::json& json, std::size_t index, Paths&&... otherPaths); +namespace utils +{ +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()) - { - return nullptr; - } - return safeGetMemberHelper(*memberIt, std::forward(otherPaths)...); - } +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()) + { + return nullptr; + } + 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) - { - return nullptr; - } - return safeGetMemberHelper(json[index], std::forward(otherPaths)...); - } - } // namespace detail +// 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) + { + return nullptr; + } + return safeGetMemberHelper(json[index], std::forward(otherPaths)...); +} +} // 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 ec470f8..1c7976d 100644 --- a/include/hueplusplus/WinHttpHandler.h +++ b/include/hueplusplus/WinHttpHandler.h @@ -32,41 +32,41 @@ namespace hueplusplus { - //! Class to handle http requests and multicast requests on windows systems - class WinHttpHandler : public BaseHttpHandler - { - 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 e374a46..c02978b 100644 --- a/src/BaseHttpHandler.cpp +++ b/src/BaseHttpHandler.cpp @@ -24,7 +24,8 @@ #include "hueplusplus/HueExceptionMacro.h" -std::string hueplusplus::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"); @@ -68,20 +69,20 @@ std::string hueplusplus::BaseHttpHandler::sendHTTPRequest(const std::string& met return sendGetHTTPBody(request.c_str(), adr, port); } -std::string hueplusplus::BaseHttpHandler::GETString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port) const +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 hueplusplus::BaseHttpHandler::POSTString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port) const +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 hueplusplus::BaseHttpHandler::PUTString(const std::string& uri, const std::string& contentType, const std::string& body, - const std::string& adr, int port) const +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); } diff --git a/src/Hue.cpp b/src/Hue.cpp index fecd25a..b8d8577 100644 --- a/src/Hue.cpp +++ b/src/Hue.cpp @@ -31,9 +31,9 @@ #include #include -#include "hueplusplus/HueDeviceTypes.h" #include "hueplusplus/ExtendedColorHueStrategy.h" #include "hueplusplus/ExtendedColorTemperatureStrategy.h" +#include "hueplusplus/HueDeviceTypes.h" #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/SimpleBrightnessStrategy.h" #include "hueplusplus/SimpleColorHueStrategy.h" @@ -238,9 +238,8 @@ hueplusplus::HueLight& hueplusplus::Hue::getLight(int id) // std::cout << state["lights"][std::to_string(id)] << std::endl; std::string type = state["lights"][std::to_string(id)]["modelid"].get(); // std::cout << type << std::endl; - auto light = MakeHueLight()(type, id, commands, simpleBrightnessStrategy, - extendedColorTemperatureStrategy, simpleColorTemperatureStrategy, extendedColorHueStrategy, - simpleColorHueStrategy); + auto light = MakeHueLight()(type, id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, + simpleColorTemperatureStrategy, extendedColorHueStrategy, simpleColorHueStrategy); lights.emplace(id, light); return lights.find(id)->second; } diff --git a/src/HueCommandAPI.cpp b/src/HueCommandAPI.cpp index e4ac202..b6e6650 100644 --- a/src/HueCommandAPI.cpp +++ b/src/HueCommandAPI.cpp @@ -30,37 +30,36 @@ constexpr std::chrono::steady_clock::duration hueplusplus::HueCommandAPI::minDel namespace { - // Runs functor with appropriate timeout and retries when timed out or connection reset - template - nlohmann::json RunWithTimeout( - std::shared_ptr timeout, std::chrono::steady_clock::duration minDelay, Fun fun) +// Runs functor with appropriate timeout and retries when timed out or connection reset +template +nlohmann::json RunWithTimeout(std::shared_ptr timeout, std::chrono::steady_clock::duration minDelay, Fun fun) +{ + auto now = std::chrono::steady_clock::now(); + std::lock_guard lock(timeout->mutex); + if (timeout->timeout > now) { - auto now = std::chrono::steady_clock::now(); - std::lock_guard lock(timeout->mutex); - if (timeout->timeout > now) - { - std::this_thread::sleep_until(timeout->timeout); - } - try - { - nlohmann::json response = fun(); - timeout->timeout = now + minDelay; - return response; - } - catch (const std::system_error& e) + std::this_thread::sleep_until(timeout->timeout); + } + try + { + nlohmann::json response = fun(); + timeout->timeout = now + minDelay; + return response; + } + catch (const std::system_error& e) + { + if (e.code() == std::errc::connection_reset || e.code() == std::errc::timed_out) { - if (e.code() == std::errc::connection_reset || e.code() == std::errc::timed_out) - { - // Happens when hue is too busy, wait and try again (once) - std::this_thread::sleep_for(minDelay); - nlohmann::json v = fun(); - timeout->timeout = std::chrono::steady_clock::now() + minDelay; - return v; - } - // Cannot recover from other types of errors - throw; + // Happens when hue is too busy, wait and try again (once) + std::this_thread::sleep_for(minDelay); + nlohmann::json v = fun(); + timeout->timeout = std::chrono::steady_clock::now() + minDelay; + return v; } + // Cannot recover from other types of errors + throw; } +} } // namespace hueplusplus::HueCommandAPI::HueCommandAPI( diff --git a/src/HueDeviceTypes.cpp b/src/HueDeviceTypes.cpp index 4f9f688..dabb885 100644 --- a/src/HueDeviceTypes.cpp +++ b/src/HueDeviceTypes.cpp @@ -28,42 +28,42 @@ namespace { - const std::set getGamutBTypes() - { - static const std::set c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES - = {"LCT001", "LCT002", "LCT003", "LCT007", "LLM001"}; - return c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES; - }; +const std::set getGamutBTypes() +{ + static const std::set c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES + = {"LCT001", "LCT002", "LCT003", "LCT007", "LLM001"}; + 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 hueplusplus::MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands, diff --git a/src/HueException.cpp b/src/HueException.cpp index 788c611..dc0a095 100644 --- a/src/HueException.cpp +++ b/src/HueException.cpp @@ -69,7 +69,8 @@ const std::string& hueplusplus::HueAPIResponseException::GetDescription() const return description; } -hueplusplus::HueAPIResponseException hueplusplus::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 +79,8 @@ hueplusplus::HueAPIResponseException hueplusplus::HueAPIResponseException::Creat return HueAPIResponseException(std::move(fileInfo), errorCode, std::move(address), std::move(description)); } -std::string hueplusplus::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(" "); diff --git a/src/HueLight.cpp b/src/HueLight.cpp index e57fbf3..588cf33 100644 --- a/src/HueLight.cpp +++ b/src/HueLight.cpp @@ -161,9 +161,12 @@ bool hueplusplus::HueLight::alert() return utils::validateReplyForLight(request, reply, id); } -hueplusplus::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) +{} -hueplusplus::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), @@ -224,7 +227,8 @@ bool hueplusplus::HueLight::OffNoRefresh(uint8_t transition) return utils::validateReplyForLight(request, reply, id); } -nlohmann::json hueplusplus::HueLight::SendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) +nlohmann::json hueplusplus::HueLight::SendPutRequest( + const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) { return commands.PUTRequest("/lights/" + std::to_string(id) + subPath, request, std::move(fileInfo)); } diff --git a/src/SimpleColorHueStrategy.cpp b/src/SimpleColorHueStrategy.cpp index 776353e..600580e 100644 --- a/src/SimpleColorHueStrategy.cpp +++ b/src/SimpleColorHueStrategy.cpp @@ -93,7 +93,8 @@ bool hueplusplus::SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_ return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::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(); @@ -165,7 +166,8 @@ bool hueplusplus::SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t t return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::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)) { diff --git a/src/SimpleColorTemperatureStrategy.cpp b/src/SimpleColorTemperatureStrategy.cpp index 681e0dc..c3a6041 100644 --- a/src/SimpleColorTemperatureStrategy.cpp +++ b/src/SimpleColorTemperatureStrategy.cpp @@ -30,7 +30,8 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool hueplusplus::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(); diff --git a/src/UPnP.cpp b/src/UPnP.cpp index 5df18b0..7e42e38 100644 --- a/src/UPnP.cpp +++ b/src/UPnP.cpp @@ -25,7 +25,8 @@ #include #include -std::vector> hueplusplus::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/WinHttpHandler.cpp b/src/WinHttpHandler.cpp index 6cbb07c..c393bbe 100644 --- a/src/WinHttpHandler.cpp +++ b/src/WinHttpHandler.cpp @@ -34,24 +34,24 @@ namespace { - class AddrInfoFreer - { - public: - explicit AddrInfoFreer(addrinfo* p) : p(p) {} - ~AddrInfoFreer() { freeaddrinfo(p); } - - private: - addrinfo* p; - }; - class SocketCloser - { - public: - explicit SocketCloser(SOCKET s) : s(s) {} - ~SocketCloser() { closesocket(s); } +class AddrInfoFreer +{ +public: + explicit AddrInfoFreer(addrinfo* p) : p(p) {} + ~AddrInfoFreer() { freeaddrinfo(p); } + +private: + addrinfo* p; +}; +class SocketCloser +{ +public: + explicit SocketCloser(SOCKET s) : s(s) {} + ~SocketCloser() { closesocket(s); } - private: - SOCKET s; - }; +private: + SOCKET s; +}; } // namespace hueplusplus::WinHttpHandler::WinHttpHandler() diff --git a/test/mocks/mock_HueLight.h b/test/mocks/mock_HueLight.h index 3c24bc5..6db85fe 100644 --- a/test/mocks/mock_HueLight.h +++ b/test/mocks/mock_HueLight.h @@ -28,16 +28,16 @@ #include +#include "../testhelper.h" #include "hueplusplus/HueLight.h" #include "json/json.hpp" -#include "../testhelper.h" //! Mock Class class MockHueLight : public hueplusplus::HueLight { public: MockHueLight(std::shared_ptr handler) - : HueLight(1, hueplusplus::HueCommandAPI(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler)) {}; + : HueLight(1, hueplusplus::HueCommandAPI(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler)){}; nlohmann::json& getState() { return state; }; @@ -125,8 +125,8 @@ public: MOCK_METHOD1(OffNoRefresh, bool(uint8_t transition)); - MOCK_METHOD3( - SendPutRequest, nlohmann::json(const nlohmann::json& request, const std::string& subPath, hueplusplus::FileInfo fileInfo)); + MOCK_METHOD3(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 fbafb85..28868f5 100644 --- a/test/test_BaseHttpHandler.cpp +++ b/test/test_BaseHttpHandler.cpp @@ -28,9 +28,9 @@ #include "testhelper.h" +#include "hueplusplus/HueException.h" #include "json/json.hpp" #include "mocks/mock_BaseHttpHandler.h" -#include "hueplusplus/HueException.h" using namespace hueplusplus; diff --git a/test/test_Hue.cpp b/test/test_Hue.cpp index a671192..ae10dc1 100644 --- a/test/test_Hue.cpp +++ b/test/test_Hue.cpp @@ -378,14 +378,14 @@ TEST(Hue, removeLight) { using namespace ::testing; std::shared_ptr handler = std::make_shared(); - nlohmann::json hue_bridge_state{ {"lights", + nlohmann::json hue_bridge_state{{"lights", {{"1", {{"state", {{"on", true}, {"bri", 254}, {"ct", 366}, {"alert", "none"}, {"colormode", "ct"}, {"reachable", true}}}, {"swupdate", {{"state", "noupdates"}, {"lastinstall", nullptr}}}, {"type", "Color temperature light"}, {"name", "Hue ambiance lamp 1"}, {"modelid", "LTW001"}, {"manufacturername", "Philips"}, - {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}} }; + {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}}}; EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .Times(1) @@ -421,14 +421,14 @@ TEST(Hue, getAllLights) { using namespace ::testing; std::shared_ptr handler = std::make_shared(); - nlohmann::json hue_bridge_state{ {"lights", + nlohmann::json hue_bridge_state{{"lights", {{"1", {{"state", {{"on", true}, {"bri", 254}, {"ct", 366}, {"alert", "none"}, {"colormode", "ct"}, {"reachable", true}}}, {"swupdate", {{"state", "noupdates"}, {"lastinstall", nullptr}}}, {"type", "Color temperature light"}, {"name", "Hue ambiance lamp 1"}, {"modelid", "LTW001"}, {"manufacturername", "Philips"}, - {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}} }; + {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}}}; EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -452,14 +452,14 @@ TEST(Hue, lightExists) { using namespace ::testing; std::shared_ptr handler = std::make_shared(); - nlohmann::json hue_bridge_state{ {"lights", + nlohmann::json hue_bridge_state{{"lights", {{"1", {{"state", {{"on", true}, {"bri", 254}, {"ct", 366}, {"alert", "none"}, {"colormode", "ct"}, {"reachable", true}}}, {"swupdate", {{"state", "noupdates"}, {"lastinstall", nullptr}}}, {"type", "Color temperature light"}, {"name", "Hue ambiance lamp 1"}, {"modelid", "LTW001"}, {"manufacturername", "Philips"}, - {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}} }; + {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}}}; EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .Times(AtLeast(2)) @@ -488,14 +488,14 @@ TEST(Hue, getPictureOfLight) { using namespace ::testing; std::shared_ptr handler = std::make_shared(); - nlohmann::json hue_bridge_state{ {"lights", + nlohmann::json hue_bridge_state{{"lights", {{"1", {{"state", {{"on", true}, {"bri", 254}, {"ct", 366}, {"alert", "none"}, {"colormode", "ct"}, {"reachable", true}}}, {"swupdate", {{"state", "noupdates"}, {"lastinstall", nullptr}}}, {"type", "Color temperature light"}, {"name", "Hue ambiance lamp 1"}, {"modelid", "LTW001"}, {"manufacturername", "Philips"}, - {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}} }; + {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}}}; EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) diff --git a/test/test_HueCommandAPI.cpp b/test/test_HueCommandAPI.cpp index 9049ea9..e32efb6 100644 --- a/test/test_HueCommandAPI.cpp +++ b/test/test_HueCommandAPI.cpp @@ -163,7 +163,7 @@ TEST(HueCommandAPI, GETRequest) // api returns error { const std::string path = "/test"; - const nlohmann::json errorResponse{ {"error", {{"type", 10}, {"address", path}, {"description", "Stuff"}}} }; + const nlohmann::json errorResponse{{"error", {{"type", 10}, {"address", path}, {"description", "Stuff"}}}}; EXPECT_CALL(*httpHandler, GETJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)) .WillOnce(Return(errorResponse)); EXPECT_THROW(api.GETRequest(path, request), HueAPIResponseException); @@ -233,7 +233,7 @@ TEST(HueCommandAPI, DELETERequest) // api returns error { const std::string path = "/test"; - const nlohmann::json errorResponse{ {"error", {{"type", 10}, {"address", path}, {"description", "Stuff"}}} }; + const nlohmann::json errorResponse{{"error", {{"type", 10}, {"address", path}, {"description", "Stuff"}}}}; EXPECT_CALL(*httpHandler, DELETEJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), 80)) .WillOnce(Return(errorResponse)); EXPECT_THROW(api.DELETERequest(path, request), HueAPIResponseException); diff --git a/test/test_HueLight.cpp b/test/test_HueLight.cpp index eceba76..59902a6 100644 --- a/test/test_HueLight.cpp +++ b/test/test_HueLight.cpp @@ -123,7 +123,7 @@ protected: .Times(AtLeast(1)) .WillRepeatedly(Return(hue_bridge_state["lights"]["3"])); } - ~HueLightTest() {}; + ~HueLightTest(){}; }; TEST_F(HueLightTest, Constructor)