Loading...
Searching...
No Matches
Condition.h
Go to the documentation of this file.
#ifndef INCLUDE_HUEPLUSPLUS_CONDITION_H
#define INCLUDE_HUEPLUSPLUS_CONDITION_H
#include "TimePattern.h"
#include <nlohmann/json.hpp>
namespace hueplusplus
{
class Condition
{
public:
enum class Operator
{
eq,
gt,
lt,
dx,
ddx,
stable,
notStable,
in,
notIn
};
public:
Condition(const std::string& address, Operator op, const std::string& value);
std::string getAddress() const;
Operator getOperator() const;
std::string getValue() const;
nlohmann::json toJson() const;
static Condition parse(const nlohmann::json& json);
private:
std::string address;
Operator op;
std::string value;
};
namespace detail
{
template <typename T>
class ConditionHelper
{ };
class GeneralConditionHelper
{
public:
explicit GeneralConditionHelper(const std::string& address) : address(address) { }
Condition dx() { return Condition(address, Condition::Operator::dx, ""); }
Condition ddx() { return Condition(address, Condition::Operator::ddx, ""); }
Condition stable(const std::string& value) { return Condition(address, Condition::Operator::dx, value); }
protected:
std::string address;
};
template <>
class ConditionHelper<int> : public GeneralConditionHelper
{
public:
using GeneralConditionHelper::GeneralConditionHelper;
Condition eq(int value) { return create(Condition::Operator::eq, value); }
Condition gt(int value) { return create(Condition::Operator::gt, value); }
Condition lt(int value) { return create(Condition::Operator::eq, value); }
Condition create(Condition::Operator op, int value) { return Condition(address, op, std::to_string(value)); }
};
template <>
class ConditionHelper<bool> : public GeneralConditionHelper
{
public:
using GeneralConditionHelper::GeneralConditionHelper;
Condition eq(bool value) { return create(Condition::Operator::eq, value); }
Condition create(Condition::Operator op, bool value) { return Condition(address, op, value ? "true" : "false"); }
};
template <>
class ConditionHelper<time::AbsoluteTime> : public GeneralConditionHelper
{
public:
using GeneralConditionHelper::GeneralConditionHelper;
Condition in(const time::TimeInterval& interval) { return create(Condition::Operator::in, interval); }
Condition notIn(const time::TimeInterval& interval) { return create(Condition::Operator::notIn, interval); }
Condition create(Condition::Operator op, const time::AbsoluteTime& value)
{
return Condition(address, op, value.toString());
}
Condition create(Condition::Operator op, const time::TimeInterval& interval)
{
return Condition(address, op, interval.toString());
}
};
template <typename... Ts>
struct make_void
{
typedef void type;
};
template <typename... Ts>
using void_t = typename make_void<Ts...>::type;
} // namespace detail
} // namespace hueplusplus
#endif
TimePattern.h
hueplusplus::Condition
Condition for a Rule.
Definition Condition.h:38
hueplusplus::Condition::getAddress
std::string getAddress() const
Get address on the bridge.
Definition Rule.cpp:30
hueplusplus::Condition::toJson
nlohmann::json toJson() const
Create the json form of the condition.
Definition Rule.cpp:43
hueplusplus::Condition::getValue
std::string getValue() const
Get value the attribute is checked against.
Definition Rule.cpp:38
hueplusplus::Condition::parse
static Condition parse(const nlohmann::json &json)
Parse condition from json value.
Definition Rule.cpp:87
hueplusplus::Condition::getOperator
Operator getOperator() const
Get used operator.
Definition Rule.cpp:34
hueplusplus::Condition::Operator
Operator
Specifies which operation is used to check the condition.
Definition Condition.h:42
hueplusplus::Condition::Operator::in
@ in
Time is in the given interval (triggered on start time, local time)
hueplusplus::Condition::Operator::gt
@ gt
Attribute is greater than specified value (for int)
hueplusplus::Condition::Operator::ddx
@ ddx
Delayed attribute has changed (no value given)
hueplusplus::Condition::Operator::dx
@ dx
Attribute has changed (no value given)
hueplusplus::Condition::Operator::notStable
@ notStable
Not stable for a given time. Does not trigger a rule change.
hueplusplus::Condition::Operator::lt
@ lt
Attribute is less than specified value (for int)
hueplusplus::Condition::Operator::eq
@ eq
Attribute is equal to specified value (for bool and int)
hueplusplus::Condition::Operator::notIn
@ notIn
Time is not in the interval (triggered on end time, local time)
hueplusplus::Condition::Operator::stable
@ stable
Stable for a given time. Does not trigger a rule change.
hueplusplus
Namespace for the hueplusplus library.
Definition Action.h:28