Skip to content

Commit

Permalink
Refactor InfluxDBv1+v2 functions (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slider0007 authored Jun 8, 2023
1 parent c2e4333 commit 4f59958
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 114 deletions.
60 changes: 30 additions & 30 deletions code/components/jomjol_flowcontroll/ClassFlowInfluxDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -72,6 +75,7 @@ ClassFlowInfluxDB::ClassFlowInfluxDB(std::vector<ClassFlow*>* lfc, ClassFlow *_p
bool ClassFlowInfluxDB::ReadParameter(FILE* pfile, string& aktparamgraph)
{
std::vector<string> splitted;
std::string _param;

aktparamgraph = trim(aktparamgraph);

Expand All @@ -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))
{
Expand Down Expand Up @@ -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;
Expand All @@ -149,8 +152,7 @@ bool ClassFlowInfluxDB::doFlow(string zwtime)
string zw = "";
string namenumber = "";

if (flowpostprocessing)
{
if (flowpostprocessing != NULL) {
std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();

for (int i = 0; i < (*NUMBERS).size(); ++i)
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_flowcontroll/ClassFlowInfluxDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
120 changes: 60 additions & 60 deletions code/components/jomjol_flowcontroll/ClassFlowInfluxDBv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@ static const char* TAG = "INFLUXDBV2";
void ClassFlowInfluxDBv2::SetInitialParameter(void)
{
PresetFlowStateHandler(true);
flowpostprocessing = NULL;
previousElement = NULL;
ListFlowControll = NULL;

uri = "";
database = "";
dborg = "";
dbtoken = "";
// dbfield = "";

OldValue = "";
flowpostprocessing = NULL;
previousElement = NULL;
ListFlowControll = NULL;

disabled = false;
InfluxDBenable = false;
}


ClassFlowInfluxDBv2::ClassFlowInfluxDBv2()
{
SetInitialParameter();
}


ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector<ClassFlow*>* lfc)
{
SetInitialParameter();
Expand All @@ -53,6 +57,7 @@ ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector<ClassFlow*>* lfc)
}
}


ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
{
SetInitialParameter();
Expand All @@ -73,6 +78,7 @@ ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector<ClassFlow*>* lfc, ClassFlow
bool ClassFlowInfluxDBv2::ReadParameter(FILE* pfile, string& aktparamgraph)
{
std::vector<string> splitted;
std::string _param;

aktparamgraph = trim(aktparamgraph);
printf("akt param: %s\n", aktparamgraph.c_str());
Expand All @@ -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))
{
Expand Down Expand Up @@ -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)
Expand All @@ -203,7 +159,7 @@ bool ClassFlowInfluxDBv2::doFlow(string zwtime)
string zw = "";
string namenumber = "";

if (flowpostprocessing)
if (flowpostprocessing != NULL)
{
std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();

Expand Down Expand Up @@ -236,13 +192,57 @@ 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;

return true;
}


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
Expand Down
Loading

0 comments on commit 4f59958

Please sign in to comment.