Commit f32be87270c902346e5ffd3deeb0e4c14499eed4

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent f25df3b3

Add sharedState parameter to GetBridge.

include/hueplusplus/Bridge.h
@@ -85,12 +85,13 @@ public: @@ -85,12 +85,13 @@ public:
85 //! \brief Gets a Hue bridge based on its identification 85 //! \brief Gets a Hue bridge based on its identification
86 //! 86 //!
87 //! \param identification \ref BridgeIdentification that specifies a bridge 87 //! \param identification \ref BridgeIdentification that specifies a bridge
  88 + //! \param sharedState Uses a single, shared cache for all objects on the bridge.
88 //! \return \ref Bridge class object 89 //! \return \ref Bridge class object
89 //! \throws std::system_error when system or socket operations fail 90 //! \throws std::system_error when system or socket operations fail
90 //! \throws HueException when response contained no body or username could not be requested 91 //! \throws HueException when response contained no body or username could not be requested
91 //! \throws HueAPIResponseException when response contains an error 92 //! \throws HueAPIResponseException when response contains an error
92 //! \throws nlohmann::json::parse_error when response could not be parsed 93 //! \throws nlohmann::json::parse_error when response could not be parsed
93 - Bridge GetBridge(const BridgeIdentification& identification); 94 + Bridge GetBridge(const BridgeIdentification& identification, bool sharedState = false);
94 95
95 //! \brief Function that adds a username to the usernames map 96 //! \brief Function that adds a username to the usernames map
96 //! 97 //!
src/Bridge.cpp
@@ -75,15 +75,16 @@ std::vector<BridgeFinder::BridgeIdentification> BridgeFinder::FindBridges() cons @@ -75,15 +75,16 @@ std::vector<BridgeFinder::BridgeIdentification> BridgeFinder::FindBridges() cons
75 return foundBridges; 75 return foundBridges;
76 } 76 }
77 77
78 -Bridge BridgeFinder::GetBridge(const BridgeIdentification& identification) 78 +Bridge BridgeFinder::GetBridge(const BridgeIdentification& identification, bool sharedState)
79 { 79 {
80 std::string normalizedMac = NormalizeMac(identification.mac); 80 std::string normalizedMac = NormalizeMac(identification.mac);
81 auto pos = usernames.find(normalizedMac); 81 auto pos = usernames.find(normalizedMac);
82 if (pos != usernames.end()) 82 if (pos != usernames.end())
83 { 83 {
84 - return Bridge(identification.ip, identification.port, pos->second, http_handler); 84 + return Bridge(
  85 + identification.ip, identification.port, pos->second, http_handler, std::chrono::seconds(10), sharedState);
85 } 86 }
86 - Bridge bridge(identification.ip, identification.port, "", http_handler); 87 + Bridge bridge(identification.ip, identification.port, "", http_handler, std::chrono::seconds(10), sharedState);
87 bridge.requestUsername(); 88 bridge.requestUsername();
88 if (bridge.getUsername().empty()) 89 if (bridge.getUsername().empty())
89 { 90 {
@@ -307,7 +308,7 @@ void Bridge::setHttpHandler(std::shared_ptr<const IHttpHandler> handler) @@ -307,7 +308,7 @@ void Bridge::setHttpHandler(std::shared_ptr<const IHttpHandler> handler)
307 { 308 {
308 http_handler = handler; 309 http_handler = handler;
309 stateCache = std::make_shared<APICache>("", HueCommandAPI(ip, port, username, handler), refreshDuration, nullptr); 310 stateCache = std::make_shared<APICache>("", HueCommandAPI(ip, port, username, handler), refreshDuration, nullptr);
310 - lightList = LightList(stateCache, "lights", refreshDuration,sharedState, 311 + lightList = LightList(stateCache, "lights", refreshDuration, sharedState,
311 [factory = LightFactory(stateCache->getCommandAPI(), refreshDuration)](int id, const nlohmann::json& state, 312 [factory = LightFactory(stateCache->getCommandAPI(), refreshDuration)](int id, const nlohmann::json& state,
312 const std::shared_ptr<APICache>& baseCache) mutable { return factory.createLight(state, id, baseCache); }); 313 const std::shared_ptr<APICache>& baseCache) mutable { return factory.createLight(state, id, baseCache); });
313 groupList = GroupList(stateCache, "groups", refreshDuration, sharedState); 314 groupList = GroupList(stateCache, "groups", refreshDuration, sharedState);