-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVPT.cpp
632 lines (512 loc) · 15.5 KB
/
VPT.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
/**
* @file VPT.cpp
* @Author Lukas Koszegy ([email protected])
* @date November, 2015
* @brief Definition functions for VPTSensor
*/
#include <iomanip>
#include <cassert>
#include <Poco/DateTimeFormatter.h>
#include <Poco/DateTimeParser.h>
#include <Poco/DigestStream.h>
#include <Poco/RegularExpression.h>
#include <Poco/SHA1Engine.h>
#include <Poco/Thread.h>
#include <Poco/Timezone.h>
#include "main.h"
#include "VPT.h"
#include "Parameters.h"
using namespace std;
using namespace Poco;
using Poco::AutoPtr;
using Poco::DigestEngine;
using Poco::DigestOutputStream;
using Poco::Logger;
using Poco::RegularExpression;
using Poco::Runnable;
using Poco::TimerCallback;
using Poco::SHA1Engine;
using Poco::Util::IniFileConfiguration;
#define VPT_EUID_PREFIX 0xa4000000
#define SEND_RETRY 3
#define HEX_NUMBER 16
#define SUB_MAC_START 6
#define SUB_MAC_LENGTH 6
enum ACTION {
PAIR = 0x01,
READ,
SET,
LAST, //do not use as type of action
};
const string URL_PASSWORD_KEY = "&__HOSTPWD=";
const unsigned PRESSURE_MODULE_ID = 62;
const unsigned CONVERT_VALUE = 1000;
const bool ONLY_PAIRED = true;
static float convertBarToHectopascals(float bar) {
return bar * CONVERT_VALUE;
}
static const string vpt_ini_file(void)
{
return string(MODULES_DIR) + string(MOD_VPT_SENSOR) + ".ini";
}
static string getTimeZoneWithAction(const int action)
{
stringstream stream;
string result;
int32_t zone_action = ((int32_t) Timezone::tzd() << 2) | action;
stream << hex << uppercase << setfill('0') << setw(2) << zone_action;
result = stream.str();
return result.substr(result.length()-2); //Get last byte
}
static string generateBeeeonTimestamp(long long int adapter_id,
uint32_t ip_address,
const int action)
{
stringstream stream;
string result;
uint32_t time = (uint32_t) Timestamp().epochTime();
//Format: 4B - IP, 7B - ADAPTER IP, 4B - TIME, 1B - ZONE AND ACTION
//Hex: 8 + 14 + 8 + 2 = 32 characters
stream << hex << uppercase << setfill('0') <<
setw(8) << ip_address <<
setw(14) << adapter_id <<
setw(8) << time;
result = stream.str() + getTimeZoneWithAction(action);
return result;
}
static string extractRandomNumber(const string & content) {
const static string prefix = "var randnum = ";
string ret = "";
RegularExpression regex(prefix + "([0-9]+)");
RegularExpression::Match position;
regex.match(content, 0, position);
if (position.length > 0)
ret = content.substr(position.offset+prefix.length(),position.length-prefix.length());
return ret;
}
static string generateSHAHash(const string & password, const string & rand_number) {
SHA1Engine sha1;
DigestOutputStream str(sha1);
str << rand_number + password;
str.flush();
const DigestEngine::Digest & digest = sha1.digest();
return DigestEngine::digestToHex(digest);
}
VPTSensor::VPTSensor(IOTMessage _msg, shared_ptr<Aggregator> _agg, long long int _adapter_id) :
adapter_id(_adapter_id),
agg(_agg),
log(Poco::Logger::get("Adaapp-VPT")),
msg(_msg)
{
AutoPtr<IniFileConfiguration> cfg;
try {
cfg = new IniFileConfiguration(vpt_ini_file());
}
catch (Poco::Exception& ex) {
log.fatal("Exception with config file reading:\n" + ex.displayText());
exit (EXIT_FAILURE);
}
const string passwordKey(string(MOD_VPT_SENSOR)+".password");
if (cfg->hasProperty(passwordKey)) {
try {
password = cfg->getString(passwordKey, "");
log.notice("VPT: Using a shared password");
}
catch (...) {
log.warning("VPT: Failed to load password (no password is used)");
password = "";
}
} else {
log.notice("VPT: No password is used");
password = "";
}
msg.state = "data";
msg.priority = MSG_PRIO_SENSOR;
msg.offset = 0;
tt = fillDeviceTable();
json.reset(new JSONDevices);
http_client.reset(new HTTPClient(cfg->getUInt("port", 80)));
listen_cmd_timer.setPeriodicInterval(0);
listen_cmd_timer.setStartInterval(0);
detect_devs_timer.setPeriodicInterval(0);
detect_devs_timer.setStartInterval(0);
}
void VPTSensor::fetchAndSendMessage(map<euid_t, VPTDevice>::iterator &device)
{
bool error = false;
if (device->second.paired && device->second.active <= VPTDevice::INACTIVE)
return;
try {
pair<bool, Command> response;
if (createMsg(device->second)) {
log.notice("VPT: Sending values to server");
response = agg->sendData(msg);
if (response.first) {
parseCmdFromServer(response.second);
}
updateTimestampOnVPT(device->second, ACTION::READ);
if (!device->second.paired)
device->second.active -= device->second.wake_up_time;
}
else {
log.error("Can't load new value of VPT sensor, send terminated");
error = true;
}
}
catch (Poco::Exception & exc) {
log.error(exc.displayText());
error = true;
}
if (error)
device->second.active -= device->second.wake_up_time;
}
unsigned int VPTSensor::nextWakeup(void)
{
unsigned int min_time = ~0;
ScopedLock<Poco::Mutex> guard(devs_lock);
for (auto it = map_devices.begin(); it != map_devices.end(); it++) {
VPTDevice &device = it->second;
if (device.time_left < min_time)
min_time = device.time_left;
}
if (map_devices.empty())
min_time = VPTDevice::DEFAULT_WAKEUP_TIME;
return min_time;
}
/**
* Main method of this thread.
* Periodically send "data" messages with current pressure sensor value.
*
* Naturally end when quit_global_flag is set to true,
* so the Adaapp is terminating.
*/
void VPTSensor::run(){
unsigned int next_wakeup = 0;
initPairedDevices();
devs_lock.lock();
for (auto device = map_devices.begin(); device != map_devices.end(); device++)
fetchAndSendMessage(device);
devs_lock.unlock();
vector<map<euid_t, VPTDevice>::iterator> delete_devices;
while(!quit_global_flag) {
delete_devices.clear();
devs_lock.lock();
for (auto it = map_devices.begin(); it != map_devices.end(); it++) {
VPTDevice &device = it->second;
if (device.time_left == 0) {
fetchAndSendMessage(it);
if (!device.paired && device.active <= VPTDevice::INACTIVE) {
delete_devices.push_back(it);
continue;
}
if (device.paired
&& device.active <= VPTDevice::INACTIVE
&& detect_devs_timer.getStartInterval() == 0)
{
detect_devs_timer.stop();
detect_devs_timer.setStartInterval(VPTDevice::FIFTEEN_MINUTES);
detect_devs_timer.start(TimerCallback<VPTSensor>(*this,
&VPTSensor::checkPairedDevices));
}
device.time_left = device.wake_up_time;
}
assert(next_wakeup <= device.time_left);
device.time_left -= next_wakeup;
}
devs_lock.unlock();
deleteDevices(delete_devices);
next_wakeup = nextWakeup();
for (unsigned int i = 0; i < next_wakeup; i++) {
if (quit_global_flag)
break;
sleep(1);
}
}
}
string VPTSensor::buildPasswordHash(std::string content) {
string hash = "";
string random_number = extractRandomNumber(content);
log.debug("VPT: Random number: " + random_number);
if (random_number.empty())
return hash;
try {
hash = generateSHAHash(password, random_number);
}
catch (...) {
log.warning("VPT: HASH: Generate hash failed");
}
log.information("VPT: HASH: " + hash);
return hash;
}
euid_t VPTSensor::parseDeviceId(string &content)
{
string id = json->getParameterValuesFromContent("id", content);
euid_t euid = stoull(id.substr(SUB_MAC_START, SUB_MAC_LENGTH), nullptr, HEX_NUMBER);
return VPT_EUID_PREFIX | (euid & EUID_MASK);
}
void VPTSensor::detectDevices(bool only_paired) {
log.notice("VPT: Start device discovery");
euid_t id;
vector<string> devices = http_client->discoverDevices();
VPTDevice device;
device.active = VPTDevice::FIVE_MINUTES;
ScopedLock<Mutex> guard(devs_lock);
for (vector<string>::iterator it = devices.begin(); it != devices.end(); it++) {
try {
string content = http_client->sendRequest(*it);
id = parseDeviceId(content);
if (map_devices.find(id) != map_devices.end())
device.paired = map_devices[id].paired;
else
device.paired = false;
if (only_paired && !device.paired)
continue;
device.name = json->getParameterValuesFromContent("device", content);
device.page_version = json->getParameterValuesFromContent("version", content);
device.specification = device.name +"_"+ device.page_version;
device.ip = *it;
device.sensor.version = 1;
device.sensor.euid = id;
device.sensor.pairs = 0;
device.sensor.values.clear();
log.information("VPT: Detected device " + device.name + " with ip " + device.ip + " and version of web page " + device.page_version);
map_devices[id] = device;
}
catch (...) {/* exceptions are cought in the caller */}
}
log.notice("VPT: Stop device discovery");
}
bool VPTSensor::isVPTSensor(euid_t euid) {
return map_devices.find(euid) != map_devices.end();
}
void VPTSensor::initPairedDevices()
{
Parameters parameter(*(agg.get()), msg, log);
CmdParam result = parameter.getAllPairedDevices();
VPTDevice device;
if (!result.status)
return;
device.paired = true;
try {
ScopedLock<Mutex> guard(devs_lock);
for (auto const& euid: result.getEuides(VPT_EUID_PREFIX)) {
map_devices[euid] = device;
log.information("Paired VPT with device euid: " + to_string(euid));
}
}
catch (Poco::Exception& exc) {
log.error("Initialization paired device failed with error: " + exc.displayText());
}
if (!map_devices.empty()) {
detectDevices(ONLY_PAIRED);
pairDevices();
}
}
void VPTSensor::updateDeviceWakeUp(euid_t euid, unsigned int time)
{
auto it = map_devices.find(euid);
if (it == map_devices.end()) {
log.warning("VPT: Setting wake up on unknown device " + to_string(euid));
return;
}
VPTDevice &dev = it->second;
if (time < VPTDevice::DEFAULT_WAKEUP_TIME)
time = VPTDevice::DEFAULT_WAKEUP_TIME;
dev.wake_up_time = time;
dev.time_left = dev.wake_up_time;
log.debug("VPT: Update resfresh time for device " + to_string(euid) + " on " + to_string(time) + " seconds");
}
void VPTSensor::processCmdSet(Command cmd)
{
auto it = map_devices.find(cmd.euid);
if (it == map_devices.end()) {
log.error("VPT: Setting actuator on unknown device " + to_string(cmd.euid));
return;
}
VPTDevice &dev = it->second;
pair<int, float> value = cmd.values.at(0);
log.information("VPT: " + dev.ip + ": Set actuator with ID:" + to_string(value.first) + " on " + to_string((int)value.second));
string url_value = json->generateRequestURL(dev.specification, value.first, value.second);
if (url_value.empty()) {
log.error("VPT: Setting actuator failed - device or actuator not found");
return;
}
if (url_value.empty()) {
log.error("VPT: Setting actuator failed - device or actuator not found");
return;
}
if (sendSetRequest(dev, url_value))
updateTimestampOnVPT(dev, ACTION::SET);
}
void VPTSensor::updateTimestampOnVPT(VPTDevice &dev, const int action) {
string request = "/values.json?BEEE0=";
if (action <= 0 || action >= ACTION::LAST) {
log.warning("VPT: Generate timestamp with bad action code");
return;
}
request += generateBeeeonTimestamp(adapter_id, http_client->findAdapterIP(dev.ip), action);
log.debug("VPT: Set BeeeOn timestamp");
sendSetRequest(dev, request);
}
bool VPTSensor::sendSetRequest(VPTDevice &dev, std::string url_value) {
bool result = false;
string request = url_value;
if (!dev.password_hash.empty())
request += URL_PASSWORD_KEY + dev.password_hash;
std::string content;
for(int i = 0; i < SEND_RETRY; i++) {
try {
content = http_client->sendRequest(dev.ip, request);
}
catch(Poco::Exception &exc) {
log.error("VPT: " + exc.displayText());
}
if (!json->isJSONFormat(content)) {
log.notice("VPT: Using password");
dev.password_hash = buildPasswordHash(content);
if (dev.password_hash.empty())
continue;
request = url_value + URL_PASSWORD_KEY + dev.password_hash;
}
else {
result = true;
break;
}
}
return result;
}
void VPTSensor::processCmdListen(void)
{
try {
detectDevices();
pairDevices();
listen_cmd_timer.stop();
listen_cmd_timer.setStartInterval(VPTDevice::THREE_MINUTES);
listen_cmd_timer.start(TimerCallback<VPTSensor>(*this,
&VPTSensor::checkPairedDevices));
}
catch (Poco::Exception & exc) {
log.error("VPT: " + exc.displayText());
}
}
void VPTSensor::parseCmdFromServer(Command cmd){
if (cmd.state == "update") {
updateDeviceWakeUp(cmd.euid, cmd.time);
return;
}
else if (cmd.state == "set") {
processCmdSet(cmd);
return;
}
else if (cmd.state == "listen") {
processCmdListen();
return;
}
log.error("Unexpected answer from server, received command: " + cmd.state);
}
bool VPTSensor::createMsg(VPTDevice &device) {
string website = http_client->sendRequest(device.ip);
try {
device.sensor.values = json->getSensors(website, device.specification);
}
catch (Poco::Exception & exc) {
log.error("VPT: " + exc.displayText());
return false;
}
convertPressure(device.sensor.values);
device.sensor.pairs = device.sensor.values.size();
device.sensor.device_id = json->getID(device.specification);
msg.device = device.sensor;
msg.time = time(NULL);
return true;
}
void VPTSensor::convertPressure(vector<Value> &values) {
for (auto itr = values.begin(); itr != values.end(); itr++) {
if (itr->mid == PRESSURE_MODULE_ID) {
itr->value = convertBarToHectopascals(itr->value);
break;
}
}
}
void VPTSensor::checkPairedDevices(Timer& timer)
{
bool find_devices_on_network = false;
VPTDevice device;
Parameters parameter(*(agg.get()), msg, log);
CmdParam result = parameter.getAllPairedDevices();
if (!result.status)
return;
setAllDevicesNotPaired();
log.debug("Checking paired devices");
device.paired = true;
try {
ScopedLock<Mutex> guard(devs_lock);
for (auto const& euid: result.getEuides(VPT_EUID_PREFIX)) {
map<euid_t, VPTDevice>::iterator it = map_devices.find(euid);
if (it != map_devices.end()) {
log.information("Device with euid " + to_string(euid)
+ " set as paired");
it->second.paired = true;
if (it->second.active <= VPTDevice::INACTIVE)
find_devices_on_network = true;
}
else {
log.information("Device with euid " + to_string(euid)
+ " was not detected on LAN in last 2 minutes");
find_devices_on_network = true;
map_devices[euid] = device;
}
}
}
catch (Poco::Exception& exc) {
log.error("Checking paired devices failed with error: " + exc.displayText());
}
if (find_devices_on_network) {
log.information("Trying detect paired devices, which can be detect in last "
+ to_string((timer.getStartInterval() / 1000) / 60) + " minutes");
detectDevices(ONLY_PAIRED);
pairDevices();
}
log.debug("Checking paired devices is ending");
timer.setStartInterval(0);
}
void VPTSensor::pairDevices(void) {
vector<map<euid_t, VPTDevice>::iterator> devices_del;
ScopedLockWithUnlock<Mutex> guard(devs_lock);
for(auto device = map_devices.begin(); device != map_devices.end(); device++) {
try {
json->loadDeviceConfiguration(device->second.name, device->second.page_version);
updateTimestampOnVPT(device->second, ACTION::PAIR);
}
catch (Poco::Exception &e) {
if (!device->second.paired)
devices_del.push_back(device);
log.error("VPT: " + e.displayText());
}
}
guard.unlock();
deleteDevices(devices_del);
}
void VPTSensor::deleteDevices(vector<euid_t> euides)
{
ScopedLock<Mutex> guard(devs_lock);
for (auto euid: euides) {
log.debug("Delete VPT with euid: " + to_string(euid));
map_devices.erase(euid);
}
}
void VPTSensor::setAllDevicesNotPaired()
{
log.debug("Setting all devices as not paired");
ScopedLock<Mutex> guard(devs_lock);
for (auto& it: map_devices)
it.second.paired = false;
}
void VPTSensor::deleteDevices(vector<map<euid_t, VPTDevice>::iterator> iterators)
{
ScopedLock<Mutex> guard(devs_lock);
for (const auto it: iterators) {
log.debug("Delete VPT with euid: " + to_string(it->first));
map_devices.erase(it);
}
}