-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.cpp
More file actions
350 lines (326 loc) · 8.1 KB
/
control.cpp
File metadata and controls
350 lines (326 loc) · 8.1 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
/*
* Fledge Control Delivery plugin
*
* Copyright (c) 2022 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Mark Riddoch
*/
#include <plugin_api.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string>
#include <logger.h>
#include <plugin_exception.h>
#include <iostream>
#include <config_category.h>
#include "rapidjson/document.h"
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/error/en.h>
#include <rapidjson/writer.h>
#include <rapidjson/pointer.h>
#include <sstream>
#include <unistd.h>
#include <control.h>
#include <string_utils.h>
#include <service_record.h>
using namespace std;
using namespace rapidjson;
using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
#define INSTRUMENT 1
#define INSTTHRESHOLD 5
/**
* Construct for ControlDelivery class
*
* @param category The configuration of the plugin
*/
ControlDelivery::ControlDelivery(ConfigCategory *category)
{
// Configuration set is protected by a lock
lock_guard<mutex> guard(m_configMutex);
// Create default values
m_enable = false;
// Set configuration
this->configure(category);
}
/**
* The destructor for the ControlDelivery class
*/
ControlDelivery::~ControlDelivery()
{
}
/**
* Send a notification This simply sets a configuration option
*
* @param notificationName The name of this notification
* @param triggerReason Why the notification is being sent
* @param message The message to send
*/
bool ControlDelivery::notify(const string& notificationName,
const string& triggerReason,
const string& customMessage)
{
Logger *log = Logger::getLogger();
log->info("Delivery plugin %s: "
"called with trigger reason '%s'",
PLUGIN_NAME,
triggerReason.c_str());
// Configuration fetch is protected by a mutex
m_configMutex.lock();
// Check for enable and for required clients
if (!m_enable)
{
// Release lock
m_configMutex.unlock();
log->debug("Plugin is not enabled, delivery of control message will not occur");
return false;
}
/*
* Parse the triggerReason docuemnt and determine of this is a
* trigger event or a clear event. Then set the value accordingly
*/
string value;
string reason;
Document doc;
doc.Parse(triggerReason.c_str());
if (!doc.HasParseError())
{
if (doc.HasMember("reason"))
{
if (doc["reason"].IsString())
{
reason = doc["reason"].GetString();
if (reason.compare("triggered") == 0)
{
value = m_triggerValue;
}
else
{
value = m_clearValue;
}
}
else
{
log->error("Unable to determine reason for delivery, control message will not be sent");
m_configMutex.unlock();
return false;
}
}
else
{
log->error("No reason for delivery is given, control message will not be sent");
m_configMutex.unlock();
return false;
}
if (doc.HasMember("data") && doc["data"].IsObject())
{
dataSubstitution(value, doc["data"].GetObject());
}
}
else
{
log->error("Unable to parse reason, control message will not be sent");
m_configMutex.unlock();
return false;
}
m_configMutex.unlock();
// Look at the JSON document in value and construct the payload to send to
// the dispatcher service
string path = "/dispatch/write";
string payload;
Document cdoc;
cdoc.Parse(value.c_str());
if (cdoc.HasParseError())
{
log->error("Failed to parse %s value: %s", reason.c_str(), value.c_str());
return false;
}
else
{
payload = "{ \"destination\" : \"";
if (cdoc.HasMember("service"))
{
payload += "service\", \"name\" : \"";
payload += cdoc["service"].GetString();
payload += "\", ";
}
else if (cdoc.HasMember("asset"))
{
payload += "asset\", \"name\" : \"";
payload += cdoc["asset"].GetString();
payload += "\", ";
}
else if (cdoc.HasMember("script"))
{
payload += "script\", \"name\" : \"";
payload += cdoc["script"].GetString();
payload += "\",";
}
else
{
payload += "broadcast\", ";
}
if (cdoc.HasMember("write"))
{
Value& w = cdoc["write"];
payload += "\"write\" : { ";
bool first = true;
for (Value::ConstMemberIterator itr = w.MemberBegin();
itr != w.MemberEnd(); ++itr)
{
if (first)
first = false;
else
payload += ",";
payload += "\"";
payload += itr->name.GetString();
payload += "\" : \"";
if (itr->value.IsString())
payload += itr->value.GetString();
payload += "\"";
}
payload += "} }";
}
else if (cdoc.HasMember("operation"))
{
Value& op = cdoc["operation"];
payload += "\"operation\" : { ";
payload += "\"";
payload += op["name"].GetString();
payload += "\" : { ";
bool first = true;
for (Value::ConstMemberIterator itr = op.MemberBegin();
itr != op.MemberEnd(); ++itr)
{
if (first)
first = false;
else
payload += ",";
payload += "\"";
payload += itr->name.GetString();
payload += "\" : \"";
if (itr->value.IsString())
payload += itr->value.GetString();
payload += "\"";
}
payload += "} }";
}
else
{
log->warn("No 'operation' or 'write' in notification control request for reason %s", reason.c_str());
log->warn("Request is: %s", value.c_str());
return false;
}
}
log->debug("Send to dispatcher %s, %s", path.c_str(), payload.c_str());
return m_service->sendToDispatcher(path, payload);
}
/**
* Reconfigure the delivery plugin
*
* @param newConfig The new configuration
*/
void ControlDelivery::reconfigure(const string& newConfig)
{
ConfigCategory category("new", newConfig);
// Configuration change is protected by a lock
lock_guard<mutex> guard(m_configMutex);
// Set the new configuration
this->configure(&category);
}
/**
* Configure the delivery plugin
*
* @param category The plugin configuration
*/
void ControlDelivery::configure(ConfigCategory *category)
{
// Get the configuration category we are changing
if (category->itemExists("service"))
{
m_southService = category->getValue("service");
}
// Get value to set on triggering
if (category->itemExists("triggerValue"))
{
m_triggerValue = category->getValue("triggerValue");
}
// Get value to set on clearing
if (category->itemExists("clearValue"))
{
m_clearValue = category->getValue("clearValue");
}
if (category->itemExists("enable"))
{
m_enable = category->getValue("enable").compare("true") == 0 ||
category->getValue("enable").compare("True") == 0;
}
}
/**
* Substitute varaibles with reading data
*/
void ControlDelivery::dataSubstitution(string& message, const Value& obj)
{
string rval("");
size_t p1 = 0;
size_t dstart;
while ((dstart = message.find_first_of("$", p1)) != string::npos)
{
rval.append(message.substr(p1, dstart - p1));
dstart++;
size_t dend = message.find_first_of ("$", dstart);
if (dend != string::npos)
{
string var = message.substr(dstart, dend - dstart);
size_t p2 = var.find_first_of(".");
string asset = var.substr(0, p2);
string datapoint = var.substr(p2 + 1);
Logger::getLogger()->debug("Looking for asset %s, data point %s",
asset.c_str(), datapoint.c_str());
if (obj.HasMember(asset.c_str()) && obj[asset.c_str()].IsObject())
{
const Value& dp = obj[asset.c_str()];
if (dp.HasMember(datapoint.c_str()))
{
const Value& dpv = dp[datapoint.c_str()];
if (dpv.IsString())
{
rval.append(dpv.GetString());
}
else if (dpv.IsDouble())
{
char buf[40];
snprintf(buf, sizeof(buf), "%f", dpv.GetDouble());
rval.append(buf);
}
else if (dpv.IsInt64())
{
char buf[40];
snprintf(buf, sizeof(buf), "%ld", dpv.GetInt64());
rval.append(buf);
}
}
else
{
Logger::getLogger()->error("There is no datapoint '%s' in the '%s asset received",
datapoint.c_str(), asset.c_str());
}
}
else
{
Logger::getLogger()->error("There is no asset '%s' in the data received", asset.c_str());
}
}
else
{
Logger::getLogger()->error("Unterminated macro substitution in '%s':%ld", message.c_str(), p1);
}
p1 = dend + 1;
}
rval.append(message.substr(p1));
Logger::getLogger()->debug("'%s'", message.c_str());
Logger::getLogger()->debug("became '%s'", rval.c_str());
message = rval;
}