Commit 69254c49db051dca7f1ac71c08c9f2966346cd27

Authored by Stefan Herbrechtsmeier
Committed by Moritz Wirger
1 parent 51785aeb

Add sensor support

include/hueplusplus/Hue.h
@@ -38,6 +38,7 @@ @@ -38,6 +38,7 @@
38 #include "HueCommandAPI.h" 38 #include "HueCommandAPI.h"
39 #include "HueDeviceTypes.h" 39 #include "HueDeviceTypes.h"
40 #include "HueLight.h" 40 #include "HueLight.h"
  41 +#include "HueSensor.h"
41 #include "IHttpHandler.h" 42 #include "IHttpHandler.h"
42 #include "ResourceList.h" 43 #include "ResourceList.h"
43 #include "Scene.h" 44 #include "Scene.h"
include/hueplusplus/HueLight.h
@@ -30,6 +30,7 @@ @@ -30,6 +30,7 @@
30 #include "ColorHueStrategy.h" 30 #include "ColorHueStrategy.h"
31 #include "ColorTemperatureStrategy.h" 31 #include "ColorTemperatureStrategy.h"
32 #include "HueCommandAPI.h" 32 #include "HueCommandAPI.h"
  33 +#include "HueThing.h"
33 #include "StateTransaction.h" 34 #include "StateTransaction.h"
34 35
35 #include "json/json.hpp" 36 #include "json/json.hpp"
@@ -95,7 +96,7 @@ enum class ColorType @@ -95,7 +96,7 @@ enum class ColorType
95 //! \brief Class for Hue Light fixtures 96 //! \brief Class for Hue Light fixtures
96 //! 97 //!
97 //! Provides methods to query and control lights. 98 //! Provides methods to query and control lights.
98 -class HueLight 99 +class HueLight : public HueThing
99 { 100 {
100 friend class HueLightFactory; 101 friend class HueLightFactory;
101 friend class SimpleBrightnessStrategy; 102 friend class SimpleBrightnessStrategy;
@@ -111,6 +112,31 @@ public: @@ -111,6 +112,31 @@ public:
111 //! \name General information 112 //! \name General information
112 ///@{ 113 ///@{
113 114
  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 +
114 //! \brief Const function that returns the id of this light 140 //! \brief Const function that returns the id of this light
115 //! 141 //!
116 //! \return integer representing the light id 142 //! \return integer representing the light id
@@ -118,13 +144,7 @@ public: @@ -118,13 +144,7 @@ public:
118 144
119 //! \brief Const function that returns the light type 145 //! \brief Const function that returns the light type
120 //! 146 //!
121 - //! The type determines which functions the light has.  
122 //! \return String containing the type 147 //! \return String containing the type
123 - //! - "On/Off light": on/off  
124 - //! - "Dimmable light": on/off, brightness  
125 - //! - "Color light": on/off, brightness, color hue/sat/xy  
126 - //! - "Color temperature light": on/off, brightness, color temperature  
127 - //! - "Extended color light": on/off, brightness, color temperature, color hue/sat/xy  
128 virtual std::string getType() const; 148 virtual std::string getType() const;
129 149
130 //! \brief Function that returns the name of the light. 150 //! \brief Function that returns the name of the light.
@@ -142,15 +162,6 @@ public: @@ -142,15 +162,6 @@ public:
142 //! \return String containig the name of the light 162 //! \return String containig the name of the light
143 virtual std::string getName() const; 163 virtual std::string getName() const;
144 164
145 - //! \brief Function that sets the name of the light  
146 - //!  
147 - //! \return Bool that is true on success  
148 - //! \throws std::system_error when system or socket operations fail  
149 - //! \throws HueException when response contained no body  
150 - //! \throws HueAPIResponseException when response contains an error  
151 - //! \throws nlohmann::json::parse_error when response could not be parsed  
152 - virtual bool setName(const std::string& name);  
153 -  
154 //! \brief Const function that returns the modelid of the light 165 //! \brief Const function that returns the modelid of the light
155 //! 166 //!
156 //! \return String conatining the modelid 167 //! \return String conatining the modelid
@@ -180,22 +191,6 @@ public: @@ -180,22 +191,6 @@ public:
180 //! \return String containing the luminaireuniqueid or an empty string when the function is not supported 191 //! \return String containing the luminaireuniqueid or an empty string when the function is not supported
181 virtual std::string getLuminaireUId() const; 192 virtual std::string getLuminaireUId() const;
182 193
183 - //! \brief Function that returns the software version of the light  
184 - //!  
185 - //! \return String containing the software version  
186 - //! \throws std::system_error when system or socket operations fail  
187 - //! \throws HueException when response contained no body  
188 - //! \throws HueAPIResponseException when response contains an error  
189 - //! \throws nlohmann::json::parse_error when response could not be parsed  
190 - virtual std::string getSwVersion();  
191 -  
192 - //! \brief Const function that returns the software version of the light  
193 - //!  
194 - //! \note This will not refresh the light state  
195 - //! \return String containing the software version  
196 - virtual std::string getSwVersion() const;  
197 -  
198 -  
199 //! \brief Const function that returns the color type of the light. 194 //! \brief Const function that returns the color type of the light.
200 //! 195 //!
201 //! \return ColorType containig the color type of the light 196 //! \return ColorType containig the color type of the light
include/hueplusplus/HueSensor.h 0 โ†’ 100644
  1 +/**
  2 + \file HueSensor.h
  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 +#ifndef INCLUDE_HUEPLUSPLUS_HUE_SENSOR_H
  23 +#define INCLUDE_HUEPLUSPLUS_HUE_SENSOR_H
  24 +
  25 +#include <memory>
  26 +
  27 +#include "HueCommandAPI.h"
  28 +#include "HueThing.h"
  29 +
  30 +#include "json/json.hpp"
  31 +
  32 +namespace hueplusplus
  33 +{
  34 +//!
  35 +//! Class for Hue Sensor fixtures
  36 +//!
  37 +class HueSensor : public HueThing
  38 +{
  39 + friend class Hue;
  40 +
  41 +public:
  42 + //! \brief std dtor
  43 + ~HueSensor() = default;
  44 +
  45 + //! \brief Function to get button event
  46 + //!
  47 + //! \return integer representing the button event
  48 + //! \throws std::system_error when system or socket operations fail
  49 + //! \throws HueException when response contained no body
  50 + //! \throws HueAPIResponseException when response contains an error
  51 + //! \throws nlohmann::json::parse_error when response could not be parsed
  52 + virtual int getButtonEvent();
  53 +
  54 + //! \brief Const function to get button event
  55 + //!
  56 + //! \note This will not refresh the sensor state
  57 + //! \return integer representing the button event
  58 + virtual int getButtonEvent() const;
  59 +
  60 + //! \brief Function to get sensor status
  61 + //!
  62 + //! \return integer representing the status
  63 + //! \throws std::system_error when system or socket operations fail
  64 + //! \throws HueException when response contained no body
  65 + //! \throws HueAPIResponseException when response contains an error
  66 + //! \throws nlohmann::json::parse_error when response could not be parsed
  67 + virtual int getStatus();
  68 +
  69 + //! \brief Const function to get sensor status
  70 + //!
  71 + //! \note This will not refresh the sensor state
  72 + //! \return integer representing the button event
  73 + virtual int getStatus() const;
  74 +
  75 + //! \brief Const function to check whether this sensor has a button event
  76 + //!
  77 + //! \return Bool that is true when the sensor has specified abilities and false
  78 + //! when not
  79 + virtual bool hasButtonEvent() const;
  80 +
  81 + //! \brief Const function to check whether this sensor has a status
  82 + //!
  83 + //! \return Bool that is true when the sensor has specified abilities and false
  84 + //! when not
  85 + virtual bool hasStatus() const;
  86 +
  87 +protected:
  88 + //! \brief Protected ctor that is used by \ref Hue class.
  89 + //!
  90 + //! \param id Integer that specifies the id of this sensor
  91 + //! \param commands HueCommandAPI for communication with the bridge
  92 + //!
  93 + //! leaves strategies unset
  94 + HueSensor(int id, const HueCommandAPI& commands);
  95 +};
  96 +} // namespace hueplusplus
  97 +
  98 +#endif
include/hueplusplus/HueThing.h 0 โ†’ 100644
  1 +/**
  2 + \file HueThing.h
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This file is part of hueplusplus.
  8 +
  9 + hueplusplus is free software: you can redistribute it and/or modify
  10 + it under the terms of the GNU Lesser General Public License as published by
  11 + the Free Software Foundation, either version 3 of the License, or
  12 + (at your option) any later version.
  13 +
  14 + hueplusplus is distributed in the hope that it will be useful,
  15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + GNU Lesser General Public License for more details.
  18 +
  19 + You should have received a copy of the GNU Lesser General Public License
  20 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  21 +**/
  22 +
  23 +#ifndef INCLUDE_HUEPLUSPLUS_HUE_THING_H
  24 +#define INCLUDE_HUEPLUSPLUS_HUE_THING_H
  25 +
  26 +#include <memory>
  27 +
  28 +#include "HueCommandAPI.h"
  29 +
  30 +#include "json/json.hpp"
  31 +
  32 +namespace hueplusplus
  33 +{
  34 +//!
  35 +//! Class for Hue Thing fixtures
  36 +//!
  37 +class HueThing
  38 +{
  39 +public:
  40 + //! \brief std dtor
  41 + ~HueThing() = default;
  42 +
  43 + //! \brief Const function that returns the id of this thing
  44 + //!
  45 + //! \return integer representing the thing id
  46 + virtual int getId() const;
  47 +
  48 + //! \brief Const function that returns the thing type
  49 + //!
  50 + //! \return String containing the type
  51 + virtual std::string getType() const;
  52 +
  53 + //! \brief Function that returns the name of the thing.
  54 + //!
  55 + //! \return String containig the name of the thing
  56 + //! \throws std::system_error when system or socket operations fail
  57 + //! \throws HueException when response contained no body
  58 + //! \throws HueAPIResponseException when response contains an error
  59 + //! \throws nlohmann::json::parse_error when response could not be parsed
  60 + virtual std::string getName();
  61 +
  62 + //! \brief Const function that returns the name of the thing.
  63 + //!
  64 + //! \note This will not refresh the thing state
  65 + //! \return String containig the name of the thing
  66 + virtual std::string getName() const;
  67 +
  68 + //! \brief Const function that returns the modelid of the thing
  69 + //!
  70 + //! \return String conatining the modelid
  71 + virtual std::string getModelId() const;
  72 +
  73 + //! \brief Const function that returns the uniqueid of the thing
  74 + //!
  75 + //! \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
  77 + virtual std::string getUId() const;
  78 +
  79 + //! \brief Const function that returns the manufacturername of the thing
  80 + //!
  81 + //! \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
  83 + virtual std::string getManufacturername() const;
  84 +
  85 + //! \brief Const function that returns the productname of the thing
  86 + //!
  87 + //! \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
  89 + virtual std::string getProductname() const;
  90 +
  91 + //! \brief Function that returns the software version of the thing
  92 + //!
  93 + //! \return String containing the software version
  94 + //! \throws std::system_error when system or socket operations fail
  95 + //! \throws HueException when response contained no body
  96 + //! \throws HueAPIResponseException when response contains an error
  97 + //! \throws nlohmann::json::parse_error when response could not be parsed
  98 + virtual std::string getSwVersion();
  99 +
  100 + //! \brief Const function that returns the software version of the thing
  101 + //!
  102 + //! \note This will not refresh the thing state
  103 + //! \return String containing the software version
  104 + virtual std::string getSwVersion() const;
  105 +
  106 + //! \brief Function that sets the name of the thing
  107 + //!
  108 + //! \return Bool that is true on success
  109 + //! \throws std::system_error when system or socket operations fail
  110 + //! \throws HueException when response contained no body
  111 + //! \throws HueAPIResponseException when response contains an error
  112 + //! \throws nlohmann::json::parse_error when response could not be parsed
  113 + virtual bool setName(const std::string& name);
  114 +
  115 +protected:
  116 + //! \brief Protected ctor that is used by \ref Hue class.
  117 + //!
  118 + //! \param id Integer that specifies the id of this thing
  119 + //! \param commands HueCommandAPI for communication with the bridge
  120 + //!
  121 + //! leaves strategies unset
  122 + HueThing(int id, const HueCommandAPI& commands, const std::string& path);
  123 +
  124 + //! \brief Protected function that sets the HueCommandAPI.
  125 + //!
  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
  134 + //! \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.
  136 + //! \return The parsed reply
  137 + //! \throws std::system_error when system or socket operations fail
  138 + //! \throws HueException when response contained no body
  139 + //! \throws HueAPIResponseException when response contains an error
  140 + //! \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();
  149 +
  150 +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
  156 +};
  157 +} // namespace hueplusplus
  158 +
  159 +#endif
src/CMakeLists.txt
@@ -14,6 +14,8 @@ set(hueplusplus_SOURCES @@ -14,6 +14,8 @@ set(hueplusplus_SOURCES
14 ModelPictures.cpp 14 ModelPictures.cpp
15 Scene.cpp 15 Scene.cpp
16 Schedule.cpp 16 Schedule.cpp
  17 + HueSensor.cpp
  18 + HueThing.cpp
17 SimpleBrightnessStrategy.cpp 19 SimpleBrightnessStrategy.cpp
18 SimpleColorHueStrategy.cpp 20 SimpleColorHueStrategy.cpp
19 SimpleColorTemperatureStrategy.cpp 21 SimpleColorTemperatureStrategy.cpp
src/Hue.cpp
@@ -231,6 +231,35 @@ const BridgeConfig&amp; Hue::config() const @@ -231,6 +231,35 @@ const BridgeConfig&amp; Hue::config() const
231 return bridgeConfig; 231 return bridgeConfig;
232 } 232 }
233 233
  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 +
234 Hue::LightList& Hue::lights() 263 Hue::LightList& Hue::lights()
235 { 264 {
236 return lightList; 265 return lightList;
@@ -246,7 +275,23 @@ Hue::GroupList&amp; Hue::groups() @@ -246,7 +275,23 @@ Hue::GroupList&amp; Hue::groups()
246 return groupList; 275 return groupList;
247 } 276 }
248 277
249 -const Hue::GroupList& Hue::groups() const 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 { 295 {
251 return groupList; 296 return groupList;
252 } 297 }
src/HueLight.cpp
@@ -27,6 +27,7 @@ @@ -27,6 +27,7 @@
27 #include <thread> 27 #include <thread>
28 28
29 #include "hueplusplus/HueExceptionMacro.h" 29 #include "hueplusplus/HueExceptionMacro.h"
  30 +#include "hueplusplus/HueThing.h"
30 #include "hueplusplus/Utils.h" 31 #include "hueplusplus/Utils.h"
31 #include "json/json.hpp" 32 #include "json/json.hpp"
32 33
@@ -52,46 +53,6 @@ bool HueLight::isOn() const @@ -52,46 +53,6 @@ bool HueLight::isOn() const
52 return state.getValue().at("state").at("on").get<bool>(); 53 return state.getValue().at("state").at("on").get<bool>();
53 } 54 }
54 55
55 -int HueLight::getId() const  
56 -{  
57 - return id;  
58 -}  
59 -  
60 -std::string HueLight::getType() const  
61 -{  
62 - return state.getValue()["type"].get<std::string>();  
63 -}  
64 -  
65 -std::string HueLight::getName()  
66 -{  
67 - return state.getValue()["name"].get<std::string>();  
68 -}  
69 -  
70 -std::string HueLight::getName() const  
71 -{  
72 - return state.getValue()["name"].get<std::string>();  
73 -}  
74 -  
75 -std::string HueLight::getModelId() const  
76 -{  
77 - return state.getValue()["modelid"].get<std::string>();  
78 -}  
79 -  
80 -std::string HueLight::getUId() const  
81 -{  
82 - return state.getValue().value("uniqueid", std::string());  
83 -}  
84 -  
85 -std::string HueLight::getManufacturername() const  
86 -{  
87 - return state.getValue().value("manufacturername", std::string());  
88 -}  
89 -  
90 -std::string HueLight::getProductname() const  
91 -{  
92 - return state.getValue().value("productname", std::string());  
93 -}  
94 -  
95 std::string HueLight::getLuminaireUId() const 56 std::string HueLight::getLuminaireUId() const
96 { 57 {
97 return state.getValue().value("luminaireuniqueid", std::string()); 58 return state.getValue().value("luminaireuniqueid", std::string());
@@ -107,18 +68,6 @@ std::string HueLight::getSwVersion() const @@ -107,18 +68,6 @@ std::string HueLight::getSwVersion() const
107 return state.getValue()["swversion"].get<std::string>(); 68 return state.getValue()["swversion"].get<std::string>();
108 } 69 }
109 70
110 -bool HueLight::setName(const std::string& name)  
111 -{  
112 - nlohmann::json request = nlohmann::json::object();  
113 - request["name"] = name;  
114 - nlohmann::json reply = sendPutRequest(request, "/name", CURRENT_FILE_INFO);  
115 - state.refresh();  
116 -  
117 - // Check whether request was successful (returned name is not necessarily the actually set name)  
118 - // If it already exists, a number is added, if it is too long to be returned, "Updated" is returned  
119 - return utils::safeGetMember(reply, 0, "success", "/lights/" + std::to_string(id) + "/name").is_string();  
120 -}  
121 -  
122 ColorType HueLight::getColorType() const 71 ColorType HueLight::getColorType() const
123 { 72 {
124 return colorType; 73 return colorType;
@@ -183,9 +132,8 @@ HueLight::HueLight(int id, const HueCommandAPI&amp; commands) : HueLight(id, command @@ -183,9 +132,8 @@ HueLight::HueLight(int id, const HueCommandAPI&amp; commands) : HueLight(id, command
183 132
184 HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr<const BrightnessStrategy> brightnessStrategy, 133 HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr<const BrightnessStrategy> brightnessStrategy,
185 std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy, 134 std::shared_ptr<const ColorTemperatureStrategy> colorTempStrategy,
186 - std::shared_ptr<const ColorHueStrategy> colorHueStrategy, std::chrono::steady_clock::duration refreshDuration)  
187 - : id(id),  
188 - state("/lights/" + std::to_string(id), commands, refreshDuration), 135 + std::shared_ptr<const ColorHueStrategy> colorHueStrategy, chrono::steady_clock::duration refreshDuration)
  136 + : HueThing(id, commands, "/lights/"),
189 colorType(ColorType::NONE), 137 colorType(ColorType::NONE),
190 brightnessStrategy(std::move(brightnessStrategy)), 138 brightnessStrategy(std::move(brightnessStrategy)),
191 colorTemperatureStrategy(std::move(colorTempStrategy)), 139 colorTemperatureStrategy(std::move(colorTempStrategy)),
src/HueSensor.cpp 0 โ†’ 100644
  1 +/**
  2 + \file HueSensor.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/HueSensor.h"
  23 +
  24 +#include "hueplusplus/HueThing.h"
  25 +#include "json/json.hpp"
  26 +
  27 +namespace hueplusplus
  28 +{
  29 +int HueSensor::getButtonEvent()
  30 +{
  31 + refreshState();
  32 + if (hasButtonEvent())
  33 + {
  34 + return state["state"]["buttonevent"];
  35 + }
  36 + return 0;
  37 +}
  38 +
  39 +int HueSensor::getButtonEvent() const
  40 +{
  41 + if (hasButtonEvent())
  42 + {
  43 + return state["state"]["buttonevent"];
  44 + }
  45 + return 0;
  46 +}
  47 +
  48 +int HueSensor::getStatus()
  49 +{
  50 + refreshState();
  51 + if (hasStatus())
  52 + {
  53 + return state["state"]["status"];
  54 + }
  55 + return 0;
  56 +}
  57 +
  58 +int HueSensor::getStatus() const
  59 +{
  60 + if (hasStatus())
  61 + {
  62 + return state["state"]["status"];
  63 + }
  64 + return 0;
  65 +}
  66 +
  67 +bool HueSensor::hasButtonEvent() const
  68 +{
  69 + return state["state"].count("buttonevent") > 0;
  70 +}
  71 +
  72 +bool HueSensor::hasStatus() const
  73 +{
  74 + return state["state"].count("status") > 0;
  75 +}
  76 +
  77 +HueSensor::HueSensor(int id, const HueCommandAPI& commands)
  78 + : HueThing(id, commands, "/sensors/")
  79 +{
  80 + refreshState();
  81 +}
  82 +} // namespace hueplusplus
src/HueThing.cpp 0 โ†’ 100644
  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