ClassFlowInfluxDB.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <sstream>
  2. #include "ClassFlowInfluxDB.h"
  3. #include "Helper.h"
  4. #include "connect_wlan.h"
  5. #include "time_sntp.h"
  6. #include "interface_influxdb.h"
  7. #include "ClassFlowPostProcessing.h"
  8. #include "esp_log.h"
  9. #include <time.h>
  10. static const char* TAG = "class_flow_influxDb";
  11. void ClassFlowInfluxDB::SetInitialParameter(void)
  12. {
  13. uri = "";
  14. database = "";
  15. measurement = "";
  16. OldValue = "";
  17. flowpostprocessing = NULL;
  18. user = "";
  19. password = "";
  20. previousElement = NULL;
  21. ListFlowControll = NULL;
  22. disabled = false;
  23. InfluxDBenable = false;
  24. }
  25. ClassFlowInfluxDB::ClassFlowInfluxDB()
  26. {
  27. SetInitialParameter();
  28. }
  29. ClassFlowInfluxDB::ClassFlowInfluxDB(std::vector<ClassFlow*>* lfc)
  30. {
  31. SetInitialParameter();
  32. ListFlowControll = lfc;
  33. for (int i = 0; i < ListFlowControll->size(); ++i)
  34. {
  35. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  36. {
  37. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  38. }
  39. }
  40. }
  41. ClassFlowInfluxDB::ClassFlowInfluxDB(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  42. {
  43. SetInitialParameter();
  44. previousElement = _prev;
  45. ListFlowControll = lfc;
  46. for (int i = 0; i < ListFlowControll->size(); ++i)
  47. {
  48. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  49. {
  50. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  51. }
  52. }
  53. }
  54. bool ClassFlowInfluxDB::ReadParameter(FILE* pfile, string& aktparamgraph)
  55. {
  56. std::vector<string> zerlegt;
  57. aktparamgraph = trim(aktparamgraph);
  58. if (aktparamgraph.size() == 0)
  59. if (!this->GetNextParagraph(pfile, aktparamgraph))
  60. return false;
  61. if (toUpper(aktparamgraph).compare("[INFLUXDB]") != 0)
  62. return false;
  63. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  64. {
  65. ESP_LOGD(TAG, "while loop reading line: %s", aktparamgraph.c_str());
  66. zerlegt = this->ZerlegeZeile(aktparamgraph);
  67. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  68. {
  69. this->user = zerlegt[1];
  70. }
  71. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  72. {
  73. this->password = zerlegt[1];
  74. }
  75. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  76. {
  77. this->uri = zerlegt[1];
  78. }
  79. if (((toUpper(zerlegt[0]) == "MEASUREMENT")) && (zerlegt.size() > 1))
  80. {
  81. this->measurement = zerlegt[1];
  82. }
  83. if (((toUpper(zerlegt[0]) == "DATABASE")) && (zerlegt.size() > 1))
  84. {
  85. this->database = zerlegt[1];
  86. }
  87. }
  88. if ((uri.length() > 0) && (database.length() > 0) && (measurement.length() > 0))
  89. {
  90. 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());
  91. InfluxDBInit(uri, database, measurement, user, password);
  92. InfluxDBenable = true;
  93. } else {
  94. ESP_LOGD(TAG, "InfluxDB init skipped as we are missing some parameters");
  95. }
  96. return true;
  97. }
  98. string ClassFlowInfluxDB::GetInfluxDBMeasurement()
  99. {
  100. return measurement;
  101. }
  102. bool ClassFlowInfluxDB::doFlow(string zwtime)
  103. {
  104. if (!InfluxDBenable)
  105. return true;
  106. std::string result;
  107. std::string resulterror = "";
  108. std::string resultraw = "";
  109. std::string resultrate = "";
  110. std::string resulttimestamp = "";
  111. string zw = "";
  112. string namenumber = "";
  113. if (flowpostprocessing)
  114. {
  115. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  116. for (int i = 0; i < (*NUMBERS).size(); ++i)
  117. {
  118. result = (*NUMBERS)[i]->ReturnValue;
  119. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  120. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  121. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  122. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  123. namenumber = (*NUMBERS)[i]->name;
  124. if (namenumber == "default")
  125. namenumber = "value";
  126. else
  127. namenumber = namenumber + "/value";
  128. if (result.length() > 0 && resulttimestamp.length() > 0)
  129. InfluxDBPublish(namenumber, result, resulttimestamp);
  130. }
  131. }
  132. OldValue = result;
  133. return true;
  134. }