hueplusplus 1.2.0
Loading...
Searching...
No Matches
Sensor.h
Go to the documentation of this file.
1
23#ifndef INCLUDE_HUEPLUSPLUS_HUE_SENSOR_H
24#define INCLUDE_HUEPLUSPLUS_HUE_SENSOR_H
25
26#include <memory>
27
28#include "BaseDevice.h"
29#include "Condition.h"
30#include "HueCommandAPI.h"
31#include "TimePattern.h"
32
33#include <nlohmann/json.hpp>
34
35namespace hueplusplus
36{
38enum class Alert
39{
40 none,
41 select,
42 lselect
43};
44
48std::string alertToString(Alert alert);
49
53Alert alertFromString(const std::string& s);
54
59class Sensor : public BaseDevice
60{
61public:
65 Sensor(int id, const std::shared_ptr<APICache>& baseCache);
66
72 Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState);
73
76
78 bool hasOn() const;
83 bool isOn() const;
89 void setOn(bool on);
90
92 bool hasBatteryState() const;
96 int getBatteryState() const;
102 void setBatteryState(int percent);
103
105 bool hasAlert() const;
109 Alert getLastAlert() const;
115 void sendAlert(Alert type);
116
118 bool hasReachable() const;
121 bool isReachable() const;
122
124 bool hasUserTest() const;
133 void setUserTest(bool enabled);
134
136 bool hasURL() const;
140 std::string getURL() const;
146 void setURL(const std::string& url);
147
153 std::vector<std::string> getPendingConfig() const;
154
156 bool hasLEDIndication() const;
159 bool getLEDIndication() const;
165 void setLEDIndication(bool on);
166
169 nlohmann::json getConfig() const;
175 void setConfigAttribute(const std::string& key, const nlohmann::json& value);
176
178
183
185 nlohmann::json getState() const;
191 void setStateAttribute(const std::string& key, const nlohmann::json& value);
192
196 std::string getStateAddress(const std::string& key) const;
197
199 bool isCertified() const;
204 bool isPrimary() const;
205
209 template <typename T>
210 T asSensorType() const&
211 {
212 if (getType() != T::typeStr)
213 {
214 throw HueException(FileInfo {__FILE__, __LINE__, __func__}, "Sensor type does not match: " + getType());
215 }
216 return T(*this);
217 }
223 template <typename T>
225 {
226 if (getType() != T::typeStr)
227 {
228 throw HueException(FileInfo {__FILE__, __LINE__, __func__}, "Sensor type does not match: " + getType());
229 }
230 return T(std::move(*this));
231 }
232};
233
238{
239public:
248 CreateSensor(const std::string& name, const std::string& modelid, const std::string& swversion,
249 const std::string& type, const std::string& uniqueid, const std::string& manufacturername);
250
254 CreateSensor& setState(const nlohmann::json& state);
258 CreateSensor& setConfig(const nlohmann::json& config);
261 CreateSensor& setRecycle(bool recycle);
262
265 nlohmann::json getRequest() const;
266
267protected:
268 nlohmann::json request;
269};
270
274namespace sensors
275{
280{
281public:
283 explicit DaylightSensor(Sensor sensor) : BaseDevice(std::move(sensor)) { }
284
288 bool isOn() const;
289
295 void setOn(bool on);
296
298 bool hasBatteryState() const;
302 int getBatteryState() const;
308 void setBatteryState(int percent);
309
318 void setCoordinates(const std::string& latitude, const std::string& longitude);
322 bool isConfigured() const;
323
327 int getSunriseOffset() const;
334 void setSunriseOffset(int minutes);
335
339 int getSunsetOffset() const;
346 void setSunsetOffset(int minutes);
347
349 bool isDaylight() const;
350
355
357 static constexpr const char* typeStr = "Daylight";
358};
359
360detail::ConditionHelper<bool> makeCondition(const DaylightSensor& sensor);
361
362template <typename SensorT, detail::void_t<decltype(std::declval<const SensorT>().getLastUpdated())>* = nullptr>
363detail::ConditionHelper<time::AbsoluteTime> makeConditionLastUpdate(const SensorT& sensor)
364{
365 return detail::ConditionHelper<time::AbsoluteTime>(
366 "/sensors/" + std::to_string(sensor.getId()) + "/state/lastupdated");
367}
368
369template <typename ButtonSensor, detail::void_t<decltype(std::declval<const ButtonSensor>().getButtonEvent())>* = nullptr>
370detail::ConditionHelper<int> makeCondition(const ButtonSensor& sensor)
371{
372 return detail::ConditionHelper<int>(
373 "/sensors/" + std::to_string(sensor.getId()) + "/state/buttonevent");
374}
375
376template <typename PresenceSensor, detail::void_t<decltype(std::declval<const PresenceSensor>().getPresence())>* = nullptr>
377detail::ConditionHelper<bool> makeCondition(const PresenceSensor& sensor)
378{
379 return detail::ConditionHelper<bool>(
380 "/sensors/" + std::to_string(sensor.getId()) + "/state/presence");
381}
382
383template <typename TemperatureSensor, detail::void_t<decltype(std::declval<const TemperatureSensor>().getPresence())>* = nullptr>
384detail::ConditionHelper<int> makeCondition(const TemperatureSensor& sensor)
385{
386 return detail::ConditionHelper<int>(
387 "/sensors/" + std::to_string(sensor.getId()) + "/state/temperature");
388}
389
390} // namespace sensors
391
392} // namespace hueplusplus
393
394#endif
Base class for physical devices connected to the bridge (sensor or light).
Definition BaseDevice.h:36
virtual std::string getType() const
Const function that returns the device type.
Definition BaseDevice.cpp:39
Parameters for creating a new Sensor.
Definition Sensor.h:238
CreateSensor & setRecycle(bool recycle)
Enable recycling, delete automatically when not referenced.
Definition Sensor.cpp:260
nlohmann::json request
Definition Sensor.h:268
CreateSensor & setConfig(const nlohmann::json &config)
Set config object.
Definition Sensor.cpp:254
nlohmann::json getRequest() const
Get request to create the sensor.
Definition Sensor.cpp:266
CreateSensor & setState(const nlohmann::json &state)
Set state object.
Definition Sensor.cpp:248
Definition HueCommandAPI.h:38
Exception class with file information. Base class of all custom exception classes.
Definition HueException.h:50
Class for generic or unknown sensor types.
Definition Sensor.h:60
std::string getURL() const
Get sensor URL.
Definition Sensor.cpp:158
std::string getStateAddress(const std::string &key) const
Get address of the given state attribute, used for conditions.
Definition Sensor.cpp:206
T asSensorType() &&
Convert sensor to a specific type.
Definition Sensor.h:224
T asSensorType() const &
Convert sensor to a specific type.
Definition Sensor.h:210
void setBatteryState(int percent)
Set battery state.
Definition Sensor.cpp:83
int getBatteryState() const
Get battery state.
Definition Sensor.cpp:79
bool isOn() const
check whether the sensor is turned on
Definition Sensor.cpp:65
void setUserTest(bool enabled)
Enable or disable user test mode.
Definition Sensor.cpp:149
bool hasAlert() const
Check whether the sensor has alerts.
Definition Sensor.cpp:87
bool hasOn() const
Check whether the sensor has an on attribute.
Definition Sensor.cpp:60
void setConfigAttribute(const std::string &key, const nlohmann::json &value)
Set attribute in the sensor config.
Definition Sensor.cpp:216
bool getLEDIndication() const
Get whether the indicator LED is on.
Definition Sensor.cpp:188
bool isCertified() const
Check if the sensor is Hue certified.
Definition Sensor.cpp:221
std::vector< std::string > getPendingConfig() const
Get pending config entries, if they exist.
Definition Sensor.cpp:167
void sendAlert(Alert type)
Send alert.
Definition Sensor.cpp:107
time::AbsoluteTime getLastUpdated() const
Get time of last status update.
Definition Sensor.cpp:134
bool isReachable() const
Get whether sensor is reachable.
Definition Sensor.cpp:128
void setOn(bool on)
Turn sensor on or off.
Definition Sensor.cpp:70
bool isPrimary() const
Check if the sensor is primary sensor of the device.
Definition Sensor.cpp:227
nlohmann::json getConfig() const
Get entire config object.
Definition Sensor.cpp:211
bool hasBatteryState() const
Check whether the sensor has a battery state.
Definition Sensor.cpp:75
nlohmann::json getState() const
Get state object.
Definition Sensor.cpp:197
void setStateAttribute(const std::string &key, const nlohmann::json &value)
Set part of the sensor state.
Definition Sensor.cpp:201
void setURL(const std::string &url)
Set sensor URL.
Definition Sensor.cpp:162
bool hasUserTest() const
Check whether the sensor has a user test mode.
Definition Sensor.cpp:145
bool hasLEDIndication() const
Check whether the sensor has an LED indicator.
Definition Sensor.cpp:184
bool hasURL() const
Check whether the sensor has a URL.
Definition Sensor.cpp:154
void setLEDIndication(bool on)
Turn LED indicator on or off.
Definition Sensor.cpp:192
bool hasReachable() const
Check whether the sensor has reachable validation.
Definition Sensor.cpp:124
Alert getLastAlert() const
Get last sent alert.
Definition Sensor.cpp:91
Daylight sensor to detect sunrise and sunset.
Definition Sensor.h:280
void setSunriseOffset(int minutes)
Set sunrise offset time.
Definition Sensor.cpp:321
bool isOn() const
Check if the sensor is on.
Definition Sensor.cpp:276
void setSunsetOffset(int minutes)
Set sunset offset time.
Definition Sensor.cpp:330
static constexpr const char * typeStr
Daylight sensor type name.
Definition Sensor.h:357
int getSunsetOffset() const
Get time offset in minutes to sunset.
Definition Sensor.cpp:326
int getBatteryState() const
Get battery state.
Definition Sensor.cpp:290
bool isConfigured() const
Check whether coordinates are configured.
Definition Sensor.cpp:313
bool isDaylight() const
Check whether it is daylight or not.
Definition Sensor.cpp:335
int getSunriseOffset() const
Get time offset in minutes to sunrise.
Definition Sensor.cpp:317
DaylightSensor(Sensor sensor)
Construct from generic sensor.
Definition Sensor.h:283
void setCoordinates(const std::string &latitude, const std::string &longitude)
Set GPS coordinates for the calculation.
Definition Sensor.cpp:298
void setBatteryState(int percent)
Set battery state.
Definition Sensor.cpp:294
bool hasBatteryState() const
Check whether the sensor has a battery state.
Definition Sensor.cpp:286
void setOn(bool on)
Enable or disable sensor.
Definition Sensor.cpp:281
time::AbsoluteTime getLastUpdated() const
Get time of last status update.
Definition Sensor.cpp:340
One-time, absolute time point.
Definition TimePattern.h:73
detail::ConditionHelper< bool > makeCondition(const CLIPOpenClose &sensor)
Definition CLIPSensors.cpp:103
detail::ConditionHelper< time::AbsoluteTime > makeConditionLastUpdate(const SensorT &sensor)
Definition Sensor.h:363
Namespace for the hueplusplus library.
Definition Action.h:28
std::string alertToString(Alert alert)
Convert alert to string form.
Definition Sensor.cpp:30
Alert alertFromString(const std::string &s)
Convert string to Alert enum.
Definition Sensor.cpp:44
Alert
Specifies light alert modes.
Definition Sensor.h:39
@ lselect
Long select alert (15s breathe)
@ select
Select alert (breathe cycle)
Contains information about error location, use CURRENT_FILE_INFO to create.
Definition HueException.h:35