From 0294e540145afba74c5e5242c75dcb9a218aaf3d Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Tue, 5 May 2020 21:33:46 +0200 Subject: [PATCH] Add scheduleScene to Group so scenes can be scheduled. --- include/hueplusplus/APICache.h | 2 ++ include/hueplusplus/Group.h | 7 +++++++ include/hueplusplus/HueCommandAPI.h | 8 ++++---- src/APICache.cpp | 5 +++++ src/Group.cpp | 11 ++++++++++- test/test_Group.cpp | 13 +++++++++++++ 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/include/hueplusplus/APICache.h b/include/hueplusplus/APICache.h index 15e1f26..d5314c8 100644 --- a/include/hueplusplus/APICache.h +++ b/include/hueplusplus/APICache.h @@ -74,6 +74,8 @@ public: //! \brief Get HueCommandAPI used for requests HueCommandAPI& getCommandAPI(); + //! \brief Get HueCommandAPI used for requests + const HueCommandAPI& getCommandAPI() const; //! \brief Get path the cache is refreshed from //! \returns Request path as passed to HueCommandAPI::GETRequest diff --git a/include/hueplusplus/Group.h b/include/hueplusplus/Group.h index 7d44fb7..c61e596 100644 --- a/include/hueplusplus/Group.h +++ b/include/hueplusplus/Group.h @@ -264,6 +264,13 @@ public: //! \param scene Scene name. void setScene(const std::string& scene); + //! \brief Get ScheduleCommand to set scene + //! \param scene Scene name + //! \returns A ScheduleCommand that can be used to set the scene on a Schedule + //! + //! To set other light properties in a scene, use transaction(). + ScheduleCommand scheduleScene(const std::string& scene) const; + ///@} protected: diff --git a/include/hueplusplus/HueCommandAPI.h b/include/hueplusplus/HueCommandAPI.h index ab747cd..eca0bf9 100644 --- a/include/hueplusplus/HueCommandAPI.h +++ b/include/hueplusplus/HueCommandAPI.h @@ -62,7 +62,7 @@ public: //! \brief Sends a HTTP PUT request to the bridge and returns the response //! - //! This function will block until at least \ref minDelay has passed to any previous request + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request //! \param path API request path (appended after /api/{username}) //! \param request Request to the api, may be empty //! \param fileInfo File information for thrown exceptions. @@ -76,7 +76,7 @@ public: //! \brief Sends a HTTP GET request to the bridge and returns the response //! - //! This function will block until at least \ref minDelay has passed to any previous request + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request //! \param path API request path (appended after /api/{username}) //! \param request Request to the api, may be empty //! \param fileInfo File information for thrown exceptions. @@ -91,7 +91,7 @@ public: //! \brief Sends a HTTP DELETE request to the bridge and returns the response //! - //! This function will block until at least \ref minDelay has passed to any previous request + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request //! \param path API request path (appended after /api/{username}) //! \param request Request to the api, may be empty //! \param fileInfo File information for thrown exceptions. @@ -106,7 +106,7 @@ public: //! \brief Sends a HTTP POST request to the bridge and returns the response //! - //! This function will block until at least \ref minDelay has passed to any previous request + //! This function will block until at least Config::getBridgeRequestDelay() has passed to any previous request //! \param path API request path (appended after /api/{username}) //! \param request Request to the api, may be empty //! \param fileInfo File information for thrown exceptions. diff --git a/src/APICache.cpp b/src/APICache.cpp index 7b3d151..5b03074 100644 --- a/src/APICache.cpp +++ b/src/APICache.cpp @@ -118,6 +118,11 @@ HueCommandAPI& APICache::getCommandAPI() return commands; } +const HueCommandAPI& APICache::getCommandAPI() const +{ + return commands; +} + bool APICache::needsRefresh() { using clock = std::chrono::steady_clock; diff --git a/src/Group.cpp b/src/Group.cpp index 1254e56..ddaa962 100644 --- a/src/Group.cpp +++ b/src/Group.cpp @@ -141,7 +141,8 @@ std::string Group::getActionColorMode() const StateTransaction Group::transaction() { // Do not pass state, because it is not the state of ALL lights in the group - return StateTransaction(state.getCommandAPI(), "/groups/" + std::to_string(id) + "/action", nlohmann::json::object()); + return StateTransaction( + state.getCommandAPI(), "/groups/" + std::to_string(id) + "/action", nlohmann::json::object()); } void Group::setOn(bool on, uint8_t transition) @@ -179,6 +180,14 @@ void Group::setScene(const std::string& scene) sendPutRequest({{"scene", scene}}, "/action", CURRENT_FILE_INFO); } +ScheduleCommand Group::scheduleScene(const std::string& scene) const +{ + const nlohmann::json command {{"method", "PUT"}, + {"address", state.getCommandAPI().combinedPath("/groups/" + std::to_string(id) + "/action")}, + {"body", {{"scene", scene}}}}; + return ScheduleCommand(command); +} + nlohmann::json Group::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) { return state.getCommandAPI().PUTRequest("/groups/" + std::to_string(id) + subPath, request, std::move(fileInfo)); diff --git a/test/test_Group.cpp b/test/test_Group.cpp index 8a2fbcc..e9b1e2e 100644 --- a/test/test_Group.cpp +++ b/test/test_Group.cpp @@ -253,6 +253,19 @@ TEST_F(GroupTest, setScene) group.setScene(scene); } +TEST_F(GroupTest, scheduleScene) +{ + const int id = 1; + expectGetState(id); + const Group group(id, commands, std::chrono::steady_clock::duration::max()); + const std::string scene = "testScene"; + nlohmann::json request = { {"scene", scene} }; + ScheduleCommand command = group.scheduleScene(scene); + EXPECT_EQ(ScheduleCommand::Method::put, command.getMethod()); + EXPECT_EQ("/api/" + getBridgeUsername() + "/groups/1/action", command.getAddress()); + EXPECT_EQ(request, command.getBody()); +} + TEST(CreateGroup, LightGroup) { EXPECT_EQ(nlohmann::json({{"lights", {"1"}}, {"type", "LightGroup"}, {"name", "Name"}}), -- libgit2 0.21.4