forked from mrWheel/DSMRloggerAPI
-
Notifications
You must be signed in to change notification settings - Fork 4
/
handleInfluxDB.ino
155 lines (141 loc) · 6.11 KB
/
handleInfluxDB.ino
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
/*
***************************************************************************
** Program : handleInfluxDB - part of DSMRlogger-Next
** Version : v2.3.0-rc5
**
** Copyright (c) 2020 Robert van den Breemen
**
** Description: Handles sending all data directly to an influxdb instance
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
* Created by Robert van den Breemen (26 june 2020)
*/
#ifdef USE_INFLUXDB
// InfluxDB server url. Don't use localhost, always server name or ip address.
// For InfluxDB 2 e.g. http://192.168.1.48:9999 (Use: InfluxDB UI -> Load Data -> Client Libraries),
// For InfluxDB 1 e.g. http://192.168.1.48:8086
#define INFLUXDB_URL "http://192.168.88.254:8086"
//#define INFLUXDB_URL "http://127.0.0.1:8086"
// InfluxDB 2 server or cloud API authentication token (Use: InfluxDB UI -> Load Data -> Tokens -> <select token>)
//#define INFLUXDB_TOKEN "toked-id"
// InfluxDB 2 organization id (Use: InfluxDB UI -> Settings -> Profile -> <name under tile> )
//#define INFLUXDB_ORG "org"
// InfluxDB 2 bucket name (Use: InfluxDB UI -> Load Data -> Buckets)
//#define INFLUXDB_BUCKET "bucket"
// InfluxDB v1 database name
#define INFLUXDB_DB_NAME "dsmr-api"
#include <InfluxDbClient.h>
// InfluxDB client instance
//InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN);
// InfluxDB client instance for InfluxDB 1
InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
// Set timezone string according to https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
// Examples:
// Pacific Time: "PST8PDT"
// Eastern: "EST5EDT"
// Japanesse: "JST-9"
// Central Europe: "CET-1CEST,M3.5.0,M10.5.0/3"
#define TZ_INFO "CET-1CEST,M3.5.0,M10.5.0/3"
#define WRITE_PRECISION WritePrecision::S
#define MAX_BATCH_SIZE 16
#define WRITE_BUFFER_SIZE 32
time_t thisEpoch;
void initInfluxDB()
{
if ( sizeof(settingInfluxDBhostname) < 8 ) return;
// Set InfluxDB 1 authentication params
// Only needed when query's are done?
//client.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME, INFLUXDB_USER, INFLUXDB_PASSWORD);
snprintf(cMsg, sizeof(cMsg), "http://%s:%d", settingInfluxDBhostname , settingInfluxDBport);
DebugTf("InfluxDB Connection Setup: [%s] - database: [%s]\r\n", cMsg , settingInfluxDBdatabasename);
client.setConnectionParamsV1(cMsg, settingInfluxDBdatabasename);
Debugln("InfluxDB client setConnectionsParamsV1 done!");
// Connect to influxdb server connection
if (client.validateConnection()) {
DebugT("Connected to InfluxDB: ");
Debugln(client.getServerUrl());
} else {
DebugT("InfluxDB connection failed: ");
Debugln(client.getLastErrorMessage());
}
//Enable messages batching and retry buffer
//deprecated writeoptions:
client.setWriteOptions(WRITE_PRECISION, MAX_BATCH_SIZE, WRITE_BUFFER_SIZE);
// client.setWriteOptions(WriteOptions().writePrecision(WRITE_PRECISION));
// client.setWriteOptions(WriteOptions().batchSize(MAX_BATCH_SIZE));
// client.setWriteOptions(WriteOptions().bufferSize(WRITE_BUFFER_SIZE));
//setup the HTTPoptions to reuse HTTP
//client.setHTTPOptions(HTTPOptions().connectionReuse(true));
}
struct writeInfluxDataPoints {
template<typename Item>
void apply(Item &i) {
if (i.present())
{
if (strlen(Item::unit()) != 0)
{
//when there is a unit, then it is a measurement
Point pointItem(Item::unit());
pointItem.setTime(thisEpoch);
pointItem.addTag("instance",Item::name);
pointItem.addField("value", i.val());
if (Verbose1) {
DebugT("Writing to influxdb:");
Debugln(pointItem.toLineProtocol());
}
if (!client.writePoint(pointItem)) {
DebugT("InfluxDB write failed: ");
Debugln(client.getLastErrorMessage());
}
}//writing to influxdb
}
}
};
void handleInfluxDB()
{
static uint32_t lastTelegram = 0;
if (!client.validateConnection()) {
DebugTln("InfluxDB connection problem!");
return; // only write if there is a valid connection to InfluxDB
}
if ((telegramCount - lastTelegram)> 0)
{
//New telegram received, let's forward that to influxDB
lastTelegram = telegramCount;
//Setup the timestamp for this telegram, so all points for this batch are the same.
// ThisEpoch needs to be the true epoch being the UTC epoch. As the clock is synced to NL timezone, it needs to be lowered by 7200 in summer, and 3600 in winter
thisEpoch = now()- (isDST ? 7200 : 3600);
uint32_t timeThis = millis();
DSMRdata.applyEach(writeInfluxDataPoints());
// Check whether buffer in not empty
if (!client.isBufferEmpty()) {
// Write all remaining points to db
client.flushBuffer();
}
DebugTf("Influxdb write took [%d] ms\r\n", (int)(millis()-timeThis));
}
}
#endif
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/