This repository has been archived by the owner on May 21, 2021. It is now read-only.
forked from shantanoo-desai/olimax-esp32-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUMG_Node.cpp
162 lines (134 loc) · 3.62 KB
/
UMG_Node.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
#include "UMG_Node.h"
#include "Arduino.h"
/**
* influxMeasurement Class Methods
*/
/* Constructor */
influxMeasurement::influxMeasurement(String meas)
{
measurement = meas;
}
void influxMeasurement::empty() {
_data = "";
_tag = "";
}
void influxMeasurement::addField(String key, float value)
{
_data = (_data == "") ? (" ") : (_data += ",");
_data += key + "=" + String(value);
}
void influxMeasurement::addTag(String key, String value)
{
_tag += "," + key + "=" + value;
}
void influxMeasurement::addTimeStamp(uint32_t ts)
{
if (ts > 1) { // check if timestamp is a valid one
_data += " " + String(ts);
}
}
void influxMeasurement::addTimeStamp_ms(uint64_t ts_ms)
{
// convert uint64t to String
if (ts_ms > 1) {
String _result = _uint64tToString(ts_ms);
_data += " " + _result;
}
}
String influxMeasurement::postString()
{
return measurement + _tag + _data;
}
String URLEncode(String msg)
{
const char *hex = "0123456789abcdef";
String encodedMsg = "";
uint16_t i;
for (i = 0; i < msg.length(); i++) {
if (('a' <= msg.charAt(i) && msg.charAt(i) <= 'z') ||
('A' <= msg.charAt(i) && msg.charAt(i) <= 'Z') ||
('0' <= msg.charAt(i) && msg.charAt(i) <= '9')) {
encodedMsg += msg.charAt(i);
} else {
encodedMsg += '%';
encodedMsg += hex[msg.charAt(i) >> 4];
encodedMsg += hex[msg.charAt(i) & 15];
}
}
return encodedMsg;
}
String influxMeasurement::_uint64tToString(uint64_t value)
{
String result = "";
uint8_t base = 10;
do {
char c = value % base;
value /= base;
if (c < 10)
c +='0';
else
c += 'A' - 10;
result = c + result;
} while (value);
return result;
}
influxMeasurement::~influxMeasurement()
{
}
/**
* InfluxDB Class Methods
*/
InfluxDB::InfluxDB()
{
}
InfluxDB::InfluxDB(const char* host, uint16_t port, const char* dbname)
{
_port = String(port);
_host = String(host);
_dbname = String(dbname);
}
HTTP_RESPONSE InfluxDB::httpWrite(influxMeasurement mes_data, String timestamp_precision)
{
return _write(mes_data.postString(), timestamp_precision);
}
HTTP_RESPONSE InfluxDB::httpResponse()
{
return _response;
}
size_t InfluxDB::udpWrite(IPAddress host_add, uint16_t udp_port, influxMeasurement measurement)
{
AsyncUDP udp;
// shoot and forget UDP approach
udp.connect(host_add, udp_port);
udp.print(measurement.postString().c_str());
udp.close();
}
HTTP_RESPONSE InfluxDB::_write(String mes_dat, String ts_precision)
{
HTTPClient http;
if (ts_precision == "") {
// send without precision
http.begin("http://" + _host + ":" + _port + "/write?db=" + _dbname);
}
else if (ts_precision == "s" || ts_precision == "ms" || ts_precision == "u" || ts_precision == "ns") {
// send with defined precision
http.begin("http://" + _host + ":" + _port + "/write?db=" + _dbname + "&precision=" + ts_precision);
}
else {
// send with default precision of nanoseconds
ts_precision = "ns";
http.begin("http://" + _host + ":" + _port + "/write?db=" + _dbname + "&precision=" + ts_precision);
}
http.addHeader("Content-Type", "text/plain");
int httpResponseCode = http.POST(mes_dat);
if (httpResponseCode == 204) {
_response = INFLUX_SUCCESS;
} else {
_response = INFLUX_ERROR;
}
http.end();
return _response;
}
InfluxDB::~InfluxDB()
{
}