From 984de8e32048631bf3ace565fc6f45ddb4747161 Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Mon, 20 Apr 2020 20:40:15 +0200 Subject: [PATCH] Move strategies from Hue to HueLightFactory. --- include/hueplusplus/Hue.h | 15 +++------------ include/hueplusplus/HueDeviceTypes.h | 20 +++++++++++++------- include/hueplusplus/HueLight.h | 3 +-- src/Hue.cpp | 21 ++++++--------------- src/HueDeviceTypes.cpp | 45 ++++++++++++++++++++++++--------------------- 5 files changed, 47 insertions(+), 57 deletions(-) diff --git a/include/hueplusplus/Hue.h b/include/hueplusplus/Hue.h index ad63a10..e14efea 100644 --- a/include/hueplusplus/Hue.h +++ b/include/hueplusplus/Hue.h @@ -35,6 +35,7 @@ #include "ColorTemperatureStrategy.h" #include "Group.h" #include "HueCommandAPI.h" +#include "HueDeviceTypes.h" #include "HueLight.h" #include "IHttpHandler.h" @@ -245,7 +246,7 @@ public: //! \brief Get group specified by id. //! \param id ID of the group. //! \returns Group that can be controlled. - //! \note Every bridge has a special group 0 which contains all lights + //! \note Every bridge has a special group 0 which contains all lights //! and is not visible to getAllGroups(). //! \throws std::system_error when system or socket operations fail //! \throws HueException when id does not exist @@ -318,21 +319,11 @@ private: std::map lights; //!< Maps ids to HueLights that are controlled by this bridge std::map groups; //!< Maps ids to Groups - 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 APICache stateCache; //!< The state of the hue bridge as it is returned from it + HueLightFactory lightFactory; }; } // namespace hueplusplus diff --git a/include/hueplusplus/HueDeviceTypes.h b/include/hueplusplus/HueDeviceTypes.h index f32cca3..c7dc455 100644 --- a/include/hueplusplus/HueDeviceTypes.h +++ b/include/hueplusplus/HueDeviceTypes.h @@ -30,14 +30,20 @@ namespace hueplusplus { -struct MakeHueLight +class HueLightFactory { - 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; +public: + HueLightFactory(const HueCommandAPI& commands); + + HueLight createLight(const std::string& type, int id); + +private: + HueCommandAPI commands; + std::shared_ptr simpleBrightness; + std::shared_ptr simpleColorTemperature; + std::shared_ptr extendedColorTemperature; + std::shared_ptr simpleColorHue; + std::shared_ptr extendedColorHue; }; } // namespace hueplusplus diff --git a/include/hueplusplus/HueLight.h b/include/hueplusplus/HueLight.h index 2b5b4b5..ecaee77 100644 --- a/include/hueplusplus/HueLight.h +++ b/include/hueplusplus/HueLight.h @@ -97,8 +97,7 @@ enum class ColorType //! Provides methods to query and control lights. class HueLight { - friend class Hue; - friend struct MakeHueLight; + friend class HueLightFactory; friend class SimpleBrightnessStrategy; friend class SimpleColorHueStrategy; friend class ExtendedColorHueStrategy; diff --git a/src/Hue.cpp b/src/Hue.cpp index 9088643..bb040e2 100644 --- a/src/Hue.cpp +++ b/src/Hue.cpp @@ -31,13 +31,7 @@ #include #include -#include "hueplusplus/ExtendedColorHueStrategy.h" -#include "hueplusplus/ExtendedColorTemperatureStrategy.h" -#include "hueplusplus/HueDeviceTypes.h" #include "hueplusplus/HueExceptionMacro.h" -#include "hueplusplus/SimpleBrightnessStrategy.h" -#include "hueplusplus/SimpleColorHueStrategy.h" -#include "hueplusplus/SimpleColorTemperatureStrategy.h" #include "hueplusplus/UPnP.h" #include "hueplusplus/Utils.h" @@ -140,14 +134,10 @@ Hue::Hue(const std::string& ip, const int port, const std::string& username, : ip(ip), port(port), username(username), - simpleBrightnessStrategy(std::make_shared()), - simpleColorHueStrategy(std::make_shared()), - extendedColorHueStrategy(std::make_shared()), - simpleColorTemperatureStrategy(std::make_shared()), - extendedColorTemperatureStrategy(std::make_shared()), http_handler(std::move(handler)), commands(ip, port, username, http_handler), - stateCache("", commands, refreshDuration) + stateCache("", commands, refreshDuration), + lightFactory(commands) {} std::string Hue::getBridgeIP() @@ -191,6 +181,7 @@ std::string Hue::requestUsername() // Update commands with new username and ip commands = HueCommandAPI(ip, port, username, http_handler); stateCache = APICache("", commands, stateCache.getRefreshDuration()); + lightFactory = HueLightFactory(commands); std::cout << "Success! Link button was pressed!\n"; std::cout << "Username is \"" << username << "\"\n"; break; @@ -231,7 +222,7 @@ HueLight& Hue::getLight(int id) auto pos = lights.find(id); if (pos != lights.end()) { - pos->second.state.refresh(); + pos->second.refresh(); return pos->second; } const nlohmann::json& lightsCache = stateCache.getValue()["lights"]; @@ -241,8 +232,7 @@ HueLight& Hue::getLight(int id) throw HueException(CURRENT_FILE_INFO, "Light id is not valid"); } std::string type = lightsCache[std::to_string(id)]["modelid"].get(); - auto light = MakeHueLight()(type, id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, - simpleColorTemperatureStrategy, extendedColorHueStrategy, simpleColorHueStrategy); + auto light = lightFactory.createLight(type, id); lights.emplace(id, light); return lights.find(id)->second; } @@ -534,5 +524,6 @@ void Hue::setHttpHandler(std::shared_ptr handler) http_handler = handler; commands = HueCommandAPI(ip, port, username, handler); stateCache = APICache("", commands, stateCache.getRefreshDuration()); + lightFactory = HueLightFactory(commands); } } // namespace hueplusplus diff --git a/src/HueDeviceTypes.cpp b/src/HueDeviceTypes.cpp index f8d69ce..bff961e 100644 --- a/src/HueDeviceTypes.cpp +++ b/src/HueDeviceTypes.cpp @@ -24,7 +24,13 @@ #include +#include "hueplusplus/ExtendedColorHueStrategy.h" +#include "hueplusplus/ExtendedColorTemperatureStrategy.h" +#include "hueplusplus/HueDeviceTypes.h" #include "hueplusplus/HueExceptionMacro.h" +#include "hueplusplus/SimpleBrightnessStrategy.h" +#include "hueplusplus/SimpleColorHueStrategy.h" +#include "hueplusplus/SimpleColorTemperatureStrategy.h" namespace hueplusplus { @@ -60,8 +66,7 @@ const std::set& getNoColorTypes() const std::set& getNonDimmableTypes() { - static const std::set c_NON_DIMMABLE_TYPES - = {"Plug 01"}; + static const std::set c_NON_DIMMABLE_TYPES = {"Plug 01"}; return c_NON_DIMMABLE_TYPES; } @@ -75,52 +80,50 @@ const std::set& getTemperatureLightTypes() } } // namespace -auto MakeHueLight::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 +HueLightFactory::HueLightFactory(const HueCommandAPI& commands) + : commands(commands), + simpleBrightness(std::make_shared()), + simpleColorHue(std::make_shared()), + extendedColorHue(std::make_shared()), + simpleColorTemperature(std::make_shared()), + extendedColorTemperature(std::make_shared()) +{} + +HueLight HueLightFactory::createLight(const std::string& type, int id) { if (getGamutBTypes().count(type)) { - auto light = HueLight( - id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); + auto light = HueLight(id, commands, simpleBrightness, extendedColorTemperature, extendedColorHue); light.colorType = ColorType::GAMUT_B; return light; } - else if (getGamutCTypes().count(type)) { - auto light = HueLight( - id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); + auto light = HueLight(id, commands, simpleBrightness, extendedColorTemperature, extendedColorHue); light.colorType = ColorType::GAMUT_C; return light; } - else if (getGamutATypes().count(type)) { - auto light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, simpleColorHueStrategy); + auto light = HueLight(id, commands, simpleBrightness, nullptr, simpleColorHue); light.colorType = ColorType::GAMUT_A; return light; } - else if (getNoColorTypes().count(type)) { - auto light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, nullptr); + auto light = HueLight(id, commands, simpleBrightness, nullptr, nullptr); light.colorType = ColorType::NONE; return light; } - - else if (getNonDimmableTypes().count(type)) { + else if (getNonDimmableTypes().count(type)) + { auto light = HueLight(id, commands); light.colorType = ColorType::NONE; return light; } - else if (getTemperatureLightTypes().count(type)) { - auto light = HueLight(id, commands, simpleBrightnessStrategy, simpleColorTemperatureStrategy, nullptr); + auto light = HueLight(id, commands, simpleBrightness, simpleColorTemperature, nullptr); light.colorType = ColorType::TEMPERATURE; return light; } -- libgit2 0.21.4