From 8878f286e9c5f646cb8fdb911d5b318fa6e8c472 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 16 Jan 2018 22:40:16 +0100 Subject: [PATCH] Single exit point from idbSend method --- code/espurna/config/prototypes.h | 6 ---- code/espurna/influxdb.ino | 59 +++++++++++++++++--------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index f98c3205..3ddaf53e 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -85,12 +85,6 @@ void debugSend_P(PGM_P format, ...); template void domoticzSend(const char * key, T value); template void domoticzSend(const char * key, T nvalue, const char * svalue); -// ----------------------------------------------------------------------------- -// InfluxDB -// ----------------------------------------------------------------------------- -template bool idbSend(const char * topic, T payload); -template bool idbSend(const char * topic, unsigned char id, T payload); - // ----------------------------------------------------------------------------- // Utils // ----------------------------------------------------------------------------- diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index e9431f8a..47140e02 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -36,7 +36,7 @@ void _idbConfigure() { // ----------------------------------------------------------------------------- -template bool idbSend(const char * topic, T payload) { +bool idbSend(const char * topic, const char * payload) { if (!_idb_enabled) return true; if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true; @@ -46,43 +46,46 @@ template bool idbSend(const char * topic, T payload) { h = mdnsResolve(h); #endif char * host = strdup(h.c_str()); + unsigned int port = getSetting("idbPort", INFLUXDB_PORT).toInt(); + DEBUG_MSG("[INFLUXDB] Sending to %s:%u\n", host, port); - int port = getSetting("idbPort", INFLUXDB_PORT).toInt(); + bool success = false; - DEBUG_MSG("[INFLUXDB] Sending to %s:%u\n", host, port); _idb_client.setTimeout(2); - if (!_idb_client.connect(host, port)) { + if (_idb_client.connect((const char *) host, port)) { + + char data[128]; + snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str()); + DEBUG_MSG("[INFLUXDB] Data: %s\n", data); + + char request[256]; + snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s", + getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(), + getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(), + host, port, strlen(data), data); + + if (_idb_client.printf(request) > 0) { + while (_idb_client.connected() && _idb_client.available() == 0) delay(1); + while (_idb_client.available()) _idb_client.read(); + if (_idb_client.connected()) _idb_client.stop(); + success = true; + } else { + DEBUG_MSG("[INFLUXDB] Sent failed\n"); + } + + _idb_client.stop(); + while (_idb_client.connected()) yield(); + + } else { DEBUG_MSG("[INFLUXDB] Connection failed\n"); - free(host); - return false; } - char data[128]; - snprintf(data, sizeof(data), "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str()); - DEBUG_MSG("[INFLUXDB] Data: %s\n", data); - - char request[256]; - snprintf(request, sizeof(request), "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s", - getSetting("idbDatabase", INFLUXDB_DATABASE).c_str(), - getSetting("idbUsername", INFLUXDB_USERNAME).c_str(), getSetting("idbPassword", INFLUXDB_PASSWORD).c_str(), - host, port, strlen(data), data); free(host); - - if (_idb_client.printf(request) > 0) { - while (_idb_client.connected() && _idb_client.available() == 0) delay(1); - while (_idb_client.available()) _idb_client.read(); - if (_idb_client.connected()) _idb_client.stop(); - return true; - } - - _idb_client.stop(); - DEBUG_MSG("[INFLUXDB] Sent failed\n"); - while (_idb_client.connected()) delay(0); - return false; + return success; } -template bool idbSend(const char * topic, unsigned char id, T payload) { +bool idbSend(const char * topic, unsigned char id, const char * payload) { char measurement[64]; snprintf(measurement, sizeof(measurement), "%s,id=%d", topic, id); return idbSend(topic, payload);