forked from fledge-iot/fledge-filter-threshold
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreshold.cpp
More file actions
195 lines (180 loc) · 5.07 KB
/
threshold.cpp
File metadata and controls
195 lines (180 loc) · 5.07 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
/*
* Fledge "expression" filter plugin.
*
* Copyright (c) 2018 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Mark Riddoch
*/
#include <reading.h>
#include <reading_set.h>
#include <utility>
#include <logger.h>
#include <exprtk.hpp>
#include <threshold.h>
#include <math.h>
using namespace std;
using namespace rapidjson;
#define MAX_VARS 20 // Maximum number of variables supported in an expression
/**
* Construct a ThresholdFilter, call the base class constructor and handle the
* parsing of the configuration category the required expression
*/
ThresholdFilter::ThresholdFilter(const std::string& filterName,
ConfigCategory& filterConfig,
OUTPUT_HANDLE *outHandle,
OUTPUT_STREAM out) : m_triggered(false),
FledgeFilter(filterName, filterConfig,
outHandle, out)
{
handleConfig(filterConfig);
}
/**
* Destructor for the filter.
*/
ThresholdFilter::~ThresholdFilter()
{
}
/**
* Called with a set of readings, iterates over the readings applying
* the expression to create the new data point
*
* @param readings The readings to process
*/
void ThresholdFilter::ingest(vector<Reading *> *readings, vector<Reading *>& out)
{
exprtk::expression<double> expression;
exprtk::symbol_table<double> symbolTable;
exprtk::parser<double> parser;
double variables[MAX_VARS];
string variableNames[MAX_VARS];
Reading *reading;
int varCount = 0;
if (!readings->size())
{
return;
}
lock_guard<mutex> guard(m_configMutex);
/* Use the first reading to work out what the variables are */
reading = (*readings)[0];
vector<Datapoint *> datapoints = reading->getReadingData();
for (auto it = datapoints.begin(); it != datapoints.end(); it++)
{
DatapointValue& dpvalue = (*it)->getData();
if (dpvalue.getType() == DatapointValue::T_INTEGER ||
dpvalue.getType() == DatapointValue::T_FLOAT)
{
variableNames[varCount++] = (*it)->getName();
}
if (varCount == MAX_VARS)
{
Logger::getLogger()->error("Too many datapoints in reading");
break;
}
}
for (int i = 0; i < varCount; i++)
{
symbolTable.add_variable(variableNames[i], variables[i]);
}
symbolTable.add_constants();
expression.register_symbol_table(symbolTable);
if (!parser.compile(m_expression.c_str(), expression))
{
Logger::getLogger()->error("Failed to compile expression: %s", parser.error().c_str());
}
// Iterate over the readings
for (vector<Reading *>::const_iterator reading = readings->begin();
reading != readings->end();
++reading)
{
datapoints = (*reading)->getReadingData();
m_triggered = false;
for (auto it = datapoints.begin(); it != datapoints.end(); it++)
{
string name = (*it)->getName();
double value = 0.0;
DatapointValue& dpvalue = (*it)->getData();
if (dpvalue.getType() == DatapointValue::T_INTEGER)
{
value = dpvalue.toInt();
}
else if (dpvalue.getType() == DatapointValue::T_FLOAT)
{
value = dpvalue.toDouble();
}
else
{
continue; // Unsupported type
}
bool found = false;
for (int i = 0; i < varCount; i++)
{
if (variableNames[i].compare(name) == 0)
{
variables[i] = value;
found = true;
break;
}
}
if (found == false && varCount < MAX_VARS - 1)
{
// Not previously seen this data point, add it.
variableNames[varCount] = name;
variables[varCount] = value;
symbolTable.add_variable(variableNames[varCount], variables[varCount]);
varCount++;
// We have added a new variable so must re-parse the expression
expression.register_symbol_table(symbolTable);
if (!parser.compile(m_expression.c_str(), expression))
{
Logger::getLogger()->error("Failed to compile expression: %s", parser.error().c_str());
}
}
}
try {
double newValue = expression.value();
// If we yield a valid result add it as a data point
if (std::isnan(newValue) == false && isfinite(newValue))
{
m_triggered = newValue != 0.0;
}
} catch (exception &e) {
// Failed to evaluate expression, so just continue
Logger::getLogger()->error("Exception processing expression %s", m_expression.c_str());
}
if (m_triggered)
{
out.push_back(*reading);
}
else
{
// Need to delete source readings if they are not put into output reading set,
// since image datapoints need to be de-allocated from heap
delete(*reading);
}
}
readings->clear();
}
/**
* Reconfigure the filter. We must hold the mutex here to stop the ingest
* as we manipulate the m_scaleSet vector when recreating the scale sets
*
* @param conf The new configuration to apply
*/
void ThresholdFilter::reconfigure(const string& conf)
{
lock_guard<mutex> guard(m_configMutex);
setConfig(conf);
handleConfig(m_config);
}
/**
* Handle the configuration of the plugin.
*
*
* @param conf The configuration category for the filter.
*/
void ThresholdFilter::handleConfig(const ConfigCategory& config)
{
setExpression(config.getValue("expression"));
}