hueplusplus  1.0.0
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 "json/json.hpp"
28 
29 namespace hueplusplus
30 {
37 class Condition
38 {
39 public:
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 
54 public:
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 
78 private:
79  std::string address;
80  Operator op;
81  std::string value;
82 };
83 
84 namespace detail
85 {
89 template <typename T>
90 class ConditionHelper
91 { };
92 
94 class GeneralConditionHelper
95 {
96 public:
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 
105 protected:
106  std::string address;
107 };
108 
110 template <>
111 class ConditionHelper<int> : public GeneralConditionHelper
112 {
113 public:
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 
124 template <>
125 class ConditionHelper<bool> : public GeneralConditionHelper
126 {
127 public:
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 
136 template <>
137 class ConditionHelper<time::AbsoluteTime> : public GeneralConditionHelper
138 {
139 public:
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 
155 template <typename... Ts>
156 struct make_void
157 {
158  typedef void type;
159 };
161 template <typename... Ts>
162 using 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:37
Time is in the given interval (triggered on start time, local time)
Stable for a given time. Does not trigger a rule change.
std::string getValue() const
Get value the attribute is checked against.
Definition: Rule.cpp:38
std::string toString() const
Get formatted string as expected by Hue API.
Definition: TimePattern.cpp:159
Operator
Specifies which operation is used to check the condition.
Definition: Condition.h:41
Condition(const std::string &address, Operator op, const std::string &value)
Create a condition from any address on the bridge.
Definition: Rule.cpp:27
Time interval repeated daily to weekly.
Definition: TimePattern.h:260
Delayed attribute has changed (no value given)
Namespace for the hueplusplus library.
Definition: Action.h:27
std::string getAddress() const
Get address on the bridge.
Definition: Rule.cpp:30
std::string toString() const
Get formatted string as expected by Hue API.
Definition: TimePattern.cpp:379
Operator getOperator() const
Get used operator.
Definition: Rule.cpp:34
One-time, absolute time point.
Definition: TimePattern.h:72
static Condition parse(const nlohmann::json &json)
Parse condition from json value.
Definition: Rule.cpp:87
Attribute is less than specified value (for int)
Not stable for a given time. Does not trigger a rule change.
Attribute is greater than specified value (for int)
Attribute is equal to specified value (for bool and int)
Time is not in the interval (triggered on end time, local time)
nlohmann::json toJson() const
Create the json form of the condition.
Definition: Rule.cpp:43
Attribute has changed (no value given)