ClassFlowInfluxDBv2.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #ifdef ENABLE_INFLUXDB
  2. #include <sstream>
  3. #include "ClassFlowInfluxDBv2.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 = "INFLUXDBV2";
  14. void ClassFlowInfluxDBv2::SetInitialParameter(void)
  15. {
  16. uri = "";
  17. database = "";
  18. measurement = "";
  19. dborg = "";
  20. dbtoken = "";
  21. // dbfield = "";
  22. OldValue = "";
  23. flowpostprocessing = NULL;
  24. previousElement = NULL;
  25. ListFlowControll = NULL;
  26. disabled = false;
  27. InfluxDBenable = false;
  28. }
  29. ClassFlowInfluxDBv2::ClassFlowInfluxDBv2()
  30. {
  31. SetInitialParameter();
  32. }
  33. ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector<ClassFlow*>* lfc)
  34. {
  35. SetInitialParameter();
  36. ListFlowControll = lfc;
  37. for (int i = 0; i < ListFlowControll->size(); ++i)
  38. {
  39. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  40. {
  41. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  42. }
  43. }
  44. }
  45. ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  46. {
  47. SetInitialParameter();
  48. previousElement = _prev;
  49. ListFlowControll = lfc;
  50. for (int i = 0; i < ListFlowControll->size(); ++i)
  51. {
  52. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  53. {
  54. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  55. }
  56. }
  57. }
  58. bool ClassFlowInfluxDBv2::ReadParameter(FILE* pfile, string& aktparamgraph)
  59. {
  60. std::vector<string> splitted;
  61. aktparamgraph = trim(aktparamgraph);
  62. printf("akt param: %s\n", aktparamgraph.c_str());
  63. if (aktparamgraph.size() == 0)
  64. if (!this->GetNextParagraph(pfile, aktparamgraph))
  65. return false;
  66. if (toUpper(aktparamgraph).compare("[INFLUXDBV2]") != 0)
  67. return false;
  68. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  69. {
  70. // ESP_LOGD(TAG, "while loop reading line: %s", aktparamgraph.c_str());
  71. splitted = ZerlegeZeile(aktparamgraph);
  72. std::string _param = GetParameterName(splitted[0]);
  73. if ((toUpper(_param) == "ORG") && (splitted.size() > 1))
  74. {
  75. this->dborg = splitted[1];
  76. }
  77. if ((toUpper(_param) == "TOKEN") && (splitted.size() > 1))
  78. {
  79. this->dbtoken = splitted[1];
  80. }
  81. if ((toUpper(_param) == "URI") && (splitted.size() > 1))
  82. {
  83. this->uri = splitted[1];
  84. }
  85. if (((toUpper(_param) == "MEASUREMENT")) && (splitted.size() > 1))
  86. {
  87. this->measurement = splitted[1];
  88. }
  89. if (((toUpper(_param) == "FIELDNAME")) && (splitted.size() > 1))
  90. {
  91. handleFieldname(splitted[0], splitted[1]);
  92. }
  93. if (((toUpper(splitted[0]) == "DATABASE")) && (splitted.size() > 1))
  94. {
  95. this->database = splitted[1];
  96. }
  97. }
  98. printf("uri: %s\n", uri.c_str());
  99. printf("measurement: %s\n", measurement.c_str());
  100. printf("org: %s\n", dborg.c_str());
  101. printf("token: %s\n", dbtoken.c_str());
  102. if ((uri.length() > 0) && (database.length() > 0) && (measurement.length() > 0) && (dbtoken.length() > 0) && (dborg.length() > 0))
  103. {
  104. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init InfluxDB with uri: " + uri + ", measurement: " + measurement + ", org: " + dborg + ", token: *****");
  105. // printf("vor V2 Init\n");
  106. InfluxDB_V2_Init(uri, database, measurement, dborg, dbtoken);
  107. // printf("nach V2 Init\n");
  108. InfluxDBenable = true;
  109. } else {
  110. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBv2 (Verion2 !!!) init skipped as we are missing some parameters");
  111. }
  112. return true;
  113. }
  114. string ClassFlowInfluxDBv2::GetInfluxDBMeasurement()
  115. {
  116. return measurement;
  117. }
  118. void ClassFlowInfluxDBv2::handleFieldname(string _decsep, string _value)
  119. {
  120. string _digit, _decpos;
  121. int _pospunkt = _decsep.find_first_of(".");
  122. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  123. if (_pospunkt > -1)
  124. _digit = _decsep.substr(0, _pospunkt);
  125. else
  126. _digit = "default";
  127. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  128. {
  129. if (_digit == "default") // Set to default first (if nothing else is set)
  130. {
  131. flowpostprocessing->NUMBERS[j]->Fieldname = _value;
  132. }
  133. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  134. {
  135. flowpostprocessing->NUMBERS[j]->Fieldname = _value;
  136. }
  137. }
  138. }
  139. bool ClassFlowInfluxDBv2::doFlow(string zwtime)
  140. {
  141. if (!InfluxDBenable)
  142. return true;
  143. std::string result;
  144. std::string resulterror = "";
  145. std::string resultraw = "";
  146. std::string resultrate = "";
  147. std::string resulttimestamp = "";
  148. string zw = "";
  149. string namenumber = "";
  150. if (flowpostprocessing)
  151. {
  152. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  153. for (int i = 0; i < (*NUMBERS).size(); ++i)
  154. {
  155. result = (*NUMBERS)[i]->ReturnValue;
  156. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  157. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  158. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  159. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  160. if ((*NUMBERS)[i]->Fieldname.length() > 0)
  161. {
  162. namenumber = (*NUMBERS)[i]->Fieldname;
  163. }
  164. else
  165. {
  166. namenumber = (*NUMBERS)[i]->name;
  167. if (namenumber == "default")
  168. namenumber = "value";
  169. else
  170. namenumber = namenumber + "/value";
  171. }
  172. printf("vor sende Influx_DB_V2 - namenumber. %s, result: %s, timestampt: %s", namenumber.c_str(), result.c_str(), resulttimestamp.c_str());
  173. if (result.length() > 0)
  174. InfluxDB_V2_Publish(namenumber, result, resulttimestamp);
  175. // InfluxDB_V2_Publish(namenumber, result, resulttimestamp);
  176. }
  177. }
  178. OldValue = result;
  179. return true;
  180. }
  181. #endif //ENABLE_INFLUXDB