Commit 18b84291a0d87d88542600a9af0714ad647cb173

Authored by Moritz Wirger
1 parent cd25b44c

add new ctor, change naming of variables and use shared_ptr of const Strategies

hueplusplus/HueLight.cpp
@@ -78,13 +78,17 @@ bool HueLight::alert() @@ -78,13 +78,17 @@ bool HueLight::alert()
78 return false; 78 return false;
79 } 79 }
80 80
81 -HueLight::HueLight(const std::string& ip, const std::string& username, int id) :  
82 -ip(ip),  
83 -username(username),  
84 -id(id),  
85 -_brightnessStrategy(nullptr),  
86 -_colorTemperatureStrategy(nullptr),  
87 -_colorHueStategy(nullptr) 81 +HueLight::HueLight(const std::string& ip, const std::string& username, int id)
  82 + : HueLight(ip, username, id, nullptr, nullptr, nullptr)
  83 +{}
  84 +
  85 +HueLight::HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr<const BrightnessStrategy> brightnessStrategy, std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy, std::shared_ptr<const ColorHueStrategy> colorHueStrategy)
  86 + : ip(ip),
  87 + username(username),
  88 + id(id),
  89 + brightnessStrategy(std::move(brightnessStrategy)),
  90 + colorTemperatureStrategy(std::move(colorTempStrategy)),
  91 + colorHueStrategy(std::move(colorHueStrategy))
88 { 92 {
89 refreshState(); 93 refreshState();
90 } 94 }
hueplusplus/include/HueLight.h
@@ -83,6 +83,9 @@ enum ColorType @@ -83,6 +83,9 @@ enum ColorType
83 GAMUT_C_TEMPERATURE 83 GAMUT_C_TEMPERATURE
84 }; 84 };
85 85
  86 +//!
  87 +//! Class for Hue Light fixtures
  88 +//!
86 class HueLight 89 class HueLight
87 { 90 {
88 friend class Hue; 91 friend class Hue;
@@ -132,10 +135,11 @@ public: @@ -132,10 +135,11 @@ public:
132 //! \return Bool that is true on success 135 //! \return Bool that is true on success
133 bool setBrightness(unsigned int bri, uint8_t transition = 4) 136 bool setBrightness(unsigned int bri, uint8_t transition = 4)
134 { 137 {
135 - if (_brightnessStrategy) 138 + if (brightnessStrategy)
136 { 139 {
137 - return (*_brightnessStrategy).setBrightness(bri, transition, *this); 140 + return brightnessStrategy->setBrightness(bri, transition, *this);
138 } 141 }
  142 + return false;
139 }; 143 };
140 144
141 //! Fucntion that sets the color temperature of this light in mired if the 145 //! Fucntion that sets the color temperature of this light in mired if the
@@ -146,10 +150,11 @@ public: @@ -146,10 +150,11 @@ public:
146 //! \return Bool that is true on success 150 //! \return Bool that is true on success
147 bool setColorTemperature(unsigned int mired, uint8_t transition = 4) 151 bool setColorTemperature(unsigned int mired, uint8_t transition = 4)
148 { 152 {
149 - if (_colorTemperatureStrategy) 153 + if (colorTemperatureStrategy)
150 { 154 {
151 - return (*_colorTemperatureStrategy).setColorTemperature(mired, transition, *this); 155 + return colorTemperatureStrategy->setColorTemperature(mired, transition, *this);
152 } 156 }
  157 + return false;
153 }; 158 };
154 159
155 //! Function to set the color of this light with specified hue if the 160 //! Function to set the color of this light with specified hue if the
@@ -160,10 +165,11 @@ public: @@ -160,10 +165,11 @@ public:
160 //! \return Bool that is true on success 165 //! \return Bool that is true on success
161 bool setColorHue(uint16_t hue, uint8_t transition = 4) 166 bool setColorHue(uint16_t hue, uint8_t transition = 4)
162 { 167 {
163 - if(_colorHueStategy) 168 + if(colorHueStrategy)
164 { 169 {
165 - return (*_colorHueStategy).setColorHue(hue, transition, *this); 170 + return colorHueStrategy->setColorHue(hue, transition, *this);
166 } 171 }
  172 + return false;
167 }; 173 };
168 174
169 //! Function to set the saturation of color of this light with specified saturation if the 175 //! Function to set the saturation of color of this light with specified saturation if the
@@ -174,10 +180,11 @@ public: @@ -174,10 +180,11 @@ public:
174 //! \return Bool that is true on success 180 //! \return Bool that is true on success
175 bool setColorSaturation(uint8_t sat, uint8_t transition = 4) 181 bool setColorSaturation(uint8_t sat, uint8_t transition = 4)
176 { 182 {
177 - if(_colorHueStategy) 183 + if(colorHueStrategy)
178 { 184 {
179 - return (*_colorHueStategy).setColorSaturation(sat, transition, *this); 185 + return colorHueStrategy->setColorSaturation(sat, transition, *this);
180 } 186 }
  187 + return false;
181 }; 188 };
182 189
183 //! Function to set the color of this light with specified hue and saturation if the 190 //! Function to set the color of this light with specified hue and saturation if the
@@ -188,10 +195,11 @@ public: @@ -188,10 +195,11 @@ public:
188 //! \return Bool that is true on success 195 //! \return Bool that is true on success
189 bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4) 196 bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4)
190 { 197 {
191 - if(_colorHueStategy) 198 + if(colorHueStrategy)
192 { 199 {
193 - return (*_colorHueStategy).setColorHueSaturation(hue, sat, transition, *this); 200 + return colorHueStrategy->setColorHueSaturation(hue, sat, transition, *this);
194 } 201 }
  202 + return false;
195 }; 203 };
196 204
197 //! Function to set the color of this light in CIE with specified x y if the 205 //! Function to set the color of this light in CIE with specified x y if the
@@ -203,10 +211,11 @@ public: @@ -203,10 +211,11 @@ public:
203 //! \return Bool that is true on success 211 //! \return Bool that is true on success
204 bool setColorXY(float x, float y, uint8_t transition = 4) 212 bool setColorXY(float x, float y, uint8_t transition = 4)
205 { 213 {
206 - if(_colorHueStategy) 214 + if(colorHueStrategy)
207 { 215 {
208 - return (*_colorHueStategy).setColorXY(x, y, transition, *this); 216 + return colorHueStrategy->setColorXY(x, y, transition, *this);
209 } 217 }
  218 + return false;
210 }; 219 };
211 220
212 //! Function to set the color of this light with red green and blue values if the 221 //! Function to set the color of this light with red green and blue values if the
@@ -219,10 +228,11 @@ public: @@ -219,10 +228,11 @@ public:
219 //! \return Bool that is true on success 228 //! \return Bool that is true on success
220 bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition = 4) 229 bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition = 4)
221 { 230 {
222 - if(_colorHueStategy) 231 + if(colorHueStrategy)
223 { 232 {
224 - return (*_colorHueStategy).setColorRGB(r, g, b, transition, *this); 233 + return colorHueStrategy->setColorRGB(r, g, b, transition, *this);
225 } 234 }
  235 + return false;
226 }; 236 };
227 237
228 //! Function that lets the light perform one breath cycle. 238 //! Function that lets the light perform one breath cycle.
@@ -236,10 +246,11 @@ public: @@ -236,10 +246,11 @@ public:
236 //! \return Bool that is true on success 246 //! \return Bool that is true on success
237 bool alertTemperature(unsigned int mired) 247 bool alertTemperature(unsigned int mired)
238 { 248 {
239 - if(_colorTemperatureStrategy) 249 + if(colorTemperatureStrategy)
240 { 250 {
241 - return (*_colorTemperatureStrategy).alertTemperature(mired, *this); 251 + return colorTemperatureStrategy->alertTemperature(mired, *this);
242 } 252 }
  253 + return false;
243 }; 254 };
244 255
245 //! Function that lets the light perform one breath cycle in specified color if the 256 //! Function that lets the light perform one breath cycle in specified color if the
@@ -250,10 +261,11 @@ public: @@ -250,10 +261,11 @@ public:
250 //! \return Bool that is true on success 261 //! \return Bool that is true on success
251 bool alertHueSaturation(uint16_t hue, uint8_t sat) 262 bool alertHueSaturation(uint16_t hue, uint8_t sat)
252 { 263 {
253 - if(_colorHueStategy) 264 + if(colorHueStrategy)
254 { 265 {
255 - return (*_colorHueStategy).alertHueSaturation(hue, sat, *this); 266 + return colorHueStrategy->alertHueSaturation(hue, sat, *this);
256 } 267 }
  268 + return false;
257 }; 269 };
258 270
259 //! Function that lets the light perform one breath cycle in specified color if the 271 //! Function that lets the light perform one breath cycle in specified color if the
@@ -265,10 +277,11 @@ public: @@ -265,10 +277,11 @@ public:
265 //! \return Bool that is true on success 277 //! \return Bool that is true on success
266 bool alertXY(float x, float y) 278 bool alertXY(float x, float y)
267 { 279 {
268 - if(_colorHueStategy) 280 + if(colorHueStrategy)
269 { 281 {
270 - return (*_colorHueStategy).alertXY(x, y, *this); 282 + return colorHueStrategy->alertXY(x, y, *this);
271 } 283 }
  284 + return false;
272 }; 285 };
273 286
274 //! Function that lets the light perform one breath cycle in specified color if the 287 //! Function that lets the light perform one breath cycle in specified color if the
@@ -281,10 +294,11 @@ public: @@ -281,10 +294,11 @@ public:
281 //! \return Bool that is true on success 294 //! \return Bool that is true on success
282 bool alertRGB(uint8_t r, uint8_t g, uint8_t b) 295 bool alertRGB(uint8_t r, uint8_t g, uint8_t b)
283 { 296 {
284 - if(_colorHueStategy) 297 + if(colorHueStrategy)
285 { 298 {
286 - return (*_colorHueStategy).alertRGB(r, g, b, *this); 299 + return colorHueStrategy->alertRGB(r, g, b, *this);
287 } 300 }
  301 + return false;
288 }; 302 };
289 303
290 //! Function to enable colorloop effect if the 304 //! Function to enable colorloop effect if the
@@ -298,10 +312,11 @@ public: @@ -298,10 +312,11 @@ public:
298 //! \return Bool that is true on success 312 //! \return Bool that is true on success
299 bool setColorLoop(bool on) 313 bool setColorLoop(bool on)
300 { 314 {
301 - if(_colorHueStategy) 315 + if(colorHueStrategy)
302 { 316 {
303 - return (*_colorHueStategy).setColorLoop(on, *this); 317 + return colorHueStrategy->setColorLoop(on, *this);
304 } 318 }
  319 + return false;
305 }; 320 };
306 321
307 protected: 322 protected:
@@ -309,22 +324,33 @@ protected: @@ -309,22 +324,33 @@ protected:
309 //! \param ip String that specifies the ip of the Hue bridge 324 //! \param ip String that specifies the ip of the Hue bridge
310 //! \param username String that specifies the username used to control the bridge 325 //! \param username String that specifies the username used to control the bridge
311 //! \param id Integer that specifies the id of this light 326 //! \param id Integer that specifies the id of this light
  327 + //!
  328 + //! leaves strategies unset
312 HueLight(const std::string& ip, const std::string& username, int id); 329 HueLight(const std::string& ip, const std::string& username, int id);
  330 +
  331 + //! protected ctor that is used by \ref Hue class, also sets strategies.
  332 + //! \param ip String that specifies the ip of the Hue bridge
  333 + //! \param username String that specifies the username used to control the bridge
  334 + //! \param id Integer that specifies the id of this light
  335 + //! \param brightnessStrategy Strategy for brightness. May be nullptr.
  336 + //! \param colorTempStrategy Strategy for color temperature. May be nullptr.
  337 + //! \param colorHueStrategy Strategy for color hue/saturation. May be nullptr.
  338 + HueLight(const std::string& ip, const std::string& username, int id, std::shared_ptr<const BrightnessStrategy> brightnessStrategy, std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy, std::shared_ptr<const ColorHueStrategy> colorHueStrategy);
313 339
314 //! protected function that sets the brightness strategy which defines how 340 //! protected function that sets the brightness strategy which defines how
315 //! specific commands that deal with brightness control are executed 341 //! specific commands that deal with brightness control are executed
316 //! \param strat a strategy of type \ref BrightnessStrategy 342 //! \param strat a strategy of type \ref BrightnessStrategy
317 - void setBrightnessStrategy(std::shared_ptr<BrightnessStrategy> strat) { _brightnessStrategy = strat; }; 343 + void setBrightnessStrategy(std::shared_ptr<const BrightnessStrategy> strat) { brightnessStrategy = std::move(strat); };
318 344
319 //! protected function that sets the colorTemperature strategy which defines how 345 //! protected function that sets the colorTemperature strategy which defines how
320 //! specific commands that deal with colortemperature control are executed 346 //! specific commands that deal with colortemperature control are executed
321 //! \param strat a strategy of type \ref ColorTemperatureStrategy 347 //! \param strat a strategy of type \ref ColorTemperatureStrategy
322 - void setColorTemperatureStrategy(std::shared_ptr<ColorTemperatureStrategy> strat) { _colorTemperatureStrategy = strat; }; 348 + void setColorTemperatureStrategy(std::shared_ptr<const ColorTemperatureStrategy> strat) { colorTemperatureStrategy = std::move(strat); };
323 349
324 //! protected function that sets the colorHue strategy which defines how 350 //! protected function that sets the colorHue strategy which defines how
325 //! specific commands that deal with color control are executed 351 //! specific commands that deal with color control are executed
326 //! \param strat a strategy of type \ref ColorHueStrategy 352 //! \param strat a strategy of type \ref ColorHueStrategy
327 - void setColorHueStrategy(std::shared_ptr<ColorHueStrategy> strat) { _colorHueStategy = strat; }; 353 + void setColorHueStrategy(std::shared_ptr<const ColorHueStrategy> strat) { colorHueStrategy = std::move(strat); };
328 354
329 //! Function that turns the light on without refreshing its state. 355 //! Function that turns the light on without refreshing its state.
330 //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms 356 //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms
@@ -351,9 +377,9 @@ protected: @@ -351,9 +377,9 @@ protected:
351 Json::Value state; //!< holds the current state of the light updated by \ref refreshState 377 Json::Value state; //!< holds the current state of the light updated by \ref refreshState
352 ColorType colorType; //!< holds the \ref ColorType of the light 378 ColorType colorType; //!< holds the \ref ColorType of the light
353 379
354 - std::shared_ptr<BrightnessStrategy> _brightnessStrategy; //!< holds a reference to the strategy that handles brighntess commands  
355 - std::shared_ptr<ColorTemperatureStrategy> _colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands  
356 - std::shared_ptr<ColorHueStrategy> _colorHueStategy; //!< holds a reference to the strategy that handles all color commands 380 + std::shared_ptr<const BrightnessStrategy> brightnessStrategy; //!< holds a reference to the strategy that handles brightness commands
  381 + std::shared_ptr<const ColorTemperatureStrategy> colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands
  382 + std::shared_ptr<const ColorHueStrategy> colorHueStrategy; //!< holds a reference to the strategy that handles all color commands
357 }; 383 };
358 384
359 #endif 385 #endif