ClassFlowInfluxDB.cpp 4.5 KB

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