Commit c5123c134cb61775aa7a7f4f280b07ddcb30b54e

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent a8eb8aa7

Rename ScheduleCommand to Action, to make it more general.

.gitignore
@@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
30 30
31 # build directory 31 # build directory
32 /build* 32 /build*
  33 +/out
33 34
34 # Generated documentation 35 # Generated documentation
35 /doc/html 36 /doc/html
include/hueplusplus/Action.h 0 → 100644
  1 +/**
  2 + \file Action.h
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 +
  6 + This file is part of hueplusplus.
  7 +
  8 + hueplusplus is free software: you can redistribute it and/or modify
  9 + it under the terms of the GNU Lesser General Public License as published by
  10 + the Free Software Foundation, either version 3 of the License, or
  11 + (at your option) any later version.
  12 +
  13 + hueplusplus is distributed in the hope that it will be useful,
  14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 + GNU Lesser General Public License for more details.
  17 +
  18 + You should have received a copy of the GNU Lesser General Public License
  19 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  20 +**/
  21 +
  22 +#ifndef INCLUDE_HUEPLUSPLUS_ACTION_H
  23 +#define INCLUDE_HUEPLUSPLUS_ACTION_H
  24 +
  25 +#include "json/json.hpp"
  26 +
  27 +namespace hueplusplus
  28 +{
  29 +//! \brief Action executed by the bridge, e.g. as a Schedule command
  30 +//!
  31 +//! The action makes either a POST, PUT or DELETE request with a given body
  32 +//! to an address on the bridge.
  33 +//!
  34 +//! The Action can also be created by StateTransaction::toAction().
  35 +class Action
  36 +{
  37 +public:
  38 + //! \brief Create Action from json
  39 + //! \param json JSON object with address, method and body
  40 + explicit Action(const nlohmann::json& json);
  41 +
  42 + //! \brief Method used for the command
  43 + enum class Method
  44 + {
  45 + post, //!< POST request
  46 + put, //!< PUT request
  47 + deleteMethod //!< DELETE request
  48 + };
  49 +
  50 + //! \brief Get address the request is made to
  51 + std::string getAddress() const;
  52 + //! \brief Get request method
  53 + Method getMethod() const;
  54 + //! \brief Get request body
  55 + const nlohmann::json& getBody() const;
  56 +
  57 + //! \brief Get json object of command
  58 + const nlohmann::json& toJson() const;
  59 +
  60 +public:
  61 + //! \brief Parse Method from string
  62 + //! \param s \c POST, \c PUT or \c DELETE
  63 + static Method parseMethod(const std::string& s);
  64 + //! \brief Get string from Method
  65 + //! \returns \c POST, \c PUT or \c DELETE
  66 + static std::string methodToString(Method m);
  67 +
  68 +private:
  69 + nlohmann::json json;
  70 +};
  71 +} // namespace hueplusplus
  72 +
  73 +#endif
include/hueplusplus/Group.h
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
27 #include <vector> 27 #include <vector>
28 28
29 #include "APICache.h" 29 #include "APICache.h"
  30 +#include "Action.h"
30 #include "HueCommandAPI.h" 31 #include "HueCommandAPI.h"
31 #include "StateTransaction.h" 32 #include "StateTransaction.h"
32 33
@@ -265,12 +266,12 @@ public: @@ -265,12 +266,12 @@ public:
265 //! \param scene Scene name. 266 //! \param scene Scene name.
266 void setScene(const std::string& scene); 267 void setScene(const std::string& scene);
267 268
268 - //! \brief Get ScheduleCommand to set scene 269 + //! \brief Get Action to set scene
269 //! \param scene Scene name 270 //! \param scene Scene name
270 - //! \returns A ScheduleCommand that can be used to set the scene on a Schedule 271 + //! \returns A Action that can be used to set the scene on a Schedule
271 //! 272 //!
272 //! To set other light properties in a scene, use transaction(). 273 //! To set other light properties in a scene, use transaction().
273 - ScheduleCommand scheduleScene(const std::string& scene) const; 274 + Action createSceneAction(const std::string& scene) const;
274 275
275 ///@} 276 ///@}
276 277
include/hueplusplus/Rule.h
@@ -25,8 +25,8 @@ @@ -25,8 +25,8 @@
25 #include <string> 25 #include <string>
26 26
27 #include "APICache.h" 27 #include "APICache.h"
  28 +#include "Action.h"
28 #include "HueCommandAPI.h" 29 #include "HueCommandAPI.h"
29 -#include "Schedule.h"  
30 #include "TimePattern.h" 30 #include "TimePattern.h"
31 31
32 namespace hueplusplus 32 namespace hueplusplus
@@ -59,6 +59,7 @@ public: @@ -59,6 +59,7 @@ public:
59 nlohmann::json toJson() const; 59 nlohmann::json toJson() const;
60 60
61 static Condition parse(const nlohmann::json& json); 61 static Condition parse(const nlohmann::json& json);
  62 +
62 private: 63 private:
63 std::string address; 64 std::string address;
64 Operator op; 65 Operator op;
@@ -105,7 +106,8 @@ public: @@ -105,7 +106,8 @@ public:
105 std::string getOwner() const; 106 std::string getOwner() const;
106 107
107 std::vector<Condition> getConditions() const; 108 std::vector<Condition> getConditions() const;
108 - std::vector<ScheduleCommand> getActions() const; 109 + std::vector<Action> getActions() const;
  110 +
109 private: 111 private:
110 int id; 112 int id;
111 APICache state; 113 APICache state;
include/hueplusplus/Schedule.h
@@ -23,53 +23,11 @@ @@ -23,53 +23,11 @@
23 #define INCLUDE_HUEPLUSPLUS_SCHEDULE_H 23 #define INCLUDE_HUEPLUSPLUS_SCHEDULE_H
24 24
25 #include "APICache.h" 25 #include "APICache.h"
  26 +#include "Action.h"
26 #include "TimePattern.h" 27 #include "TimePattern.h"
27 28
28 namespace hueplusplus 29 namespace hueplusplus
29 { 30 {
30 -//! \brief Command executed on a Schedule  
31 -//!  
32 -//! The command makes either a POST, PUT or DELETE request with a given body  
33 -//! to an address on the bridge.  
34 -//!  
35 -//! A ScheduleCommand can also be created by StateTransaction::toScheduleCommand().  
36 -class ScheduleCommand  
37 -{  
38 -public:  
39 - //! \brief Create ScheduleCommand from json  
40 - //! \param json JSON object with address, method and body  
41 - explicit ScheduleCommand(const nlohmann::json& json);  
42 -  
43 - //! \brief Method used for the command  
44 - enum class Method  
45 - {  
46 - post, //!< POST request  
47 - put, //!< PUT request  
48 - deleteMethod //!< DELETE request  
49 - };  
50 -  
51 - //! \brief Get address the request is made to  
52 - std::string getAddress() const;  
53 - //! \brief Get request method  
54 - Method getMethod() const;  
55 - //! \brief Get request body  
56 - const nlohmann::json& getBody() const;  
57 -  
58 - //! \brief Get json object of command  
59 - const nlohmann::json& toJson() const;  
60 -  
61 -private:  
62 - //! \brief Parse Method from string  
63 - //! \param s \c POST, \c PUT or \c DELETE  
64 - static Method parseMethod(const std::string& s);  
65 - //! \brief Get string from Method  
66 - //! \returns \c POST, \c PUT or \c DELETE  
67 - static std::string methodToString(Method m);  
68 -  
69 -private:  
70 - nlohmann::json json;  
71 -};  
72 -  
73 //! \brief Schedule stored in the bridge 31 //! \brief Schedule stored in the bridge
74 //! 32 //!
75 //! A schedule can be created by the user to trigger actions at specific times. 33 //! A schedule can be created by the user to trigger actions at specific times.
@@ -99,7 +57,7 @@ public: @@ -99,7 +57,7 @@ public:
99 //! \brief Get schedule description 57 //! \brief Get schedule description
100 std::string getDescription() const; 58 std::string getDescription() const;
101 //! \brief Get schedule command 59 //! \brief Get schedule command
102 - ScheduleCommand getCommand() const; 60 + Action getCommand() const;
103 //! \brief Get time when the event(s) will occur 61 //! \brief Get time when the event(s) will occur
104 //! \returns TimePattern in local timezone 62 //! \returns TimePattern in local timezone
105 time::TimePattern getTime() const; 63 time::TimePattern getTime() const;
@@ -134,12 +92,12 @@ public: @@ -134,12 +92,12 @@ public:
134 //! \throws nlohmann::json::parse_error when response could not be parsed 92 //! \throws nlohmann::json::parse_error when response could not be parsed
135 void setDescription(const std::string& description); 93 void setDescription(const std::string& description);
136 //! \brief Set schedule command 94 //! \brief Set schedule command
137 - //! \param command New command that is executed when the time event occurs. 95 + //! \param command New action that is executed when the time event occurs.
138 //! \throws std::system_error when system or socket operations fail 96 //! \throws std::system_error when system or socket operations fail
139 //! \throws HueException when response contained no body 97 //! \throws HueException when response contained no body
140 //! \throws HueAPIResponseException when response contains an error 98 //! \throws HueAPIResponseException when response contains an error
141 //! \throws nlohmann::json::parse_error when response could not be parsed 99 //! \throws nlohmann::json::parse_error when response could not be parsed
142 - void setCommand(const ScheduleCommand& command); 100 + void setCommand(const Action& command);
143 //! \brief Set new time when the event will occur 101 //! \brief Set new time when the event will occur
144 //! \param timePattern Any possible value of TimePattern 102 //! \param timePattern Any possible value of TimePattern
145 //! \throws std::system_error when system or socket operations fail 103 //! \throws std::system_error when system or socket operations fail
@@ -191,7 +149,7 @@ public: @@ -191,7 +149,7 @@ public:
191 CreateSchedule& setDescription(const std::string& description); 149 CreateSchedule& setDescription(const std::string& description);
192 //! \brief Set command 150 //! \brief Set command
193 //! \see Schedule::setCommand 151 //! \see Schedule::setCommand
194 - CreateSchedule& setCommand(const ScheduleCommand& command); 152 + CreateSchedule& setCommand(const Action& command);
195 //! \brief Set time 153 //! \brief Set time
196 //! \see Schedule::setTime 154 //! \see Schedule::setTime
197 CreateSchedule& setTime(const time::TimePattern& time); 155 CreateSchedule& setTime(const time::TimePattern& time);
include/hueplusplus/StateTransaction.h
@@ -25,9 +25,9 @@ @@ -25,9 +25,9 @@
25 25
26 #include <string> 26 #include <string>
27 27
  28 +#include "Action.h"
28 #include "ColorUnits.h" 29 #include "ColorUnits.h"
29 #include "HueCommandAPI.h" 30 #include "HueCommandAPI.h"
30 -#include "Schedule.h"  
31 31
32 #include "json/json.hpp" 32 #include "json/json.hpp"
33 33
@@ -54,7 +54,7 @@ namespace hueplusplus @@ -54,7 +54,7 @@ namespace hueplusplus
54 //! In this case, it is especially important that the light and the state of the light MUST NOT invalidate. 54 //! In this case, it is especially important that the light and the state of the light MUST NOT invalidate.
55 //! That means 55 //! That means
56 //! \li the light variable has to live longer than the transaction 56 //! \li the light variable has to live longer than the transaction
57 -//! \li especially no non-const method calls on the light while the transaction is open, 57 +//! \li especially no non-const method calls on the light while the transaction is open,
58 //! or committing other transactions 58 //! or committing other transactions
59 //! 59 //!
60 //! In general, this method is easier to screw up and should only be used when really necessary. 60 //! In general, this method is easier to screw up and should only be used when really necessary.
@@ -85,9 +85,9 @@ public: @@ -85,9 +85,9 @@ public:
85 //! \throws nlohmann::json::parse_error when response could not be parsed 85 //! \throws nlohmann::json::parse_error when response could not be parsed
86 bool commit(bool trimRequest = true); 86 bool commit(bool trimRequest = true);
87 87
88 - //! \brief Create a ScheduleCommand from the transaction  
89 - //! \returns A ScheduleCommand that can be used to execute this transaction on a Schedule.  
90 - ScheduleCommand toScheduleCommand(); 88 + //! \brief Create an Action from the transaction
  89 + //! \returns An Action that can be used to execute this transaction on a Schedule or Rule.
  90 + Action toAction();
91 91
92 //! \brief Turn light on or off. 92 //! \brief Turn light on or off.
93 //! \param on true for on, false for off 93 //! \param on true for on, false for off
src/Action.cpp 0 → 100644
  1 +/**
  2 + \file Action.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 +
  6 + This file is part of hueplusplus.
  7 +
  8 + hueplusplus is free software: you can redistribute it and/or modify
  9 + it under the terms of the GNU Lesser General Public License as published by
  10 + the Free Software Foundation, either version 3 of the License, or
  11 + (at your option) any later version.
  12 +
  13 + hueplusplus is distributed in the hope that it will be useful,
  14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 + GNU Lesser General Public License for more details.
  17 +
  18 + You should have received a copy of the GNU Lesser General Public License
  19 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  20 +**/
  21 +
  22 +#include <hueplusplus/Action.h>
  23 +#include <hueplusplus/HueExceptionMacro.h>
  24 +
  25 +namespace hueplusplus
  26 +{
  27 +
  28 +Action::Action(const nlohmann::json& json) : json(json) { }
  29 +
  30 +std::string Action::getAddress() const
  31 +{
  32 + return json.at("address").get<std::string>();
  33 +}
  34 +
  35 +Action::Method Action::getMethod() const
  36 +{
  37 + return parseMethod(json.at("method").get<std::string>());
  38 +}
  39 +
  40 +const nlohmann::json& Action::getBody() const
  41 +{
  42 + return json.at("body");
  43 +}
  44 +
  45 +const nlohmann::json& Action::toJson() const
  46 +{
  47 + return json;
  48 +}
  49 +
  50 +Action::Method Action::parseMethod(const std::string& s)
  51 +{
  52 + if (s == "POST")
  53 + {
  54 + return Method::post;
  55 + }
  56 + else if (s == "PUT")
  57 + {
  58 + return Method::put;
  59 + }
  60 + else if (s == "DELETE")
  61 + {
  62 + return Method::deleteMethod;
  63 + }
  64 + throw HueException(CURRENT_FILE_INFO, "Unknown ScheduleCommand method: " + s);
  65 +}
  66 +
  67 +std::string Action::methodToString(Method m)
  68 +{
  69 + switch (m)
  70 + {
  71 + case Method::post:
  72 + return "POST";
  73 + case Method::put:
  74 + return "PUT";
  75 + case Method::deleteMethod:
  76 + return "DELETE";
  77 + default:
  78 + throw HueException(
  79 + CURRENT_FILE_INFO, "Unknown ScheduleCommand method enum: " + std::to_string(static_cast<int>(m)));
  80 + }
  81 +}
  82 +
  83 +} // namespace hueplusplus
0 \ No newline at end of file 84 \ No newline at end of file
src/CMakeLists.txt
@@ -22,7 +22,7 @@ set(hueplusplus_SOURCES @@ -22,7 +22,7 @@ set(hueplusplus_SOURCES
22 StateTransaction.cpp 22 StateTransaction.cpp
23 TimePattern.cpp 23 TimePattern.cpp
24 UPnP.cpp 24 UPnP.cpp
25 - Utils.cpp "ZLLSensors.cpp" "CLIPSensors.cpp" "NewDeviceList.cpp") 25 + Utils.cpp "ZLLSensors.cpp" "CLIPSensors.cpp" "NewDeviceList.cpp" "Action.cpp")
26 26
27 # on windows we want to compile the WinHttpHandler 27 # on windows we want to compile the WinHttpHandler
28 if(WIN32) 28 if(WIN32)
src/Group.cpp
@@ -190,12 +190,12 @@ void Group::setScene(const std::string&amp; scene) @@ -190,12 +190,12 @@ void Group::setScene(const std::string&amp; scene)
190 sendPutRequest("/action", {{"scene", scene}}, CURRENT_FILE_INFO); 190 sendPutRequest("/action", {{"scene", scene}}, CURRENT_FILE_INFO);
191 } 191 }
192 192
193 -ScheduleCommand Group::scheduleScene(const std::string& scene) const 193 +Action Group::createSceneAction(const std::string& scene) const
194 { 194 {
195 const nlohmann::json command {{"method", "PUT"}, 195 const nlohmann::json command {{"method", "PUT"},
196 {"address", state.getCommandAPI().combinedPath("/groups/" + std::to_string(id) + "/action")}, 196 {"address", state.getCommandAPI().combinedPath("/groups/" + std::to_string(id) + "/action")},
197 {"body", {{"scene", scene}}}}; 197 {"body", {{"scene", scene}}}};
198 - return ScheduleCommand(command); 198 + return Action(command);
199 } 199 }
200 200
201 nlohmann::json Group::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) 201 nlohmann::json Group::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo)
src/Schedule.cpp
@@ -24,62 +24,6 @@ @@ -24,62 +24,6 @@
24 24
25 namespace hueplusplus 25 namespace hueplusplus
26 { 26 {
27 -  
28 -ScheduleCommand::ScheduleCommand(const nlohmann::json& json) : json(json) {}  
29 -  
30 -std::string ScheduleCommand::getAddress() const  
31 -{  
32 - return json.at("address").get<std::string>();  
33 -}  
34 -  
35 -ScheduleCommand::Method ScheduleCommand::getMethod() const  
36 -{  
37 - return parseMethod(json.at("method").get<std::string>());  
38 -}  
39 -  
40 -const nlohmann::json& ScheduleCommand::getBody() const  
41 -{  
42 - return json.at("body");  
43 -}  
44 -  
45 -const nlohmann::json& ScheduleCommand::toJson() const  
46 -{  
47 - return json;  
48 -}  
49 -  
50 -ScheduleCommand::Method ScheduleCommand::parseMethod(const std::string& s)  
51 -{  
52 - if (s == "POST")  
53 - {  
54 - return Method::post;  
55 - }  
56 - else if (s == "PUT")  
57 - {  
58 - return Method::put;  
59 - }  
60 - else if (s == "DELETE")  
61 - {  
62 - return Method::deleteMethod;  
63 - }  
64 - throw HueException(CURRENT_FILE_INFO, "Unknown ScheduleCommand method: " + s);  
65 -}  
66 -  
67 -std::string ScheduleCommand::methodToString(Method m)  
68 -{  
69 - switch (m)  
70 - {  
71 - case Method::post:  
72 - return "POST";  
73 - case Method::put:  
74 - return "PUT";  
75 - case Method::deleteMethod:  
76 - return "DELETE";  
77 - default:  
78 - throw HueException(  
79 - CURRENT_FILE_INFO, "Unknown ScheduleCommand method enum: " + std::to_string(static_cast<int>(m)));  
80 - }  
81 -}  
82 -  
83 Schedule::Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) 27 Schedule::Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration)
84 : id(id), state("/schedules/" + std::to_string(id), commands, refreshDuration) 28 : id(id), state("/schedules/" + std::to_string(id), commands, refreshDuration)
85 { 29 {
@@ -106,9 +50,9 @@ std::string Schedule::getDescription() const @@ -106,9 +50,9 @@ std::string Schedule::getDescription() const
106 return state.getValue().at("description").get<std::string>(); 50 return state.getValue().at("description").get<std::string>();
107 } 51 }
108 52
109 -ScheduleCommand Schedule::getCommand() const 53 +Action Schedule::getCommand() const
110 { 54 {
111 - return ScheduleCommand(state.getValue().at("command")); 55 + return Action(state.getValue().at("command"));
112 } 56 }
113 57
114 time::TimePattern Schedule::getTime() const 58 time::TimePattern Schedule::getTime() const
@@ -154,7 +98,7 @@ void Schedule::setDescription(const std::string&amp; description) @@ -154,7 +98,7 @@ void Schedule::setDescription(const std::string&amp; description)
154 refresh(); 98 refresh();
155 } 99 }
156 100
157 -void Schedule::setCommand(const ScheduleCommand& command) 101 +void Schedule::setCommand(const Action& command)
158 { 102 {
159 sendPutRequest({{"command", command.toJson()}}, CURRENT_FILE_INFO); 103 sendPutRequest({{"command", command.toJson()}}, CURRENT_FILE_INFO);
160 refresh(); 104 refresh();
@@ -203,7 +147,7 @@ CreateSchedule&amp; CreateSchedule::setDescription(const std::string&amp; description) @@ -203,7 +147,7 @@ CreateSchedule&amp; CreateSchedule::setDescription(const std::string&amp; description)
203 return *this; 147 return *this;
204 } 148 }
205 149
206 -CreateSchedule& CreateSchedule::setCommand(const ScheduleCommand& command) 150 +CreateSchedule& CreateSchedule::setCommand(const Action& command)
207 { 151 {
208 request["command"] = command.toJson(); 152 request["command"] = command.toJson();
209 return *this; 153 return *this;
src/StateTransaction.cpp
@@ -83,10 +83,10 @@ bool StateTransaction::commit(bool trimRequest) @@ -83,10 +83,10 @@ bool StateTransaction::commit(bool trimRequest)
83 return true; 83 return true;
84 } 84 }
85 85
86 -ScheduleCommand StateTransaction::toScheduleCommand() 86 +Action StateTransaction::toAction()
87 { 87 {
88 nlohmann::json command {{"method", "PUT"}, {"address", commands.combinedPath(path)}, {"body", request}}; 88 nlohmann::json command {{"method", "PUT"}, {"address", commands.combinedPath(path)}, {"body", request}};
89 - return ScheduleCommand(command); 89 + return Action(command);
90 } 90 }
91 91
92 StateTransaction& StateTransaction::setOn(bool on) 92 StateTransaction& StateTransaction::setOn(bool on)
test/CMakeLists.txt
@@ -54,7 +54,7 @@ set(TEST_SOURCES @@ -54,7 +54,7 @@ set(TEST_SOURCES
54 test_SimpleColorHueStrategy.cpp 54 test_SimpleColorHueStrategy.cpp
55 test_SimpleColorTemperatureStrategy.cpp 55 test_SimpleColorTemperatureStrategy.cpp
56 test_StateTransaction.cpp 56 test_StateTransaction.cpp
57 - test_TimePattern.cpp "test_NewDeviceList.cpp") 57 + test_TimePattern.cpp "test_NewDeviceList.cpp" "test_Action.cpp")
58 58
59 set(HuePlusPlus_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include") 59 set(HuePlusPlus_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/include")
60 60
test/test_Action.cpp 0 → 100644
  1 +/**
  2 + \file test_Action.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 +
  6 + This file is part of hueplusplus.
  7 +
  8 + hueplusplus is free software: you can redistribute it and/or modify
  9 + it under the terms of the GNU Lesser General Public License as published by
  10 + the Free Software Foundation, either version 3 of the License, or
  11 + (at your option) any later version.
  12 +
  13 + hueplusplus is distributed in the hope that it will be useful,
  14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 + GNU Lesser General Public License for more details.
  17 +
  18 + You should have received a copy of the GNU Lesser General Public License
  19 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  20 +**/
  21 +
  22 +#include <hueplusplus/Action.h>
  23 +#include <hueplusplus/HueException.h>
  24 +
  25 +#include <gtest/gtest.h>
  26 +
  27 +using namespace testing;
  28 +using hueplusplus::Action;
  29 +
  30 +TEST(Action, Constructor)
  31 +{
  32 + const std::string address = "/api/abcd/test";
  33 + const nlohmann::json body = {{"test", "value"}};
  34 + const nlohmann::json json = {{"address", address}, {"method", "PUT"}, {"body", body}};
  35 + Action command(json);
  36 +
  37 + EXPECT_EQ(address, command.getAddress());
  38 + EXPECT_EQ(Action::Method::put, command.getMethod());
  39 + EXPECT_EQ(body, command.getBody());
  40 + EXPECT_EQ(json, command.toJson());
  41 +}
  42 +
  43 +TEST(Action, getMethod)
  44 +{
  45 + nlohmann::json json = {{"address", "/test"}, {"method", "PUT"}, {"body", {}}};
  46 + EXPECT_EQ(Action::Method::put, Action(json).getMethod());
  47 + json["method"] = "POST";
  48 + EXPECT_EQ(Action::Method::post, Action(json).getMethod());
  49 + json["method"] = "DELETE";
  50 + EXPECT_EQ(Action::Method::deleteMethod, Action(json).getMethod());
  51 + json["method"] = "unknown";
  52 + EXPECT_THROW(Action(json).getMethod(), hueplusplus::HueException);
  53 +}
  54 +
  55 +TEST(Action, parseMethod)
  56 +{
  57 + using M = Action::Method;
  58 + EXPECT_EQ(M::put, Action::parseMethod("PUT"));
  59 + EXPECT_EQ(M::post, Action::parseMethod("POST"));
  60 + EXPECT_EQ(M::deleteMethod, Action::parseMethod("DELETE"));
  61 + EXPECT_THROW(Action::parseMethod("unknown"), hueplusplus::HueException);
  62 +}
  63 +
  64 +TEST(Action, methodToString)
  65 +{
  66 + using M = Action::Method;
  67 + EXPECT_EQ("POST", Action::methodToString(M::post));
  68 + EXPECT_EQ("PUT", Action::methodToString(M::put));
  69 + EXPECT_EQ("DELETE", Action::methodToString(M::deleteMethod));
  70 +}
test/test_Group.cpp
@@ -253,15 +253,15 @@ TEST_F(GroupTest, setScene) @@ -253,15 +253,15 @@ TEST_F(GroupTest, setScene)
253 group.setScene(scene); 253 group.setScene(scene);
254 } 254 }
255 255
256 -TEST_F(GroupTest, scheduleScene) 256 +TEST_F(GroupTest, createSceneAction)
257 { 257 {
258 const int id = 1; 258 const int id = 1;
259 expectGetState(id); 259 expectGetState(id);
260 const Group group(id, commands, std::chrono::steady_clock::duration::max()); 260 const Group group(id, commands, std::chrono::steady_clock::duration::max());
261 const std::string scene = "testScene"; 261 const std::string scene = "testScene";
262 nlohmann::json request = { {"scene", scene} }; 262 nlohmann::json request = { {"scene", scene} };
263 - ScheduleCommand command = group.scheduleScene(scene);  
264 - EXPECT_EQ(ScheduleCommand::Method::put, command.getMethod()); 263 + hueplusplus::Action command = group.createSceneAction(scene);
  264 + EXPECT_EQ(hueplusplus::Action::Method::put, command.getMethod());
265 EXPECT_EQ("/api/" + getBridgeUsername() + "/groups/1/action", command.getAddress()); 265 EXPECT_EQ("/api/" + getBridgeUsername() + "/groups/1/action", command.getAddress());
266 EXPECT_EQ(request, command.getBody()); 266 EXPECT_EQ(request, command.getBody());
267 } 267 }
test/test_Schedule.cpp
@@ -29,31 +29,6 @@ @@ -29,31 +29,6 @@
29 using namespace hueplusplus; 29 using namespace hueplusplus;
30 using namespace testing; 30 using namespace testing;
31 31
32 -TEST(ScheduleCommand, Constructor)  
33 -{  
34 - const std::string address = "/api/abcd/test";  
35 - const nlohmann::json body = {{"test", "value"}};  
36 - const nlohmann::json json = {{"address", address}, {"method", "PUT"}, {"body", body}};  
37 - ScheduleCommand command(json);  
38 -  
39 - EXPECT_EQ(address, command.getAddress());  
40 - EXPECT_EQ(ScheduleCommand::Method::put, command.getMethod());  
41 - EXPECT_EQ(body, command.getBody());  
42 - EXPECT_EQ(json, command.toJson());  
43 -}  
44 -  
45 -TEST(ScheduleCommand, getMethod)  
46 -{  
47 - nlohmann::json json = {{"address", "/test"}, {"method", "PUT"}, {"body", {}}};  
48 - EXPECT_EQ(ScheduleCommand::Method::put, ScheduleCommand(json).getMethod());  
49 - json["method"] = "POST";  
50 - EXPECT_EQ(ScheduleCommand::Method::post, ScheduleCommand(json).getMethod());  
51 - json["method"] = "DELETE";  
52 - EXPECT_EQ(ScheduleCommand::Method::deleteMethod, ScheduleCommand(json).getMethod());  
53 - json["method"] = "unknown";  
54 - EXPECT_THROW(ScheduleCommand(json).getMethod(), HueException);  
55 -}  
56 -  
57 class ScheduleTest : public Test 32 class ScheduleTest : public Test
58 { 33 {
59 protected: 34 protected:
@@ -125,10 +100,10 @@ TEST_F(ScheduleTest, getCommand) @@ -125,10 +100,10 @@ TEST_F(ScheduleTest, getCommand)
125 scheduleState["command"] = {{"address", addr}, {"body", body}, {"method", "PUT"}}; 100 scheduleState["command"] = {{"address", addr}, {"body", body}, {"method", "PUT"}};
126 expectGetState(id); 101 expectGetState(id);
127 const Schedule schedule(id, commands, std::chrono::seconds(0)); 102 const Schedule schedule(id, commands, std::chrono::seconds(0));
128 - ScheduleCommand command = schedule.getCommand(); 103 + hueplusplus::Action command = schedule.getCommand();
129 EXPECT_EQ(addr, command.getAddress()); 104 EXPECT_EQ(addr, command.getAddress());
130 EXPECT_EQ(body, command.getBody()); 105 EXPECT_EQ(body, command.getBody());
131 - EXPECT_EQ(ScheduleCommand::Method::put, command.getMethod()); 106 + EXPECT_EQ(hueplusplus::Action::Method::put, command.getMethod());
132 } 107 }
133 108
134 TEST_F(ScheduleTest, getTime) 109 TEST_F(ScheduleTest, getTime)
@@ -224,7 +199,7 @@ TEST_F(ScheduleTest, setCommand) @@ -224,7 +199,7 @@ TEST_F(ScheduleTest, setCommand)
224 const int id = 1; 199 const int id = 1;
225 expectGetState(id); 200 expectGetState(id);
226 Schedule schedule(id, commands, std::chrono::steady_clock::duration::max()); 201 Schedule schedule(id, commands, std::chrono::steady_clock::duration::max());
227 - const ScheduleCommand command({{"address", "abcd"}, {"body", {}}, {"method", "PUT"}}); 202 + const hueplusplus::Action command({{"address", "abcd"}, {"body", {}}, {"method", "PUT"}});
228 nlohmann::json request = {{"command", command.toJson()}}; 203 nlohmann::json request = {{"command", command.toJson()}};
229 nlohmann::json response = {{"success", {"/schedules/1/command", command.toJson()}}}; 204 nlohmann::json response = {{"success", {"/schedules/1/command", command.toJson()}}};
230 EXPECT_CALL( 205 EXPECT_CALL(
@@ -313,7 +288,7 @@ TEST(CreateSchedule, setDescription) @@ -313,7 +288,7 @@ TEST(CreateSchedule, setDescription)
313 TEST(CreateSchedule, setCommand) 288 TEST(CreateSchedule, setCommand)
314 { 289 {
315 const nlohmann::json commandJson = {{"address", "/api/asdf"}, {"method", "PUT"}, {"body", {}}}; 290 const nlohmann::json commandJson = {{"address", "/api/asdf"}, {"method", "PUT"}, {"body", {}}};
316 - ScheduleCommand command {commandJson}; 291 + hueplusplus::Action command {commandJson};
317 const nlohmann::json request = {{"command", commandJson}}; 292 const nlohmann::json request = {{"command", commandJson}};
318 EXPECT_EQ(request, CreateSchedule().setCommand(command).getRequest()); 293 EXPECT_EQ(request, CreateSchedule().setCommand(command).getRequest());
319 } 294 }
test/test_StateTransaction.cpp
@@ -60,17 +60,17 @@ TEST(StateTransaction, commit) @@ -60,17 +60,17 @@ TEST(StateTransaction, commit)
60 } 60 }
61 } 61 }
62 62
63 -TEST(StateTransaction, toScheduleCommand) 63 +TEST(StateTransaction, toAction)
64 { 64 {
65 auto handler = std::make_shared<MockHttpHandler>(); 65 auto handler = std::make_shared<MockHttpHandler>();
66 HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); 66 HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
67 const std::string requestPath = "/api/" + getBridgeUsername() + "/path"; 67 const std::string requestPath = "/api/" + getBridgeUsername() + "/path";
68 nlohmann::json request = {{"on", false}, {"bri", 100}}; 68 nlohmann::json request = {{"on", false}, {"bri", 100}};
69 69
70 - ScheduleCommand command  
71 - = StateTransaction(commands, "/path", nullptr).setOn(false).setBrightness(100).toScheduleCommand(); 70 + hueplusplus::Action command
  71 + = StateTransaction(commands, "/path", nullptr).setOn(false).setBrightness(100).toAction();
72 Mock::VerifyAndClearExpectations(handler.get()); 72 Mock::VerifyAndClearExpectations(handler.get());
73 - EXPECT_EQ(ScheduleCommand::Method::put, command.getMethod()); 73 + EXPECT_EQ(hueplusplus::Action::Method::put, command.getMethod());
74 EXPECT_EQ(request, command.getBody()); 74 EXPECT_EQ(request, command.getBody());
75 EXPECT_EQ(requestPath, command.getAddress()); 75 EXPECT_EQ(requestPath, command.getAddress());
76 } 76 }