Commit 9db7c1abe2e9d7d51fd22cc1f73a8892492592b0
Committed by
Moritz Wirger
1 parent
505a0e05
Add toScheduleCommand to StateTransaction.
Showing
5 changed files
with
45 additions
and
9 deletions
include/hueplusplus/HueCommandAPI.h
| @@ -119,6 +119,9 @@ public: | @@ -119,6 +119,9 @@ public: | ||
| 119 | //! \overload | 119 | //! \overload |
| 120 | nlohmann::json POSTRequest(const std::string& path, const nlohmann::json& request) const; | 120 | nlohmann::json POSTRequest(const std::string& path, const nlohmann::json& request) const; |
| 121 | 121 | ||
| 122 | + //! \brief Combines path with api prefix and username | ||
| 123 | + //! \returns "/api/<username>/<path>" | ||
| 124 | + std::string combinedPath(const std::string& path) const; | ||
| 122 | private: | 125 | private: |
| 123 | struct TimeoutData | 126 | struct TimeoutData |
| 124 | { | 127 | { |
| @@ -131,10 +134,6 @@ private: | @@ -131,10 +134,6 @@ private: | ||
| 131 | //! \returns \ref response if there is no error | 134 | //! \returns \ref response if there is no error |
| 132 | nlohmann::json HandleError(FileInfo fileInfo, const nlohmann::json& response) const; | 135 | nlohmann::json HandleError(FileInfo fileInfo, const nlohmann::json& response) const; |
| 133 | 136 | ||
| 134 | - //! \brief Combines path with api prefix and username | ||
| 135 | - //! \returns "/api/<username>/<path>" | ||
| 136 | - std::string CombinedPath(const std::string& path) const; | ||
| 137 | - | ||
| 138 | private: | 137 | private: |
| 139 | std::string ip; | 138 | std::string ip; |
| 140 | int port; | 139 | int port; |
include/hueplusplus/StateTransaction.h
| @@ -26,6 +26,7 @@ | @@ -26,6 +26,7 @@ | ||
| 26 | #include <string> | 26 | #include <string> |
| 27 | 27 | ||
| 28 | #include "HueCommandAPI.h" | 28 | #include "HueCommandAPI.h" |
| 29 | +#include "Schedule.h" | ||
| 29 | 30 | ||
| 30 | #include "json/json.hpp" | 31 | #include "json/json.hpp" |
| 31 | 32 | ||
| @@ -64,6 +65,10 @@ public: | @@ -64,6 +65,10 @@ public: | ||
| 64 | //! \throws nlohmann::json::parse_error when response could not be parsed | 65 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 65 | bool commit(bool trimRequest = true) &&; | 66 | bool commit(bool trimRequest = true) &&; |
| 66 | 67 | ||
| 68 | + //! \brief Create a ScheduleCommand from the transaction | ||
| 69 | + //! \returns A ScheduleCommand that can be used to execute this transaction on a Schedule. | ||
| 70 | + ScheduleCommand toScheduleCommand() &&; | ||
| 71 | + | ||
| 67 | //! \brief Turn light on or off. | 72 | //! \brief Turn light on or off. |
| 68 | //! \param on true for on, false for off | 73 | //! \param on true for on, false for off |
| 69 | //! \returns This transaction for chaining calls | 74 | //! \returns This transaction for chaining calls |
src/HueCommandAPI.cpp
| @@ -81,7 +81,7 @@ nlohmann::json HueCommandAPI::PUTRequest( | @@ -81,7 +81,7 @@ nlohmann::json HueCommandAPI::PUTRequest( | ||
| 81 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const | 81 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const |
| 82 | { | 82 | { |
| 83 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { | 83 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { |
| 84 | - return httpHandler->PUTJson(CombinedPath(path), request, ip, port); | 84 | + return httpHandler->PUTJson(combinedPath(path), request, ip, port); |
| 85 | })); | 85 | })); |
| 86 | } | 86 | } |
| 87 | 87 | ||
| @@ -94,7 +94,7 @@ nlohmann::json HueCommandAPI::GETRequest( | @@ -94,7 +94,7 @@ nlohmann::json HueCommandAPI::GETRequest( | ||
| 94 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const | 94 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const |
| 95 | { | 95 | { |
| 96 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { | 96 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { |
| 97 | - return httpHandler->GETJson(CombinedPath(path), request, ip, port); | 97 | + return httpHandler->GETJson(combinedPath(path), request, ip, port); |
| 98 | })); | 98 | })); |
| 99 | } | 99 | } |
| 100 | 100 | ||
| @@ -107,7 +107,7 @@ nlohmann::json HueCommandAPI::DELETERequest( | @@ -107,7 +107,7 @@ nlohmann::json HueCommandAPI::DELETERequest( | ||
| 107 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const | 107 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const |
| 108 | { | 108 | { |
| 109 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { | 109 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { |
| 110 | - return httpHandler->DELETEJson(CombinedPath(path), request, ip, port); | 110 | + return httpHandler->DELETEJson(combinedPath(path), request, ip, port); |
| 111 | })); | 111 | })); |
| 112 | } | 112 | } |
| 113 | 113 | ||
| @@ -120,7 +120,7 @@ nlohmann::json HueCommandAPI::POSTRequest( | @@ -120,7 +120,7 @@ nlohmann::json HueCommandAPI::POSTRequest( | ||
| 120 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const | 120 | const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const |
| 121 | { | 121 | { |
| 122 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { | 122 | return HandleError(std::move(fileInfo), RunWithTimeout(timeout, Config::instance().getBridgeRequestDelay(), [&]() { |
| 123 | - return httpHandler->POSTJson(CombinedPath(path), request, ip, port); | 123 | + return httpHandler->POSTJson(combinedPath(path), request, ip, port); |
| 124 | })); | 124 | })); |
| 125 | } | 125 | } |
| 126 | 126 | ||
| @@ -143,7 +143,7 @@ nlohmann::json HueCommandAPI::HandleError(FileInfo fileInfo, const nlohmann::jso | @@ -143,7 +143,7 @@ nlohmann::json HueCommandAPI::HandleError(FileInfo fileInfo, const nlohmann::jso | ||
| 143 | return response; | 143 | return response; |
| 144 | } | 144 | } |
| 145 | 145 | ||
| 146 | -std::string HueCommandAPI::CombinedPath(const std::string& path) const | 146 | +std::string HueCommandAPI::combinedPath(const std::string& path) const |
| 147 | { | 147 | { |
| 148 | std::string result = "/api/"; | 148 | std::string result = "/api/"; |
| 149 | result.append(username); | 149 | result.append(username); |
src/StateTransaction.cpp
| @@ -68,6 +68,12 @@ bool StateTransaction::commit(bool trimRequest) && | @@ -68,6 +68,12 @@ bool StateTransaction::commit(bool trimRequest) && | ||
| 68 | return true; | 68 | return true; |
| 69 | } | 69 | } |
| 70 | 70 | ||
| 71 | +ScheduleCommand StateTransaction::toScheduleCommand() && | ||
| 72 | +{ | ||
| 73 | + nlohmann::json command {{"method", "PUT"}, {"address", commands.combinedPath(path)}, {"body", request}}; | ||
| 74 | + return ScheduleCommand(command); | ||
| 75 | +} | ||
| 76 | + | ||
| 71 | StateTransaction&& StateTransaction::setOn(bool on) && | 77 | StateTransaction&& StateTransaction::setOn(bool on) && |
| 72 | { | 78 | { |
| 73 | request["on"] = on; | 79 | request["on"] = on; |
test/test_StateTransaction.cpp
| @@ -49,6 +49,32 @@ TEST(StateTransaction, commit) | @@ -49,6 +49,32 @@ TEST(StateTransaction, commit) | ||
| 49 | StateTransaction(commands, "/path", nlohmann::json::object()).setOn(false).setBrightness(100).commit()); | 49 | StateTransaction(commands, "/path", nlohmann::json::object()).setOn(false).setBrightness(100).commit()); |
| 50 | Mock::VerifyAndClearExpectations(handler.get()); | 50 | Mock::VerifyAndClearExpectations(handler.get()); |
| 51 | } | 51 | } |
| 52 | + // Do not trim | ||
| 53 | + { | ||
| 54 | + const nlohmann::json request = {{"on", false}, {"bri", 100}}; | ||
| 55 | + const nlohmann::json state = {{"on", false}, {"bri", 100}}; | ||
| 56 | + nlohmann::json response = {{{"success", {{"/path/on", false}}}}, {{"success", {{"/path/bri", 100}}}}}; | ||
| 57 | + EXPECT_CALL(*handler, PUTJson(requestPath, request, getBridgeIp(), getBridgePort())).WillOnce(Return(response)); | ||
| 58 | + EXPECT_TRUE(StateTransaction(commands, "/path", state).setOn(false).setBrightness(100).commit(false)); | ||
| 59 | + Mock::VerifyAndClearExpectations(handler.get()); | ||
| 60 | + } | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +TEST(StateTransaction, toScheduleCommand) | ||
| 64 | +{ | ||
| 65 | + auto handler = std::make_shared<MockHttpHandler>(); | ||
| 66 | + HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); | ||
| 67 | + const std::string requestPath = "/api/" + getBridgeUsername() + "/path"; | ||
| 68 | + nlohmann::json request = {{"on", false}, {"bri", 100}}; | ||
| 69 | + | ||
| 70 | + ScheduleCommand command = StateTransaction(commands, "/path", nlohmann::json::object()) | ||
| 71 | + .setOn(false) | ||
| 72 | + .setBrightness(100) | ||
| 73 | + .toScheduleCommand(); | ||
| 74 | + Mock::VerifyAndClearExpectations(handler.get()); | ||
| 75 | + EXPECT_EQ(ScheduleCommand::Method::put, command.getMethod()); | ||
| 76 | + EXPECT_EQ(request, command.getBody()); | ||
| 77 | + EXPECT_EQ(requestPath, command.getAddress()); | ||
| 52 | } | 78 | } |
| 53 | 79 | ||
| 54 | TEST(StateTransaction, setOn) | 80 | TEST(StateTransaction, setOn) |