Skip to content

Commit 659ffc9

Browse files
committed
Added prepare() and post() and applied Google formatting.
1 parent b5420b2 commit 659ffc9

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

InfluxData.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11

2-
class InfluxData
3-
{
4-
5-
public:
2+
class InfluxData {
3+
public:
64
InfluxData(String measurement) : _measurement(measurement) {}
75

8-
void addTag(String key, String value)
9-
{
10-
_tags += "," + key + "=" + value;
11-
}
12-
void addValue(String key, float value)
13-
{
6+
void addTag(String key, String value) { _tags += "," + key + "=" + value; }
7+
void addValue(String key, float value) {
148
_values = (_values == "") ? (" ") : (_values += ",");
159
_values += key + "=" + String(value);
1610
}
1711

18-
String toString()
19-
{
20-
return _measurement + _tags + _values;
21-
}
12+
String toString() const { return _measurement + _tags + _values; }
2213

23-
private:
14+
private:
2415
String _measurement;
2516
String _tags;
2617
String _values;
27-
2818
};

InfluxDb.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
#include "Arduino.h"
21
#include "InfluxDb.h"
2+
#include "Arduino.h"
33

44
/**
55
* Construct an InfluxDb instance.
66
* @param host the InfluxDb host
77
* @param port the InfluxDb port
88
*/
9-
Influxdb::Influxdb(String host, uint16_t port)
10-
{
9+
Influxdb::Influxdb(String host, uint16_t port) {
1110
_port = port;
1211
_host = host;
1312
}
1413

1514
/**
1615
* Set the database to be used.
1716
*/
18-
void Influxdb::setDb(String db)
19-
{
17+
void Influxdb::setDb(String db) {
2018
_db = String(db);
2119
// TODO: recreate client on db change
2220
// http = new HTTPClient();
@@ -26,19 +24,25 @@ void Influxdb::setDb(String db)
2624

2725
// TODO: set db with user & password
2826

27+
void Influxdb::prepare(InfluxData data) { prepared.push_back(data); }
28+
29+
boolean Influxdb::post() {
30+
String data = "";
31+
for (auto const& i : prepared) {
32+
data = (data == "") ? (i.toString()) : (data + "\n" + i.toString());
33+
}
34+
return post(data);
35+
}
36+
2937
/**
3038
* Send a single measurement to the InfluxDb.
3139
*/
32-
boolean Influxdb::post(InfluxData data)
33-
{
34-
return post(data.toString());
35-
}
40+
boolean Influxdb::post(InfluxData data) { return post(data.toString()); }
3641

3742
/**
3843
* Send raw data to InfluxDb.
3944
*/
40-
boolean Influxdb::post(String data)
41-
{
45+
boolean Influxdb::post(String data) {
4246
Serial.print(" -> writing to " + _db + ": ");
4347
Serial.println(data);
4448

@@ -50,13 +54,9 @@ boolean Influxdb::post(String data)
5054
Serial.println(" \"" + response + "\"");
5155

5256
boolean success;
53-
if (httpResponseCode == 204)
54-
{
55-
57+
if (httpResponseCode == 204) {
5658
success = true;
57-
}
58-
else
59-
{
59+
} else {
6060
Serial.println("#####\nPOST FAILED\n#####");
6161
success = false;
6262
}

InfluxDb.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
#include "Arduino.h"
33
#include <ESP8266HTTPClient.h>
4+
#include <list>
5+
46
#include "InfluxData.h"
57

68
class Influxdb
@@ -10,6 +12,9 @@ class Influxdb
1012

1113
void setDb(String db);
1214

15+
void prepare(InfluxData data);
16+
boolean post();
17+
1318
boolean post(InfluxData data);
1419
boolean post(String data);
1520

@@ -18,4 +23,5 @@ class Influxdb
1823
String _host;
1924
uint16_t _port;
2025
String _db;
26+
std::list<InfluxData> prepared;
2127
};

0 commit comments

Comments
 (0)