Commit 5f351ad1b3188336f513847a708df8b9cf72ed8e
Committed by
Moritz Wirger
1 parent
69254c49
Rename HueThing to BaseDevice, fix compile errors.
Showing
10 changed files
with
196 additions
and
381 deletions
include/hueplusplus/HueThing.h renamed to include/hueplusplus/BaseDevice.h
| 1 | /** | 1 | /** |
| 2 | - \file HueThing.h | 2 | + \file BaseDevice.h |
| 3 | Copyright Notice\n | 3 | Copyright Notice\n |
| 4 | Copyright (C) 2017 Jan Rogall - developer\n | 4 | Copyright (C) 2017 Jan Rogall - developer\n |
| 5 | Copyright (C) 2017 Moritz Wirger - developer\n | 5 | Copyright (C) 2017 Moritz Wirger - developer\n |
| @@ -25,70 +25,68 @@ | @@ -25,70 +25,68 @@ | ||
| 25 | 25 | ||
| 26 | #include <memory> | 26 | #include <memory> |
| 27 | 27 | ||
| 28 | -#include "HueCommandAPI.h" | 28 | +#include "APICache.h" |
| 29 | 29 | ||
| 30 | #include "json/json.hpp" | 30 | #include "json/json.hpp" |
| 31 | 31 | ||
| 32 | namespace hueplusplus | 32 | namespace hueplusplus |
| 33 | { | 33 | { |
| 34 | -//! | ||
| 35 | -//! Class for Hue Thing fixtures | ||
| 36 | -//! | ||
| 37 | -class HueThing | 34 | +//! \brief Base class for physical devices connected to the bridge (sensor or light). |
| 35 | +class BaseDevice | ||
| 38 | { | 36 | { |
| 39 | public: | 37 | public: |
| 40 | - //! \brief std dtor | ||
| 41 | - ~HueThing() = default; | 38 | + //! \brief Virtual destructor |
| 39 | + virtual ~BaseDevice() = default; | ||
| 42 | 40 | ||
| 43 | - //! \brief Const function that returns the id of this thing | 41 | + //! \brief Const function that returns the id of this device |
| 44 | //! | 42 | //! |
| 45 | - //! \return integer representing the thing id | 43 | + //! \return integer representing the device id |
| 46 | virtual int getId() const; | 44 | virtual int getId() const; |
| 47 | 45 | ||
| 48 | - //! \brief Const function that returns the thing type | 46 | + //! \brief Const function that returns the device type |
| 49 | //! | 47 | //! |
| 50 | //! \return String containing the type | 48 | //! \return String containing the type |
| 51 | virtual std::string getType() const; | 49 | virtual std::string getType() const; |
| 52 | 50 | ||
| 53 | - //! \brief Function that returns the name of the thing. | 51 | + //! \brief Function that returns the name of the device. |
| 54 | //! | 52 | //! |
| 55 | - //! \return String containig the name of the thing | 53 | + //! \return String containig the name of the device |
| 56 | //! \throws std::system_error when system or socket operations fail | 54 | //! \throws std::system_error when system or socket operations fail |
| 57 | //! \throws HueException when response contained no body | 55 | //! \throws HueException when response contained no body |
| 58 | //! \throws HueAPIResponseException when response contains an error | 56 | //! \throws HueAPIResponseException when response contains an error |
| 59 | //! \throws nlohmann::json::parse_error when response could not be parsed | 57 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 60 | virtual std::string getName(); | 58 | virtual std::string getName(); |
| 61 | 59 | ||
| 62 | - //! \brief Const function that returns the name of the thing. | 60 | + //! \brief Const function that returns the name of the device. |
| 63 | //! | 61 | //! |
| 64 | - //! \note This will not refresh the thing state | 62 | + //! \note This will not refresh the device state |
| 65 | //! \return String containig the name of the thing | 63 | //! \return String containig the name of the thing |
| 66 | virtual std::string getName() const; | 64 | virtual std::string getName() const; |
| 67 | 65 | ||
| 68 | - //! \brief Const function that returns the modelid of the thing | 66 | + //! \brief Const function that returns the modelid of the device |
| 69 | //! | 67 | //! |
| 70 | - //! \return String conatining the modelid | 68 | + //! \return String containing the modelid |
| 71 | virtual std::string getModelId() const; | 69 | virtual std::string getModelId() const; |
| 72 | 70 | ||
| 73 | - //! \brief Const function that returns the uniqueid of the thing | 71 | + //! \brief Const function that returns the uniqueid of the device |
| 74 | //! | 72 | //! |
| 75 | //! \note Only working on bridges with versions starting at 1.4 | 73 | //! \note Only working on bridges with versions starting at 1.4 |
| 76 | //! \return String containing the uniqueid or an empty string when the function is not supported | 74 | //! \return String containing the uniqueid or an empty string when the function is not supported |
| 77 | virtual std::string getUId() const; | 75 | virtual std::string getUId() const; |
| 78 | 76 | ||
| 79 | - //! \brief Const function that returns the manufacturername of the thing | 77 | + //! \brief Const function that returns the manufacturername of the device |
| 80 | //! | 78 | //! |
| 81 | //! \note Only working on bridges with versions starting at 1.7 | 79 | //! \note Only working on bridges with versions starting at 1.7 |
| 82 | //! \return String containing the manufacturername or an empty string when the function is not supported | 80 | //! \return String containing the manufacturername or an empty string when the function is not supported |
| 83 | virtual std::string getManufacturername() const; | 81 | virtual std::string getManufacturername() const; |
| 84 | 82 | ||
| 85 | - //! \brief Const function that returns the productname of the thing | 83 | + //! \brief Const function that returns the productname of the device |
| 86 | //! | 84 | //! |
| 87 | //! \note Only working on bridges with versions starting at 1.24 | 85 | //! \note Only working on bridges with versions starting at 1.24 |
| 88 | //! \return String containing the productname or an empty string when the function is not supported | 86 | //! \return String containing the productname or an empty string when the function is not supported |
| 89 | virtual std::string getProductname() const; | 87 | virtual std::string getProductname() const; |
| 90 | 88 | ||
| 91 | - //! \brief Function that returns the software version of the thing | 89 | + //! \brief Function that returns the software version of the device |
| 92 | //! | 90 | //! |
| 93 | //! \return String containing the software version | 91 | //! \return String containing the software version |
| 94 | //! \throws std::system_error when system or socket operations fail | 92 | //! \throws std::system_error when system or socket operations fail |
| @@ -97,13 +95,13 @@ public: | @@ -97,13 +95,13 @@ public: | ||
| 97 | //! \throws nlohmann::json::parse_error when response could not be parsed | 95 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 98 | virtual std::string getSwVersion(); | 96 | virtual std::string getSwVersion(); |
| 99 | 97 | ||
| 100 | - //! \brief Const function that returns the software version of the thing | 98 | + //! \brief Const function that returns the software version of the device |
| 101 | //! | 99 | //! |
| 102 | - //! \note This will not refresh the thing state | 100 | + //! \note This will not refresh the device state |
| 103 | //! \return String containing the software version | 101 | //! \return String containing the software version |
| 104 | virtual std::string getSwVersion() const; | 102 | virtual std::string getSwVersion() const; |
| 105 | 103 | ||
| 106 | - //! \brief Function that sets the name of the thing | 104 | + //! \brief Function that sets the name of the device |
| 107 | //! | 105 | //! |
| 108 | //! \return Bool that is true on success | 106 | //! \return Bool that is true on success |
| 109 | //! \throws std::system_error when system or socket operations fail | 107 | //! \throws std::system_error when system or socket operations fail |
| @@ -112,24 +110,25 @@ public: | @@ -112,24 +110,25 @@ public: | ||
| 112 | //! \throws nlohmann::json::parse_error when response could not be parsed | 110 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 113 | virtual bool setName(const std::string& name); | 111 | virtual bool setName(const std::string& name); |
| 114 | 112 | ||
| 113 | + //! \brief Refreshes internal cached state. | ||
| 114 | + //! \throws std::system_error when system or socket operations fail | ||
| 115 | + //! \throws HueException when response contained no body | ||
| 116 | + //! \throws HueAPIResponseException when response contains an error | ||
| 117 | + //! \throws nlohmann::json::parse_error when response could not be parsed | ||
| 118 | + virtual void refresh(); | ||
| 119 | + | ||
| 115 | protected: | 120 | protected: |
| 116 | - //! \brief Protected ctor that is used by \ref Hue class. | 121 | + //! \brief Protected ctor that is used by subclasses. |
| 117 | //! | 122 | //! |
| 118 | - //! \param id Integer that specifies the id of this thing | 123 | + //! \param id Integer that specifies the id of this device |
| 119 | //! \param commands HueCommandAPI for communication with the bridge | 124 | //! \param commands HueCommandAPI for communication with the bridge |
| 120 | - //! | ||
| 121 | - //! leaves strategies unset | ||
| 122 | - HueThing(int id, const HueCommandAPI& commands, const std::string& path); | 125 | + //! \param path Base path for the resource type, ending with a '/'. Example: \c "/lights/" |
| 126 | + //! \param refreshDuration Time between refreshing the cached state. | ||
| 127 | + BaseDevice(int id, const HueCommandAPI& commands, const std::string& path, | ||
| 128 | + std::chrono::steady_clock::duration refreshDuration); | ||
| 123 | 129 | ||
| 124 | - //! \brief Protected function that sets the HueCommandAPI. | 130 | + //! \brief Utility function to send a put request to the device. |
| 125 | //! | 131 | //! |
| 126 | - //! The HueCommandAPI is used for bridge communication | ||
| 127 | - //! \param commandAPI the new HueCommandAPI | ||
| 128 | - virtual void setCommandAPI(const HueCommandAPI& commandAPI) { commands = commandAPI; }; | ||
| 129 | - | ||
| 130 | - //! \brief Utility function to send a put request to the thing. | ||
| 131 | - //! | ||
| 132 | - //! \throws nlohmann::json::parse_error if the reply could not be parsed | ||
| 133 | //! \param request A nlohmann::json aka the request to send | 132 | //! \param request A nlohmann::json aka the request to send |
| 134 | //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") | 133 | //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") |
| 135 | //! \param fileInfo FileInfo from calling function for exception details. | 134 | //! \param fileInfo FileInfo from calling function for exception details. |
| @@ -138,21 +137,12 @@ protected: | @@ -138,21 +137,12 @@ protected: | ||
| 138 | //! \throws HueException when response contained no body | 137 | //! \throws HueException when response contained no body |
| 139 | //! \throws HueAPIResponseException when response contains an error | 138 | //! \throws HueAPIResponseException when response contains an error |
| 140 | //! \throws nlohmann::json::parse_error when response could not be parsed | 139 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 141 | - virtual nlohmann::json SendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); | ||
| 142 | - | ||
| 143 | - //! \brief Virtual function that refreshes the \ref state of the thing. | ||
| 144 | - //! \throws std::system_error when system or socket operations fail | ||
| 145 | - //! \throws HueException when response contained no body | ||
| 146 | - //! \throws HueAPIResponseException when response contains an error | ||
| 147 | - //! \throws nlohmann::json::parse_error when response could not be parsed | ||
| 148 | - virtual void refreshState(); | 140 | + virtual nlohmann::json sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); |
| 149 | 141 | ||
| 150 | protected: | 142 | protected: |
| 151 | - int id; //!< holds the id of the thing | ||
| 152 | - std::string path; //!< holds the path of the thing | ||
| 153 | - nlohmann::json state; //!< holds the current state of the thing updated by \ref refreshState | ||
| 154 | - | ||
| 155 | - HueCommandAPI commands; //!< A IHttpHandler that is used to communicate with the bridge | 143 | + int id; //!< holds the id of the device |
| 144 | + std::string path; //!< holds the path of the device | ||
| 145 | + APICache state; //!< holds the current state of the device | ||
| 156 | }; | 146 | }; |
| 157 | } // namespace hueplusplus | 147 | } // namespace hueplusplus |
| 158 | 148 |
include/hueplusplus/Hue.h
| @@ -132,6 +132,7 @@ public: | @@ -132,6 +132,7 @@ public: | ||
| 132 | using GroupList = GroupResourceList<Group, CreateGroup>; | 132 | using GroupList = GroupResourceList<Group, CreateGroup>; |
| 133 | using ScheduleList = CreateableResourceList<Schedule, int, CreateSchedule>; | 133 | using ScheduleList = CreateableResourceList<Schedule, int, CreateSchedule>; |
| 134 | using SceneList = CreateableResourceList<Scene, std::string, CreateScene>; | 134 | using SceneList = CreateableResourceList<Scene, std::string, CreateScene>; |
| 135 | + using SensorList = ResourceList<HueSensor, int>; | ||
| 135 | 136 | ||
| 136 | public: | 137 | public: |
| 137 | //! \brief Constructor of Hue class | 138 | //! \brief Constructor of Hue class |
| @@ -223,6 +224,12 @@ public: | @@ -223,6 +224,12 @@ public: | ||
| 223 | //! \note Does not refresh state. | 224 | //! \note Does not refresh state. |
| 224 | const SceneList& scenes() const; | 225 | const SceneList& scenes() const; |
| 225 | 226 | ||
| 227 | + //! \brief Provides access to the HueSensor%s on the bridge. | ||
| 228 | + SensorList& sensors(); | ||
| 229 | + //! \brief Provides access to the HueSensor%s on the bridge. | ||
| 230 | + //! \note Does not refresh state. | ||
| 231 | + const SensorList& sensors() const; | ||
| 232 | + | ||
| 226 | private: | 233 | private: |
| 227 | //! \brief Function that sets the HttpHandler and updates the HueCommandAPI. | 234 | //! \brief Function that sets the HttpHandler and updates the HueCommandAPI. |
| 228 | //! \param handler a HttpHandler of type \ref IHttpHandler | 235 | //! \param handler a HttpHandler of type \ref IHttpHandler |
| @@ -247,6 +254,7 @@ private: | @@ -247,6 +254,7 @@ private: | ||
| 247 | detail::MakeCopyable<GroupList> groupList; | 254 | detail::MakeCopyable<GroupList> groupList; |
| 248 | detail::MakeCopyable<ScheduleList> scheduleList; | 255 | detail::MakeCopyable<ScheduleList> scheduleList; |
| 249 | detail::MakeCopyable<SceneList> sceneList; | 256 | detail::MakeCopyable<SceneList> sceneList; |
| 257 | + detail::MakeCopyable<SensorList> sensorList; | ||
| 250 | detail::MakeCopyable<BridgeConfig> bridgeConfig; | 258 | detail::MakeCopyable<BridgeConfig> bridgeConfig; |
| 251 | }; | 259 | }; |
| 252 | } // namespace hueplusplus | 260 | } // namespace hueplusplus |
include/hueplusplus/HueLight.h
| @@ -26,11 +26,11 @@ | @@ -26,11 +26,11 @@ | ||
| 26 | #include <memory> | 26 | #include <memory> |
| 27 | 27 | ||
| 28 | #include "APICache.h" | 28 | #include "APICache.h" |
| 29 | +#include "BaseDevice.h" | ||
| 29 | #include "BrightnessStrategy.h" | 30 | #include "BrightnessStrategy.h" |
| 30 | #include "ColorHueStrategy.h" | 31 | #include "ColorHueStrategy.h" |
| 31 | #include "ColorTemperatureStrategy.h" | 32 | #include "ColorTemperatureStrategy.h" |
| 32 | #include "HueCommandAPI.h" | 33 | #include "HueCommandAPI.h" |
| 33 | -#include "HueThing.h" | ||
| 34 | #include "StateTransaction.h" | 34 | #include "StateTransaction.h" |
| 35 | 35 | ||
| 36 | #include "json/json.hpp" | 36 | #include "json/json.hpp" |
| @@ -96,7 +96,7 @@ enum class ColorType | @@ -96,7 +96,7 @@ enum class ColorType | ||
| 96 | //! \brief Class for Hue Light fixtures | 96 | //! \brief Class for Hue Light fixtures |
| 97 | //! | 97 | //! |
| 98 | //! Provides methods to query and control lights. | 98 | //! Provides methods to query and control lights. |
| 99 | -class HueLight : public HueThing | 99 | +class HueLight : public BaseDevice |
| 100 | { | 100 | { |
| 101 | friend class HueLightFactory; | 101 | friend class HueLightFactory; |
| 102 | friend class SimpleBrightnessStrategy; | 102 | friend class SimpleBrightnessStrategy; |
| @@ -106,85 +106,9 @@ class HueLight : public HueThing | @@ -106,85 +106,9 @@ class HueLight : public HueThing | ||
| 106 | friend class ExtendedColorTemperatureStrategy; | 106 | friend class ExtendedColorTemperatureStrategy; |
| 107 | 107 | ||
| 108 | public: | 108 | public: |
| 109 | - //! \brief std dtor | ||
| 110 | - ~HueLight() = default; | ||
| 111 | - | ||
| 112 | //! \name General information | 109 | //! \name General information |
| 113 | ///@{ | 110 | ///@{ |
| 114 | 111 | ||
| 115 | - //! \brief Function that turns the light off. | ||
| 116 | - //! | ||
| 117 | - //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms | ||
| 118 | - //! \return Bool that is true on success | ||
| 119 | - //! \throws std::system_error when system or socket operations fail | ||
| 120 | - //! \throws HueException when response contained no body | ||
| 121 | - //! \throws HueAPIResponseException when response contains an error | ||
| 122 | - //! \throws nlohmann::json::parse_error when response could not be parsed | ||
| 123 | - virtual bool Off(uint8_t transition = 4); | ||
| 124 | - | ||
| 125 | - //! \brief Function to check whether a light is on or off | ||
| 126 | - //! | ||
| 127 | - //! \return Bool that is true, when the light is on and false, when off | ||
| 128 | - //! \throws std::system_error when system or socket operations fail | ||
| 129 | - //! \throws HueException when response contained no body | ||
| 130 | - //! \throws HueAPIResponseException when response contains an error | ||
| 131 | - //! \throws nlohmann::json::parse_error when response could not be parsed | ||
| 132 | - virtual bool isOn(); | ||
| 133 | - | ||
| 134 | - //! \brief Const function to check whether a light is on or off | ||
| 135 | - //! | ||
| 136 | - //! \note This will not refresh the light state | ||
| 137 | - //! \return Bool that is true, when the light is on and false, when off | ||
| 138 | - virtual bool isOn() const; | ||
| 139 | - | ||
| 140 | - //! \brief Const function that returns the id of this light | ||
| 141 | - //! | ||
| 142 | - //! \return integer representing the light id | ||
| 143 | - virtual int getId() const; | ||
| 144 | - | ||
| 145 | - //! \brief Const function that returns the light type | ||
| 146 | - //! | ||
| 147 | - //! \return String containing the type | ||
| 148 | - virtual std::string getType() const; | ||
| 149 | - | ||
| 150 | - //! \brief Function that returns the name of the light. | ||
| 151 | - //! | ||
| 152 | - //! \return String containig the name of the light | ||
| 153 | - //! \throws std::system_error when system or socket operations fail | ||
| 154 | - //! \throws HueException when response contained no body | ||
| 155 | - //! \throws HueAPIResponseException when response contains an error | ||
| 156 | - //! \throws nlohmann::json::parse_error when response could not be parsed | ||
| 157 | - virtual std::string getName(); | ||
| 158 | - | ||
| 159 | - //! \brief Const function that returns the name of the light. | ||
| 160 | - //! | ||
| 161 | - //! \note This will not refresh the light state | ||
| 162 | - //! \return String containig the name of the light | ||
| 163 | - virtual std::string getName() const; | ||
| 164 | - | ||
| 165 | - //! \brief Const function that returns the modelid of the light | ||
| 166 | - //! | ||
| 167 | - //! \return String conatining the modelid | ||
| 168 | - virtual std::string getModelId() const; | ||
| 169 | - | ||
| 170 | - //! \brief Const function that returns the uniqueid of the light | ||
| 171 | - //! | ||
| 172 | - //! \note Only working on bridges with versions starting at 1.4 | ||
| 173 | - //! \return String containing the uniqueid or an empty string when the function is not supported | ||
| 174 | - virtual std::string getUId() const; | ||
| 175 | - | ||
| 176 | - //! \brief Const function that returns the manufacturername of the light | ||
| 177 | - //! | ||
| 178 | - //! \note Only working on bridges with versions starting at 1.7 | ||
| 179 | - //! \return String containing the manufacturername or an empty string when the function is not supported | ||
| 180 | - virtual std::string getManufacturername() const; | ||
| 181 | - | ||
| 182 | - //! \brief Const function that returns the productname of the light | ||
| 183 | - //! | ||
| 184 | - //! \note Only working on bridges with versions starting at 1.24 | ||
| 185 | - //! \return String containing the productname or an empty string when the function is not supported | ||
| 186 | - virtual std::string getProductname() const; | ||
| 187 | - | ||
| 188 | //! \brief Const function that returns the luminaireuniqueid of the light | 112 | //! \brief Const function that returns the luminaireuniqueid of the light |
| 189 | //! | 113 | //! |
| 190 | //! \note Only working on bridges with versions starting at 1.9 | 114 | //! \note Only working on bridges with versions starting at 1.9 |
| @@ -325,7 +249,7 @@ public: | @@ -325,7 +249,7 @@ public: | ||
| 325 | return 0; | 249 | return 0; |
| 326 | }; | 250 | }; |
| 327 | 251 | ||
| 328 | - //! \brief Fucntion that sets the color temperature of this light in mired. | 252 | + //! \brief Function that sets the color temperature of this light in mired. |
| 329 | //! | 253 | //! |
| 330 | //! \note The color temperature will only be set if the light has a reference | 254 | //! \note The color temperature will only be set if the light has a reference |
| 331 | //! to a specific \ref ColorTemperatureStrategy. The color temperature can | 255 | //! to a specific \ref ColorTemperatureStrategy. The color temperature can |
| @@ -670,13 +594,6 @@ public: | @@ -670,13 +594,6 @@ public: | ||
| 670 | 594 | ||
| 671 | ///@} | 595 | ///@} |
| 672 | 596 | ||
| 673 | - //! \brief Refreshes internal cached state. | ||
| 674 | - //! \throws std::system_error when system or socket operations fail | ||
| 675 | - //! \throws HueException when response contained no body | ||
| 676 | - //! \throws HueAPIResponseException when response contains an error | ||
| 677 | - //! \throws nlohmann::json::parse_error when response could not be parsed | ||
| 678 | - virtual void refresh(); | ||
| 679 | - | ||
| 680 | protected: | 597 | protected: |
| 681 | //! \brief Protected ctor that is used by \ref Hue class. | 598 | //! \brief Protected ctor that is used by \ref Hue class. |
| 682 | //! | 599 | //! |
| @@ -734,21 +651,7 @@ protected: | @@ -734,21 +651,7 @@ protected: | ||
| 734 | colorHueStrategy = std::move(strat); | 651 | colorHueStrategy = std::move(strat); |
| 735 | }; | 652 | }; |
| 736 | 653 | ||
| 737 | - //! \brief Utility function to send a put request to the light. | ||
| 738 | - //! | ||
| 739 | - //! \param request A nlohmann::json aka the request to send | ||
| 740 | - //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") | ||
| 741 | - //! \param fileInfo FileInfo from calling function for exception details. | ||
| 742 | - //! \return The parsed reply | ||
| 743 | - //! \throws std::system_error when system or socket operations fail | ||
| 744 | - //! \throws HueException when response contained no body | ||
| 745 | - //! \throws HueAPIResponseException when response contains an error | ||
| 746 | - //! \throws nlohmann::json::parse_error when response could not be parsed | ||
| 747 | - virtual nlohmann::json sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); | ||
| 748 | - | ||
| 749 | protected: | 654 | protected: |
| 750 | - int id; //!< holds the id of the light | ||
| 751 | - APICache state; //!< holds the current state of the light | ||
| 752 | ColorType colorType; //!< holds the \ref ColorType of the light | 655 | ColorType colorType; //!< holds the \ref ColorType of the light |
| 753 | 656 | ||
| 754 | std::shared_ptr<const BrightnessStrategy> | 657 | std::shared_ptr<const BrightnessStrategy> |
include/hueplusplus/HueSensor.h
| @@ -24,8 +24,8 @@ | @@ -24,8 +24,8 @@ | ||
| 24 | 24 | ||
| 25 | #include <memory> | 25 | #include <memory> |
| 26 | 26 | ||
| 27 | +#include "BaseDevice.h" | ||
| 27 | #include "HueCommandAPI.h" | 28 | #include "HueCommandAPI.h" |
| 28 | -#include "HueThing.h" | ||
| 29 | 29 | ||
| 30 | #include "json/json.hpp" | 30 | #include "json/json.hpp" |
| 31 | 31 | ||
| @@ -34,7 +34,7 @@ namespace hueplusplus | @@ -34,7 +34,7 @@ namespace hueplusplus | ||
| 34 | //! | 34 | //! |
| 35 | //! Class for Hue Sensor fixtures | 35 | //! Class for Hue Sensor fixtures |
| 36 | //! | 36 | //! |
| 37 | -class HueSensor : public HueThing | 37 | +class HueSensor : public BaseDevice |
| 38 | { | 38 | { |
| 39 | friend class Hue; | 39 | friend class Hue; |
| 40 | 40 | ||
| @@ -89,9 +89,8 @@ protected: | @@ -89,9 +89,8 @@ protected: | ||
| 89 | //! | 89 | //! |
| 90 | //! \param id Integer that specifies the id of this sensor | 90 | //! \param id Integer that specifies the id of this sensor |
| 91 | //! \param commands HueCommandAPI for communication with the bridge | 91 | //! \param commands HueCommandAPI for communication with the bridge |
| 92 | - //! | ||
| 93 | - //! leaves strategies unset | ||
| 94 | - HueSensor(int id, const HueCommandAPI& commands); | 92 | + //! \param refreshDuration Time between refreshing the cached state. |
| 93 | + HueSensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); | ||
| 95 | }; | 94 | }; |
| 96 | } // namespace hueplusplus | 95 | } // namespace hueplusplus |
| 97 | 96 |
src/BaseDevice.cpp
0 โ 100644
| 1 | +/** | ||
| 2 | + \file BaseDevice.cpp | ||
| 3 | + Copyright Notice\n | ||
| 4 | + Copyright (C) 2020 Stefan Herbrechtsmeier - 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/BaseDevice.h" | ||
| 23 | + | ||
| 24 | +#include <cmath> | ||
| 25 | +#include <iostream> | ||
| 26 | +#include <thread> | ||
| 27 | + | ||
| 28 | +#include "hueplusplus/HueExceptionMacro.h" | ||
| 29 | +#include "hueplusplus/Utils.h" | ||
| 30 | +#include "json/json.hpp" | ||
| 31 | + | ||
| 32 | +namespace hueplusplus | ||
| 33 | +{ | ||
| 34 | +int BaseDevice::getId() const | ||
| 35 | +{ | ||
| 36 | + return id; | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +std::string BaseDevice::getType() const | ||
| 40 | +{ | ||
| 41 | + return state.getValue().at("type").get<std::string>(); | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +std::string BaseDevice::getName() | ||
| 45 | +{ | ||
| 46 | + return state.getValue().at("name").get<std::string>(); | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +std::string BaseDevice::getName() const | ||
| 50 | +{ | ||
| 51 | + return state.getValue().at("name").get<std::string>(); | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +std::string BaseDevice::getModelId() const | ||
| 55 | +{ | ||
| 56 | + return state.getValue().at("modelid").get<std::string>(); | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +std::string BaseDevice::getUId() const | ||
| 60 | +{ | ||
| 61 | + return state.getValue().value("uniqueid", ""); | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +std::string BaseDevice::getManufacturername() const | ||
| 65 | +{ | ||
| 66 | + return state.getValue().value("manufacturername", ""); | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +std::string BaseDevice::getProductname() const | ||
| 70 | +{ | ||
| 71 | + return state.getValue().value("productname", ""); | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +std::string BaseDevice::getSwVersion() | ||
| 75 | +{ | ||
| 76 | + return state.getValue().at("swversion").get<std::string>(); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +std::string BaseDevice::getSwVersion() const | ||
| 80 | +{ | ||
| 81 | + return state.getValue().at("swversion").get<std::string>(); | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +bool BaseDevice::setName(const std::string& name) | ||
| 85 | +{ | ||
| 86 | + nlohmann::json request = { {"name", name} }; | ||
| 87 | + nlohmann::json reply = sendPutRequest(request, "/name", CURRENT_FILE_INFO); | ||
| 88 | + | ||
| 89 | + // Check whether request was successful (returned name is not necessarily the actually set name) | ||
| 90 | + // If it already exists, a number is added, if it is too long to be returned, "Updated" is returned | ||
| 91 | + return utils::safeGetMember(reply, 0, "success", "/lights/" + std::to_string(id) + "/name").is_string(); | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +BaseDevice::BaseDevice( | ||
| 95 | + int id, const HueCommandAPI& commands, const std::string& path, std::chrono::steady_clock::duration refreshDuration) | ||
| 96 | + : id(id), state(path + std::to_string(id), commands, refreshDuration), path(path) | ||
| 97 | +{ | ||
| 98 | + state.refresh(); | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +nlohmann::json BaseDevice::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) | ||
| 102 | +{ | ||
| 103 | + return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +void BaseDevice::refresh() | ||
| 107 | +{ | ||
| 108 | + state.refresh(); | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +} // namespace hueplusplus |
src/CMakeLists.txt
| 1 | set(hueplusplus_SOURCES | 1 | set(hueplusplus_SOURCES |
| 2 | APICache.cpp | 2 | APICache.cpp |
| 3 | + BaseDevice.cpp | ||
| 3 | BaseHttpHandler.cpp | 4 | BaseHttpHandler.cpp |
| 4 | BridgeConfig.cpp | 5 | BridgeConfig.cpp |
| 5 | ColorUnits.cpp | 6 | ColorUnits.cpp |
| @@ -11,11 +12,10 @@ set(hueplusplus_SOURCES | @@ -11,11 +12,10 @@ set(hueplusplus_SOURCES | ||
| 11 | HueDeviceTypes.cpp | 12 | HueDeviceTypes.cpp |
| 12 | HueException.cpp | 13 | HueException.cpp |
| 13 | HueLight.cpp | 14 | HueLight.cpp |
| 15 | + HueSensor.cpp | ||
| 14 | ModelPictures.cpp | 16 | ModelPictures.cpp |
| 15 | Scene.cpp | 17 | Scene.cpp |
| 16 | Schedule.cpp | 18 | Schedule.cpp |
| 17 | - HueSensor.cpp | ||
| 18 | - HueThing.cpp | ||
| 19 | SimpleBrightnessStrategy.cpp | 19 | SimpleBrightnessStrategy.cpp |
| 20 | SimpleColorHueStrategy.cpp | 20 | SimpleColorHueStrategy.cpp |
| 21 | SimpleColorTemperatureStrategy.cpp | 21 | SimpleColorTemperatureStrategy.cpp |
src/Hue.cpp
| @@ -145,6 +145,7 @@ Hue::Hue(const std::string& ip, const int port, const std::string& username, | @@ -145,6 +145,7 @@ Hue::Hue(const std::string& ip, const int port, const std::string& username, | ||
| 145 | groupList(stateCache, "groups", refreshDuration), | 145 | groupList(stateCache, "groups", refreshDuration), |
| 146 | scheduleList(stateCache, "schedules", refreshDuration), | 146 | scheduleList(stateCache, "schedules", refreshDuration), |
| 147 | sceneList(stateCache, "scenes", refreshDuration), | 147 | sceneList(stateCache, "scenes", refreshDuration), |
| 148 | + sensorList(stateCache, "sensors", refreshDuration), | ||
| 148 | bridgeConfig(stateCache, refreshDuration) | 149 | bridgeConfig(stateCache, refreshDuration) |
| 149 | { } | 150 | { } |
| 150 | 151 | ||
| @@ -231,35 +232,6 @@ const BridgeConfig& Hue::config() const | @@ -231,35 +232,6 @@ const BridgeConfig& Hue::config() const | ||
| 231 | return bridgeConfig; | 232 | return bridgeConfig; |
| 232 | } | 233 | } |
| 233 | 234 | ||
| 234 | -HueSensor& Hue::getSensor(int id) | ||
| 235 | -{ | ||
| 236 | - auto pos = sensors.find(id); | ||
| 237 | - if (pos != sensors.end()) | ||
| 238 | - { | ||
| 239 | - pos->second.refreshState(); | ||
| 240 | - return pos->second; | ||
| 241 | - } | ||
| 242 | - refreshState(); | ||
| 243 | - if (!state["sensors"].count(std::to_string(id))) | ||
| 244 | - { | ||
| 245 | - std::cerr << "Error in Hue getSensor(): sensor with id " << id << " is not valid\n"; | ||
| 246 | - throw HueException(CURRENT_FILE_INFO, "Sensor id is not valid"); | ||
| 247 | - } | ||
| 248 | - // std::cout << state["sensors"][std::to_string(id)] << std::endl; | ||
| 249 | - std::string type = state["sensors"][std::to_string(id)]["modelid"]; | ||
| 250 | - // std::cout << type << std::endl; | ||
| 251 | - if (type == "RWL021" || type == "PHDL00" || type == "PHWA01") | ||
| 252 | - { | ||
| 253 | - // Hue dimmer switch | ||
| 254 | - HueSensor sensor = HueSensor(id, commands); | ||
| 255 | - sensors.emplace(id, sensor); | ||
| 256 | - return sensors.find(id)->second; | ||
| 257 | - } | ||
| 258 | - std::cerr << "Could not determine HueSensor type:" << type << "!\n"; | ||
| 259 | - throw HueException(CURRENT_FILE_INFO, "Could not determine HueSensor type!"); | ||
| 260 | -} | ||
| 261 | - | ||
| 262 | - | ||
| 263 | Hue::LightList& Hue::lights() | 235 | Hue::LightList& Hue::lights() |
| 264 | { | 236 | { |
| 265 | return lightList; | 237 | return lightList; |
| @@ -275,23 +247,7 @@ Hue::GroupList& Hue::groups() | @@ -275,23 +247,7 @@ Hue::GroupList& Hue::groups() | ||
| 275 | return groupList; | 247 | return groupList; |
| 276 | } | 248 | } |
| 277 | 249 | ||
| 278 | -std::vector<std::reference_wrapper<HueSensor>> Hue::getAllSensors() | ||
| 279 | -{ | ||
| 280 | - refreshState(); | ||
| 281 | - nlohmann::json sensorsState = state["sensors"]; | ||
| 282 | - for (nlohmann::json::iterator it = sensorsState.begin(); it != sensorsState.end(); ++it) | ||
| 283 | - { | ||
| 284 | - getSensor(std::stoi(it.key())); | ||
| 285 | - } | ||
| 286 | - std::vector<std::reference_wrapper<HueSensor>> result; | ||
| 287 | - for (auto& entry : sensors) | ||
| 288 | - { | ||
| 289 | - result.emplace_back(entry.second); | ||
| 290 | - } | ||
| 291 | - return result; | ||
| 292 | -} | ||
| 293 | - | ||
| 294 | -bool Hue::lightExists(int id) | 250 | +const Hue::GroupList& Hue::groups() const |
| 295 | { | 251 | { |
| 296 | return groupList; | 252 | return groupList; |
| 297 | } | 253 | } |
| @@ -316,6 +272,16 @@ const Hue::SceneList& Hue::scenes() const | @@ -316,6 +272,16 @@ const Hue::SceneList& Hue::scenes() const | ||
| 316 | return sceneList; | 272 | return sceneList; |
| 317 | } | 273 | } |
| 318 | 274 | ||
| 275 | +Hue::SensorList& Hue::sensors() | ||
| 276 | +{ | ||
| 277 | + return sensorList; | ||
| 278 | +} | ||
| 279 | + | ||
| 280 | +const Hue::SensorList& Hue::sensors() const | ||
| 281 | +{ | ||
| 282 | + return sensorList; | ||
| 283 | +} | ||
| 284 | + | ||
| 319 | void Hue::setHttpHandler(std::shared_ptr<const IHttpHandler> handler) | 285 | void Hue::setHttpHandler(std::shared_ptr<const IHttpHandler> handler) |
| 320 | { | 286 | { |
| 321 | http_handler = handler; | 287 | http_handler = handler; |
| @@ -323,9 +289,10 @@ void Hue::setHttpHandler(std::shared_ptr<const IHttpHandler> handler) | @@ -323,9 +289,10 @@ void Hue::setHttpHandler(std::shared_ptr<const IHttpHandler> handler) | ||
| 323 | lightList = ResourceList<HueLight, int>(stateCache, "lights", refreshDuration, | 289 | lightList = ResourceList<HueLight, int>(stateCache, "lights", refreshDuration, |
| 324 | [factory = HueLightFactory(stateCache->getCommandAPI(), refreshDuration)]( | 290 | [factory = HueLightFactory(stateCache->getCommandAPI(), refreshDuration)]( |
| 325 | int id, const nlohmann::json& state) mutable { return factory.createLight(state, id); }); | 291 | int id, const nlohmann::json& state) mutable { return factory.createLight(state, id); }); |
| 326 | - groupList = GroupResourceList<Group, CreateGroup>(stateCache, "groups", refreshDuration); | ||
| 327 | - scheduleList = CreateableResourceList<Schedule, int, CreateSchedule>(stateCache, "schedules", refreshDuration); | ||
| 328 | - sceneList = CreateableResourceList<Scene, std::string, CreateScene>(stateCache, "scenes", refreshDuration); | 292 | + groupList = GroupList(stateCache, "groups", refreshDuration); |
| 293 | + scheduleList = ScheduleList(stateCache, "schedules", refreshDuration); | ||
| 294 | + sceneList = SceneList(stateCache, "scenes", refreshDuration); | ||
| 295 | + sensorList = SensorList(stateCache, "sensors", refreshDuration); | ||
| 329 | bridgeConfig = BridgeConfig(stateCache, refreshDuration); | 296 | bridgeConfig = BridgeConfig(stateCache, refreshDuration); |
| 330 | stateCache->refresh(); | 297 | stateCache->refresh(); |
| 331 | } | 298 | } |
src/HueLight.cpp
| @@ -27,7 +27,6 @@ | @@ -27,7 +27,6 @@ | ||
| 27 | #include <thread> | 27 | #include <thread> |
| 28 | 28 | ||
| 29 | #include "hueplusplus/HueExceptionMacro.h" | 29 | #include "hueplusplus/HueExceptionMacro.h" |
| 30 | -#include "hueplusplus/HueThing.h" | ||
| 31 | #include "hueplusplus/Utils.h" | 30 | #include "hueplusplus/Utils.h" |
| 32 | #include "json/json.hpp" | 31 | #include "json/json.hpp" |
| 33 | 32 | ||
| @@ -58,16 +57,6 @@ std::string HueLight::getLuminaireUId() const | @@ -58,16 +57,6 @@ std::string HueLight::getLuminaireUId() const | ||
| 58 | return state.getValue().value("luminaireuniqueid", std::string()); | 57 | return state.getValue().value("luminaireuniqueid", std::string()); |
| 59 | } | 58 | } |
| 60 | 59 | ||
| 61 | -std::string HueLight::getSwVersion() | ||
| 62 | -{ | ||
| 63 | - return state.getValue()["swversion"].get<std::string>(); | ||
| 64 | -} | ||
| 65 | - | ||
| 66 | -std::string HueLight::getSwVersion() const | ||
| 67 | -{ | ||
| 68 | - return state.getValue()["swversion"].get<std::string>(); | ||
| 69 | -} | ||
| 70 | - | ||
| 71 | ColorType HueLight::getColorType() const | 60 | ColorType HueLight::getColorType() const |
| 72 | { | 61 | { |
| 73 | return colorType; | 62 | return colorType; |
| @@ -123,27 +112,16 @@ StateTransaction HueLight::transaction() | @@ -123,27 +112,16 @@ StateTransaction HueLight::transaction() | ||
| 123 | state.getCommandAPI(), "/lights/" + std::to_string(id) + "/state", &state.getValue().at("state")); | 112 | state.getCommandAPI(), "/lights/" + std::to_string(id) + "/state", &state.getValue().at("state")); |
| 124 | } | 113 | } |
| 125 | 114 | ||
| 126 | -void HueLight::refresh() | ||
| 127 | -{ | ||
| 128 | - state.refresh(); | ||
| 129 | -} | ||
| 130 | - | ||
| 131 | -HueLight::HueLight(int id, const HueCommandAPI& commands) : HueLight(id, commands, nullptr, nullptr, nullptr) {} | 115 | +HueLight::HueLight(int id, const HueCommandAPI& commands) : HueLight(id, commands, nullptr, nullptr, nullptr) { } |
| 132 | 116 | ||
| 133 | HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr<const BrightnessStrategy> brightnessStrategy, | 117 | HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr<const BrightnessStrategy> brightnessStrategy, |
| 134 | std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy, | 118 | std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy, |
| 135 | - std::shared_ptr<const ColorHueStrategy> colorHueStrategy, chrono::steady_clock::duration refreshDuration) | ||
| 136 | - : HueThing(id, commands, "/lights/"), | 119 | + std::shared_ptr<const ColorHueStrategy> colorHueStrategy, std::chrono::steady_clock::duration refreshDuration) |
| 120 | + : BaseDevice(id, commands, "/lights/", refreshDuration), | ||
| 137 | colorType(ColorType::NONE), | 121 | colorType(ColorType::NONE), |
| 138 | brightnessStrategy(std::move(brightnessStrategy)), | 122 | brightnessStrategy(std::move(brightnessStrategy)), |
| 139 | colorTemperatureStrategy(std::move(colorTempStrategy)), | 123 | colorTemperatureStrategy(std::move(colorTempStrategy)), |
| 140 | colorHueStrategy(std::move(colorHueStrategy)) | 124 | colorHueStrategy(std::move(colorHueStrategy)) |
| 141 | { | 125 | { |
| 142 | - state.refresh(); | ||
| 143 | -} | ||
| 144 | - | ||
| 145 | -nlohmann::json HueLight::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) | ||
| 146 | -{ | ||
| 147 | - return state.getCommandAPI().PUTRequest("/lights/" + std::to_string(id) + subPath, request, std::move(fileInfo)); | ||
| 148 | } | 126 | } |
| 149 | } // namespace hueplusplus | 127 | } // namespace hueplusplus |
src/HueSensor.cpp
| @@ -21,17 +21,15 @@ | @@ -21,17 +21,15 @@ | ||
| 21 | 21 | ||
| 22 | #include "hueplusplus/HueSensor.h" | 22 | #include "hueplusplus/HueSensor.h" |
| 23 | 23 | ||
| 24 | -#include "hueplusplus/HueThing.h" | ||
| 25 | #include "json/json.hpp" | 24 | #include "json/json.hpp" |
| 26 | 25 | ||
| 27 | namespace hueplusplus | 26 | namespace hueplusplus |
| 28 | { | 27 | { |
| 29 | int HueSensor::getButtonEvent() | 28 | int HueSensor::getButtonEvent() |
| 30 | { | 29 | { |
| 31 | - refreshState(); | ||
| 32 | if (hasButtonEvent()) | 30 | if (hasButtonEvent()) |
| 33 | { | 31 | { |
| 34 | - return state["state"]["buttonevent"]; | 32 | + return state.getValue().at("state").at("buttonevent"); |
| 35 | } | 33 | } |
| 36 | return 0; | 34 | return 0; |
| 37 | } | 35 | } |
| @@ -40,17 +38,16 @@ int HueSensor::getButtonEvent() const | @@ -40,17 +38,16 @@ int HueSensor::getButtonEvent() const | ||
| 40 | { | 38 | { |
| 41 | if (hasButtonEvent()) | 39 | if (hasButtonEvent()) |
| 42 | { | 40 | { |
| 43 | - return state["state"]["buttonevent"]; | 41 | + return state.getValue().at("state").at("buttonevent"); |
| 44 | } | 42 | } |
| 45 | return 0; | 43 | return 0; |
| 46 | } | 44 | } |
| 47 | 45 | ||
| 48 | int HueSensor::getStatus() | 46 | int HueSensor::getStatus() |
| 49 | { | 47 | { |
| 50 | - refreshState(); | ||
| 51 | if (hasStatus()) | 48 | if (hasStatus()) |
| 52 | { | 49 | { |
| 53 | - return state["state"]["status"]; | 50 | + return state.getValue().at("state").at("status"); |
| 54 | } | 51 | } |
| 55 | return 0; | 52 | return 0; |
| 56 | } | 53 | } |
| @@ -59,24 +56,22 @@ int HueSensor::getStatus() const | @@ -59,24 +56,22 @@ int HueSensor::getStatus() const | ||
| 59 | { | 56 | { |
| 60 | if (hasStatus()) | 57 | if (hasStatus()) |
| 61 | { | 58 | { |
| 62 | - return state["state"]["status"]; | 59 | + return state.getValue().at("state").at("status"); |
| 63 | } | 60 | } |
| 64 | return 0; | 61 | return 0; |
| 65 | } | 62 | } |
| 66 | 63 | ||
| 67 | bool HueSensor::hasButtonEvent() const | 64 | bool HueSensor::hasButtonEvent() const |
| 68 | { | 65 | { |
| 69 | - return state["state"].count("buttonevent") > 0; | 66 | + return state.getValue().at("state").count("buttonevent") != 0; |
| 70 | } | 67 | } |
| 71 | 68 | ||
| 72 | bool HueSensor::hasStatus() const | 69 | bool HueSensor::hasStatus() const |
| 73 | { | 70 | { |
| 74 | - return state["state"].count("status") > 0; | 71 | + return state.getValue().at("state").count("status") != 0; |
| 75 | } | 72 | } |
| 76 | 73 | ||
| 77 | -HueSensor::HueSensor(int id, const HueCommandAPI& commands) | ||
| 78 | - : HueThing(id, commands, "/sensors/") | ||
| 79 | -{ | ||
| 80 | - refreshState(); | ||
| 81 | -} | 74 | +HueSensor::HueSensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) |
| 75 | + : BaseDevice(id, commands, "/sensors/", refreshDuration) | ||
| 76 | +{ } | ||
| 82 | } // namespace hueplusplus | 77 | } // namespace hueplusplus |
src/HueThing.cpp deleted
| 1 | -/** | ||
| 2 | - \file HueThing.cpp | ||
| 3 | - Copyright Notice\n | ||
| 4 | - Copyright (C) 2020 Stefan Herbrechtsmeier - 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/HueThing.h" | ||
| 23 | - | ||
| 24 | -#include <cmath> | ||
| 25 | -#include <iostream> | ||
| 26 | -#include <thread> | ||
| 27 | - | ||
| 28 | -#include "hueplusplus/HueExceptionMacro.h" | ||
| 29 | -#include "hueplusplus/Utils.h" | ||
| 30 | -#include "json/json.hpp" | ||
| 31 | - | ||
| 32 | -namespace hueplusplus | ||
| 33 | -{ | ||
| 34 | -int HueThing::getId() const | ||
| 35 | -{ | ||
| 36 | - return id; | ||
| 37 | -} | ||
| 38 | - | ||
| 39 | -std::string HueThing::getType() const | ||
| 40 | -{ | ||
| 41 | - return state["type"]; | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -std::string HueThing::getName() | ||
| 45 | -{ | ||
| 46 | - refreshState(); | ||
| 47 | - return state["name"]; | ||
| 48 | -} | ||
| 49 | - | ||
| 50 | -std::string HueThing::getName() const | ||
| 51 | -{ | ||
| 52 | - return state["name"]; | ||
| 53 | -} | ||
| 54 | - | ||
| 55 | -std::string HueThing::getModelId() const | ||
| 56 | -{ | ||
| 57 | - return state["modelid"]; | ||
| 58 | -} | ||
| 59 | - | ||
| 60 | -std::string HueThing::getUId() const | ||
| 61 | -{ | ||
| 62 | - if (state.count("uniqueid")) | ||
| 63 | - { | ||
| 64 | - return state["uniqueid"]; | ||
| 65 | - } | ||
| 66 | - return std::string(); | ||
| 67 | -} | ||
| 68 | - | ||
| 69 | -std::string HueThing::getManufacturername() const | ||
| 70 | -{ | ||
| 71 | - if (state.count("manufacturername")) | ||
| 72 | - { | ||
| 73 | - return state["manufacturername"]; | ||
| 74 | - } | ||
| 75 | - return std::string(); | ||
| 76 | -} | ||
| 77 | - | ||
| 78 | -std::string HueThing::getProductname() const | ||
| 79 | -{ | ||
| 80 | - if (state.count("productname")) | ||
| 81 | - { | ||
| 82 | - return state["productname"]; | ||
| 83 | - } | ||
| 84 | - return std::string(); | ||
| 85 | -} | ||
| 86 | - | ||
| 87 | -std::string HueThing::getSwVersion() | ||
| 88 | -{ | ||
| 89 | - refreshState(); | ||
| 90 | - return state["swversion"]; | ||
| 91 | -} | ||
| 92 | - | ||
| 93 | -std::string HueThing::getSwVersion() const | ||
| 94 | -{ | ||
| 95 | - return state["swversion"]; | ||
| 96 | -} | ||
| 97 | - | ||
| 98 | -bool HueThing::setName(const std::string& name) | ||
| 99 | -{ | ||
| 100 | - nlohmann::json request = nlohmann::json::object(); | ||
| 101 | - request["name"] = name; | ||
| 102 | - nlohmann::json reply = SendPutRequest(request, "/name", CURRENT_FILE_INFO); | ||
| 103 | - | ||
| 104 | - // Check whether request was successful | ||
| 105 | - return utils::safeGetMember(reply, 0, "success", path + std::to_string(id) + "/name") == name; | ||
| 106 | -} | ||
| 107 | - | ||
| 108 | -HueThing::HueThing(int id, const HueCommandAPI& commands, const std::string& path) | ||
| 109 | - : id(id), | ||
| 110 | - commands(commands), | ||
| 111 | - path(path) | ||
| 112 | -{ | ||
| 113 | - refreshState(); | ||
| 114 | -} | ||
| 115 | - | ||
| 116 | -nlohmann::json HueThing::SendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) | ||
| 117 | -{ | ||
| 118 | - return commands.PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); | ||
| 119 | -} | ||
| 120 | - | ||
| 121 | -void HueThing::refreshState() | ||
| 122 | -{ | ||
| 123 | - nlohmann::json answer | ||
| 124 | - = commands.GETRequest(path + std::to_string(id), nlohmann::json::object(), CURRENT_FILE_INFO); | ||
| 125 | - if (answer.count("state")) | ||
| 126 | - { | ||
| 127 | - state = answer; | ||
| 128 | - } | ||
| 129 | - else | ||
| 130 | - { | ||
| 131 | - std::cout << "Answer in HueThing::refreshState of " | ||
| 132 | - "http_handler->GETJson(...) is not expected!\nAnswer:\n\t" | ||
| 133 | - << answer.dump() << std::endl; | ||
| 134 | - } | ||
| 135 | -} | ||
| 136 | -} // namespace hueplusplus |