forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.cpp
More file actions
465 lines (400 loc) · 10.9 KB
/
Copy pathrule.cpp
File metadata and controls
465 lines (400 loc) · 10.9 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* 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.
*
*/
#include "rule.h"
/*! Constructor. */
Rule::Rule() :
lastVerify(0),
m_state(StateNormal),
m_id("notSet"),
m_name("notSet"),
m_lastTriggered("none"),
m_creationtime("notSet"),
m_timesTriggered(0),
m_triggerPeriodic(0),
m_owner("notSet"),
m_status("enabled")
{
}
/*! Returns the rule state.
*/
Rule::State Rule::state() const
{
return m_state;
}
/*! Sets the rule state.
\param state the rule state
*/
void Rule::setState(State state)
{
m_state = state;
}
/*! Returns the rule id.
*/
const QString &Rule::id() const
{
return m_id;
}
/*! Sets the rule id.
\param id the rule id
*/
void Rule::setId(const QString &id)
{
m_id = id;
}
/*! Returns the rule name.
*/
const QString &Rule::name() const
{
return m_name;
}
/*! Sets the rule name.
\param name the rule name
*/
void Rule::setName(const QString &name)
{
m_name = name;
}
/*! Returns the date the rule was last triggered.
*/
const QString &Rule::lastTriggered() const
{
return this->m_lastTriggered;
}
/*! Sets the date the rule was last triggered.
\param lastTriggered the date the rule was last triggered
*/
void Rule::setLastTriggered(const QString &lastTriggered)
{
m_lastTriggered = lastTriggered;
m_lastTriggeredTime.start();
}
/*! Returns the timestamp the rule was last triggered.
*/
const QTime &Rule::lastTriggeredTime() const
{
return m_lastTriggeredTime;
}
/*! Returns the date the rule was created.
*/
const QString &Rule::creationtime() const
{
return this->m_creationtime;
}
/*! Sets the date the rule was created.
\param creationtime the date the rule was created
*/
void Rule::setCreationtime(const QString &creationtime)
{
this->m_creationtime = creationtime;
}
/*! Returns the count the rule was triggered.
*/
const quint32 &Rule::timesTriggered() const
{
return this->m_timesTriggered;
}
/*! Sets the count the rule was triggered.
\param timesTriggered the count the rule was triggered
*/
void Rule::setTimesTriggered(const quint32 ×Triggered)
{
this->m_timesTriggered = timesTriggered;
}
/*! Returns the trigger periodic time value in milliseconds.
val < 0 trigger disabled
val == 0 trigger on event
val > 0 trigger every <val> ms
*/
int Rule::triggerPeriodic() const
{
return m_triggerPeriodic;
}
void Rule::setTriggerPeriodic(int ms)
{
m_triggerPeriodic = ms;
}
/*! Returns the owner of the rule.
*/
const QString &Rule::owner() const
{
return this->m_owner;
}
/*! Sets the owner of the rule.
\param owner the owner of the rule
*/
void Rule::setOwner(const QString &owner)
{
this->m_owner = owner;
}
/*! Returns the status of the rule.
*/
const QString &Rule::status() const
{
return this->m_status;
}
/*! Sets the status of the rule.
\param status the status of the rule
*/
void Rule::setStatus(const QString &status)
{
this->m_status = status;
}
/*! Returns the rule conditions.
*/
const std::vector<RuleCondition> &Rule::conditions() const
{
return this->m_conditions;
}
/*! Sets the rule conditions.
\param conditions the rule conditions
*/
void Rule::setConditions(const std::vector<RuleCondition> &conditions)
{
this->m_conditions = conditions;
}
/*! Returns the rule actions.
*/
const std::vector<RuleAction> &Rule::actions() const
{
return this->m_actions;
}
/*! Sets the rule actions.
\param actions the rule actions
*/
void Rule::setActions(const std::vector<RuleAction> &actions)
{
this->m_actions = actions;
}
/*! Transfers actions into JSONString.
\param actions vector<Action>
*/
QString Rule::actionsToString(const std::vector<RuleAction> &actions)
{
QString jsonString = QLatin1String("[");
std::vector<RuleAction>::const_iterator i = actions.begin();
std::vector<RuleAction>::const_iterator i_end = actions.end();
for (; i != i_end; ++i)
{
jsonString.append(QLatin1String("{\"address\":"));
jsonString.append(QLatin1String("\"") + i->address() + QLatin1String("\","));
jsonString.append(QLatin1String("\"body\":") + i->body() + QLatin1String(","));
jsonString.append(QLatin1String("\"method\":\"") + i->method() + QLatin1String("\"},"));
}
jsonString.chop(1);
jsonString.append(QLatin1String("]"));
return jsonString;
}
/*! Transfers conditions into JSONString.
\param conditions vector<Condition>
*/
QString Rule::conditionsToString(const std::vector<RuleCondition> &conditions)
{
QString jsonString = QLatin1String("[");
std::vector<RuleCondition>::const_iterator i = conditions.begin();
std::vector<RuleCondition>::const_iterator i_end = conditions.end();
for (; i != i_end; ++i)
{
jsonString.append(QLatin1String("{\"address\":"));
jsonString.append(QLatin1String("\"") + i->address() + QLatin1String("\","));
jsonString.append(QLatin1String("\"operator\":\"") + i->ooperator() + QLatin1String("\","));
jsonString.append(QLatin1String("\"value\":\"") + i->value() + QLatin1String("\"},"));
}
jsonString.chop(1);
jsonString.append(QLatin1String("]"));
return jsonString;
}
/*! Parse a JSON string into RuleAction array.
\param json - a JSON list of actions
*/
std::vector<RuleAction> Rule::jsonToActions(const QString &json)
{
bool ok;
std::vector<RuleAction> actions;
QVariantList var = Json::parse(json, ok).toList();
if (!ok)
{
return actions;
}
QVariantList::const_iterator i = var.begin();
QVariantList::const_iterator end = var.end();
for (; i != end; ++i)
{
RuleAction action;
QVariantMap map = i->toMap();
action.setAddress(map["address"].toString());
QVariantMap bodymap = i->toMap()["body"].toMap();
action.setBody(Json::serialize(bodymap));
action.setMethod(map["method"].toString());
actions.push_back(action);
}
return actions;
}
std::vector<RuleCondition> Rule::jsonToConditions(const QString &json)
{
bool ok;
QVariantList var = (Json::parse(json, ok)).toList();
QVariantMap map;
RuleCondition condition;
std::vector<RuleCondition> conditions;
QVariantList::const_iterator i = var.begin();
QVariantList::const_iterator i_end = var.end();
for (; i != i_end; ++i)
{
map = i->toMap();
condition.setAddress(map["address"].toString());
condition.setOperator(map["operator"].toString());
condition.setValue(map["value"].toString());
conditions.push_back(condition);
}
return conditions;
}
// Action
RuleAction::RuleAction() :
m_address(""),
m_method(""),
m_body("")
{
}
/*! Sets the action address.
Path to a light resource, a group resource or any other bridge resource
\param address the action address
*/
void RuleAction::setAddress(const QString &address)
{
m_address = address;
}
/*! Returns the action address.
*/
const QString &RuleAction::address() const
{
return m_address;
}
/*! Sets the action method.
The HTTP method used to send the body to the given address.
Either "POST", "PUT", "BIND", "DELETE" for local addresses.
\param method the action method
*/
void RuleAction::setMethod(const QString &method)
{
DBG_Assert((method == "POST") || (method == "PUT") || (method == "DELETE") || (method == "BIND"));
if (!((method == "POST") || (method == "PUT") || (method == "DELETE") || (method == "BIND")))
{
DBG_Printf(DBG_INFO, "actions method must be either POST, PUT, BIND or DELETE\n");
return;
}
m_method = method;
}
/*! Returns the action method.
*/
const QString &RuleAction::method() const
{
return m_method;
}
/*! Sets the action body.
JSON string to be send to the relevant resource.
\param body the action body
*/
void RuleAction::setBody(const QString &body)
{
QString str = body;
m_body = str.replace( " ", "" );
}
bool RuleAction::operator==(const RuleAction &other) const
{
return(m_address == other.m_address &&
m_body == other.m_body &&
m_method == other.m_method);
}
/*! Returns the action body.
*/
const QString &RuleAction::body() const
{
return m_body;
}
// Condition
RuleCondition::RuleCondition() :
m_address(""),
m_operator(""),
m_value("")
{
}
/*! Sets the condition address.
Path to an attribute of a sensor resource.
\param address the condition address
*/
void RuleCondition::setAddress(const QString &address)
{
m_address = address;
}
/*! Returns the condition address.
*/
const QString &RuleCondition::address() const
{
return m_address;
}
/*! Sets the condition operator.
The operator can be 'eq', 'gt', 'lt' or 'dx'
\param operator the condition operator
*/
void RuleCondition::setOperator(const QString &aOperator)
{
DBG_Assert((aOperator == "eq") || (aOperator == "gt") || (aOperator == "lt") || (aOperator == "dx"));
if (!((aOperator == "eq") || (aOperator == "gt") || (aOperator == "lt") || (aOperator == "dx")))
{
DBG_Printf(DBG_INFO, "actions operator must be either 'eq', 'gt', 'lt' or 'dx'\n");
return;
}
m_operator = aOperator;
}
/*! Returns the condition address.
*/
const QString &RuleCondition::ooperator() const
{
return m_operator;
}
/*! Sets the condition value.
The resource attribute is compared to this value using the given operator.
The value is cast to the data type of the resource attribute (in case of time, casted to a timePattern).
If the cast fails or the operator does not support the data type the value is cast to the rule is rejected.
\param value the condition value
*/
void RuleCondition::setValue(const QString &value)
{
m_value = value;
}
bool RuleCondition::operator==(const RuleCondition &other) const
{
return (m_address == other.m_address &&
m_operator == other.m_operator &&
m_value == other.m_value);
}
/*! Returns the condition value.
*/
const QString &RuleCondition::value() const
{
return m_value;
}
/*! Returns true if two BindingTasks are equal.
*/
bool BindingTask::operator==(const BindingTask &rhs) const
{
if (rhs.action == action &&
rhs.binding == binding)
{
return true;
}
return false;
}
/*! Returns true if two BindingTasks are unequal.
*/
bool BindingTask::operator!=(const BindingTask &rhs) const
{
return !(*this == rhs);
}