Commit 6a9b8f2cfe886257878859515f69a43a3adab0f2
Committed by
Moritz Wirger
1 parent
6dfa7633
Make refresh method only refresh when necessary by default.
Showing
12 changed files
with
62 additions
and
25 deletions
include/hueplusplus/BaseDevice.h
| ... | ... | @@ -111,11 +111,13 @@ public: |
| 111 | 111 | virtual bool setName(const std::string& name); |
| 112 | 112 | |
| 113 | 113 | //! \brief Refreshes internal cached state. |
| 114 | + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. | |
| 115 | + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). | |
| 114 | 116 | //! \throws std::system_error when system or socket operations fail |
| 115 | 117 | //! \throws HueException when response contained no body |
| 116 | 118 | //! \throws HueAPIResponseException when response contains an error |
| 117 | 119 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 118 | - virtual void refresh(); | |
| 120 | + virtual void refresh(bool force = false); | |
| 119 | 121 | |
| 120 | 122 | protected: |
| 121 | 123 | //! \brief Protected ctor that is used by subclasses. | ... | ... |
include/hueplusplus/BridgeConfig.h
| ... | ... | @@ -59,11 +59,13 @@ public: |
| 59 | 59 | BridgeConfig(std::shared_ptr<APICache> baseCache, std::chrono::steady_clock::duration refreshDuration); |
| 60 | 60 | |
| 61 | 61 | //! \brief Refreshes internal cached state. |
| 62 | + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. | |
| 63 | + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). | |
| 62 | 64 | //! \throws std::system_error when system or socket operations fail |
| 63 | 65 | //! \throws HueException when response contained no body |
| 64 | 66 | //! \throws HueAPIResponseException when response contains an error |
| 65 | 67 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 66 | - void refresh(); | |
| 68 | + void refresh(bool force = false); | |
| 67 | 69 | |
| 68 | 70 | //! \brief Get the list of whitelisted users |
| 69 | 71 | //! \returns All users authorized for API access | ... | ... |
include/hueplusplus/Group.h
| ... | ... | @@ -47,11 +47,13 @@ public: |
| 47 | 47 | Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); |
| 48 | 48 | |
| 49 | 49 | //! \brief Refreshes internal cached state. |
| 50 | + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. | |
| 51 | + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). | |
| 50 | 52 | //! \throws std::system_error when system or socket operations fail |
| 51 | 53 | //! \throws HueException when response contained no body |
| 52 | 54 | //! \throws HueAPIResponseException when response contains an error |
| 53 | 55 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 54 | - void refresh(); | |
| 56 | + void refresh(bool force = false); | |
| 55 | 57 | |
| 56 | 58 | //! \name General information |
| 57 | 59 | ///@{ | ... | ... |
include/hueplusplus/ResourceList.h
include/hueplusplus/Scene.h
| ... | ... | @@ -29,8 +29,8 @@ |
| 29 | 29 | #include <json/json.hpp> |
| 30 | 30 | |
| 31 | 31 | #include "APICache.h" |
| 32 | -#include "TimePattern.h" | |
| 33 | 32 | #include "ColorUnits.h" |
| 33 | +#include "TimePattern.h" | |
| 34 | 34 | |
| 35 | 35 | namespace hueplusplus |
| 36 | 36 | { |
| ... | ... | @@ -130,11 +130,13 @@ public: |
| 130 | 130 | Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); |
| 131 | 131 | |
| 132 | 132 | //! \brief Refreshes internal cached state |
| 133 | + //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. | |
| 134 | + //! \c false to only refresh when enough time has passed (needed e.g. when calling only const methods). | |
| 133 | 135 | //! \throws std::system_error when system or socket operations fail |
| 134 | 136 | //! \throws HueException when response contained no body |
| 135 | 137 | //! \throws HueAPIResponseException when response contains an error |
| 136 | 138 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 137 | - void refresh(); | |
| 139 | + void refresh(bool force = false); | |
| 138 | 140 | |
| 139 | 141 | //! \brief Get scene identifier |
| 140 | 142 | std::string getId() const; | ... | ... |
src/BaseDevice.cpp
| ... | ... | @@ -83,7 +83,7 @@ std::string BaseDevice::getSwVersion() const |
| 83 | 83 | |
| 84 | 84 | bool BaseDevice::setName(const std::string& name) |
| 85 | 85 | { |
| 86 | - nlohmann::json request = { {"name", name} }; | |
| 86 | + nlohmann::json request = {{"name", name}}; | |
| 87 | 87 | nlohmann::json reply = sendPutRequest("/name", request, CURRENT_FILE_INFO); |
| 88 | 88 | |
| 89 | 89 | // 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 |
| 103 | 103 | return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | -void BaseDevice::refresh() | |
| 106 | +void BaseDevice::refresh(bool force) | |
| 107 | 107 | { |
| 108 | - state.refresh(); | |
| 108 | + if (force) | |
| 109 | + { | |
| 110 | + state.refresh(); | |
| 111 | + } | |
| 112 | + else | |
| 113 | + { | |
| 114 | + state.getValue(); | |
| 115 | + } | |
| 109 | 116 | } |
| 110 | 117 | |
| 111 | 118 | } // namespace hueplusplus | ... | ... |
src/BridgeConfig.cpp
| ... | ... | @@ -27,9 +27,17 @@ namespace hueplusplus |
| 27 | 27 | BridgeConfig::BridgeConfig(std::shared_ptr<APICache> baseCache, std::chrono::steady_clock::duration refreshDuration) |
| 28 | 28 | : cache(std::move(baseCache), "config", refreshDuration) |
| 29 | 29 | { } |
| 30 | -void BridgeConfig::refresh() | |
| 30 | + | |
| 31 | +void BridgeConfig::refresh(bool force) | |
| 31 | 32 | { |
| 32 | - cache.refresh(); | |
| 33 | + if (force) | |
| 34 | + { | |
| 35 | + cache.refresh(); | |
| 36 | + } | |
| 37 | + else | |
| 38 | + { | |
| 39 | + cache.getValue(); | |
| 40 | + } | |
| 33 | 41 | } |
| 34 | 42 | std::vector<WhitelistedUser> BridgeConfig::getWhitelistedUsers() const |
| 35 | 43 | { | ... | ... |
src/Group.cpp
| ... | ... | @@ -10,9 +10,16 @@ Group::Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::d |
| 10 | 10 | state.refresh(); |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | -void Group::refresh() | |
| 13 | +void Group::refresh(bool force) | |
| 14 | 14 | { |
| 15 | - state.refresh(); | |
| 15 | + if (force) | |
| 16 | + { | |
| 17 | + state.refresh(); | |
| 18 | + } | |
| 19 | + else | |
| 20 | + { | |
| 21 | + state.getValue(); | |
| 22 | + } | |
| 16 | 23 | } |
| 17 | 24 | |
| 18 | 25 | int Group::getId() const |
| ... | ... | @@ -50,7 +57,7 @@ void Group::setName(const std::string& name) |
| 50 | 57 | { |
| 51 | 58 | nlohmann::json request = {{"name", name}}; |
| 52 | 59 | sendPutRequest("", request, CURRENT_FILE_INFO); |
| 53 | - refresh(); | |
| 60 | + refresh(true); | |
| 54 | 61 | } |
| 55 | 62 | |
| 56 | 63 | void Group::setLights(const std::vector<int>& ids) |
| ... | ... | @@ -61,7 +68,7 @@ void Group::setLights(const std::vector<int>& ids) |
| 61 | 68 | lights.push_back(std::to_string(id)); |
| 62 | 69 | } |
| 63 | 70 | sendPutRequest("", {{"lights", lights}}, CURRENT_FILE_INFO); |
| 64 | - refresh(); | |
| 71 | + refresh(true); | |
| 65 | 72 | } |
| 66 | 73 | |
| 67 | 74 | bool Group::getAllOn() |
| ... | ... | @@ -204,7 +211,7 @@ std::string Group::getRoomType() const |
| 204 | 211 | void Group::setRoomType(const std::string& type) |
| 205 | 212 | { |
| 206 | 213 | sendPutRequest("", {{"class", type}}, CURRENT_FILE_INFO); |
| 207 | - refresh(); | |
| 214 | + refresh(true); | |
| 208 | 215 | } |
| 209 | 216 | |
| 210 | 217 | std::string Group::getModelId() const | ... | ... |
src/Scene.cpp
| ... | ... | @@ -24,7 +24,7 @@ |
| 24 | 24 | |
| 25 | 25 | namespace hueplusplus |
| 26 | 26 | { |
| 27 | -LightState::LightState(const nlohmann::json& state) : state(state) {} | |
| 27 | +LightState::LightState(const nlohmann::json& state) : state(state) { } | |
| 28 | 28 | |
| 29 | 29 | bool LightState::isOn() const |
| 30 | 30 | { |
| ... | ... | @@ -156,9 +156,16 @@ Scene::Scene(const std::string& id, const HueCommandAPI& commands, std::chrono:: |
| 156 | 156 | refresh(); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | -void Scene::refresh() | |
| 159 | +void Scene::refresh(bool force) | |
| 160 | 160 | { |
| 161 | - state.refresh(); | |
| 161 | + if (force) | |
| 162 | + { | |
| 163 | + state.refresh(); | |
| 164 | + } | |
| 165 | + else | |
| 166 | + { | |
| 167 | + state.getValue(); | |
| 168 | + } | |
| 162 | 169 | } |
| 163 | 170 | |
| 164 | 171 | std::string Scene::getId() const | ... | ... |
test/test_BridgeConfig.cpp
| ... | ... | @@ -44,7 +44,7 @@ TEST(BridgeConfig, refresh) |
| 44 | 44 | EXPECT_CALL(*handler, |
| 45 | 45 | GETJson("/api/" + getBridgeUsername() + "/config", nlohmann::json::object(), getBridgeIp(), getBridgePort())) |
| 46 | 46 | .WillOnce(Return(nlohmann::json::object())); |
| 47 | - config.refresh(); | |
| 47 | + config.refresh(true); | |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | TEST(BridgeConfig, getWhitelistedUsers) |
| ... | ... | @@ -107,7 +107,7 @@ TEST(BridgeConfig, getLinkButton) |
| 107 | 107 | EXPECT_CALL(*handler, |
| 108 | 108 | GETJson("/api/" + getBridgeUsername() + "/config", nlohmann::json::object(), getBridgeIp(), getBridgePort())) |
| 109 | 109 | .WillOnce(Return(nlohmann::json {{"linkbutton", false}})); |
| 110 | - config.refresh(); | |
| 110 | + config.refresh(true); | |
| 111 | 111 | EXPECT_FALSE(config.getLinkButton()); |
| 112 | 112 | } |
| 113 | 113 | ... | ... |
test/test_ResourceList.cpp
| ... | ... | @@ -34,7 +34,7 @@ class TestResource |
| 34 | 34 | public: |
| 35 | 35 | TestResource(int id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration) : id(id) {} |
| 36 | 36 | |
| 37 | - void refresh() {} | |
| 37 | + void refresh(bool force = false) {} | |
| 38 | 38 | |
| 39 | 39 | public: |
| 40 | 40 | int id; |
| ... | ... | @@ -42,7 +42,7 @@ public: |
| 42 | 42 | class TestResourceFactory |
| 43 | 43 | { |
| 44 | 44 | public: |
| 45 | - void refresh() {} | |
| 45 | + void refresh(bool force = false) {} | |
| 46 | 46 | }; |
| 47 | 47 | class TestStringResource |
| 48 | 48 | { |
| ... | ... | @@ -50,7 +50,7 @@ public: |
| 50 | 50 | TestStringResource(const std::string& id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration) |
| 51 | 51 | : id(id) |
| 52 | 52 | {} |
| 53 | - void refresh() {} | |
| 53 | + void refresh(bool force = false) {} | |
| 54 | 54 | |
| 55 | 55 | public: |
| 56 | 56 | std::string id; | ... | ... |
test/test_Scene.cpp