forked from OpenSprinkler/OpenSprinkler-Firmware
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathosinfluxdb.cpp
424 lines (371 loc) · 11.6 KB
/
osinfluxdb.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* OpenSprinkler Unified (AVR/RPI/BBB/LINUX/ESP8266) Firmware
* Copyright (C) 2024 by Stefan Schmaltz ([email protected])
*
* OpenSprinkler library header file
* Sep 2024 @ OpenSprinklerShop.de
*
* This file is part of the OpenSprinkler library
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "osinfluxdb.h"
#include "utils.h"
#include "defines.h"
#include "OpenSprinkler.h"
extern char tmp_buffer[TMP_BUFFER_SIZE*2];
extern OpenSprinkler os;
#define INFLUX_CONFIG_FILE "influx.json"
void OSInfluxDB::set_influx_config(int enabled, char *url, uint16_t port, char *org, char *bucket, char *token) {
ArduinoJson::JsonDocument doc;
doc["en"] = enabled;
doc["url"] = url;
doc["port]"] = port;
doc["org"] = org;
doc["bucket"] = bucket;
doc["token"] = token;
set_influx_config(doc);
}
void OSInfluxDB::set_influx_config(ArduinoJson::JsonDocument &doc) {
size_t size = ArduinoJson::serializeJson(doc, tmp_buffer);
remove_file(INFLUX_CONFIG_FILE);
file_write_block(INFLUX_CONFIG_FILE, tmp_buffer, 0, size);
if (client)
{
delete client;
client = NULL;
}
enabled = doc["en"];
initialized = true;
}
void OSInfluxDB::set_influx_config(const char *data) {
size_t size = strlen(data);
remove_file(INFLUX_CONFIG_FILE);
file_write_block(INFLUX_CONFIG_FILE, "{", 0, 1);
file_write_block(INFLUX_CONFIG_FILE, data, 1, size);
file_write_block(INFLUX_CONFIG_FILE, "}", size+1, 1);
if (client)
{
delete client;
client = NULL;
}
enabled = false;
initialized = false;
}
void OSInfluxDB::get_influx_config(char *json) {
json[0] = 0;
if (file_exists(INFLUX_CONFIG_FILE))
{
ulong size = file_read_block(INFLUX_CONFIG_FILE, json, 0, TMP_BUFFER_SIZE*2);
//DEBUG_PRINT(F("influx config size="));
//DEBUG_PRINTLN(size);
json[size] = 0;
//DEBUG_PRINT(F("influx config="));
//DEBUG_PRINTLN(tmp_buffer);
if (size > 10 && (json[size-2] != '"' || json[size-1] != '}')) {
strcpy(json, "{\"en\":0}");
set_influx_config(json);
}
}
if (json[0] != '{' || strchr(json, '}') != strrchr(json, '}')) {
strcpy(json, "{\"en\":0}");
set_influx_config(json);
}
}
void OSInfluxDB::get_influx_config(ArduinoJson::JsonDocument &doc) {
//DEBUG_PRINTLN("Load influx config");
get_influx_config(tmp_buffer);
ArduinoJson::DeserializationError error = ArduinoJson::deserializeJson(doc, tmp_buffer);
if (error || doc.isNull() || !doc.containsKey("en")) {
if (error) {
DEBUG_PRINT(F("influxdb: deserializeJson() failed: "));
DEBUG_PRINTLN(error.c_str());
}
doc["en"] = 0;
doc["url"] = "";
doc["port"] = 8086;
doc["org"] = "";
doc["bucket"] = "";
doc["token"] = "";
set_influx_config(doc);
}
enabled = doc["en"];
initialized = true;
}
void OSInfluxDB::init() {
ArduinoJson::JsonDocument doc;
get_influx_config(doc);
enabled = doc["en"];
initialized = true;
}
boolean OSInfluxDB::isEnabled() {
if (!initialized) {
init();
}
return enabled;
}
#if defined(ESP8266)
OSInfluxDB::~OSInfluxDB() {
if (client) delete client;
}
void OSInfluxDB::write_influx_data(Point &sensor_data) {
if (!initialized) init();
if (!enabled)
return;
if (!client) {
if (!file_exists(INFLUX_CONFIG_FILE))
return;
//Load influx config:
ArduinoJson::JsonDocument doc;
get_influx_config(doc);
if (doc["en"] == 0)
return;
//InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
String url = doc["url"];
int port = doc["port"];
if (port == 0)
port = 8086;
url = url + ":" + port;
client = new InfluxDBClient(url, doc["org"], doc["bucket"], doc["token"], InfluxDbCloud2CACert);
}
if (client) {
// Print what are we exactly writing
DEBUG_PRINT("Writing: ");
DEBUG_PRINTLN(sensor_data.toLineProtocol());
// Write point
if (!client->writePoint(sensor_data)) {
DEBUG_PRINT("influxdb write failed: ");
DEBUG_PRINTLN(client->getLastErrorMessage());
}
}
}
#else
OSInfluxDB::~OSInfluxDB() {
if (client) delete client;
}
influxdb_cpp::server_info *OSInfluxDB::get_client() {
if (!initialized) init();
if (!enabled)
return NULL;
if (!client) {
//Load influx config:
ArduinoJson::JsonDocument doc;
get_influx_config(doc);
if (doc["en"] == 0)
return NULL;
client = new influxdb_cpp::server_info(doc["url"], doc["port"], doc["bucket"], "", "", "ms", doc["token"]);
}
return client;
}
#endif
#if defined(ESP8266)
void OSInfluxDB::influxdb_send_state(const char *name, int state) {
char tmp[TMP_BUFFER_SIZE];
Point data("opensprinkler");
os.sopt_load(SOPT_DEVICE_NAME, tmp);
data.addTag("devicename", tmp);
data.addTag("name", name);
data.addField("state", state);
write_influx_data(data);
}
void OSInfluxDB::influxdb_send_station(const char *name, uint32_t station, int state) {
Point data("opensprinkler");
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
data.addTag("devicename", tmp);
data.addTag("name", name);
data.addField("station", station);
data.addField("state", state);
write_influx_data(data);
}
void OSInfluxDB::influxdb_send_program(const char *name, uint32_t nr, float level) {
Point data("opensprinkler");
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
data.addTag("devicename", tmp);
data.addTag("name", name);
data.addField("program", nr);
data.addField("level", level);
write_influx_data(data);
}
void OSInfluxDB::influxdb_send_flowsensor(const char *name, uint32_t count, float volume) {
Point data("opensprinkler");
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
data.addTag("devicename", tmp);
data.addTag("name", name);
data.addField("count", count);
data.addField("volume", volume);
write_influx_data(data);
}
void OSInfluxDB::influxdb_send_flowalert(const char *name, uint32_t station, int f1, int f2, int f3, int f4, int f5) {
Point data("opensprinkler");
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
data.addTag("devicename", tmp);
data.addTag("name", name);
data.addField("station", station);
data.addField("flowrate", (double)(f1)+(double)(f2)/100);
data.addField("duration", f3);
data.addField("alert_setpoint", (double)(f4)+(double)(f5)/100);
write_influx_data(data);
}
void OSInfluxDB::influxdb_send_warning(const char *name, uint32_t level, float value) {
Point data("opensprinkler");
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
data.addTag("devicename", tmp);
data.addTag("warning", name);
data.addField("level", (int)level);
data.addField("currentvalue", value);
write_influx_data(data);
}
#else
void OSInfluxDB::influxdb_send_state(const char *name, int state) {
influxdb_cpp::server_info * client = get_client();
if (!client)
return;
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
influxdb_cpp::builder()
.meas("opensprinkler")
.tag("devicename", tmp)
.tag("name", name)
.field("state", state)
.timestamp(millis())
.post_http(*client);
}
void OSInfluxDB::influxdb_send_station(const char *name, uint32_t station, int state) {
influxdb_cpp::server_info * client = get_client();
if (!client)
return;
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
influxdb_cpp::builder()
.meas("opensprinkler")
.tag("devicename", tmp)
.tag("name", name)
.field("station", (int)station)
.field("state", state)
.timestamp(millis())
.post_http(*client);
}
void OSInfluxDB::influxdb_send_program(const char *name, uint32_t nr, float level) {
influxdb_cpp::server_info * client = get_client();
if (!client)
return;
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
influxdb_cpp::builder()
.meas("opensprinkler")
.tag("devicename", tmp)
.tag("name", name)
.field("program", (int)nr)
.field("level", level)
.timestamp(millis())
.post_http(*client);
}
void OSInfluxDB::influxdb_send_flowsensor(const char *name, uint32_t count, float volume) {
influxdb_cpp::server_info * client = get_client();
if (!client)
return;
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
influxdb_cpp::builder()
.meas("opensprinkler")
.tag("devicename", tmp)
.tag("name", name)
.field("count", (int)count)
.field("volume", volume)
.timestamp(millis())
.post_http(*client);
}
void OSInfluxDB::influxdb_send_flowalert(const char *name, uint32_t station, int f1, int f2, int f3, int f4, int f5) {
influxdb_cpp::server_info * client = get_client();
if (!client)
return;
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
influxdb_cpp::builder()
.meas("opensprinkler")
.tag("devicename", tmp)
.tag("name", name)
.field("station", (int)(station))
.field("flowrate", (double)(f1)+(double)(f2)/100)
.field("duration", f3)
.field("alert_setpoint", (double)(f4)+(double)(f5)/100)
.timestamp(millis())
.post_http(*client);
}
void OSInfluxDB::influxdb_send_warning(const char *name, uint32_t level, float value) {
influxdb_cpp::server_info * client = get_client();
if (!client)
return;
char tmp[TMP_BUFFER_SIZE];
os.sopt_load(SOPT_DEVICE_NAME, tmp);
influxdb_cpp::builder()
.meas("opensprinkler")
.tag("devicename", tmp)
.tag("warning", name)
.field("level", (int)level)
.field("currentvalue", value)
.timestamp(millis())
.post_http(*client);
}
#endif
void OSInfluxDB::push_message(uint16_t type, uint32_t lval, float fval, const char* sval) {
if (!isEnabled())
return;
uint32_t volume;
switch(type) {
case NOTIFY_STATION_ON:
influxdb_send_station("station", lval, 1);
break;
case NOTIFY_FLOW_ALERT:{
//influxdb_send_flowalert("flowalert", lval, f1, f2, f3, f4, f5);
break;
}
case NOTIFY_STATION_OFF:
influxdb_send_station("station", lval, 0);
break;
case NOTIFY_PROGRAM_SCHED:
influxdb_send_program("program sched", lval, fval);
break;
case NOTIFY_SENSOR1:
influxdb_send_state("sensor1", (int)fval);
break;
case NOTIFY_SENSOR2:
influxdb_send_state("sensor2", (int)fval);
break;
case NOTIFY_RAINDELAY:
influxdb_send_state("raindelay", (int)fval);
break;
case NOTIFY_FLOWSENSOR:
volume = os.iopts[IOPT_PULSE_RATE_1];
volume = (volume<<8)+os.iopts[IOPT_PULSE_RATE_0];
volume = lval*volume;
influxdb_send_flowsensor("flowsensor", lval, (float)volume/100);
break;
case NOTIFY_WEATHER_UPDATE:
influxdb_send_state("waterlevel", (int)fval);
break;
case NOTIFY_REBOOT:
break;
case NOTIFY_MONITOR_LOW:
case NOTIFY_MONITOR_MID:
case NOTIFY_MONITOR_HIGH:
influxdb_send_flowsensor(sval, lval, fval);
break;
}
}