Commit 374adda3ce84fe0b70af4f4a5173684db60a1a75

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent 1721da0d

Add BridgeConfig for configuration api.

include/hueplusplus/HueConfig.h renamed to include/hueplusplus/APIConfig.h
include/hueplusplus/BridgeConfig.h 0 → 100644
  1 +/**
  2 + \file BridgeConfig.h
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 +
  6 + This file is part of hueplusplus.
  7 +
  8 + hueplusplus is free software: you can redistribute it and/or modify
  9 + it under the terms of the GNU Lesser General Public License as published by
  10 + the Free Software Foundation, either version 3 of the License, or
  11 + (at your option) any later version.
  12 +
  13 + hueplusplus is distributed in the hope that it will be useful,
  14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 + GNU Lesser General Public License for more details.
  17 +
  18 + You should have received a copy of the GNU Lesser General Public License
  19 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  20 +**/
  21 +
  22 +#include <string>
  23 +#include <vector>
  24 +
  25 +#include "APICache.h"
  26 +#include "TimePattern.h"
  27 +
  28 +namespace hueplusplus
  29 +{
  30 +struct Version
  31 +{
  32 + int major;
  33 + int minor;
  34 + int patch;
  35 +};
  36 +
  37 +struct WhitelistedUser
  38 +{
  39 + std::string key;
  40 + std::string name;
  41 + time::AbsoluteTime lastUsed;
  42 + time::AbsoluteTime created;
  43 +};
  44 +
  45 +class BridgeConfig
  46 +{
  47 +public:
  48 + BridgeConfig(std::shared_ptr<APICache> baseCache, std::chrono::steady_clock::duration refreshDuration);
  49 +
  50 + void refresh();
  51 +
  52 + std::vector<WhitelistedUser> getWhitelistedUsers() const;
  53 + void removeUser(const std::string& userKey);
  54 +
  55 + bool getLinkButton() const;
  56 + void pressLinkButton();
  57 +
  58 + void touchLink();
  59 +
  60 + std::string getMACAddress() const;
  61 + time::AbsoluteTime getUTCTime() const;
  62 + std::string getTimezone() const;
  63 +
  64 +protected:
  65 + BridgeConfig(const BridgeConfig&) = default;
  66 + BridgeConfig(BridgeConfig&&) = default;
  67 + BridgeConfig& operator=(const BridgeConfig&) = default;
  68 + BridgeConfig& operator=(BridgeConfig&&) = default;
  69 +
  70 +private:
  71 + APICache cache;
  72 +};
  73 +} // namespace hueplusplus
0 74 \ No newline at end of file
... ...
include/hueplusplus/Hue.h
... ... @@ -30,6 +30,7 @@
30 30 #include <vector>
31 31  
32 32 #include "APICache.h"
  33 +#include "BridgeConfig.h"
33 34 #include "BrightnessStrategy.h"
34 35 #include "ColorHueStrategy.h"
35 36 #include "ColorTemperatureStrategy.h"
... ... @@ -117,6 +118,14 @@ private:
117 118 std::shared_ptr<const IHttpHandler> http_handler;
118 119 };
119 120  
  121 +template <typename T>
  122 +class MakeCopyable : public T
  123 +{
  124 +public:
  125 + using T::T;
  126 + using T::operator=;
  127 +};
  128 +
120 129 //! \brief Hue class for a bridge.
121 130 //!
122 131 //! This is the main class used to interact with the Hue bridge.
... ... @@ -184,6 +193,8 @@ public:
184 193 //! \param handler a HttpHandler of type \ref IHttpHandler
185 194 void setHttpHandler(std::shared_ptr<const IHttpHandler> handler);
186 195  
  196 + BridgeConfig& config();
  197 + const BridgeConfig& config() const;
187 198 ///@}
188 199 //! \name Lights
189 200 ///@{
... ... @@ -353,7 +364,6 @@ public:
353 364 //! \throws nlohmann::json::parse_error when response could not be parsed
354 365 Scene& getScene(const std::string& id);
355 366  
356   -
357 367 //! \brief Checks whether a scene exists.
358 368 //! \param id ID of the scene.
359 369 //! \returns true when the scene exists.
... ... @@ -381,10 +391,11 @@ private:
381 391 //!< bridge
382 392 std::chrono::steady_clock::duration refreshDuration;
383 393 std::shared_ptr<APICache> stateCache;
384   - ResourceList<HueLight, int> lights;
385   - GroupResourceList<Group, CreateGroup> groups;
386   - CreateableResourceList<Schedule, int, CreateSchedule> schedules;
387   - CreateableResourceList<Scene, std::string, CreateScene> scenes;
  394 + MakeCopyable<ResourceList<HueLight, int>> lights;
  395 + MakeCopyable<GroupResourceList<Group, CreateGroup>> groups;
  396 + MakeCopyable<CreateableResourceList<Schedule, int, CreateSchedule>> schedules;
  397 + MakeCopyable<CreateableResourceList<Scene, std::string, CreateScene>> scenes;
  398 + MakeCopyable<BridgeConfig> bridgeConfig;
388 399 };
389 400 } // namespace hueplusplus
390 401  
... ...
include/hueplusplus/ResourceList.h
... ... @@ -57,7 +57,7 @@ public:
57 57 std::chrono::steady_clock::duration refreshDuration,
58 58 const std::function<Resource(IdType, const nlohmann::json&)>& factory = nullptr)
59 59 : stateCache(baseCache, cacheEntry, refreshDuration), factory(factory), path(stateCache.getRequestPath() + '/')
60   - {}
  60 + { }
61 61 //! \brief Construct ResourceList with a separate cache and optional factory function
62 62 //! \param commands HueCommandAPI for requests
63 63 //! \param path Path of the resource list
... ... @@ -68,16 +68,12 @@ public:
68 68 std::chrono::steady_clock::duration refreshDuration,
69 69 const std::function<Resource(IdType, const nlohmann::json&)>& factory = nullptr)
70 70 : stateCache(path, commands, refreshDuration), factory(factory), path(path + '/')
71   - {}
  71 + { }
72 72  
73 73 //! \brief Deleted copy constructor
74 74 ResourceList(const ResourceList&) = delete;
75   - //! \brief Defaulted move constructor
76   - ResourceList(ResourceList&&) = default;
77 75 //! \brief Deleted copy assignment
78 76 ResourceList& operator=(const ResourceList&) = delete;
79   - //! \brief Defaulted move assignment
80   - ResourceList& operator=(ResourceList&&) = default;
81 77  
82 78 //! \brief Refreshes internal state now
83 79 void refresh() { stateCache.refresh(); }
... ... @@ -198,6 +194,11 @@ protected:
198 194 id, state, std::is_constructible<Resource, IdType, HueCommandAPI, std::chrono::steady_clock::duration> {});
199 195 }
200 196  
  197 + //! \brief Protected defaulted move constructor
  198 + ResourceList(ResourceList&&) = default;
  199 + //! \brief Protected defaulted move assignment
  200 + ResourceList& operator=(ResourceList&&) = default;
  201 +
201 202 private:
202 203 // Resource is constructable
203 204 Resource construct(const IdType& id, const nlohmann::json& state, std::true_type)
... ... @@ -274,6 +275,11 @@ public:
274 275 }
275 276 return IdType {};
276 277 }
  278 +protected:
  279 + //! \brief Protected defaulted move constructor
  280 + CreateableResourceList(CreateableResourceList&&) = default;
  281 + //! \brief Protected defaulted move assignment
  282 + CreateableResourceList& operator=(CreateableResourceList&&) = default;
277 283 };
278 284  
279 285 //! \brief Handles a group list with the special group 0
... ... @@ -306,6 +312,12 @@ public:
306 312 //! \brief Get group, specially handles group 0
307 313 //! \see ResourceList::exists
308 314 bool exists(int id) const { return id == 0 || CreateableResourceList<Resource, int, CreateType>::exists(id); }
  315 +
  316 +protected:
  317 + //! \brief Protected defaulted move constructor
  318 + GroupResourceList(GroupResourceList&&) = default;
  319 + //! \brief Protected defaulted move assignment
  320 + GroupResourceList& operator=(GroupResourceList&&) = default;
309 321 };
310 322 } // namespace hueplusplus
311 323  
... ...
src/BridgeConfig.cpp 0 → 100644
  1 +/**
  2 + \file BridgeConfig.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 +
  6 + This file is part of hueplusplus.
  7 +
  8 + hueplusplus is free software: you can redistribute it and/or modify
  9 + it under the terms of the GNU Lesser General Public License as published by
  10 + the Free Software Foundation, either version 3 of the License, or
  11 + (at your option) any later version.
  12 +
  13 + hueplusplus is distributed in the hope that it will be useful,
  14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 + GNU Lesser General Public License for more details.
  17 +
  18 + You should have received a copy of the GNU Lesser General Public License
  19 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  20 +**/
  21 +
  22 +#include <hueplusplus/BridgeConfig.h>
  23 +#include <hueplusplus/HueExceptionMacro.h>
  24 +
  25 +namespace hueplusplus
  26 +{
  27 +BridgeConfig::BridgeConfig(std::shared_ptr<APICache> baseCache, std::chrono::steady_clock::duration refreshDuration)
  28 + : cache(std::move(baseCache), "config", refreshDuration)
  29 +{ }
  30 +void BridgeConfig::refresh()
  31 +{
  32 + cache.refresh();
  33 +}
  34 +std::vector<WhitelistedUser> BridgeConfig::getWhitelistedUsers() const
  35 +{
  36 + const nlohmann::json& whitelist = cache.getValue().at("whitelist");
  37 + std::vector<WhitelistedUser> users;
  38 + for (auto it = whitelist.begin(); it != whitelist.end(); ++it)
  39 + {
  40 + users.push_back({it.key(), it->at("name").get<std::string>(),
  41 + time::AbsoluteTime::parseUTC(it->at("last use date").get<std::string>()),
  42 + time::AbsoluteTime::parseUTC(it->at("create date").get<std::string>())});
  43 + }
  44 + return users;
  45 +}
  46 +void BridgeConfig::removeUser(const std::string& userKey)
  47 +{
  48 + cache.getCommandAPI().DELETERequest("/config/whitelist/" + userKey, nlohmann::json::object());
  49 +}
  50 +bool BridgeConfig::getLinkButton() const
  51 +{
  52 + return cache.getValue().at("linkbutton").get<bool>();
  53 +}
  54 +void BridgeConfig::pressLinkButton()
  55 +{
  56 + cache.getCommandAPI().PUTRequest("/config", {{"linkbutton", true}}, CURRENT_FILE_INFO);
  57 +}
  58 +void BridgeConfig::touchLink()
  59 +{
  60 + cache.getCommandAPI().PUTRequest("/config", {{"touchlink", true}}, CURRENT_FILE_INFO);
  61 +}
  62 +std::string BridgeConfig::getMACAddress() const
  63 +{
  64 + return cache.getValue().at("mac").get<std::string>();
  65 +}
  66 +time::AbsoluteTime BridgeConfig::getUTCTime() const
  67 +{
  68 + return time::AbsoluteTime::parseUTC(cache.getValue().at("UTC").get<std::string>());
  69 +}
  70 +std::string BridgeConfig::getTimezone() const
  71 +{
  72 + return cache.getValue().at("timezone").get<std::string>();
  73 +}
  74 +} // namespace hueplusplus
0 75 \ No newline at end of file
... ...
src/CMakeLists.txt
... ... @@ -19,7 +19,7 @@ set(hueplusplus_SOURCES
19 19 StateTransaction.cpp
20 20 TimePattern.cpp
21 21 UPnP.cpp
22   - Utils.cpp)
  22 + Utils.cpp "BridgeConfig.cpp")
23 23  
24 24 # on windows we want to compile the WinHttpHandler
25 25 if(WIN32)
... ...
src/ExtendedColorHueStrategy.cpp
... ... @@ -26,7 +26,7 @@
26 26 #include <iostream>
27 27 #include <thread>
28 28  
29   -#include "hueplusplus/HueConfig.h"
  29 +#include "hueplusplus/APIConfig.h"
30 30  
31 31 namespace hueplusplus
32 32 {
... ...
src/ExtendedColorTemperatureStrategy.cpp
... ... @@ -26,7 +26,7 @@
26 26 #include <iostream>
27 27 #include <thread>
28 28  
29   -#include "hueplusplus/HueConfig.h"
  29 +#include "hueplusplus/APIConfig.h"
30 30 #include "hueplusplus/HueExceptionMacro.h"
31 31 #include "hueplusplus/Utils.h"
32 32  
... ...
src/Hue.cpp
... ... @@ -31,14 +31,14 @@
31 31 #include <stdexcept>
32 32 #include <thread>
33 33  
34   -#include "hueplusplus/HueConfig.h"
  34 +#include "hueplusplus/APIConfig.h"
35 35 #include "hueplusplus/HueExceptionMacro.h"
36 36 #include "hueplusplus/UPnP.h"
37 37 #include "hueplusplus/Utils.h"
38 38  
39 39 namespace hueplusplus
40 40 {
41   -HueFinder::HueFinder(std::shared_ptr<const IHttpHandler> handler) : http_handler(std::move(handler)) {}
  41 +HueFinder::HueFinder(std::shared_ptr<const IHttpHandler> handler) : http_handler(std::move(handler)) { }
42 42  
43 43 std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const
44 44 {
... ... @@ -144,8 +144,9 @@ Hue::Hue(const std::string&amp; ip, const int port, const std::string&amp; username,
144 144 int id, const nlohmann::json& state) mutable { return factory.createLight(state, id); }),
145 145 groups(stateCache, "groups", refreshDuration),
146 146 schedules(stateCache, "schedules", refreshDuration),
147   - scenes(stateCache, "scenes", refreshDuration)
148   -{}
  147 + scenes(stateCache, "scenes", refreshDuration),
  148 + bridgeConfig(stateCache, refreshDuration)
  149 +{ }
149 150  
150 151 void Hue::refresh()
151 152 {
... ... @@ -220,6 +221,16 @@ void Hue::setPort(const int port)
220 221 this->port = port;
221 222 }
222 223  
  224 +BridgeConfig& Hue::config()
  225 +{
  226 + return bridgeConfig;
  227 +}
  228 +
  229 +const BridgeConfig& Hue::config() const
  230 +{
  231 + return bridgeConfig;
  232 +}
  233 +
223 234 HueLight& Hue::getLight(int id)
224 235 {
225 236 return lights.get(id);
... ... @@ -315,5 +326,6 @@ void Hue::setHttpHandler(std::shared_ptr&lt;const IHttpHandler&gt; handler)
315 326 groups = GroupResourceList<Group, CreateGroup>(stateCache, "groups", refreshDuration);
316 327 schedules = CreateableResourceList<Schedule, int, CreateSchedule>(stateCache, "schedules", refreshDuration);
317 328 scenes = CreateableResourceList<Scene, std::string, CreateScene>(stateCache, "scenes", refreshDuration);
  329 + bridgeConfig = BridgeConfig(stateCache, refreshDuration);
318 330 }
319 331 } // namespace hueplusplus
... ...
src/HueCommandAPI.cpp
... ... @@ -24,7 +24,7 @@
24 24  
25 25 #include <thread>
26 26  
27   -#include "hueplusplus/HueConfig.h"
  27 +#include "hueplusplus/APIConfig.h"
28 28 #include "hueplusplus/HueExceptionMacro.h"
29 29  
30 30 namespace hueplusplus
... ...
src/SimpleColorHueStrategy.cpp
... ... @@ -26,7 +26,7 @@
26 26 #include <iostream>
27 27 #include <thread>
28 28  
29   -#include "hueplusplus/HueConfig.h"
  29 +#include "hueplusplus/APIConfig.h"
30 30 #include "hueplusplus/HueExceptionMacro.h"
31 31 #include "hueplusplus/Utils.h"
32 32  
... ...
src/SimpleColorTemperatureStrategy.cpp
... ... @@ -26,7 +26,7 @@
26 26 #include <iostream>
27 27 #include <thread>
28 28  
29   -#include "hueplusplus/HueConfig.h"
  29 +#include "hueplusplus/APIConfig.h"
30 30 #include "hueplusplus/HueExceptionMacro.h"
31 31 #include "hueplusplus/Utils.h"
32 32  
... ...
src/UPnP.cpp
... ... @@ -25,7 +25,7 @@
25 25 #include <algorithm>
26 26 #include <iostream>
27 27  
28   -#include "hueplusplus/HueConfig.h"
  28 +#include "hueplusplus/APIConfig.h"
29 29  
30 30 namespace hueplusplus
31 31 {
... ...
test/test_Hue.cpp
... ... @@ -28,8 +28,8 @@
28 28  
29 29 #include "testhelper.h"
30 30  
  31 +#include "hueplusplus/APIConfig.h"
31 32 #include "hueplusplus/Hue.h"
32   -#include "hueplusplus/HueConfig.h"
33 33 #include "json/json.hpp"
34 34 #include "mocks/mock_HttpHandler.h"
35 35  
... ...
test/test_Main.cpp
... ... @@ -21,7 +21,7 @@
21 21 **/
22 22  
23 23 #include <gtest/gtest.h>
24   -#include <hueplusplus/HueConfig.h>
  24 +#include <hueplusplus/APIConfig.h>
25 25  
26 26 namespace
27 27 {
... ...
test/test_UPnP.cpp
... ... @@ -26,7 +26,7 @@
26 26 #include "iostream"
27 27 #include "testhelper.h"
28 28  
29   -#include "hueplusplus/HueConfig.h"
  29 +#include "hueplusplus/APIConfig.h"
30 30 #include "hueplusplus/UPnP.h"
31 31 #include "json/json.hpp"
32 32 #include "mocks/mock_HttpHandler.h"
... ...