-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathml2rule.cpp
321 lines (263 loc) · 7.54 KB
/
ml2rule.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
#include "ml2classes.h"
#include <SDConfigFile.h>
extern EventManager outputEM;
extern OutputList outputList;
extern InputList inputList;
uint32_t parseTime(const char* v) {
String s = v;
s.trim();
if (!s.length())
return 0;
s.toUpperCase();
uint32_t r = 0;
byte i = 0;
do {
char c = s[i];
if (!c) {
break;
} else if (c >= '0' && c <= '9') {
++i;
} else {
if (c == 'D') {
r += strtoul(s.substring(0, i).c_str(), NULL, 10) * 24 * 60 * 60 * 1000;
} else if (c == 'H') {
r += strtoul(s.substring(0, i).c_str(), NULL, 10) * 60 * 60 * 1000;
} else if (c == 'M') {
r += strtoul(s.substring(0, i).c_str(), NULL, 10) * 60 * 1000;
} else if (c == 'S') {
r += strtoul(s.substring(0, i).c_str(), NULL, 10) * 1000;
}
s.remove(0, i + 1);
i = 0;
}
} while (true);
return r + strtoul(s.c_str(), NULL, 10);
}
int eval_token(char *expr)
{
String s = expr;
s.trim();
if (!s.length())
return 0;
s.toUpperCase();
char c = s[0];
if ((c == 'R') || (c == 'V'))
{
bool needValue = (c == 'V');
s.remove(0, 1);
c = s[0];
int i = 0;
while (c && ExpressionEvaluator::istoken_char(c))
c = s[++i];
s.remove(i);
ML2Output *output = outputList.find(s.c_str());
if (output)
return needValue ? output->value() : (output->on() ? 1 : 0);
}
else if (c == 'B')
{
s.remove(0, 1);
ButtonState::State state;
c = s[0];
switch (c) {
case 'U' :
state = ButtonState::Up;
s.remove(0, 1);
c = s[0];
break;
case 'H' :
state = ButtonState::HoldState;
s.remove(0, 1);
c = s[0];
break;
default:
state = ButtonState::Down;
}
int i = 0;
while (c && ExpressionEvaluator::istoken_char(c))
c = s[++i];
s.remove(i);
ML2Input *input = inputList.find(s.c_str());
if (input)
return input->state() == state ? 1 : 0;
}
else
{
return s.toInt();
}
return 0;
}
GenericTokenEvaluator<int(char*)> tokenEvaluator(&eval_token);
ExpressionEvaluator evaluator(&tokenEvaluator);
bool evalRuleExpr(const String &expr)
{
int r = evaluator.eval(expr.c_str());
if (r == EVAL_FAILURE)
{
Serialprint("Error evaluating expression: %s", expr.c_str());
return false;
}
return r != 0;
}
ML2Rule::ML2Rule(const String &id)
: ID(id)
, final(false)
{
for (int i = 0; i < ButtonEvent::EventsCount; i++)
{
eventActions[i].action = OutputAction::Unassigned;
eventActions[i].param = 0;
eventActions[i].timeout = 0;
eventActions[i].condition = "";
}
}
ML2Rule::~ML2Rule()
{
clearOutputs();
}
bool ML2Rule::addInput(const char *id)
{
ML2Input *input = inputList.find(id);
if (!input)
return false;
return inputlist.addInput(input);
}
bool ML2Rule::hasOutput(ML2Output *output)
{
for (OutputList::iterator itr = outputlist.begin(); itr != outputlist.end(); ++itr)
{
if ((*itr) == output)
return true;
}
return false;
}
bool ML2Rule::addOutput(const char *id)
{
ML2Output *output = outputList.find(id);
if (!output)
return false;
return outputlist.addOutput(output);
}
bool ML2Rule::removeOutput(const char *id)
{
ML2Output *output = outputList.find(id);
if (!output)
return false;
for (OutputList::iterator itr = outputlist.begin(); itr != outputlist.end(); ++itr)
{
if ((*itr) == output)
{
outputlist.erase(itr);
return true;
}
}
return false;
}
bool ML2Rule::hasOutput(const char *id)
{
ML2Output *output = outputList.find(id);
if (!output)
return false;
return hasOutput(output);
}
int ML2Rule::outputCount()
{
return outputlist.size();
}
void ML2Rule::clearOutputs()
{
outputlist.clear();
}
void ML2Rule::setAction(ButtonEvent::Type event, OutputAction::Action action, int param, uint32_t timeout)
{
eventActions[event].action = action;
eventActions[event].param = param;
eventActions[event].timeout = timeout;
}
void ML2Rule::unsetAction(ButtonEvent::Type event)
{
eventActions[event].action = OutputAction::Unassigned;
}
void ML2Rule::setCondition(ButtonEvent::Type event, const char *condition)
{
eventActions[event].condition = condition;
}
bool ML2Rule::processButtonEvent(int event, ML2Input *input)
{
OutputAction::Action action = eventActions[event].action;
if (action == OutputAction::Unassigned)
return false;
if (eventActions[event].condition.length() && !evalRuleExpr(eventActions[event].condition))
return false;
for (OutputList::iterator itr = outputlist.begin(); itr != outputlist.end(); ++itr)
outputEM.queueEvent(action, new OutputEventParam((*itr), eventActions[event].param, eventActions[event].timeout));
return true;
}
ML2Rule *ML2Rule::fromFile(const String &path) {
const uint8_t CONFIG_LINE_LENGTH = 127;
SDConfigFile cfg;
String fullPath = String(RULES_PATH) + path;
if (!cfg.begin(fullPath.c_str(), CONFIG_LINE_LENGTH)) {
Serialprint("Failed to open rule file: %s", path.c_str());
return 0;
}
ML2Rule *rule = new ML2Rule(path);
ButtonEvent::Type currEvent = ButtonEvent::EventsCount;
// Read each setting from the file.
while (cfg.readNextSetting()) {
if (cfg.nameIs("final")) {
rule->final = cfg.getBooleanValue();
} else if (cfg.nameIs("input")) {
String inputId = cfg.getValue();
inputId.toUpperCase();
if (!rule->addInput(inputId.c_str()))
Serialprint("Could not add input %s to rule %s\r\n", inputId.c_str(), path.c_str());
} else if (cfg.nameIs("output")) {
String outputId = cfg.getValue();
outputId.toUpperCase();
if (!rule->addOutput(outputId.c_str()))
Serialprint("Could not add output %s to rule %s\r\n", outputId.c_str(), path.c_str());
} else if (cfg.nameIs("event")) {
String pu = cfg.getValue();
if (pu == "change")
currEvent = ButtonEvent::StateChanged;
else if (pu == "press")
currEvent = ButtonEvent::Pressed;
else if (pu == "release")
currEvent = ButtonEvent::Released;
else if (pu == "repeat")
currEvent = ButtonEvent::Repeat;
else if (pu == "hold")
currEvent = ButtonEvent::Hold;
else if (pu == "lclick")
currEvent = ButtonEvent::LongClick;
else if (pu == "click")
currEvent = ButtonEvent::Click;
else if (pu == "dclick")
currEvent = ButtonEvent::DoubleClick;
} else if (cfg.nameIs("action") && (currEvent != ButtonEvent::EventsCount)) {
String pu = cfg.getValue();
if (pu == "no")
rule->eventAction(currEvent).action = OutputAction::NoAction;
else if (pu == "on")
rule->eventAction(currEvent).action = OutputAction::On;
else if (pu == "off")
rule->eventAction(currEvent).action = OutputAction::Off;
else if (pu == "toggle")
rule->eventAction(currEvent).action = OutputAction::Toggle;
else if (pu == "value")
rule->eventAction(currEvent).action = OutputAction::Value;
else if (pu == "incvalue")
rule->eventAction(currEvent).action = OutputAction::IncValue;
} else if (cfg.nameIs("param") && (currEvent != ButtonEvent::EventsCount)) {
rule->eventAction(currEvent).param = cfg.getIntValue();
} else if (cfg.nameIs("timeout") && (currEvent != ButtonEvent::EventsCount)) {
rule->eventAction(currEvent).timeout = parseTime(cfg.getValue());
} else if (cfg.nameIs("condition") && (currEvent != ButtonEvent::EventsCount)) {
rule->eventAction(currEvent).condition = cfg.getValue();
}
}
// clean up
cfg.end();
return rule;
}