diff --git a/include/hueplusplus/BaseDevice.h b/include/hueplusplus/BaseDevice.h index 5a8e108..e791bd1 100644 --- a/include/hueplusplus/BaseDevice.h +++ b/include/hueplusplus/BaseDevice.h @@ -111,11 +111,13 @@ public: virtual bool setName(const std::string& name); //! \brief Refreshes internal cached state. + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). //! \throws std::system_error when system or socket operations fail //! \throws HueException when response contained no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - virtual void refresh(); + virtual void refresh(bool force = false); protected: //! \brief Protected ctor that is used by subclasses. diff --git a/include/hueplusplus/BridgeConfig.h b/include/hueplusplus/BridgeConfig.h index b252346..0d70049 100644 --- a/include/hueplusplus/BridgeConfig.h +++ b/include/hueplusplus/BridgeConfig.h @@ -59,11 +59,13 @@ public: BridgeConfig(std::shared_ptr baseCache, std::chrono::steady_clock::duration refreshDuration); //! \brief Refreshes internal cached state. + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). //! \throws std::system_error when system or socket operations fail //! \throws HueException when response contained no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - void refresh(); + void refresh(bool force = false); //! \brief Get the list of whitelisted users //! \returns All users authorized for API access diff --git a/include/hueplusplus/Group.h b/include/hueplusplus/Group.h index c7911b4..7e60ec3 100644 --- a/include/hueplusplus/Group.h +++ b/include/hueplusplus/Group.h @@ -47,11 +47,13 @@ public: Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); //! \brief Refreshes internal cached state. + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). //! \throws std::system_error when system or socket operations fail //! \throws HueException when response contained no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - void refresh(); + void refresh(bool force = false); //! \name General information ///@{ diff --git a/include/hueplusplus/ResourceList.h b/include/hueplusplus/ResourceList.h index f7f913d..f1df811 100644 --- a/include/hueplusplus/ResourceList.h +++ b/include/hueplusplus/ResourceList.h @@ -112,7 +112,7 @@ public: auto pos = resources.find(id); if (pos != resources.end()) { - pos->second.refresh(); + pos->second.refresh(true); return pos->second; } const nlohmann::json& state = stateCache.getValue(); diff --git a/include/hueplusplus/Scene.h b/include/hueplusplus/Scene.h index b8a68e6..5df95de 100644 --- a/include/hueplusplus/Scene.h +++ b/include/hueplusplus/Scene.h @@ -29,8 +29,8 @@ #include #include "APICache.h" -#include "TimePattern.h" #include "ColorUnits.h" +#include "TimePattern.h" namespace hueplusplus { @@ -130,11 +130,13 @@ public: Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); //! \brief Refreshes internal cached state + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). //! \throws std::system_error when system or socket operations fail //! \throws HueException when response contained no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - void refresh(); + void refresh(bool force = false); //! \brief Get scene identifier std::string getId() const; diff --git a/src/BaseDevice.cpp b/src/BaseDevice.cpp index 70ab841..8290f95 100644 --- a/src/BaseDevice.cpp +++ b/src/BaseDevice.cpp @@ -83,7 +83,7 @@ std::string BaseDevice::getSwVersion() const bool BaseDevice::setName(const std::string& name) { - nlohmann::json request = { {"name", name} }; + nlohmann::json request = {{"name", name}}; nlohmann::json reply = sendPutRequest("/name", request, CURRENT_FILE_INFO); // Check whether request was successful (returned name is not necessarily the actually set name) @@ -103,9 +103,16 @@ nlohmann::json BaseDevice::sendPutRequest(const std::string& subPath, const nloh return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); } -void BaseDevice::refresh() +void BaseDevice::refresh(bool force) { - state.refresh(); + if (force) + { + state.refresh(); + } + else + { + state.getValue(); + } } } // namespace hueplusplus diff --git a/src/BridgeConfig.cpp b/src/BridgeConfig.cpp index 4f0296e..0953e6d 100644 --- a/src/BridgeConfig.cpp +++ b/src/BridgeConfig.cpp @@ -27,9 +27,17 @@ namespace hueplusplus BridgeConfig::BridgeConfig(std::shared_ptr baseCache, std::chrono::steady_clock::duration refreshDuration) : cache(std::move(baseCache), "config", refreshDuration) { } -void BridgeConfig::refresh() + +void BridgeConfig::refresh(bool force) { - cache.refresh(); + if (force) + { + cache.refresh(); + } + else + { + cache.getValue(); + } } std::vector BridgeConfig::getWhitelistedUsers() const { diff --git a/src/Group.cpp b/src/Group.cpp index 70d260e..dad2142 100644 --- a/src/Group.cpp +++ b/src/Group.cpp @@ -10,9 +10,16 @@ Group::Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::d state.refresh(); } -void Group::refresh() +void Group::refresh(bool force) { - state.refresh(); + if (force) + { + state.refresh(); + } + else + { + state.getValue(); + } } int Group::getId() const @@ -50,7 +57,7 @@ void Group::setName(const std::string& name) { nlohmann::json request = {{"name", name}}; sendPutRequest("", request, CURRENT_FILE_INFO); - refresh(); + refresh(true); } void Group::setLights(const std::vector& ids) @@ -61,7 +68,7 @@ void Group::setLights(const std::vector& ids) lights.push_back(std::to_string(id)); } sendPutRequest("", {{"lights", lights}}, CURRENT_FILE_INFO); - refresh(); + refresh(true); } bool Group::getAllOn() @@ -204,7 +211,7 @@ std::string Group::getRoomType() const void Group::setRoomType(const std::string& type) { sendPutRequest("", {{"class", type}}, CURRENT_FILE_INFO); - refresh(); + refresh(true); } std::string Group::getModelId() const diff --git a/src/Scene.cpp b/src/Scene.cpp index 8ed70a6..1328f1c 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -24,7 +24,7 @@ namespace hueplusplus { -LightState::LightState(const nlohmann::json& state) : state(state) {} +LightState::LightState(const nlohmann::json& state) : state(state) { } bool LightState::isOn() const { @@ -156,9 +156,16 @@ Scene::Scene(const std::string& id, const HueCommandAPI& commands, std::chrono:: refresh(); } -void Scene::refresh() +void Scene::refresh(bool force) { - state.refresh(); + if (force) + { + state.refresh(); + } + else + { + state.getValue(); + } } std::string Scene::getId() const diff --git a/test/test_BridgeConfig.cpp b/test/test_BridgeConfig.cpp index 5b3a391..dd4568b 100644 --- a/test/test_BridgeConfig.cpp +++ b/test/test_BridgeConfig.cpp @@ -44,7 +44,7 @@ TEST(BridgeConfig, refresh) EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + "/config", nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(nlohmann::json::object())); - config.refresh(); + config.refresh(true); } TEST(BridgeConfig, getWhitelistedUsers) @@ -107,7 +107,7 @@ TEST(BridgeConfig, getLinkButton) EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + "/config", nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(nlohmann::json {{"linkbutton", false}})); - config.refresh(); + config.refresh(true); EXPECT_FALSE(config.getLinkButton()); } diff --git a/test/test_ResourceList.cpp b/test/test_ResourceList.cpp index 8fbcd30..a3d529c 100644 --- a/test/test_ResourceList.cpp +++ b/test/test_ResourceList.cpp @@ -34,7 +34,7 @@ class TestResource public: TestResource(int id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration) : id(id) {} - void refresh() {} + void refresh(bool force = false) {} public: int id; @@ -42,7 +42,7 @@ public: class TestResourceFactory { public: - void refresh() {} + void refresh(bool force = false) {} }; class TestStringResource { @@ -50,7 +50,7 @@ public: TestStringResource(const std::string& id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration) : id(id) {} - void refresh() {} + void refresh(bool force = false) {} public: std::string id; diff --git a/test/test_Scene.cpp b/test/test_Scene.cpp index 96d06de..46ac93f 100644 --- a/test/test_Scene.cpp +++ b/test/test_Scene.cpp @@ -319,7 +319,7 @@ TEST_F(SceneTest, refresh) expectGetState(id); Scene scene(id, commands, std::chrono::seconds(0)); expectGetState(id); - scene.refresh(); + scene.refresh(true); } TEST_F(SceneTest, setName)