-
Notifications
You must be signed in to change notification settings - Fork 92
/
InfluxDb.cpp
179 lines (161 loc) · 4.11 KB
/
InfluxDb.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
/**
ESP8266 InfluxDb: Influxdb.cpp
Purpose: Helps with sending measurements to an Influx database.
@author Tobias Schürg
*/
#include "InfluxDb.h"
#include "Arduino.h"
/**
* Construct an InfluxDb instance.
* @param host the InfluxDb host
* @param port the InfluxDb port
*/
Influxdb::Influxdb(String host, uint16_t port) {
_port = port;
_host = host;
}
/**
* Set the database to be used.
* @param db the Influx Database to be written to.
*/
void Influxdb::setDb(String db) {
_db = String(db);
begin();
}
/**
* Set the database to be used with authentication.
*/
void Influxdb::setDbAuth(String db, String user, String pass) {
_db = String(db);
_user = user;
_pass = pass;
begin();
}
/**
* Set the Bucket to be used v2.0 ONLY.
* @param bucket the InfluxDB Bucket which must already exist
*/
void Influxdb::setBucket(String bucket) {
_bucket = String(bucket);
begin();
}
/**
* Set the influxDB port.
* @param port v1.x uses 8086, v2 uses 9999
*/
void Influxdb::setPort(uint16_t port){
_port = port;
begin();
}
/**
* Set the Organization to be used v2.0 ONLY
* @param org the Name of the organization unit to use which must already exist
*/
void Influxdb::setOrg(String org){
_org = String(org);
begin();
}
/**
* Set the authorization token v2.0 ONLY
* @param token the Auth Token from InfluxDBv2 *required*
*/
void Influxdb::setToken(String token){
_token = String(token);
begin();
}
/**
* Set the version of InfluxDB to write to
* @param version accepts 1 for version 1.x or 2 for version 2.x
*/
void Influxdb::setVersion(uint16_t version){
_db_v = version;
begin();
}
#if defined(ESP8266)
/**
* Set servers finger print for HTTPS v2 Influx proto
* @param version accepts 1 for version 1.x or 2 for version 2.x
*/
void Influxdb::setFingerPrint(char *fingerPrint){
_fingerPrint = fingerPrint;
begin();
}
#endif
void Influxdb::begin() {
// TODO: recreate HttpClient on db change?
if(_db_v == 2){
#if defined(ESP8266)
if (_port == 443) {
if (_fingerPrint)
client.setFingerprint(_fingerPrint);
http.begin(client, _host, _port, "/api/v2/write?org=" + _org + "&bucket=" + _bucket, true);
}
else
#endif
{
http.begin(_host, _port, "/api/v2/write?org=" + _org + "&bucket=" + _bucket);
}
} else {
if (_user && _pass) {
http.begin(_host, _port, "/write?u=" + _user + "&p=" + _pass + "&db=" + _db);
} else {
http.begin(_host, _port, "/write?db=" + _db);
}
}
http.addHeader("Content-Type", "text/plain");
}
/**
* Prepare a measurement to be sent.
*/
void Influxdb::prepare(InfluxData data) { prepared.push_back(data); }
/**
* Write all prepared measurements into the db.
*/
boolean Influxdb::write() {
String data = "";
for (auto const& i : prepared) {
data = (data == "") ? (i.toString()) : (data + "\n" + i.toString());
}
prepared.clear();
return write(data);
}
/**
* Write a single measurement into the db.
*/
boolean Influxdb::write(InfluxData data) { return write(data.toString()); }
/**
* Send raw data to InfluxDb.
*
* @see
* https://github.com/esp8266/Arduino/blob/cc0bfa04d401810ed3f5d7d01be6e88b9011997f/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h#L44-L55
* for a list of error codes.
*/
boolean Influxdb::write(String data) {
if(_db_v == 2){
if(_token == NULL || _token.length() < 10){
Serial.println("#####\nInvalid Access Token\n#####");
return false;
}
Serial.print(" --> writing to host: " + _host + " Port: " + _port + " URL: /api/v2/write?org=" + _org + "&bucket=" + _bucket + ":\n");
Serial.println(data);
} else {
Serial.print(" --> writing to " + _db + ":\n");
Serial.println(data);
}
if(_db_v == 2)
http.addHeader("Authorization", "Token " + _token);
int httpResponseCode = http.POST(data);
Serial.print(" <-- Response: ");
Serial.print(httpResponseCode);
String response = http.getString();
Serial.println(" \"" + response + "\"");
boolean success;
if (httpResponseCode == 204) {
success = true;
} else {
Serial.println("#####\nPOST FAILED\n#####");
success = false;
}
http.end();
return success;
}