hueplusplus 1.2.0
Loading...
Searching...
No Matches
Condition.h
Go to the documentation of this file.
1
22#ifndef INCLUDE_HUEPLUSPLUS_CONDITION_H
23#define INCLUDE_HUEPLUSPLUS_CONDITION_H
24
25#include "TimePattern.h"
26
27#include <nlohmann/json.hpp>
28
29namespace hueplusplus
30{
38{
39public:
41 enum class Operator
42 {
43 eq,
44 gt,
45 lt,
46 dx,
47 ddx,
48 stable,
49 notStable,
50 in,
51 notIn
52 };
53
54public:
59 Condition(const std::string& address, Operator op, const std::string& value);
60
62 std::string getAddress() const;
64 Operator getOperator() const;
66 std::string getValue() const;
67
70 nlohmann::json toJson() const;
71
76 static Condition parse(const nlohmann::json& json);
77
78private:
79 std::string address;
80 Operator op;
81 std::string value;
82};
83
84namespace detail
85{
89template <typename T>
90class ConditionHelper
91{ };
92
94class GeneralConditionHelper
95{
96public:
97 explicit GeneralConditionHelper(const std::string& address) : address(address) { }
98
99 Condition dx() { return Condition(address, Condition::Operator::dx, ""); }
100 Condition ddx() { return Condition(address, Condition::Operator::ddx, ""); }
103 Condition stable(const std::string& value) { return Condition(address, Condition::Operator::dx, value); }
104
105protected:
106 std::string address;
107};
108
110template <>
111class ConditionHelper<int> : public GeneralConditionHelper
112{
113public:
114 using GeneralConditionHelper::GeneralConditionHelper;
115
116 Condition eq(int value) { return create(Condition::Operator::eq, value); }
117 Condition gt(int value) { return create(Condition::Operator::gt, value); }
118 Condition lt(int value) { return create(Condition::Operator::eq, value); }
119
120 Condition create(Condition::Operator op, int value) { return Condition(address, op, std::to_string(value)); }
121};
122
124template <>
125class ConditionHelper<bool> : public GeneralConditionHelper
126{
127public:
128 using GeneralConditionHelper::GeneralConditionHelper;
129
130 Condition eq(bool value) { return create(Condition::Operator::eq, value); }
131
132 Condition create(Condition::Operator op, bool value) { return Condition(address, op, value ? "true" : "false"); }
133};
134
136template <>
137class ConditionHelper<time::AbsoluteTime> : public GeneralConditionHelper
138{
139public:
140 using GeneralConditionHelper::GeneralConditionHelper;
141
142 Condition in(const time::TimeInterval& interval) { return create(Condition::Operator::in, interval); }
143 Condition notIn(const time::TimeInterval& interval) { return create(Condition::Operator::notIn, interval); }
144
145 Condition create(Condition::Operator op, const time::AbsoluteTime& value)
146 {
147 return Condition(address, op, value.toString());
148 }
149 Condition create(Condition::Operator op, const time::TimeInterval& interval)
150 {
151 return Condition(address, op, interval.toString());
152 }
153};
154
155template <typename... Ts>
156struct make_void
157{
158 typedef void type;
159};
161template <typename... Ts>
162using void_t = typename make_void<Ts...>::type;
163
164} // namespace detail
165
166} // namespace hueplusplus
167
168#endif
Condition for a Rule.
Definition Condition.h:38
std::string getAddress() const
Get address on the bridge.
Definition Rule.cpp:30
nlohmann::json toJson() const
Create the json form of the condition.
Definition Rule.cpp:43
std::string getValue() const
Get value the attribute is checked against.
Definition Rule.cpp:38
static Condition parse(const nlohmann::json &json)
Parse condition from json value.
Definition Rule.cpp:87
Operator getOperator() const
Get used operator.
Definition Rule.cpp:34
Operator
Specifies which operation is used to check the condition.
Definition Condition.h:42
@ in
Time is in the given interval (triggered on start time, local time)
@ gt
Attribute is greater than specified value (for int)
@ ddx
Delayed attribute has changed (no value given)
@ dx
Attribute has changed (no value given)
@ notStable
Not stable for a given time. Does not trigger a rule change.
@ lt
Attribute is less than specified value (for int)
@ eq
Attribute is equal to specified value (for bool and int)
@ notIn
Time is not in the interval (triggered on end time, local time)
@ stable
Stable for a given time. Does not trigger a rule change.
Namespace for the hueplusplus library.
Definition Action.h:28