-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVPT.h
122 lines (106 loc) · 3.02 KB
/
VPT.h
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
/**
* @file VPT.h
* @Author Lukas Koszegy ([email protected])
* @date November, 2015
* @brief Class for devices from company Thermona, which use json outputs via HTTP
*/
#ifndef VPTSENSOR_H
#define VPTSENSOR_H
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <Poco/AutoPtr.h>
#include <Poco/Logger.h>
#include <Poco/Mutex.h>
#include <Poco/Net/IPAddress.h>
#include <Poco/Runnable.h>
#include <Poco/Timer.h>
#include <Poco/Util/IniFileConfiguration.h>
#include "device_table.h"
#include "HTTP.h"
#include "JSON.h"
#include "utils.h"
extern bool quit_global_flag;
typedef struct VPTDevice {
std::string name;
std::string ip;
std::string page_version;
std::string specification;
std::string password_hash;
Device sensor;
unsigned int wake_up_time;
unsigned int time_left;
int active;
bool paired;
// Enum for time interval
enum {
INACTIVE = 0,
ACTIVE = 1,
DEFAULT_WAKEUP_TIME = 15, // 15 seconds
THREE_MINUTES = 3 * 60 * 1000, //3 min in milliseconds
FIVE_MINUTES = 5 * 60, // 5 min in seconds
FIFTEEN_MINUTES = 15 * 60 * 1000, //15 min in milliseconds
};
VPTDevice()
{
active = INACTIVE;
paired = false;
wake_up_time = DEFAULT_WAKEUP_TIME;
time_left = DEFAULT_WAKEUP_TIME;
}
} VPTDevice;
class Aggregator;
class VPTSensor: public Poco::Runnable {
public:
VPTSensor(IOTMessage _msg, std::shared_ptr<Aggregator>agg, long long int _adapter_id);
virtual void run();
/**
* Detect devices on LAN. If only_paired is true, only known already paired
* devices are detected and the other devices are ignored.
*/
void detectDevices(bool only_paired = false);
bool isVPTSensor(euid_t sensor_id);
void parseCmdFromServer(Command cmd);
private:
long long int adapter_id;
std::shared_ptr<Aggregator> agg;
std::unique_ptr<HTTPClient> http_client;
std::unique_ptr<JSONDevices> json;
Poco::Logger& log;
Poco::Mutex devs_lock;
/*
* other threads must always call timer.stop() prior to setting this
* interval to avoid races.
*/
Poco::Timer listen_cmd_timer;
Poco::Timer detect_devs_timer;
IOTMessage msg;
std::map<euid_t, VPTDevice> map_devices;
std::string password;
TT_Table tt;
bool createMsg(VPTDevice &device);
std::string buildPasswordHash(std::string content);
void convertPressure(std::vector<Value> &values);
void checkPairedDevices(Poco::Timer& timer);
void deleteDevices(std::vector<euid_t> euides);
void deleteDevices(std::vector<std::map<euid_t, VPTDevice>::iterator> iterators);
unsigned int nextWakeup(void);
void fetchAndSendMessage(std::map<euid_t, VPTDevice>::iterator &device);
void initPairedDevices();
void pairDevices();
euid_t parseDeviceId(std::string &content);
void updateDeviceWakeUp(euid_t euid, unsigned int time);
void updateTimestampOnVPT(VPTDevice &dev, const int action);
bool sendSetRequest(VPTDevice &dev, std::string url_value);
void setAllDevicesNotPaired();
void processCmdSet(Command cmd);
void processCmdListen(void);
};
#endif