Commit 749fa82130033b983ffd30fadfe36b21d64ab86a

Authored by Sieren
Committed by Moritz Wirger
1 parent 83d48832

Use Factory function to create HueLights

Reduce complexity in Hue.cpp by using a separate
factory function to identify the lights.
hueplusplus/CMakeLists.txt
@@ -5,6 +5,7 @@ set(hueplusplus_SOURCES @@ -5,6 +5,7 @@ set(hueplusplus_SOURCES
5 ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorTemperatureStrategy.cpp 5 ${CMAKE_CURRENT_SOURCE_DIR}/ExtendedColorTemperatureStrategy.cpp
6 ${CMAKE_CURRENT_SOURCE_DIR}/Hue.cpp 6 ${CMAKE_CURRENT_SOURCE_DIR}/Hue.cpp
7 ${CMAKE_CURRENT_SOURCE_DIR}/HueCommandAPI.cpp 7 ${CMAKE_CURRENT_SOURCE_DIR}/HueCommandAPI.cpp
  8 + ${CMAKE_CURRENT_SOURCE_DIR}/HueDeviceTypes.cpp
8 ${CMAKE_CURRENT_SOURCE_DIR}/HueException.cpp 9 ${CMAKE_CURRENT_SOURCE_DIR}/HueException.cpp
9 ${CMAKE_CURRENT_SOURCE_DIR}/HueLight.cpp 10 ${CMAKE_CURRENT_SOURCE_DIR}/HueLight.cpp
10 ${CMAKE_CURRENT_SOURCE_DIR}/SimpleBrightnessStrategy.cpp 11 ${CMAKE_CURRENT_SOURCE_DIR}/SimpleBrightnessStrategy.cpp
hueplusplus/Hue.cpp
@@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
31 #include <stdexcept> 31 #include <stdexcept>
32 #include <thread> 32 #include <thread>
33 33
  34 +#include "include/HueDeviceTypes.h"
34 #include "include/ExtendedColorHueStrategy.h" 35 #include "include/ExtendedColorHueStrategy.h"
35 #include "include/ExtendedColorTemperatureStrategy.h" 36 #include "include/ExtendedColorTemperatureStrategy.h"
36 #include "include/HueExceptionMacro.h" 37 #include "include/HueExceptionMacro.h"
@@ -237,58 +238,11 @@ HueLight&amp; Hue::getLight(int id) @@ -237,58 +238,11 @@ HueLight&amp; Hue::getLight(int id)
237 // std::cout << state["lights"][std::to_string(id)] << std::endl; 238 // std::cout << state["lights"][std::to_string(id)] << std::endl;
238 std::string type = state["lights"][std::to_string(id)]["modelid"].get<std::string>(); 239 std::string type = state["lights"][std::to_string(id)]["modelid"].get<std::string>();
239 // std::cout << type << std::endl; 240 // std::cout << type << std::endl;
240 - if (type == "LCT001" || type == "LCT002" || type == "LCT003" || type == "LCT007" || type == "LLM001")  
241 - {  
242 - // HueExtendedColorLight Gamut B  
243 - HueLight light = HueLight(  
244 - id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy);  
245 - light.colorType = ColorType::GAMUT_B;  
246 - lights.emplace(id, light);  
247 - return lights.find(id)->second;  
248 - }  
249 - else if (type == "LCT010" || type == "LCT011" || type == "LCT012" || type == "LCT014" || type == "LCT015"  
250 - || type == "LCT016" || type == "LLC020" || type == "LST002")  
251 - {  
252 - // HueExtendedColorLight Gamut C  
253 - HueLight light = HueLight(  
254 - id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy);  
255 - light.colorType = ColorType::GAMUT_C;  
256 - lights.emplace(id, light);  
257 - return lights.find(id)->second;  
258 - }  
259 - else if (type == "LST001" || type == "LLC005" || type == "LLC006" || type == "LLC007" || type == "LLC010"  
260 - || type == "LLC011" || type == "LLC012" || type == "LLC013" || type == "LLC014")  
261 - {  
262 - // HueColorLight Gamut A  
263 - HueLight light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, simpleColorHueStrategy);  
264 - light.colorType = ColorType::GAMUT_A;  
265 - lights.emplace(id, light);  
266 - return lights.find(id)->second;  
267 - }  
268 - else if (type == "LWB004" || type == "LWB006" || type == "LWB007" || type == "LWB010" || type == "LWB014"  
269 - || type == "LDF001" || type == "LDF002" || type == "LDD001" || type == "LDD002" || type == "MWM001")  
270 - {  
271 - // HueDimmableLight No Color Type  
272 - HueLight light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, nullptr);  
273 - light.colorType = ColorType::NONE;  
274 - lights.emplace(id, light);  
275 - return lights.find(id)->second;  
276 - }  
277 - else if (type == "LLM010" || type == "LLM011" || type == "LLM012" || type == "LTW001" || type == "LTW004"  
278 - || type == "LTW010" || type == "LTW011" || type == "LTW012" || type == "LTW013" || type == "LTW014"  
279 - || type == "LTW015" || type == "LTP001" || type == "LTP002" || type == "LTP003" || type == "LTP004"  
280 - || type == "LTP005" || type == "LTD003" || type == "LTF001" || type == "LTF002" || type == "LTC001"  
281 - || type == "LTC002" || type == "LTC003" || type == "LTC004" || type == "LTC011" || type == "LTC012"  
282 - || type == "LTD001" || type == "LTD002" || type == "LFF001" || type == "LTT001" || type == "LDT001")  
283 - {  
284 - // HueTemperatureLight  
285 - HueLight light = HueLight(id, commands, simpleBrightnessStrategy, simpleColorTemperatureStrategy, nullptr);  
286 - light.colorType = ColorType::TEMPERATURE;  
287 - lights.emplace(id, light);  
288 - return lights.find(id)->second;  
289 - }  
290 - std::cerr << "Could not determine HueLight type:" << type << "!\n";  
291 - throw HueException(CURRENT_FILE_INFO, "Could not determine HueLight type!"); 241 + auto light = MakeHueLight()(type, id, commands, simpleBrightnessStrategy,
  242 + extendedColorTemperatureStrategy, simpleColorTemperatureStrategy, extendedColorHueStrategy,
  243 + simpleColorHueStrategy);
  244 + lights.emplace(id, light);
  245 + return lights.find(id)->second;
292 } 246 }
293 247
294 bool Hue::removeLight(int id) 248 bool Hue::removeLight(int id)
hueplusplus/HueDeviceTypes.cpp 0 → 100644
  1 +/**
  2 + \file HueDeviceTypes.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This file is part of hueplusplus.
  8 +
  9 + hueplusplus is free software: you can redistribute it and/or modify
  10 + it under the terms of the GNU Lesser General Public License as published by
  11 + the Free Software Foundation, either version 3 of the License, or
  12 + (at your option) any later version.
  13 +
  14 + hueplusplus is distributed in the hope that it will be useful,
  15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + GNU Lesser General Public License for more details.
  18 +
  19 + You should have received a copy of the GNU Lesser General Public License
  20 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  21 +**/
  22 +
  23 +#include <set>
  24 +
  25 +#include "include/HueDeviceTypes.h"
  26 +#include "include/HueExceptionMacro.h"
  27 +
  28 +const std::set<std::string> getGamutBTypes()
  29 +{
  30 + static const std::set<std::string> c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES = {
  31 + "LCT001", "LCT002", "LCT003", "LCT007", "LLM001"
  32 + };
  33 + return c_EXTENDEDCOLORLIGHT_GAMUTB_TYPES;
  34 +};
  35 +
  36 +
  37 +const std::set<std::string> getGamutCTypes()
  38 +{
  39 + static const std::set<std::string> c_EXTENDEDCOLORLIGHT_GAMUTC_TYPES = {
  40 + "LCT010", "LCT011", "LCT012", "LCT014",
  41 + "LCT015", "LCT016", "LLC020", "LST002"
  42 + };
  43 + return c_EXTENDEDCOLORLIGHT_GAMUTC_TYPES;
  44 +}
  45 +
  46 +const std::set<std::string> getGamutATypes()
  47 +{
  48 + static const std::set<std::string> c_EXTENDEDCOLORLIGHT_GAMUTA_TYPES = {
  49 + "LST001", "LLC005" , "LLC006", "LLC007",
  50 + "LLC010", "LLC011", "LLC012", "LLC013",
  51 + "LLC014"
  52 + };
  53 + return c_EXTENDEDCOLORLIGHT_GAMUTA_TYPES;
  54 +}
  55 +
  56 +const std::set<std::string> getNoColorTypes()
  57 +{
  58 + static const std::set<std::string> c_DIMMABLELIGHT_NO_COLOR_TYPES = {
  59 + "LWB004", "LWB006", "LWB007", "LWB010",
  60 + "LWB014", "LDF001", "LDF002", "LDD001",
  61 + "LDD002", "MWM001"
  62 + };
  63 + return c_DIMMABLELIGHT_NO_COLOR_TYPES;
  64 +}
  65 +
  66 +const std::set<std::string> getTemperatureLightTypes()
  67 +{
  68 + static const std::set<std::string> c_TEMPERATURELIGHT_TYPES = {
  69 + "LLM010", "LLM011", "LLM012", "LTW001", "LTW004",
  70 + "LTW010", "LTW011", "LTW012", "LTW013", "LTW014",
  71 + "LTW015", "LTP001", "LTP002", "LTP003", "LTP004",
  72 + "LTP005", "LTD003", "LTF001", "LTF002", "LTC001",
  73 + "LTC002", "LTC003", "LTC004", "LTC011", "LTC012",
  74 + "LTD001", "LTD002", "LFF001", "LTT001", "LDT001"
  75 + };
  76 + return c_TEMPERATURELIGHT_TYPES;
  77 +}
  78 +
  79 +auto MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands,
  80 + std::shared_ptr<BrightnessStrategy> simpleBrightnessStrategy,
  81 + std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy,
  82 + std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy,
  83 + std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy,
  84 + std::shared_ptr<ColorHueStrategy> simpleColorHueStrategy) -> HueLight
  85 +{
  86 + if (getGamutBTypes().count(type))
  87 + {
  88 + auto light = HueLight(id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy);
  89 + light.colorType = ColorType::GAMUT_B;
  90 + return light;
  91 + }
  92 +
  93 + else if (getGamutCTypes().count(type))
  94 + {
  95 + auto light = HueLight(id, commands, simpleBrightnessStrategy, extendedColorTemperatureStrategy, extendedColorHueStrategy);
  96 + light.colorType = ColorType::GAMUT_C;
  97 + return light;
  98 + }
  99 +
  100 + else if (getGamutATypes().count(type))
  101 + {
  102 + auto light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, simpleColorHueStrategy);
  103 + light.colorType = ColorType::GAMUT_A;
  104 + return light;
  105 + }
  106 +
  107 + else if (getNoColorTypes().count(type))
  108 + {
  109 + auto light = HueLight(id, commands, simpleBrightnessStrategy, nullptr, nullptr);
  110 + light.colorType = ColorType::NONE;
  111 + return light;
  112 + }
  113 +
  114 + else if (getTemperatureLightTypes().count(type))
  115 + {
  116 + auto light = HueLight(id, commands, simpleBrightnessStrategy, simpleColorTemperatureStrategy, nullptr);
  117 + light.colorType = ColorType::TEMPERATURE;
  118 + return light;
  119 + }
  120 + std::cerr << "Could not determine HueLight type:" << type << "!\n";
  121 + throw HueException(CURRENT_FILE_INFO, "Could not determine HueLight type!");
  122 +}
hueplusplus/include/HueDeviceTypes.h 0 → 100644
  1 +/**
  2 + \file HueDeviceTypes.h
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 + Copyright (C) 2020 Moritz Wirger - developer\n
  6 +
  7 + This file is part of hueplusplus.
  8 +
  9 + hueplusplus is free software: you can redistribute it and/or modify
  10 + it under the terms of the GNU Lesser General Public License as published by
  11 + the Free Software Foundation, either version 3 of the License, or
  12 + (at your option) any later version.
  13 +
  14 + hueplusplus is distributed in the hope that it will be useful,
  15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + GNU Lesser General Public License for more details.
  18 +
  19 + You should have received a copy of the GNU Lesser General Public License
  20 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  21 +**/
  22 +
  23 +
  24 +#ifndef _HUEDEVICETYPES_H
  25 +#define _HUEDEVICETYPES_H
  26 +
  27 +#include <memory>
  28 +#include <string>
  29 +
  30 +#include "HueLight.h"
  31 +
  32 +struct MakeHueLight {
  33 + auto operator()(std::string type, int id, HueCommandAPI commands,
  34 + std::shared_ptr<BrightnessStrategy> simpleBrightnessStrategy,
  35 + std::shared_ptr<ColorTemperatureStrategy> extendedColorTemperatureStrategy,
  36 + std::shared_ptr<ColorTemperatureStrategy> simpleColorTemperatureStrategy,
  37 + std::shared_ptr<ColorHueStrategy> extendedColorHueStrategy,
  38 + std::shared_ptr<ColorHueStrategy> simpleColorHueStrategy) -> HueLight;
  39 +};
  40 +
  41 +
  42 +#endif
  43 +
hueplusplus/include/HueLight.h
@@ -94,6 +94,7 @@ enum ColorType @@ -94,6 +94,7 @@ enum ColorType
94 class HueLight 94 class HueLight
95 { 95 {
96 friend class Hue; 96 friend class Hue;
  97 + friend struct MakeHueLight;
97 friend class SimpleBrightnessStrategy; 98 friend class SimpleBrightnessStrategy;
98 friend class SimpleColorHueStrategy; 99 friend class SimpleColorHueStrategy;
99 friend class ExtendedColorHueStrategy; 100 friend class ExtendedColorHueStrategy;