hueplusplus  1.0.0
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 "json/json.hpp"
34 
35 namespace hueplusplus
36 {
38 enum class Alert
39 {
40  none,
41  select,
42  lselect
43 };
44 
48 std::string alertToString(Alert alert);
49 
53 Alert alertFromString(const std::string& s);
54 
59 class Sensor : public BaseDevice
60 {
61 public:
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 
182  time::AbsoluteTime getLastUpdated() const;
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 {
239 public:
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 
267 protected:
268  nlohmann::json request;
269 };
270 
274 namespace sensors
275 {
280 {
281 public:
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 
354  time::AbsoluteTime getLastUpdated() const;
355 
357  static constexpr const char* typeStr = "Daylight";
358 };
359 
360 detail::ConditionHelper<bool> makeCondition(const DaylightSensor& sensor);
361 
362 template <typename SensorT, detail::void_t<decltype(std::declval<const SensorT>().getLastUpdated())>* = nullptr>
363 detail::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 
369 template <typename ButtonSensor, detail::void_t<decltype(std::declval<const ButtonSensor>().getButtonEvent())>* = nullptr>
370 detail::ConditionHelper<int> makeCondition(const ButtonSensor& sensor)
371 {
372  return detail::ConditionHelper<int>(
373  "/sensors/" + std::to_string(sensor.getId()) + "/state/buttonevent");
374 }
375 
376 template <typename PresenceSensor, detail::void_t<decltype(std::declval<const PresenceSensor>().getPresence())>* = nullptr>
377 detail::ConditionHelper<bool> makeCondition(const PresenceSensor& sensor)
378 {
379  return detail::ConditionHelper<bool>(
380  "/sensors/" + std::to_string(sensor.getId()) + "/state/presence");
381 }
382 
383 template <typename TemperatureSensor, detail::void_t<decltype(std::declval<const TemperatureSensor>().getPresence())>* = nullptr>
384 detail::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
Definition: HueCommandAPI.h:37
Exception class with file information. Base class of all custom exception classes.
Definition: HueException.h:49
Long select alert (15s breathe)
DaylightSensor(Sensor sensor)
Construct from generic sensor.
Definition: Sensor.h:283
T asSensorType() &&
Convert sensor to a specific type.
Definition: Sensor.h:224
Namespace for the hueplusplus library.
Definition: Action.h:27
std::string alertToString(Alert alert)
Convert alert to string form.
Definition: Sensor.cpp:30
Alert
Specifies light alert modes.
Definition: Sensor.h:38
Base class for physical devices connected to the bridge (sensor or light).
Definition: BaseDevice.h:35
Contains information about error location, use CURRENT_FILE_INFO to create.
Definition: HueException.h:34
One-time, absolute time point.
Definition: TimePattern.h:72
nlohmann::json request
Definition: Sensor.h:268
Daylight sensor to detect sunrise and sunset.
Definition: Sensor.h:279
Class for generic or unknown sensor types.
Definition: Sensor.h:59
Alert alertFromString(const std::string &s)
Convert string to Alert enum.
Definition: Sensor.cpp:44
Select alert (breathe cycle)
T asSensorType() const &
Convert sensor to a specific type.
Definition: Sensor.h:210
Parameters for creating a new Sensor.
Definition: Sensor.h:237
detail::ConditionHelper< int > makeCondition(const TemperatureSensor &sensor)
Definition: Sensor.h:384
detail::ConditionHelper< time::AbsoluteTime > makeConditionLastUpdate(const SensorT &sensor)
Definition: Sensor.h:363