ClassFlowInfluxDB.cpp 4.4 KB

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