Commit 0294e540145afba74c5e5242c75dcb9a218aaf3d
Committed by
Moritz Wirger
1 parent
9db7c1ab
Add scheduleScene to Group so scenes can be scheduled.
Needed because setScene is not available in StateTransaction.
Showing
6 changed files
with
41 additions
and
5 deletions
include/hueplusplus/APICache.h
| ... | ... | @@ -74,6 +74,8 @@ public: |
| 74 | 74 | |
| 75 | 75 | //! \brief Get HueCommandAPI used for requests |
| 76 | 76 | HueCommandAPI& getCommandAPI(); |
| 77 | + //! \brief Get HueCommandAPI used for requests | |
| 78 | + const HueCommandAPI& getCommandAPI() const; | |
| 77 | 79 | |
| 78 | 80 | //! \brief Get path the cache is refreshed from |
| 79 | 81 | //! \returns Request path as passed to HueCommandAPI::GETRequest | ... | ... |
include/hueplusplus/Group.h
| ... | ... | @@ -264,6 +264,13 @@ public: |
| 264 | 264 | //! \param scene Scene name. |
| 265 | 265 | void setScene(const std::string& scene); |
| 266 | 266 | |
| 267 | + //! \brief Get ScheduleCommand to set scene | |
| 268 | + //! \param scene Scene name | |
| 269 | + //! \returns A ScheduleCommand that can be used to set the scene on a Schedule | |
| 270 | + //! | |
| 271 | + //! To set other light properties in a scene, use transaction(). | |
| 272 | + ScheduleCommand scheduleScene(const std::string& scene) const; | |
| 273 | + | |
| 267 | 274 | ///@} |
| 268 | 275 | |
| 269 | 276 | protected: | ... | ... |
include/hueplusplus/HueCommandAPI.h
| ... | ... | @@ -62,7 +62,7 @@ public: |
| 62 | 62 | |
| 63 | 63 | //! \brief Sends a HTTP PUT request to the bridge and returns the response |
| 64 | 64 | //! |
| 65 | - //! This function will block until at least \ref minDelay has passed to any previous request | |
| 65 | + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request | |
| 66 | 66 | //! \param path API request path (appended after /api/{username}) |
| 67 | 67 | //! \param request Request to the api, may be empty |
| 68 | 68 | //! \param fileInfo File information for thrown exceptions. |
| ... | ... | @@ -76,7 +76,7 @@ public: |
| 76 | 76 | |
| 77 | 77 | //! \brief Sends a HTTP GET request to the bridge and returns the response |
| 78 | 78 | //! |
| 79 | - //! This function will block until at least \ref minDelay has passed to any previous request | |
| 79 | + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request | |
| 80 | 80 | //! \param path API request path (appended after /api/{username}) |
| 81 | 81 | //! \param request Request to the api, may be empty |
| 82 | 82 | //! \param fileInfo File information for thrown exceptions. |
| ... | ... | @@ -91,7 +91,7 @@ public: |
| 91 | 91 | |
| 92 | 92 | //! \brief Sends a HTTP DELETE request to the bridge and returns the response |
| 93 | 93 | //! |
| 94 | - //! This function will block until at least \ref minDelay has passed to any previous request | |
| 94 | + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request | |
| 95 | 95 | //! \param path API request path (appended after /api/{username}) |
| 96 | 96 | //! \param request Request to the api, may be empty |
| 97 | 97 | //! \param fileInfo File information for thrown exceptions. |
| ... | ... | @@ -106,7 +106,7 @@ public: |
| 106 | 106 | |
| 107 | 107 | //! \brief Sends a HTTP POST request to the bridge and returns the response |
| 108 | 108 | //! |
| 109 | - //! This function will block until at least \ref minDelay has passed to any previous request | |
| 109 | + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request | |
| 110 | 110 | //! \param path API request path (appended after /api/{username}) |
| 111 | 111 | //! \param request Request to the api, may be empty |
| 112 | 112 | //! \param fileInfo File information for thrown exceptions. | ... | ... |
src/APICache.cpp
| ... | ... | @@ -118,6 +118,11 @@ HueCommandAPI& APICache::getCommandAPI() |
| 118 | 118 | return commands; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | +const HueCommandAPI& APICache::getCommandAPI() const | |
| 122 | +{ | |
| 123 | + return commands; | |
| 124 | +} | |
| 125 | + | |
| 121 | 126 | bool APICache::needsRefresh() |
| 122 | 127 | { |
| 123 | 128 | using clock = std::chrono::steady_clock; | ... | ... |
src/Group.cpp
| ... | ... | @@ -141,7 +141,8 @@ std::string Group::getActionColorMode() const |
| 141 | 141 | StateTransaction Group::transaction() |
| 142 | 142 | { |
| 143 | 143 | // Do not pass state, because it is not the state of ALL lights in the group |
| 144 | - return StateTransaction(state.getCommandAPI(), "/groups/" + std::to_string(id) + "/action", nlohmann::json::object()); | |
| 144 | + return StateTransaction( | |
| 145 | + state.getCommandAPI(), "/groups/" + std::to_string(id) + "/action", nlohmann::json::object()); | |
| 145 | 146 | } |
| 146 | 147 | |
| 147 | 148 | void Group::setOn(bool on, uint8_t transition) |
| ... | ... | @@ -179,6 +180,14 @@ void Group::setScene(const std::string& scene) |
| 179 | 180 | sendPutRequest({{"scene", scene}}, "/action", CURRENT_FILE_INFO); |
| 180 | 181 | } |
| 181 | 182 | |
| 183 | +ScheduleCommand Group::scheduleScene(const std::string& scene) const | |
| 184 | +{ | |
| 185 | + const nlohmann::json command {{"method", "PUT"}, | |
| 186 | + {"address", state.getCommandAPI().combinedPath("/groups/" + std::to_string(id) + "/action")}, | |
| 187 | + {"body", {{"scene", scene}}}}; | |
| 188 | + return ScheduleCommand(command); | |
| 189 | +} | |
| 190 | + | |
| 182 | 191 | nlohmann::json Group::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) |
| 183 | 192 | { |
| 184 | 193 | return state.getCommandAPI().PUTRequest("/groups/" + std::to_string(id) + subPath, request, std::move(fileInfo)); | ... | ... |
test/test_Group.cpp
| ... | ... | @@ -253,6 +253,19 @@ TEST_F(GroupTest, setScene) |
| 253 | 253 | group.setScene(scene); |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | +TEST_F(GroupTest, scheduleScene) | |
| 257 | +{ | |
| 258 | + const int id = 1; | |
| 259 | + expectGetState(id); | |
| 260 | + const Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 261 | + const std::string scene = "testScene"; | |
| 262 | + nlohmann::json request = { {"scene", scene} }; | |
| 263 | + ScheduleCommand command = group.scheduleScene(scene); | |
| 264 | + EXPECT_EQ(ScheduleCommand::Method::put, command.getMethod()); | |
| 265 | + EXPECT_EQ("/api/" + getBridgeUsername() + "/groups/1/action", command.getAddress()); | |
| 266 | + EXPECT_EQ(request, command.getBody()); | |
| 267 | +} | |
| 268 | + | |
| 256 | 269 | TEST(CreateGroup, LightGroup) |
| 257 | 270 | { |
| 258 | 271 | EXPECT_EQ(nlohmann::json({{"lights", {"1"}}, {"type", "LightGroup"}, {"name", "Name"}}), | ... | ... |