Loading...
Searching...
No Matches
Sensor.h
Go to the documentation of this file.
#ifndef INCLUDE_HUEPLUSPLUS_HUE_SENSOR_H
#define INCLUDE_HUEPLUSPLUS_HUE_SENSOR_H
#include <memory>
#include "BaseDevice.h"
#include "Condition.h"
#include "HueCommandAPI.h"
#include "TimePattern.h"
#include <nlohmann/json.hpp>
namespace hueplusplus
{
enum class Alert
{
none,
select,
lselect
};
std::string alertToString(Alert alert);
Alert alertFromString(const std::string& s);
class Sensor : public BaseDevice
{
public:
Sensor(int id, const std::shared_ptr<APICache>& baseCache);
Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState);
bool hasOn() const;
bool isOn() const;
void setOn(bool on);
bool hasBatteryState() const;
int getBatteryState() const;
void setBatteryState(int percent);
bool hasAlert() const;
Alert getLastAlert() const;
void sendAlert(Alert type);
bool hasReachable() const;
bool isReachable() const;
bool hasUserTest() const;
void setUserTest(bool enabled);
bool hasURL() const;
std::string getURL() const;
void setURL(const std::string& url);
std::vector<std::string> getPendingConfig() const;
bool hasLEDIndication() const;
bool getLEDIndication() const;
void setLEDIndication(bool on);
nlohmann::json getConfig() const;
void setConfigAttribute(const std::string& key, const nlohmann::json& value);
time::AbsoluteTime getLastUpdated() const;
nlohmann::json getState() const;
void setStateAttribute(const std::string& key, const nlohmann::json& value);
std::string getStateAddress(const std::string& key) const;
bool isCertified() const;
bool isPrimary() const;
template <typename T>
T asSensorType() const&
{
if (getType() != T::typeStr)
{
throw HueException(FileInfo {__FILE__, __LINE__, __func__}, "Sensor type does not match: " + getType());
}
return T(*this);
}
template <typename T>
T asSensorType() &&
{
if (getType() != T::typeStr)
{
throw HueException(FileInfo {__FILE__, __LINE__, __func__}, "Sensor type does not match: " + getType());
}
return T(std::move(*this));
}
};
class CreateSensor
{
public:
CreateSensor(const std::string& name, const std::string& modelid, const std::string& swversion,
const std::string& type, const std::string& uniqueid, const std::string& manufacturername);
CreateSensor& setState(const nlohmann::json& state);
CreateSensor& setConfig(const nlohmann::json& config);
CreateSensor& setRecycle(bool recycle);
nlohmann::json getRequest() const;
protected:
nlohmann::json request;
};
namespace sensors
{
class DaylightSensor : public BaseDevice
{
public:
explicit DaylightSensor(Sensor sensor) : BaseDevice(std::move(sensor)) { }
bool isOn() const;
void setOn(bool on);
bool hasBatteryState() const;
int getBatteryState() const;
void setBatteryState(int percent);
void setCoordinates(const std::string& latitude, const std::string& longitude);
bool isConfigured() const;
int getSunriseOffset() const;
void setSunriseOffset(int minutes);
int getSunsetOffset() const;
void setSunsetOffset(int minutes);
bool isDaylight() const;
time::AbsoluteTime getLastUpdated() const;
static constexpr const char* typeStr = "Daylight";
};
detail::ConditionHelper<bool> makeCondition(const DaylightSensor& sensor);
template <typename SensorT, detail::void_t<decltype(std::declval<const SensorT>().getLastUpdated())>* = nullptr>
detail::ConditionHelper<time::AbsoluteTime> makeConditionLastUpdate(const SensorT& sensor)
{
return detail::ConditionHelper<time::AbsoluteTime>(
"/sensors/" + std::to_string(sensor.getId()) + "/state/lastupdated");
}
template <typename ButtonSensor, detail::void_t<decltype(std::declval<const ButtonSensor>().getButtonEvent())>* = nullptr>
detail::ConditionHelper<int> makeCondition(const ButtonSensor& sensor)
{
return detail::ConditionHelper<int>(
"/sensors/" + std::to_string(sensor.getId()) + "/state/buttonevent");
}
template <typename PresenceSensor, detail::void_t<decltype(std::declval<const PresenceSensor>().getPresence())>* = nullptr>
detail::ConditionHelper<bool> makeCondition(const PresenceSensor& sensor)
{
return detail::ConditionHelper<bool>(
"/sensors/" + std::to_string(sensor.getId()) + "/state/presence");
}
template <typename TemperatureSensor, detail::void_t<decltype(std::declval<const TemperatureSensor>().getPresence())>* = nullptr>
detail::ConditionHelper<int> makeCondition(const TemperatureSensor& sensor)
{
return detail::ConditionHelper<int>(
"/sensors/" + std::to_string(sensor.getId()) + "/state/temperature");
}
} // namespace sensors
} // namespace hueplusplus
#endif
BaseDevice.h
Condition.h
HueCommandAPI.h
TimePattern.h
hueplusplus::BaseDevice
Base class for physical devices connected to the bridge (sensor or light).
Definition BaseDevice.h:36
hueplusplus::BaseDevice::getType
virtual std::string getType() const
Const function that returns the device type.
Definition BaseDevice.cpp:39
hueplusplus::CreateSensor
Parameters for creating a new Sensor.
Definition Sensor.h:238
hueplusplus::CreateSensor::setRecycle
CreateSensor & setRecycle(bool recycle)
Enable recycling, delete automatically when not referenced.
Definition Sensor.cpp:260
hueplusplus::CreateSensor::request
nlohmann::json request
Definition Sensor.h:268
hueplusplus::CreateSensor::setConfig
CreateSensor & setConfig(const nlohmann::json &config)
Set config object.
Definition Sensor.cpp:254
hueplusplus::CreateSensor::getRequest
nlohmann::json getRequest() const
Get request to create the sensor.
Definition Sensor.cpp:266
hueplusplus::CreateSensor::setState
CreateSensor & setState(const nlohmann::json &state)
Set state object.
Definition Sensor.cpp:248
hueplusplus::HueCommandAPI
Definition HueCommandAPI.h:38
hueplusplus::HueException
Exception class with file information. Base class of all custom exception classes.
Definition HueException.h:50
hueplusplus::Sensor
Class for generic or unknown sensor types.
Definition Sensor.h:60
hueplusplus::Sensor::getURL
std::string getURL() const
Get sensor URL.
Definition Sensor.cpp:158
hueplusplus::Sensor::getStateAddress
std::string getStateAddress(const std::string &key) const
Get address of the given state attribute, used for conditions.
Definition Sensor.cpp:206
hueplusplus::Sensor::asSensorType
T asSensorType() &&
Convert sensor to a specific type.
Definition Sensor.h:224
hueplusplus::Sensor::asSensorType
T asSensorType() const &
Convert sensor to a specific type.
Definition Sensor.h:210
hueplusplus::Sensor::setBatteryState
void setBatteryState(int percent)
Set battery state.
Definition Sensor.cpp:83
hueplusplus::Sensor::getBatteryState
int getBatteryState() const
Get battery state.
Definition Sensor.cpp:79
hueplusplus::Sensor::isOn
bool isOn() const
check whether the sensor is turned on
Definition Sensor.cpp:65
hueplusplus::Sensor::setUserTest
void setUserTest(bool enabled)
Enable or disable user test mode.
Definition Sensor.cpp:149
hueplusplus::Sensor::hasAlert
bool hasAlert() const
Check whether the sensor has alerts.
Definition Sensor.cpp:87
hueplusplus::Sensor::hasOn
bool hasOn() const
Check whether the sensor has an on attribute.
Definition Sensor.cpp:60
hueplusplus::Sensor::setConfigAttribute
void setConfigAttribute(const std::string &key, const nlohmann::json &value)
Set attribute in the sensor config.
Definition Sensor.cpp:216
hueplusplus::Sensor::getLEDIndication
bool getLEDIndication() const
Get whether the indicator LED is on.
Definition Sensor.cpp:188
hueplusplus::Sensor::isCertified
bool isCertified() const
Check if the sensor is Hue certified.
Definition Sensor.cpp:221
hueplusplus::Sensor::getPendingConfig
std::vector< std::string > getPendingConfig() const
Get pending config entries, if they exist.
Definition Sensor.cpp:167
hueplusplus::Sensor::sendAlert
void sendAlert(Alert type)
Send alert.
Definition Sensor.cpp:107
hueplusplus::Sensor::getLastUpdated
time::AbsoluteTime getLastUpdated() const
Get time of last status update.
Definition Sensor.cpp:134
hueplusplus::Sensor::isReachable
bool isReachable() const
Get whether sensor is reachable.
Definition Sensor.cpp:128
hueplusplus::Sensor::setOn
void setOn(bool on)
Turn sensor on or off.
Definition Sensor.cpp:70
hueplusplus::Sensor::isPrimary
bool isPrimary() const
Check if the sensor is primary sensor of the device.
Definition Sensor.cpp:227
hueplusplus::Sensor::getConfig
nlohmann::json getConfig() const
Get entire config object.
Definition Sensor.cpp:211
hueplusplus::Sensor::hasBatteryState
bool hasBatteryState() const
Check whether the sensor has a battery state.
Definition Sensor.cpp:75
hueplusplus::Sensor::getState
nlohmann::json getState() const
Get state object.
Definition Sensor.cpp:197
hueplusplus::Sensor::setStateAttribute
void setStateAttribute(const std::string &key, const nlohmann::json &value)
Set part of the sensor state.
Definition Sensor.cpp:201
hueplusplus::Sensor::setURL
void setURL(const std::string &url)
Set sensor URL.
Definition Sensor.cpp:162
hueplusplus::Sensor::hasUserTest
bool hasUserTest() const
Check whether the sensor has a user test mode.
Definition Sensor.cpp:145
hueplusplus::Sensor::hasLEDIndication
bool hasLEDIndication() const
Check whether the sensor has an LED indicator.
Definition Sensor.cpp:184
hueplusplus::Sensor::hasURL
bool hasURL() const
Check whether the sensor has a URL.
Definition Sensor.cpp:154
hueplusplus::Sensor::setLEDIndication
void setLEDIndication(bool on)
Turn LED indicator on or off.
Definition Sensor.cpp:192
hueplusplus::Sensor::hasReachable
bool hasReachable() const
Check whether the sensor has reachable validation.
Definition Sensor.cpp:124
hueplusplus::Sensor::getLastAlert
Alert getLastAlert() const
Get last sent alert.
Definition Sensor.cpp:91
hueplusplus::sensors::DaylightSensor
Daylight sensor to detect sunrise and sunset.
Definition Sensor.h:280
hueplusplus::sensors::DaylightSensor::setSunriseOffset
void setSunriseOffset(int minutes)
Set sunrise offset time.
Definition Sensor.cpp:321
hueplusplus::sensors::DaylightSensor::isOn
bool isOn() const
Check if the sensor is on.
Definition Sensor.cpp:276
hueplusplus::sensors::DaylightSensor::setSunsetOffset
void setSunsetOffset(int minutes)
Set sunset offset time.
Definition Sensor.cpp:330
hueplusplus::sensors::DaylightSensor::typeStr
static constexpr const char * typeStr
Daylight sensor type name.
Definition Sensor.h:357
hueplusplus::sensors::DaylightSensor::getSunsetOffset
int getSunsetOffset() const
Get time offset in minutes to sunset.
Definition Sensor.cpp:326
hueplusplus::sensors::DaylightSensor::getBatteryState
int getBatteryState() const
Get battery state.
Definition Sensor.cpp:290
hueplusplus::sensors::DaylightSensor::isConfigured
bool isConfigured() const
Check whether coordinates are configured.
Definition Sensor.cpp:313
hueplusplus::sensors::DaylightSensor::isDaylight
bool isDaylight() const
Check whether it is daylight or not.
Definition Sensor.cpp:335
hueplusplus::sensors::DaylightSensor::getSunriseOffset
int getSunriseOffset() const
Get time offset in minutes to sunrise.
Definition Sensor.cpp:317
hueplusplus::sensors::DaylightSensor::DaylightSensor
DaylightSensor(Sensor sensor)
Construct from generic sensor.
Definition Sensor.h:283
hueplusplus::sensors::DaylightSensor::setCoordinates
void setCoordinates(const std::string &latitude, const std::string &longitude)
Set GPS coordinates for the calculation.
Definition Sensor.cpp:298
hueplusplus::sensors::DaylightSensor::setBatteryState
void setBatteryState(int percent)
Set battery state.
Definition Sensor.cpp:294
hueplusplus::sensors::DaylightSensor::hasBatteryState
bool hasBatteryState() const
Check whether the sensor has a battery state.
Definition Sensor.cpp:286
hueplusplus::sensors::DaylightSensor::setOn
void setOn(bool on)
Enable or disable sensor.
Definition Sensor.cpp:281
hueplusplus::sensors::DaylightSensor::getLastUpdated
time::AbsoluteTime getLastUpdated() const
Get time of last status update.
Definition Sensor.cpp:340
hueplusplus::time::AbsoluteTime
One-time, absolute time point.
Definition TimePattern.h:73
hueplusplus::sensors::makeCondition
detail::ConditionHelper< bool > makeCondition(const CLIPOpenClose &sensor)
Definition CLIPSensors.cpp:103
hueplusplus::sensors::makeConditionLastUpdate
detail::ConditionHelper< time::AbsoluteTime > makeConditionLastUpdate(const SensorT &sensor)
Definition Sensor.h:363
hueplusplus
Namespace for the hueplusplus library.
Definition Action.h:28
hueplusplus::alertToString
std::string alertToString(Alert alert)
Convert alert to string form.
Definition Sensor.cpp:30
hueplusplus::alertFromString
Alert alertFromString(const std::string &s)
Convert string to Alert enum.
Definition Sensor.cpp:44
hueplusplus::Alert
Alert
Specifies light alert modes.
Definition Sensor.h:39
hueplusplus::Alert::none
@ none
No alert.
hueplusplus::Alert::lselect
@ lselect
Long select alert (15s breathe)
hueplusplus::Alert::select
@ select
Select alert (breathe cycle)
hueplusplus::FileInfo
Contains information about error location, use CURRENT_FILE_INFO to create.
Definition HueException.h:35