From 4f599584701f32b8d5186eb7aab67b8fba6833b7 Mon Sep 17 00:00:00 2001 From: Slider0007 <115730895+Slider0007@users.noreply.github.com> Date: Thu, 8 Jun 2023 20:56:08 +0200 Subject: [PATCH] Refactor InfluxDBv1+v2 functions (#30) --- .../jomjol_flowcontroll/ClassFlowInfluxDB.cpp | 60 ++++----- .../jomjol_flowcontroll/ClassFlowInfluxDB.h | 2 +- .../ClassFlowInfluxDBv2.cpp | 120 +++++++++--------- .../jomjol_influxdb/interface_influxdb.cpp | 52 ++++---- 4 files changed, 120 insertions(+), 114 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp b/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp index dc743c49d..17c9b906b 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp @@ -20,15 +20,18 @@ static const char* TAG = "INFLUXDB"; void ClassFlowInfluxDB::SetInitialParameter(void) { PresetFlowStateHandler(true); + flowpostprocessing = NULL; + previousElement = NULL; + ListFlowControll = NULL; + uri = ""; database = ""; + measurement = ""; + user = ""; + password = ""; OldValue = ""; - flowpostprocessing = NULL; - user = ""; - password = ""; - previousElement = NULL; - ListFlowControll = NULL; + disabled = false; InfluxDBenable = false; } @@ -72,6 +75,7 @@ ClassFlowInfluxDB::ClassFlowInfluxDB(std::vector* lfc, ClassFlow *_p bool ClassFlowInfluxDB::ReadParameter(FILE* pfile, string& aktparamgraph) { std::vector splitted; + std::string _param; aktparamgraph = trim(aktparamgraph); @@ -86,7 +90,7 @@ bool ClassFlowInfluxDB::ReadParameter(FILE* pfile, string& aktparamgraph) { ESP_LOGD(TAG, "while loop reading line: %s", aktparamgraph.c_str()); splitted = ZerlegeZeile(aktparamgraph); - std::string _param = GetParameterName(splitted[0]); + _param = GetParameterName(splitted[0]); if ((toUpper(_param) == "URI") && (splitted.size() > 1)) { @@ -119,16 +123,15 @@ bool ClassFlowInfluxDB::ReadParameter(FILE* pfile, string& aktparamgraph) } } - if ((uri.length() > 0) && (database.length() > 0)) - { -// ESP_LOGD(TAG, "Init InfluxDB with uri: %s, measurement: %s, user: %s, password: %s", uri.c_str(), measurement.c_str(), user.c_str(), password.c_str()); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init InfluxDB with uri: " + uri + ", user: " + user + ", password: " + password); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init with URI: " + uri + ", Database: " + database + ", User: " + user + ", Password: " + password); + + if ((uri.length() > 0) && (uri != "undefined") && (database.length() > 0) && (database != "undefined")) { InfluxDBInit(uri, database, user, password); InfluxDBenable = true; } else { - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init skipped - missing parameters"); - //return false; // TODO: Init should fail or continue flow? + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init failed, missing or wrong parameter"); + return false; } return true; @@ -149,8 +152,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime) string zw = ""; string namenumber = ""; - if (flowpostprocessing) - { + if (flowpostprocessing != NULL) { std::vector* NUMBERS = flowpostprocessing->GetNumbers(); for (int i = 0; i < (*NUMBERS).size(); ++i) @@ -179,6 +181,10 @@ bool ClassFlowInfluxDB::doFlow(string zwtime) InfluxDBPublish(measurement, namenumber, result, resulttimestamp); } } + else { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read post-processing data"); + return false; + } OldValue = result; @@ -190,21 +196,18 @@ void ClassFlowInfluxDB::handleMeasurement(string _decsep, string _value) { string _digit, _decpos; int _pospunkt = _decsep.find_first_of("."); -// ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt); + if (_pospunkt > -1) _digit = _decsep.substr(0, _pospunkt); else _digit = "default"; + for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j) { - if (_digit == "default") // Set to default first (if nothing else is set) - { - flowpostprocessing->NUMBERS[j]->MeasurementV1 = _value; - } - if (flowpostprocessing->NUMBERS[j]->name == _digit) - { + if (_digit == "default" || flowpostprocessing->NUMBERS[j]->name == _digit) flowpostprocessing->NUMBERS[j]->MeasurementV1 = _value; - } + + //ESP_LOGI(TAG, "handleMeasurement: Name: %s, Pospunkt: %d, value: %s", _digit.c_str(), _pospunkt, _value); } } @@ -213,21 +216,18 @@ void ClassFlowInfluxDB::handleFieldname(string _decsep, string _value) { string _digit, _decpos; int _pospunkt = _decsep.find_first_of("."); -// ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt); + if (_pospunkt > -1) _digit = _decsep.substr(0, _pospunkt); else _digit = "default"; + for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j) { - if (_digit == "default") // Set to default first (if nothing else is set) - { + if (_digit == "default" || flowpostprocessing->NUMBERS[j]->name == _digit) flowpostprocessing->NUMBERS[j]->FieldV1 = _value; - } - if (flowpostprocessing->NUMBERS[j]->name == _digit) - { - flowpostprocessing->NUMBERS[j]->FieldV1 = _value; - } + + //ESP_LOGI(TAG, "handleFieldname: Name: %s, Pospunkt: %d, value: %s", _digit.c_str(), _pospunkt, _value); } } diff --git a/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.h b/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.h index fadafe4ac..ae8937567 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.h +++ b/code/components/jomjol_flowcontroll/ClassFlowInfluxDB.h @@ -16,8 +16,8 @@ class ClassFlowInfluxDB : public ClassFlow protected: ClassFlowPostProcessing* flowpostprocessing; std::string uri, database, measurement; - std::string OldValue; std::string user, password; + std::string OldValue; bool InfluxDBenable; void SetInitialParameter(void); diff --git a/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp b/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp index 9e6c86e52..cd4936c89 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp @@ -20,6 +20,10 @@ static const char* TAG = "INFLUXDBV2"; void ClassFlowInfluxDBv2::SetInitialParameter(void) { PresetFlowStateHandler(true); + flowpostprocessing = NULL; + previousElement = NULL; + ListFlowControll = NULL; + uri = ""; database = ""; dborg = ""; @@ -27,18 +31,18 @@ void ClassFlowInfluxDBv2::SetInitialParameter(void) // dbfield = ""; OldValue = ""; - flowpostprocessing = NULL; - previousElement = NULL; - ListFlowControll = NULL; + disabled = false; InfluxDBenable = false; } + ClassFlowInfluxDBv2::ClassFlowInfluxDBv2() { SetInitialParameter(); } + ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector* lfc) { SetInitialParameter(); @@ -53,6 +57,7 @@ ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector* lfc) } } + ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector* lfc, ClassFlow *_prev) { SetInitialParameter(); @@ -73,6 +78,7 @@ ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector* lfc, ClassFlow bool ClassFlowInfluxDBv2::ReadParameter(FILE* pfile, string& aktparamgraph) { std::vector splitted; + std::string _param; aktparamgraph = trim(aktparamgraph); printf("akt param: %s\n", aktparamgraph.c_str()); @@ -88,7 +94,7 @@ bool ClassFlowInfluxDBv2::ReadParameter(FILE* pfile, string& aktparamgraph) { // ESP_LOGD(TAG, "while loop reading line: %s", aktparamgraph.c_str()); splitted = ZerlegeZeile(aktparamgraph); - std::string _param = GetParameterName(splitted[0]); + _param = GetParameterName(splitted[0]); if ((toUpper(_param) == "URI") && (splitted.size() > 1)) { @@ -121,73 +127,23 @@ bool ClassFlowInfluxDBv2::ReadParameter(FILE* pfile, string& aktparamgraph) } } - /*printf("uri: %s\n", uri.c_str()); - printf("org: %s\n", dborg.c_str()); - printf("token: %s\n", dbtoken.c_str()); - */ + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init with URI: " + uri + ", Database: " + database + ", Org: " + dborg + ", Token: *****"); - if ((uri.length() > 0) && (database.length() > 0) && (dbtoken.length() > 0) && (dborg.length() > 0)) + if ((uri.length() > 0 && (uri != "undefined")) && (database.length() > 0) && (database != "undefined") && + (dborg.length() > 0) && (dborg != "undefined") && (dbtoken.length() > 0) && (dbtoken != "undefined")) { - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init InfluxDB with uri: " + uri + ", org: " + dborg + ", token: *****"); -// printf("vor V2 Init\n"); InfluxDB_V2_Init(uri, database, dborg, dbtoken); -// printf("nach V2 Init\n"); InfluxDBenable = true; } else { - LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init skipped - missing parameters"); - //return false; // TODO: Init should fail or continue flow? + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Init failed, missing or wrong parameter"); + return false; } return true; } -void ClassFlowInfluxDBv2::handleFieldname(string _decsep, string _value) -{ - string _digit, _decpos; - int _pospunkt = _decsep.find_first_of("."); -// ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt); - if (_pospunkt > -1) - _digit = _decsep.substr(0, _pospunkt); - else - _digit = "default"; - for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j) - { - if (_digit == "default") // Set to default first (if nothing else is set) - { - flowpostprocessing->NUMBERS[j]->FieldV2 = _value; - } - if (flowpostprocessing->NUMBERS[j]->name == _digit) - { - flowpostprocessing->NUMBERS[j]->FieldV2 = _value; - } - } -} - -void ClassFlowInfluxDBv2::handleMeasurement(string _decsep, string _value) -{ - string _digit, _decpos; - int _pospunkt = _decsep.find_first_of("."); -// ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt); - if (_pospunkt > -1) - _digit = _decsep.substr(0, _pospunkt); - else - _digit = "default"; - for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j) - { - if (_digit == "default") // Set to default first (if nothing else is set) - { - flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value; - } - if (flowpostprocessing->NUMBERS[j]->name == _digit) - { - flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value; - } - } -} - - bool ClassFlowInfluxDBv2::doFlow(string zwtime) { if (!InfluxDBenable) @@ -203,7 +159,7 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime) string zw = ""; string namenumber = ""; - if (flowpostprocessing) + if (flowpostprocessing != NULL) { std::vector* NUMBERS = flowpostprocessing->GetNumbers(); @@ -236,6 +192,10 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime) // InfluxDB_V2_Publish(namenumber, result, resulttimestamp); } } + else { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read post-processing data"); + return false; + } OldValue = result; @@ -243,6 +203,46 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime) } +void ClassFlowInfluxDBv2::handleMeasurement(string _decsep, string _value) +{ + string _digit, _decpos; + int _pospunkt = _decsep.find_first_of("."); + + if (_pospunkt > -1) + _digit = _decsep.substr(0, _pospunkt); + else + _digit = "default"; + + for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j) + { + if (_digit == "default" || flowpostprocessing->NUMBERS[j]->name == _digit) + flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value; + + //ESP_LOGI(TAG, "handleMeasurement: Name: %s, Pospunkt: %d, value: %s", _digit.c_str(), _pospunkt, _value); + } +} + + +void ClassFlowInfluxDBv2::handleFieldname(string _decsep, string _value) +{ + string _digit, _decpos; + int _pospunkt = _decsep.find_first_of("."); + + if (_pospunkt > -1) + _digit = _decsep.substr(0, _pospunkt); + else + _digit = "default"; + + for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j) + { + if (_digit == "default" || flowpostprocessing->NUMBERS[j]->name == _digit) + flowpostprocessing->NUMBERS[j]->FieldV2 = _value; + + //ESP_LOGI(TAG, "handleFieldname: Name: %s, Pospunkt: %d, value: %s", _digit.c_str(), _pospunkt, _value); + } +} + + ClassFlowInfluxDBv2::~ClassFlowInfluxDBv2() { // nothing to do diff --git a/code/components/jomjol_influxdb/interface_influxdb.cpp b/code/components/jomjol_influxdb/interface_influxdb.cpp index bcc26efe6..379372ec5 100644 --- a/code/components/jomjol_influxdb/interface_influxdb.cpp +++ b/code/components/jomjol_influxdb/interface_influxdb.cpp @@ -9,7 +9,7 @@ #include "../../include/defines.h" -static const char *TAG = "INFLUXDB"; +static const char *TAG = "INFLUXDB_IF"; std::string _influxDBURI; std::string _influxDBDatabase; @@ -23,6 +23,7 @@ std::string _influxDB_V2_Org; static esp_err_t http_event_handler(esp_http_client_event_t *evt); + void InfluxDB_V2_Init(std::string _uri, std::string _database, std::string _org, std::string _token) { _influxDB_V2_URI = _uri; @@ -31,6 +32,7 @@ void InfluxDB_V2_Init(std::string _uri, std::string _database, std::string _org, _influxDB_V2_Token = _token; } + void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp) { //char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; @@ -74,53 +76,53 @@ void InfluxDB_V2_Publish(std::string _measurement, std::string _key, std::string payload.shrink_to_fit(); - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload); + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "InfluxDBV2: Payload: " + payload); std::string apiURI = _influxDB_V2_URI + "/api/v2/write?org=" + _influxDB_V2_Org + "&bucket=" + _influxDB_V2_Database; apiURI.shrink_to_fit(); http_config.url = apiURI.c_str(); ESP_LOGI(TAG, "http_config: %s", http_config.url); // Add mark on log to see when it restarted - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI); + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "InfluxDBV2: API URI: " + apiURI); esp_http_client_handle_t http_client = esp_http_client_init(&http_config); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP client initialized"); esp_http_client_set_header(http_client, "Content-Type", "text/plain"); std::string _zw = "Token " + _influxDB_V2_Token; // LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Tokenheader: %s\n", _zw.c_str()); esp_http_client_set_header(http_client, "Authorization", _zw.c_str()); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header setting done"); ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length())); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Payload post completed"); esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client)); if( err == ESP_OK ) { - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request completed"); int status_code = esp_http_client_get_status_code(http_client); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code)); - } else { - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP client status code: " + std::to_string(status_code)); + } + else { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP request failed"); } esp_http_client_cleanup(http_client); free_psram_heap(std::string(TAG) + "->response_buffer", response_buffer); } - static esp_err_t http_event_handler(esp_http_client_event_t *evt) { switch(evt->event_id) { case HTTP_EVENT_ERROR: - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered"); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP client error encountered"); break; case HTTP_EVENT_ON_CONNECTED: LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client connected"); - ESP_LOGI(TAG, "HTTP Client Connected"); + //ESP_LOGI(TAG, "HTTP Client Connected"); break; case HTTP_EVENT_HEADERS_SENT: LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers"); @@ -144,6 +146,7 @@ static esp_err_t http_event_handler(esp_http_client_event_t *evt) return ESP_OK; } + void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, std::string _timestamp) { //char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; @@ -193,7 +196,7 @@ void InfluxDBPublish(std::string _measurement, std::string _key, std::string _co payload.shrink_to_fit(); - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending line to influxdb:" + payload); + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "InfluxDBV1: Sending payload:" + payload); // use the default retention policy of the database @@ -203,32 +206,33 @@ void InfluxDBPublish(std::string _measurement, std::string _key, std::string _co apiURI.shrink_to_fit(); http_config.url = apiURI.c_str(); - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "API URI: " + apiURI); + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "InfluxDBV1: API URI: " + apiURI); esp_http_client_handle_t http_client = esp_http_client_init(&http_config); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "client is initialized"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP client initialized"); esp_http_client_set_header(http_client, "Content-Type", "text/plain"); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "header is set"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header setting done"); ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, payload.c_str(), payload.length())); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "post payload is set"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Payload post completed"); esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client)); if( err == ESP_OK ) { - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed"); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request performed"); int status_code = esp_http_client_get_status_code(http_client); - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code" + std::to_string(status_code)); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP client status code" + std::to_string(status_code)); } else { - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request failed"); + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP request failed"); } esp_http_client_cleanup(http_client); free_psram_heap(std::string(TAG) + "->response_buffer", response_buffer); } -void InfluxDBInit(std::string _uri, std::string _database, std::string _user, std::string _password){ +void InfluxDBInit(std::string _uri, std::string _database, std::string _user, std::string _password) +{ _influxDBURI = _uri; _influxDBDatabase = _database; _influxDBUser = _user; @@ -236,7 +240,9 @@ void InfluxDBInit(std::string _uri, std::string _database, std::string _user, st } -void InfluxDBdestroy() { +void InfluxDBdestroy() +{ + } #endif //ENABLE_INFLUXDB