forked from fledge-iot/fledge-rule-simple-expression
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
490 lines (426 loc) · 10.5 KB
/
plugin.cpp
File metadata and controls
490 lines (426 loc) · 10.5 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
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
/**
* Fledge SimpleExpression notification rule plugin
*
* Copyright (c) 2019 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Amandeep Singh Arora, Massimiliano Pinto
*/
#include <plugin_api.h>
#include <logger.h>
#include <plugin_exception.h>
#include <iosfwd>
#include <config_category.h>
#include <rapidjson/writer.h>
#include <builtin_rule.h>
#include "version.h"
#include "simple_expression.h"
#define RULE_NAME "Simple-Expression"
#define RULE_DESCRIPTION "Generate a notification based on the evaluation of a user provided expression"
/**
* Plugin configuration
*
* Example:
{
"asset": {
"description": "The asset name for which notifications will be generated.",
"name": "modbus"
},
"expression": {
"description": "The expression to evaluate",
"name": "Expression",
"type": "string",
"value": "humidity > 50"
}
}
* Expression is composed of datapoint values within given asset name.
* And if the value of boolean expression toggles, then the notification is sent.
*
* NOTE:
* Datapoint names and values are dynamically added when "plugin_eval" is called
*/
static const char * defaultConfiguration = QUOTE(
{
"plugin" : {
"description" : RULE_DESCRIPTION,
"type" : "string",
"default" : RULE_NAME,
"readonly" : "true"
},
"asset" : {
"description" : "The asset name for which notifications will be generated.",
"type" : "string",
"default" : "",
"displayName" : "Asset name",
"order" : "1"
},
"expression" : {
"description" : "Expression to apply.",
"name" : "Expression",
"type" : "string",
"default": "",
"displayName" : "Expression to apply",
"order" : "2"
}
});
using namespace std;
/**
* The C plugin interface
*/
extern "C" {
/**
* The C API rule information structure
*/
static PLUGIN_INFORMATION ruleInfo = {
RULE_NAME, // Name
VERSION, // Version
0, // Flags
PLUGIN_TYPE_NOTIFICATION_RULE, // Type
"1.0.0", // Interface version
defaultConfiguration // Configuration
};
/**
* Return the information about this plugin
*/
PLUGIN_INFORMATION *plugin_info()
{
return &ruleInfo;
}
/**
* Initialise rule objects based in configuration
*
* @param config The rule configuration category data.
* @return The rule handle.
*/
PLUGIN_HANDLE plugin_init(const ConfigCategory& config)
{
SimpleExpression* handle = new SimpleExpression();
bool rv = handle->configure(config);
if(rv == false)
{
delete handle;
Logger::getLogger()->info("plugin_init failed");
handle = NULL;
}
return (PLUGIN_HANDLE)handle;
}
/**
* Free rule resources
*/
void plugin_shutdown(PLUGIN_HANDLE handle)
{
SimpleExpression* rule = (SimpleExpression *)handle;
// Delete plugin handle
delete rule;
}
/**
* Return triggers JSON document
*
* @return JSON string
*/
string plugin_triggers(PLUGIN_HANDLE handle)
{
string ret;
SimpleExpression* rule = (SimpleExpression *)handle;
// Configuration fetch is protected by a lock
rule->lockConfig();
if (!rule->hasTriggers())
{
ret = "{\"triggers\" : []}";
rule->unlockConfig();
return ret;
}
ret = "{\"triggers\" : [ ";
std::map<std::string, RuleTrigger *> triggers = rule->getTriggers();
for (auto it = triggers.begin();
it != triggers.end();
++it)
{
ret += "{ \"asset\" : \"" + (*it).first + "\"";
ret += " }";
if (std::next(it, 1) != triggers.end())
{
ret += ", ";
}
}
ret += " ] }";
// Release lock
rule->unlockConfig();
Logger::getLogger()->debug("plugin_triggers(): ret=%s", ret.c_str());
return ret;
}
/**
* Evaluate notification data received
*
* Note: all assets must trigger in order to return TRUE
*
* @param assetValues JSON string document
* with notification data.
* @return True if the rule was triggered,
* false otherwise.
*/
bool plugin_eval(PLUGIN_HANDLE handle,
const string& assetValues)
{
Logger::getLogger()->debug("plugin_eval(): assetValues=%s", assetValues.c_str());
Document doc;
doc.Parse(assetValues.c_str());
if (doc.HasParseError())
{
return false;
}
bool eval = false;
SimpleExpression* rule = (SimpleExpression *)handle;
map<std::string, RuleTrigger *>& triggers = rule->getTriggers();
// Iterate throgh all configured assets
// If we have multiple asset the evaluation result is
// TRUE only if all assets checks returned true
for (auto & t : triggers)
{
string assetName = t.first;
string assetTimestamp = "timestamp_" + assetName;
if (!doc.HasMember(assetName.c_str()))
{
eval = false;
}
else
{
// Get all datapoints for assetName
const Value& assetValue = doc[assetName.c_str()];
// Set evaluation
eval = rule->evalAsset(assetValue);
// Add evalution timestamp
if (doc.HasMember(assetTimestamp.c_str()))
{
const Value& assetTime = doc[assetTimestamp.c_str()];
double timestamp = assetTime.GetDouble();
rule->setEvalTimestamp(timestamp);
}
}
}
// Set final state: true is all calls to evalAsset() returned true
rule->setState(eval);
return eval;
}
/**
* Return rule trigger reason: trigger or clear the notification.
*
* @return A JSON string
*/
string plugin_reason(PLUGIN_HANDLE handle)
{
SimpleExpression* rule = (SimpleExpression *)handle;
// Get state, assets and timestamp
BuiltinRule::TriggerInfo info;
rule->getFullState(info);
string ret = "{ \"reason\": \"";
ret += info.getState() == BuiltinRule::StateTriggered ? "triggered" : "cleared";
ret += "\"";
ret += ", \"asset\": " + info.getAssets();
if (rule->getEvalTimestamp())
{
ret += string(", \"timestamp\": \"") + info.getUTCTimestamp() + string("\"");
}
ret += " }";
Logger::getLogger()->debug("plugin_reason(): ret=%s", ret.c_str());
return ret;
}
/**
* Call the reconfigure method in the plugin
*
* Not implemented yet
*
* @param newConfig The new configuration for the plugin
*/
void plugin_reconfigure(PLUGIN_HANDLE handle,
const string& newConfig)
{
SimpleExpression* rule = (SimpleExpression *)handle;
ConfigCategory config("newCfg", newConfig);
bool rv = rule->configure(config);
if(rv == false)
Logger::getLogger()->info("plugin_reconfigure failed");
}
// End of extern "C"
};
/**
* Evaluate datapoints values for the given asset name
*
* @param assetValue JSON object with datapoints
*
* @return True if evalution succeded,
* false otherwise.
*/
bool SimpleExpression::evalAsset(const Value& assetValue)
{
bool foundDatapoints = false;
bool assetEval = false;
for (auto &m : assetValue.GetObject())
{
foundDatapoints = true;
double value;
if (m.value.IsDouble())
{
value = (double) m.value.GetDouble();
}
else if (m.value.IsNumber())
{
value = (double) m.value.GetInt();
}
else
{
value = 0.0;
}
if (m.value.IsDouble() ||
m.value.IsNumber())
{
// Add variable
this->getEvaluator()->addVariable(m.name.GetString(), value);
}
}
if (!foundDatapoints)
{
Logger::getLogger()->info("Couldn't find any valid datapoint in plugin_eval input data");
return false;
}
typedef exprtk::parser_error::type error_t;
// Get expression from config
string expression = this->getTrigger();
// Update SymbolTable of Evaluator
this->getEvaluator()->registerSymbolTable();
// Parse and comoile expression with variables
if (!this->getEvaluator()->parserCompile(expression))
{
Logger::getLogger()->error("Failed to compile expression: Error: %s\tExpression: %s",
this->getEvaluator()->getError().c_str(),
expression.c_str());
return false;
}
// Evaluate the expression
double evaluation = this->getEvaluator()->evaluate();
Logger::getLogger()->debug("SimpleExpression::Evaluator::evaluate(): m_expression.value()=%lf",
evaluation);
// Checks
if (std::isnan(evaluation) || !isfinite(evaluation))
{
Logger::getLogger()->error("SimpleExpression::evalAsset(): unable to evaluate expression");
}
// Set result
assetEval = (evaluation == 1.0);
Logger::getLogger()->debug("m_triggerExpression->evaluate() returned assetEval=%s",
assetEval ? "true" : "false");
// Return evaluation for current asset
return assetEval;
}
/**
* SimpleExpression rule constructor
*
* Call parent class BuiltinRule constructor
* passing a plugin handle
*/
SimpleExpression::SimpleExpression() : BuiltinRule()
{
m_triggerExpression = new Evaluator();
}
/**
* SimpleExpression destructor
*/
SimpleExpression::~SimpleExpression()
{
delete m_triggerExpression;
}
/**
* Configure the rule plugin
*
* @param config The configuration object to process
*/
bool SimpleExpression::configure(const ConfigCategory& config)
{
string assetName = config.getValue("asset");
string expression = config.getValue("expression");
if (assetName.empty() ||
expression.empty())
{
Logger::getLogger()->warn("Empty values for 'asset' or 'expression'");
// Return true, so it can be configured later
return true;
}
this->lockConfig();
if (m_triggerExpression)
{
delete m_triggerExpression;
m_triggerExpression = new Evaluator();
}
this->setTrigger(expression);
if (this->hasTriggers())
{
this->removeTriggers();
}
this->addTrigger(assetName, NULL);
// Release lock
this->unlockConfig();
return true;
}
/**
* Constructor for the evaluator class. This holds the expressions and
* variable bindings used to execute the triggers.
*/
SimpleExpression::Evaluator::Evaluator() : m_varCount(0)
{
bool rv = m_symbolTable.add_constants();
if (rv == false)
{
Logger::getLogger()->error("m_symbolTable.add_constants() failed");
throw new exception();
}
m_expression.register_symbol_table(m_symbolTable);
}
/**
* Add a variable and its value to Evaluator symbolTable
* If variable is already present just update the value
*
* We can add up to MAX_EXPRESSION_VARIABLES variables
*/
void SimpleExpression::Evaluator::addVariable(const std::string& dapointName,
double value)
{
if (!m_varCount)
{
m_variableNames[0] = dapointName;
m_variables[0] = value;
m_symbolTable.add_variable(m_variableNames[0],
m_variables[0]);
m_varCount++;
}
else
{
bool found = false;
for (int i = 0; i < m_varCount; i++)
{
if (m_variableNames[i].compare(dapointName) == 0)
{
found = true;
m_variables[i] = value;
break;
}
}
if (!found)
{
if (m_varCount < MAX_EXPRESSION_VARIABLES)
{
m_variableNames[m_varCount] = dapointName;
m_variables[m_varCount] = value;
m_symbolTable.add_variable(m_variableNames[m_varCount],
m_variables[m_varCount]);
m_varCount++;
}
else
{
Logger::getLogger()->warn("Already set %d variables, can not add the new one '%s'",
MAX_EXPRESSION_VARIABLES,
dapointName.c_str());
}
}
}
}