ClassFlowInfluxDBv2.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. bucket = "";
  18. dborg = "";
  19. dbtoken = "";
  20. // dbfield = "";
  21. OldValue = "";
  22. flowpostprocessing = NULL;
  23. previousElement = NULL;
  24. ListFlowControll = NULL;
  25. disabled = false;
  26. InfluxDBenable = false;
  27. }
  28. ClassFlowInfluxDBv2::ClassFlowInfluxDBv2()
  29. {
  30. SetInitialParameter();
  31. }
  32. ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(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. ClassFlowInfluxDBv2::ClassFlowInfluxDBv2(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 ClassFlowInfluxDBv2::ReadParameter(FILE* pfile, string& aktparamgraph)
  58. {
  59. std::vector<string> splitted;
  60. aktparamgraph = trim(aktparamgraph);
  61. printf("akt param: %s\n", aktparamgraph.c_str());
  62. if (aktparamgraph.size() == 0)
  63. if (!this->GetNextParagraph(pfile, aktparamgraph))
  64. return false;
  65. if (toUpper(aktparamgraph).compare("[INFLUXDBV2]") != 0)
  66. return false;
  67. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  68. {
  69. // ESP_LOGD(TAG, "while loop reading line: %s", aktparamgraph.c_str());
  70. splitted = ZerlegeZeile(aktparamgraph);
  71. std::string _param = GetParameterName(splitted[0]);
  72. if ((toUpper(_param) == "ORG") && (splitted.size() > 1))
  73. {
  74. this->dborg = splitted[1];
  75. }
  76. if ((toUpper(_param) == "TOKEN") && (splitted.size() > 1))
  77. {
  78. this->dbtoken = splitted[1];
  79. }
  80. if ((toUpper(_param) == "URI") && (splitted.size() > 1))
  81. {
  82. this->uri = splitted[1];
  83. }
  84. if (((toUpper(_param) == "FIELD")) && (splitted.size() > 1))
  85. {
  86. handleFieldname(splitted[0], splitted[1]);
  87. }
  88. if (((toUpper(_param) == "MEASUREMENT")) && (splitted.size() > 1))
  89. {
  90. handleMeasurement(splitted[0], splitted[1]);
  91. }
  92. if (((toUpper(splitted[0]) == "BUCKET")) && (splitted.size() > 1))
  93. {
  94. this->bucket = splitted[1];
  95. }
  96. }
  97. printf("uri: %s\n", uri.c_str());
  98. printf("org: %s\n", dborg.c_str());
  99. printf("token: %s\n", dbtoken.c_str());
  100. if ((uri.length() > 0) && (bucket.length() > 0) && (dbtoken.length() > 0) && (dborg.length() > 0))
  101. {
  102. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init InfluxDB with uri: " + uri + ", org: " + dborg + ", token: *****");
  103. // printf("vor V2 Init\n");
  104. InfluxDB_V2_Init(uri, bucket, dborg, dbtoken);
  105. // printf("nach V2 Init\n");
  106. InfluxDBenable = true;
  107. } else {
  108. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBv2 (Verion2 !!!) init skipped as we are missing some parameters");
  109. }
  110. return true;
  111. }
  112. /*
  113. string ClassFlowInfluxDBv2::GetInfluxDBMeasurement()
  114. {
  115. return measurement;
  116. }
  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]->FieldV2 = _value;
  132. }
  133. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  134. {
  135. flowpostprocessing->NUMBERS[j]->FieldV2 = _value;
  136. }
  137. }
  138. }
  139. void ClassFlowInfluxDBv2::handleMeasurement(string _decsep, string _value)
  140. {
  141. string _digit, _decpos;
  142. int _pospunkt = _decsep.find_first_of(".");
  143. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  144. if (_pospunkt > -1)
  145. _digit = _decsep.substr(0, _pospunkt);
  146. else
  147. _digit = "default";
  148. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  149. {
  150. if (_digit == "default") // Set to default first (if nothing else is set)
  151. {
  152. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  153. }
  154. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  155. {
  156. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  157. }
  158. }
  159. }
  160. bool ClassFlowInfluxDBv2::doFlow(string zwtime)
  161. {
  162. if (!InfluxDBenable)
  163. return true;
  164. std::string measurement;
  165. std::string result;
  166. std::string resulterror = "";
  167. std::string resultraw = "";
  168. std::string resultrate = "";
  169. std::string resulttimestamp = "";
  170. string zw = "";
  171. string namenumber = "";
  172. if (flowpostprocessing)
  173. {
  174. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  175. for (int i = 0; i < (*NUMBERS).size(); ++i)
  176. {
  177. measurement = (*NUMBERS)[i]->MeasurementV2;
  178. result = (*NUMBERS)[i]->ReturnValue;
  179. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  180. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  181. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  182. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  183. if ((*NUMBERS)[i]->FieldV2.length() > 0)
  184. {
  185. namenumber = (*NUMBERS)[i]->FieldV2;
  186. }
  187. else
  188. {
  189. namenumber = (*NUMBERS)[i]->name;
  190. if (namenumber == "default")
  191. namenumber = "value";
  192. else
  193. namenumber = namenumber + "/value";
  194. }
  195. printf("vor sende Influx_DB_V2 - namenumber. %s, result: %s, timestampt: %s", namenumber.c_str(), result.c_str(), resulttimestamp.c_str());
  196. if (result.length() > 0)
  197. InfluxDB_V2_Publish(measurement, namenumber, result, resulttimestamp);
  198. // InfluxDB_V2_Publish(namenumber, result, resulttimestamp);
  199. }
  200. }
  201. OldValue = result;
  202. return true;
  203. }
  204. #endif //ENABLE_INFLUXDB