1+ #include " influxdb.h"
2+ #include " configuration.h"
3+
4+ APB::InfluxDb &APB::InfluxDb::Instance = *new APB::InfluxDb{};
5+
6+ #ifdef USE_INFLUXDB
7+ #include " settings.h"
8+ #include " wifimanager.h"
9+ #include " powermonitor.h"
10+ #include " heater.h"
11+ #include " ambient/ambient.h"
12+ #include < StreamString.h>
13+ #include < ArduinoLog.h>
14+
15+
16+ #include < InfluxDbClient.h>
17+ #include < InfluxDbCloud.h>
18+ #define TZ_INFO " UTC1"
19+
20+ void setPointSource (Point &point) {
21+ point.addTag (" source" , APB::Settings::Instance.apConfiguration ().essid );
22+ }
23+
24+ InfluxDBClient influxDbClient (INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert);
25+ Point powerSensor (" power" );
26+ Point ambientSensor (" ambient" );
27+ Point heatersSensor (" heaters" );
28+
29+ void APB::InfluxDb::setup (Scheduler & scheduler) {
30+ setPointSource (powerSensor);
31+ setPointSource (ambientSensor);
32+ setPointSource (heatersSensor);
33+ WiFiManager::Instance.addOnConnectedListener (std::bind (&InfluxDb::onWiFiConnected, this ));
34+
35+ new Task (APB_INFLUXDB_TASK_SECONDS, TASK_FOREVER, std::bind (&InfluxDb::sendData, this ), &scheduler, true );
36+ }
37+
38+ void APB::InfluxDb::onWiFiConnected () {
39+ Log.infoln (" InfluxDB: onWiFiConnected" );
40+ timeSync (TZ_INFO, " pool.ntp.org" , " time.nis.gov" );
41+ connected = influxDbClient.validateConnection ();
42+ if (connected) {
43+ Log.infoln (" InfluxDB Connected" );
44+ } else {
45+ Log.errorln (" InfluxDB connection error: %s" , influxDbClient.getLastErrorMessage ().c_str ());
46+ }
47+ }
48+
49+
50+ void sendPoint (Point &point) {
51+ bool written = influxDbClient.writePoint (point);
52+ Log.traceln (" InfluxDB: Sending point <%s>: %d" , powerSensor.toLineProtocol ().c_str (), written);
53+ if (!written) {
54+ Log.errorln (" InfluxDB last error: %s" , influxDbClient.getLastErrorMessage ().c_str ());
55+ }
56+ }
57+
58+ void APB::InfluxDb::sendData () {
59+ if (!connected) {
60+ return ;
61+ }
62+ powerSensor.clearFields ();
63+ auto powerReading = PowerMonitor::Instance.status ();
64+ powerSensor.addField (" voltage" , powerReading.busVoltage );
65+ powerSensor.addField (" current" , powerReading.current );
66+ powerSensor.addField (" power" , powerReading.power );
67+ sendPoint (powerSensor);
68+
69+ #ifndef APB_AMBIENT_TEMPERATURE_SENSOR_NONE
70+ auto ambientReading = Ambient::Instance.reading ();
71+ if (ambientReading.has_value ()) {
72+ ambientSensor.addField (" temperature" , ambientReading->temperature );
73+ ambientSensor.addField (" humidity" , ambientReading->humidity );
74+ ambientSensor.addField (" dewpoint" , ambientReading->dewpoint ());
75+ sendPoint (ambientSensor);
76+ }
77+ #endif
78+ #if APB_HEATERS_SIZE > 0
79+ for (Heater &heater : Heaters::Instance) {
80+ heatersSensor.clearFields ();
81+ heatersSensor.addField (" index" , heater.index ());
82+ if (heater.temperature ().has_value ()) {
83+ heatersSensor.addField (" temperature" , heater.temperature ().value ());
84+ }
85+ if (heater.targetTemperature ().has_value ()) {
86+ heatersSensor.addField (" target_temperature" , heater.targetTemperature ().value ());
87+ }
88+ heatersSensor.addField (" duty" , heater.duty ());
89+ heatersSensor.addField (" mode" , heater.mode ());
90+ heatersSensor.addField (" active" , heater.active ());
91+ sendPoint (heatersSensor);
92+ }
93+ #endif
94+ }
95+ #else
96+ void APB::InfluxDb::setup (Scheduler & scheduler) {}
97+ void APB::InfluxDb::onWiFiConnected () {}
98+ void APB::InfluxDb::sendData () {}
99+ #endif
0 commit comments