forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule.cpp
739 lines (645 loc) · 18.9 KB
/
rule.cpp
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
* Copyright (c) 2017 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"
static int _ruleHandle = 1;
/*! Constructor. */
Rule::Rule() :
lastBindingVerify(0),
m_state(StateNormal),
//m_id(QLatin1String("none")),
m_handle(-1),
//m_name("notSet"),
//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;
m_handle = _ruleHandle++;
}
/*! 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 timestamp the rule was last triggered.
*/
const QDateTime &Rule::lastTriggered() const
{
return m_lastTriggered;
}
/*! 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;
}
/*! Returns true if rule is enabled.
*/
bool Rule::isEnabled() const
{
return m_status == QLatin1String("enabled");
}
/*! Returns the unique rule handle (only valid for this session).
*/
int Rule::handle() const
{
return m_handle;
}
/*! 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)
{
QVariantList ls;
std::vector<RuleCondition>::const_iterator i = conditions.begin();
std::vector<RuleCondition>::const_iterator iend = conditions.end();
for (; i != iend; ++i)
{
QVariantMap map;
map["address"] = i->address();
map["operator"] = i->ooperator();
if (i->value().isValid())
{
map["value"] = i->value();
}
ls.append(map);
}
return Json::serialize(ls);
}
/*! 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();
std::vector<RuleCondition> conditions;
if (!ok)
{
DBG_Printf(DBG_INFO, "failed to parse rule conditions: %s\n", qPrintable(json));
return conditions;
}
QVariantList::const_iterator i = var.begin();
QVariantList::const_iterator end = var.end();
for (; i != end; ++i)
{
const RuleCondition cond(i->toMap());
if (cond.isValid())
{
conditions.push_back(cond);
}
}
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" || method == "GET");
if (!(method == "POST" || method == "PUT" || method == "DELETE" || method == "BIND" || method == "GET"))
{
DBG_Printf(DBG_INFO, "actions method must be either GET, 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_prefix(nullptr),
m_suffix(nullptr),
m_op(OpUnknown),
m_num(0),
m_weekDays(127) // default all days enabled
{
}
/*! Constructs a RuleCondition from the data given in \p map.
The RuleCondition::isValid() method should be used to verify
the object was constructed sucessfully.
*/
RuleCondition::RuleCondition(const QVariantMap &map)
{
bool ok = false;
m_address = map["address"].toString();
m_operator = map["operator"].toString();
m_value = map["value"];
// cache id
if (m_address.startsWith(QLatin1String("/sensors")) ||
m_address.startsWith(QLatin1String("/groups")) ||
m_address.startsWith(QLatin1String("/lights"))) // /sensors/<id>/state/buttonevent, ...
{
QStringList addrList = m_address.split('/', QString::SkipEmptyParts);
if (addrList.size() > 1)
{
m_id = addrList[1];
}
}
if (m_address.startsWith(QLatin1String("/sensors")))
{
m_prefix = RSensors;
if (m_address.endsWith(QLatin1String("/illuminance")))
{ // convert old to new style
m_address.replace(QLatin1String("/illuminance"), QLatin1String("/lux"));
}
}
else if (m_address.startsWith(QLatin1String("/config")))
{
m_prefix = RConfig;
}
else if (m_address.startsWith(QLatin1String("/groups")))
{
m_prefix = RGroups;
}
else if (m_address.startsWith(QLatin1String("/lights")))
{
m_prefix = RLights;
}
ResourceItemDescriptor rid;
m_suffix = getResourceItemDescriptor(m_address, rid) ? rid.suffix
: RInvalidSuffix;
if (m_operator == QLatin1String("eq")) { m_op = OpEqual; }
else if (m_operator == QLatin1String("ne")) { m_op = OpNotEqual; }
else if (m_operator == QLatin1String("gt")) { m_op = OpGreaterThan; }
else if (m_operator == QLatin1String("lt")) { m_op = OpLowerThan; }
else if (m_operator == QLatin1String("dx")) { m_op = OpDx; }
else if (m_operator == QLatin1String("ddx")) { m_op = OpDdx; }
else if (m_operator == QLatin1String("in")) { m_op = OpIn; }
else if (m_operator == QLatin1String("not in")) { m_op = OpNotIn; }
else if (m_operator == QLatin1String("stable")) { m_op = OpStable; }
else { m_op = OpUnknown; }
// extract proper datatype
if (m_value.type() == QVariant::String)
{
const QString str = m_value.toString();
if (m_op == OpDdx || m_op == OpStable)
{
QTime t = QTime::fromString(str, "'PT'hh:mm:ss");
if (!t.isValid())
{
m_op = OpUnknown; // invalid
return;
}
m_time0 = t;
// cache duration in seconds
m_num = QTime(0,0,0).secsTo(t);
}
else if (m_op == OpIn || m_op == OpNotIn)
{
QStringList interval = str.split('/', QString::SkipEmptyParts);
if (interval.size() == 3)
{
const QRegExp rx("W([0-9]{1,3})");
const QString weekDays = interval.takeFirst();
if (rx.exactMatch(weekDays))
{
const uint w = rx.cap(1).toUInt(&ok);
if (!ok || w > 127)
{
return; // invalid
}
m_weekDays = static_cast<quint8>(w);
}
}
if (interval.size() != 2)
{
m_op = OpUnknown; // invalid
return;
}
QTime t0 = QTime::fromString(interval[0], "'T'hh:mm:ss");
QTime t1 = QTime::fromString(interval[1], "'T'hh:mm:ss");
if (t0.isValid() && t1.isValid())
{
m_time0 = t0;
m_time1 = t1;
} else { m_op = OpUnknown; } // mark invalid
}
else if (str == QLatin1String("true") ||
str == QLatin1String("false"))
{
m_value = m_value.toBool();
}
else if ((m_op == OpGreaterThan || m_op == OpLowerThan) && m_suffix == RStateLocaltime && str.endsWith(QLatin1String("/localtime")))
{
// TODO dynamically referring to other resources in conditions might be useful in general
// /config/localtime
if (str.endsWith(QLatin1String(RConfigLocalTime)))
{
m_valuePrefix = RConfig;
m_valueSuffix = RConfigLocalTime;
}
// /sensors/51/state/localtime
else if (str.startsWith(QLatin1String(RSensors)) && str.endsWith(QLatin1String(RStateLocaltime)))
{
const QStringList ls = str.split('/', QString::SkipEmptyParts); // cache resource id
// [ "sensors", "51", "state", "localtime" ]
if (ls.size() == 4)
{
m_valuePrefix = RSensors;
m_valueSuffix = RStateLocaltime;
m_valueId = ls[1];
}
else
{
m_op = OpUnknown; // invalid
}
}
else
{
m_op = OpUnknown; // invalid
}
}
else if (m_op == OpEqual || m_op == OpNotEqual || m_op == OpGreaterThan || m_op == OpLowerThan)
{
int num = str.toInt(&ok);
if (ok)
{
m_value = static_cast<double>(num);
} else { m_op = OpUnknown; } // mark invalid
}
}
if (m_value.type() == QVariant::Double ||
m_value.type() == QVariant::UInt ||
m_value.type() == QVariant::Int)
{
m_num = m_value.toInt(&ok);
if (!ok) { m_num = 0; m_op = OpUnknown; }
}
else if (m_value.type() == QVariant::Bool)
{
m_num = m_value.toBool() ? 1 : 0;
}
else if (m_value.type() == QVariant::Time)
{
}
}
/*! 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 == "ne") || (aOperator == "gt") || (aOperator == "lt") || (aOperator == "dx"));
if (!((aOperator == "eq") || (aOperator == "ne") || (aOperator == "gt") || (aOperator == "lt") || (aOperator == "dx")))
{
DBG_Printf(DBG_INFO, "actions operator must be either 'eq', 'ne', 'gt', 'lt' or 'dx'\n");
return;
}
m_operator = aOperator;
}
/*! Returns the condition address.
*/
const QString &RuleCondition::ooperator() const
{
return m_operator;
}
/*! Returns the condition value.
*/
const QVariant &RuleCondition::value() const
{
return m_value;
}
/*! 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 QVariant &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 operator as enum.
*/
RuleCondition::Operator RuleCondition::op() const
{
return m_op;
}
/*! Returns resource id of address.
*/
const QString &RuleCondition::id() const
{
return m_id;
}
/*! Returns resource id of address given in a value.
*/
const QString &RuleCondition::valueId() const
{
return m_valueId;
}
/*! Returns value as int (for numeric and bool types).
*/
int RuleCondition::numericValue() const
{
return m_num;
}
/*! Returns value as duration in seconds (for operators OpDdx, OpStable, OpIn and OpNotIn).
*/
int RuleCondition::seconds() const
{
return m_num;
}
/*! Returns start time (for operators OpIn and OpNotIn).
*/
const QTime &RuleCondition::time0() const
{
return m_time0;
}
/*! Returns end time (for operators OpIn and OpNotIn).
*/
const QTime &RuleCondition::time1() const
{
return m_time1;
}
/*! Returns true if the given weekday is enabled (for operators OpIn and OpNotIn).
The condition needs format of W[bbb]/T[hh]:[mm]:[ss]/T[hh]:[mm]:[ss].
If W[bbb] is not specified all days are enabled as of W127.
\param day - 1 Monday .. 7 Sunday
*/
bool RuleCondition::weekDayEnabled(const int day) const
{
// bbb = 0MTWTFSS – e.g. Tuesdays is 00100000 = 32
DBG_Assert(day >= 0 && day <= 7);
return (m_weekDays & (1 << (7 - day))) != 0;
}
/*! Returns the related Resource prefix like RSensors, RLights, etc.
*/
const char *RuleCondition::resource() const
{
return m_prefix;
}
/*! Returns the Resource suffix like RStateButtonevent.
*/
const char *RuleCondition::suffix() const
{
return m_suffix;
}
/*! Returns the related Resource prefix like RSensors, RLights, etc. of the value,
if value is pointing to another resource. Otherwise \p nullptr is returned.
*/
const char *RuleCondition::valueResource() const
{
return m_valuePrefix;
}
/*! Returns the Resource suffix like RStateButtonevent of the value,
if value is pointing to another resource. Otherwise \p nullptr is returned.
*/
const char *RuleCondition::valueSuffix() const
{
return m_valueSuffix;
}
/*! 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);
}