forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.h
216 lines (184 loc) · 4.92 KB
/
rule.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright (c) 2016 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#ifndef RULE_H
#define RULE_H
#include <stdint.h>
#include <QString>
#include <vector>
#include <QDateTime>
#include <deconz.h>
#include "resource.h"
#include "bindings.h"
#include "json.h"
class RuleCondition;
class RuleAction;
class RestNodeBase;
/*! Helper class to handle ZigBee binding/unbinding for Rules. */
class BindingTask
{
public:
enum Constants
{
Timeout = 20,
Retries = 2
};
enum State
{
StateIdle,
StateInProgress,
StateCheck,
StateFinished
};
enum Action
{
ActionBind,
ActionUnbind
};
BindingTask():
action(ActionBind),
state(StateCheck),
timeout(BindingTask::Timeout),
retries(BindingTask::Retries),
restNode(0)
{
}
bool operator==(const BindingTask &rhs) const;
bool operator!=(const BindingTask &rhs) const;
Action action;
State state;
quint8 zdpSeqNum;
int timeout; // seconds
int retries;
RestNodeBase *restNode;
Binding binding;
};
/*! \class Rule
Represents a Rest API Rule.
*/
class Rule
{
public:
Rule();
enum State
{
StateNormal,
StateDeleted
};
enum Constants
{
MaxVerifyDelay = 300
};
State state() const;
void setState(State state);
const QString &id() const;
void setId(const QString &id);
const QString &name() const;
void setName(const QString &name);
const QDateTime &lastTriggered() const;
const QString &creationtime() const;
void setCreationtime(const QString &creationtime);
const quint32 ×Triggered() const;
void setTimesTriggered(const quint32 ×Triggered);
int triggerPeriodic() const;
void setTriggerPeriodic(int ms);
const QString &owner() const;
void setOwner(const QString &owner);
const QString &status() const;
void setStatus(const QString &status);
const std::vector<RuleCondition> &conditions() const;
void setConditions(const std::vector<RuleCondition> &conditions);
const std::vector<RuleAction> &actions() const;
void setActions(const std::vector<RuleAction> &actions);
bool isEnabled() const;
int handle() const;
static QString actionsToString(const std::vector<RuleAction> &actions);
static QString conditionsToString(const std::vector<RuleCondition> &conditions);
static std::vector<RuleAction> jsonToActions(const QString &json);
static std::vector<RuleCondition> jsonToConditions(const QString &json);
QString etag;
QDateTime lastVerify;
QDateTime m_lastTriggered;
int lastBindingVerify; // copy of idleTotalCounter at last binding verification
private:
State m_state;
QString m_id;
int m_handle;
QString m_name;
QString m_creationtime;
quint32 m_timesTriggered;
int m_triggerPeriodic;
QString m_owner;
QString m_status;
std::vector<RuleCondition> m_conditions;
std::vector<RuleAction> m_actions;
};
class RuleAction
{
public:
RuleAction();
const QString &address() const;
void setAddress(const QString &address);
const QString &method() const;
void setMethod(const QString &method);
const QString &body() const;
void setBody(const QString &body);
bool operator==(const RuleAction &other) const;
private:
QString m_address;
QString m_method;
QString m_body;
};
class RuleCondition
{
public:
enum Operator
{
OpEqual,
OpGreaterThan,
OpLowerThan,
OpDx,
OpDdx,
OpIn,
OpNotIn,
OpStable,
OpNotStable,
OpUnknown
};
RuleCondition();
RuleCondition(const QVariantMap &map);
const QString &address() const;
void setAddress(const QString &address);
const QString &ooperator() const;
void setOperator(const QString &ooperator);
const QVariant &value() const;
void setValue(const QVariant &value);
bool operator==(const RuleCondition &other) const;
Operator op() const;
const QString id() const;
int numericValue() const;
int seconds() const;
const QTime &time0() const;
const QTime &time1() const;
const char *resource() const;
const char *suffix() const;
private:
QString m_address;
QString m_operator;
QVariant m_value;
// internal calculated values for faster access
const char *m_prefix;
const char *m_suffix;
QString m_id;
Operator m_op;
int m_num;
QTime m_time0;
QTime m_time1;
};
#endif // RULE_H