Commit 984de8e32048631bf3ace565fc6f45ddb4747161

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent 8cffc1b8

Move strategies from Hue to HueLightFactory.

Reduces complexity of the Hue class and reduces number of parameters on factory function.
include/hueplusplus/Hue.h
@@ -35,6 +35,7 @@ @@ -35,6 +35,7 @@
35 #include "ColorTemperatureStrategy.h" 35 #include "ColorTemperatureStrategy.h"
36 #include "Group.h" 36 #include "Group.h"
37 #include "HueCommandAPI.h" 37 #include "HueCommandAPI.h"
  38 +#include "HueDeviceTypes.h"
38 #include "HueLight.h" 39 #include "HueLight.h"
39 #include "IHttpHandler.h" 40 #include "IHttpHandler.h"
40 41
@@ -245,7 +246,7 @@ public: @@ -245,7 +246,7 @@ public:
245 //! \brief Get group specified by id. 246 //! \brief Get group specified by id.
246 //! \param id ID of the group. 247 //! \param id ID of the group.
247 //! \returns Group that can be controlled. 248 //! \returns Group that can be controlled.
248 - //! \note Every bridge has a special group 0 which contains all lights 249 + //! \note Every bridge has a special group 0 which contains all lights
249 //! and is not visible to getAllGroups(). 250 //! and is not visible to getAllGroups().
250 //! \throws std::system_error when system or socket operations fail 251 //! \throws std::system_error when system or socket operations fail
251 //! \throws HueException when id does not exist 252 //! \throws HueException when id does not exist
@@ -318,21 +319,11 @@ private: @@ -318,21 +319,11 @@ private:
318 std::map<uint8_t, HueLight> lights; //!< Maps ids to HueLights that are controlled by this bridge 319 std::map<uint8_t, HueLight> lights; //!< Maps ids to HueLights that are controlled by this bridge
319 std::map<uint8_t, Group> groups; //!< Maps ids to Groups 320 std::map<uint8_t, Group> groups; //!< Maps ids to Groups
320 321
321 - std::shared_ptr<BrightnessStrategy> simpleBrightnessStrategy; //!< Strategy that is used for controlling the  
322 - //!< brightness of lights  
323 - std::shared_ptr<ColorHueStrategy> simpleColorHueStrategy; //!< Strategy that is used for controlling the  
324 - //!< color of lights  
325 - std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy; //!< Strategy that is used for controlling the  
326 - //!< color of lights  
327 - std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy; //!< Strategy that is used for controlling  
328 - //!< the color temperature of lights  
329 - std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy; //!< Strategy that is used for  
330 - //!< controlling the color  
331 - //!< temperature of lights  
332 std::shared_ptr<const IHttpHandler> http_handler; //!< A IHttpHandler that is used to communicate with the 322 std::shared_ptr<const IHttpHandler> http_handler; //!< A IHttpHandler that is used to communicate with the
333 //!< bridge 323 //!< bridge
334 HueCommandAPI commands; //!< A HueCommandAPI that is used to communicate with the bridge 324 HueCommandAPI commands; //!< A HueCommandAPI that is used to communicate with the bridge
335 APICache stateCache; //!< The state of the hue bridge as it is returned from it 325 APICache stateCache; //!< The state of the hue bridge as it is returned from it
  326 + HueLightFactory lightFactory;
336 }; 327 };
337 } // namespace hueplusplus 328 } // namespace hueplusplus
338 329
include/hueplusplus/HueDeviceTypes.h
@@ -30,14 +30,20 @@ @@ -30,14 +30,20 @@
30 30
31 namespace hueplusplus 31 namespace hueplusplus
32 { 32 {
33 -struct MakeHueLight 33 +class HueLightFactory
34 { 34 {
35 - auto operator()(std::string type, int id, HueCommandAPI commands,  
36 - std::shared_ptr<BrightnessStrategy> simpleBrightnessStrategy,  
37 - std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy,  
38 - std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy,  
39 - std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy,  
40 - std::shared_ptr<ColorHueStrategy> simpleColorHueStrategy) -> HueLight; 35 +public:
  36 + HueLightFactory(const HueCommandAPI& commands);
  37 +
  38 + HueLight createLight(const std::string& type, int id);
  39 +
  40 +private:
  41 + HueCommandAPI commands;
  42 + std::shared_ptr<BrightnessStrategy> simpleBrightness;
  43 + std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperature;
  44 + std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperature;
  45 + std::shared_ptr<ColorHueStrategy> simpleColorHue;
  46 + std::shared_ptr<ColorHueStrategy> extendedColorHue;
41 }; 47 };
42 } // namespace hueplusplus 48 } // namespace hueplusplus
43 49
include/hueplusplus/HueLight.h
@@ -97,8 +97,7 @@ enum class ColorType @@ -97,8 +97,7 @@ enum class ColorType
97 //! Provides methods to query and control lights. 97 //! Provides methods to query and control lights.
98 class HueLight 98 class HueLight
99 { 99 {
100 - friend class Hue;  
101 - friend struct MakeHueLight; 100 + friend class HueLightFactory;
102 friend class SimpleBrightnessStrategy; 101 friend class SimpleBrightnessStrategy;
103 friend class SimpleColorHueStrategy; 102 friend class SimpleColorHueStrategy;
104 friend class ExtendedColorHueStrategy; 103 friend class ExtendedColorHueStrategy;
src/Hue.cpp
@@ -31,13 +31,7 @@ @@ -31,13 +31,7 @@
31 #include <stdexcept> 31 #include <stdexcept>
32 #include <thread> 32 #include <thread>
33 33
34 -#include "hueplusplus/ExtendedColorHueStrategy.h"  
35 -#include "hueplusplus/ExtendedColorTemperatureStrategy.h"  
36 -#include "hueplusplus/HueDeviceTypes.h"  
37 #include "hueplusplus/HueExceptionMacro.h" 34 #include "hueplusplus/HueExceptionMacro.h"
38 -#include "hueplusplus/SimpleBrightnessStrategy.h"  
39 -#include "hueplusplus/SimpleColorHueStrategy.h"  
40 -#include "hueplusplus/SimpleColorTemperatureStrategy.h"  
41 #include "hueplusplus/UPnP.h" 35 #include "hueplusplus/UPnP.h"
42 #include "hueplusplus/Utils.h" 36 #include "hueplusplus/Utils.h"
43 37
@@ -140,14 +134,10 @@ Hue::Hue(const std::string&amp; ip, const int port, const std::string&amp; username, @@ -140,14 +134,10 @@ Hue::Hue(const std::string&amp; ip, const int port, const std::string&amp; username,
140 : ip(ip), 134 : ip(ip),
141 port(port), 135 port(port),
142 username(username), 136 username(username),
143 - simpleBrightnessStrategy(std::make_shared<SimpleBrightnessStrategy>()),  
144 - simpleColorHueStrategy(std::make_shared<SimpleColorHueStrategy>()),  
145 - extendedColorHueStrategy(std::make_shared<ExtendedColorHueStrategy>()),  
146 - simpleColorTemperatureStrategy(std::make_shared<SimpleColorTemperatureStrategy>()),  
147 - extendedColorTemperatureStrategy(std::make_shared<ExtendedColorTemperatureStrategy>()),  
148 http_handler(std::move(handler)), 137 http_handler(std::move(handler)),
149 commands(ip, port, username, http_handler), 138 commands(ip, port, username, http_handler),
150 - stateCache("", commands, refreshDuration) 139 + stateCache("", commands, refreshDuration),
  140 + lightFactory(commands)
151 {} 141 {}
152 142
153 std::string Hue::getBridgeIP() 143 std::string Hue::getBridgeIP()
@@ -191,6 +181,7 @@ std::string Hue::requestUsername() @@ -191,6 +181,7 @@ std::string Hue::requestUsername()
191 // Update commands with new username and ip 181 // Update commands with new username and ip
192 commands = HueCommandAPI(ip, port, username, http_handler); 182 commands = HueCommandAPI(ip, port, username, http_handler);
193 stateCache = APICache("", commands, stateCache.getRefreshDuration()); 183 stateCache = APICache("", commands, stateCache.getRefreshDuration());
  184 + lightFactory = HueLightFactory(commands);
194 std::cout << "Success! Link button was pressed!\n"; 185 std::cout << "Success! Link button was pressed!\n";
195 std::cout << "Username is \"" << username << "\"\n"; 186 std::cout << "Username is \"" << username << "\"\n";
196 break; 187 break;
@@ -231,7 +222,7 @@ HueLight&amp; Hue::getLight(int id) @@ -231,7 +222,7 @@ HueLight&amp; Hue::getLight(int id)
231 auto pos = lights.find(id); 222 auto pos = lights.find(id);
232 if (pos != lights.end()) 223 if (pos != lights.end())
233 { 224 {
234 - pos->second.state.refresh(); 225 + pos->second.refresh();
235 return pos->second; 226 return pos->second;
236 } 227 }
237 const nlohmann::json& lightsCache = stateCache.getValue()["lights"]; 228 const nlohmann::json& lightsCache = stateCache.getValue()["lights"];
@@ -241,8 +232,7 @@ HueLight&amp; Hue::getLight(int id) @@ -241,8 +232,7 @@ HueLight&amp; Hue::getLight(int id)
241 throw HueException(CURRENT_FILE_INFO, "Light id is not valid"); 232 throw HueException(CURRENT_FILE_INFO, "Light id is not valid");
242 } 233 }
243 std::string type = lightsCache[std::to_string(id)]["modelid"].get<std::string>(); 234 std::string type = lightsCache[std::to_string(id)]["modelid"].get<std::string>();
244 - auto light = MakeHueLight()(type, id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy,  
245 - simpleColorTemperatureStrategy, extendedColorHueStrategy, simpleColorHueStrategy); 235 + auto light = lightFactory.createLight(type, id);
246 lights.emplace(id, light); 236 lights.emplace(id, light);
247 return lights.find(id)->second; 237 return lights.find(id)->second;
248 } 238 }
@@ -534,5 +524,6 @@ void Hue::setHttpHandler(std::shared_ptr&lt;const IHttpHandler&gt; handler) @@ -534,5 +524,6 @@ void Hue::setHttpHandler(std::shared_ptr&lt;const IHttpHandler&gt; handler)
534 http_handler = handler; 524 http_handler = handler;
535 commands = HueCommandAPI(ip, port, username, handler); 525 commands = HueCommandAPI(ip, port, username, handler);
536 stateCache = APICache("", commands, stateCache.getRefreshDuration()); 526 stateCache = APICache("", commands, stateCache.getRefreshDuration());
  527 + lightFactory = HueLightFactory(commands);
537 } 528 }
538 } // namespace hueplusplus 529 } // namespace hueplusplus
src/HueDeviceTypes.cpp
@@ -24,7 +24,13 @@ @@ -24,7 +24,13 @@
24 24
25 #include <set> 25 #include <set>
26 26
  27 +#include "hueplusplus/ExtendedColorHueStrategy.h"
  28 +#include "hueplusplus/ExtendedColorTemperatureStrategy.h"
  29 +#include "hueplusplus/HueDeviceTypes.h"
27 #include "hueplusplus/HueExceptionMacro.h" 30 #include "hueplusplus/HueExceptionMacro.h"
  31 +#include "hueplusplus/SimpleBrightnessStrategy.h"
  32 +#include "hueplusplus/SimpleColorHueStrategy.h"
  33 +#include "hueplusplus/SimpleColorTemperatureStrategy.h"
28 34
29 namespace hueplusplus 35 namespace hueplusplus
30 { 36 {
@@ -60,8 +66,7 @@ const std::set&lt;std::string&gt;&amp; getNoColorTypes() @@ -60,8 +66,7 @@ const std::set&lt;std::string&gt;&amp; getNoColorTypes()
60 66
61 const std::set<std::string>& getNonDimmableTypes() 67 const std::set<std::string>& getNonDimmableTypes()
62 { 68 {
63 - static const std::set<std::string> c_NON_DIMMABLE_TYPES  
64 - = {"Plug 01"}; 69 + static const std::set<std::string> c_NON_DIMMABLE_TYPES = {"Plug 01"};
65 return c_NON_DIMMABLE_TYPES; 70 return c_NON_DIMMABLE_TYPES;
66 } 71 }
67 72
@@ -75,52 +80,50 @@ const std::set&lt;std::string&gt;&amp; getTemperatureLightTypes() @@ -75,52 +80,50 @@ const std::set&lt;std::string&gt;&amp; getTemperatureLightTypes()
75 } 80 }
76 } // namespace 81 } // namespace
77 82
78 -auto MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands,  
79 - std::shared_ptr<BrightnessStrategy> simpleBrightnessStrategy,  
80 - std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy,  
81 - std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy,  
82 - std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy,  
83 - std::shared_ptr<ColorHueStrategy> simpleColorHueStrategy) -> HueLight 83 +HueLightFactory::HueLightFactory(const HueCommandAPI& commands)
  84 + : commands(commands),
  85 + simpleBrightness(std::make_shared<SimpleBrightnessStrategy>()),
  86 + simpleColorHue(std::make_shared<SimpleColorHueStrategy>()),
  87 + extendedColorHue(std::make_shared<ExtendedColorHueStrategy>()),
  88 + simpleColorTemperature(std::make_shared<SimpleColorTemperatureStrategy>()),
  89 + extendedColorTemperature(std::make_shared<ExtendedColorTemperatureStrategy>())
  90 +{}
  91 +
  92 +HueLight HueLightFactory::createLight(const std::string& type, int id)
84 { 93 {
85 if (getGamutBTypes().count(type)) 94 if (getGamutBTypes().count(type))
86 { 95 {
87 - auto light = HueLight(  
88 - id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); 96 + auto light = HueLight(id, commands, simpleBrightness, extendedColorTemperature, extendedColorHue);
89 light.colorType = ColorType::GAMUT_B; 97 light.colorType = ColorType::GAMUT_B;
90 return light; 98 return light;
91 } 99 }
92 -  
93 else if (getGamutCTypes().count(type)) 100 else if (getGamutCTypes().count(type))
94 { 101 {
95 - auto light = HueLight(  
96 - id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy); 102 + auto light = HueLight(id, commands, simpleBrightness, extendedColorTemperature, extendedColorHue);
97 light.colorType = ColorType::GAMUT_C; 103 light.colorType = ColorType::GAMUT_C;
98 return light; 104 return light;
99 } 105 }
100 -  
101 else if (getGamutATypes().count(type)) 106 else if (getGamutATypes().count(type))
102 { 107 {
103 - auto light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, simpleColorHueStrategy); 108 + auto light = HueLight(id, commands, simpleBrightness, nullptr, simpleColorHue);
104 light.colorType = ColorType::GAMUT_A; 109 light.colorType = ColorType::GAMUT_A;
105 return light; 110 return light;
106 } 111 }
107 -  
108 else if (getNoColorTypes().count(type)) 112 else if (getNoColorTypes().count(type))
109 { 113 {
110 - auto light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, nullptr); 114 + auto light = HueLight(id, commands, simpleBrightness, nullptr, nullptr);
111 light.colorType = ColorType::NONE; 115 light.colorType = ColorType::NONE;
112 return light; 116 return light;
113 } 117 }
114 -  
115 - else if (getNonDimmableTypes().count(type)) { 118 + else if (getNonDimmableTypes().count(type))
  119 + {
116 auto light = HueLight(id, commands); 120 auto light = HueLight(id, commands);
117 light.colorType = ColorType::NONE; 121 light.colorType = ColorType::NONE;
118 return light; 122 return light;
119 } 123 }
120 -  
121 else if (getTemperatureLightTypes().count(type)) 124 else if (getTemperatureLightTypes().count(type))
122 { 125 {
123 - auto light = HueLight(id, commands, simpleBrightnessStrategy, simpleColorTemperatureStrategy, nullptr); 126 + auto light = HueLight(id, commands, simpleBrightness, simpleColorTemperature, nullptr);
124 light.colorType = ColorType::TEMPERATURE; 127 light.colorType = ColorType::TEMPERATURE;
125 return light; 128 return light;
126 } 129 }