ClassFlowInfluxDB.cpp 4.7 KB

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