ClassFlowInfluxDBv2.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. ////////////////////////////////////////// NEW ////////////////////////////////////////////
  105. // InfluxDB_V2_Init(uri, bucket, dborg, dbtoken);
  106. // InfluxDB_V2_Init(uri, bucket, dborg, dbtoken);
  107. influxdb.InfluxDBInitV2(uri, bucket, dborg, dbtoken);
  108. ////////////////////////////////////////// NEW ////////////////////////////////////////////
  109. // printf("nach V2 Init\n");
  110. InfluxDBenable = true;
  111. } else {
  112. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBv2 (Verion2 !!!) init skipped as we are missing some parameters");
  113. }
  114. return true;
  115. }
  116. /*
  117. string ClassFlowInfluxDBv2::GetInfluxDBMeasurement()
  118. {
  119. return measurement;
  120. }
  121. */
  122. void ClassFlowInfluxDBv2::handleFieldname(string _decsep, string _value)
  123. {
  124. string _digit, _decpos;
  125. int _pospunkt = _decsep.find_first_of(".");
  126. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  127. if (_pospunkt > -1)
  128. _digit = _decsep.substr(0, _pospunkt);
  129. else
  130. _digit = "default";
  131. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  132. {
  133. if (_digit == "default") // Set to default first (if nothing else is set)
  134. {
  135. flowpostprocessing->NUMBERS[j]->FieldV2 = _value;
  136. }
  137. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  138. {
  139. flowpostprocessing->NUMBERS[j]->FieldV2 = _value;
  140. }
  141. }
  142. }
  143. void ClassFlowInfluxDBv2::handleMeasurement(string _decsep, string _value)
  144. {
  145. string _digit, _decpos;
  146. int _pospunkt = _decsep.find_first_of(".");
  147. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  148. if (_pospunkt > -1)
  149. _digit = _decsep.substr(0, _pospunkt);
  150. else
  151. _digit = "default";
  152. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  153. {
  154. if (_digit == "default") // Set to default first (if nothing else is set)
  155. {
  156. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  157. }
  158. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  159. {
  160. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  161. }
  162. }
  163. }
  164. bool ClassFlowInfluxDBv2::doFlow(string zwtime)
  165. {
  166. if (!InfluxDBenable)
  167. return true;
  168. std::string measurement;
  169. std::string result;
  170. std::string resulterror = "";
  171. std::string resultraw = "";
  172. std::string resultrate = "";
  173. std::string resulttimestamp = "";
  174. long int resulttimeutc = 0;
  175. string zw = "";
  176. string namenumber = "";
  177. if (flowpostprocessing)
  178. {
  179. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  180. for (int i = 0; i < (*NUMBERS).size(); ++i)
  181. {
  182. measurement = (*NUMBERS)[i]->MeasurementV2;
  183. result = (*NUMBERS)[i]->ReturnValue;
  184. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  185. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  186. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  187. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  188. resulttimeutc = (*NUMBERS)[i]->timeStampTimeUTC;
  189. if ((*NUMBERS)[i]->FieldV2.length() > 0)
  190. {
  191. namenumber = (*NUMBERS)[i]->FieldV2;
  192. }
  193. else
  194. {
  195. namenumber = (*NUMBERS)[i]->name;
  196. if (namenumber == "default")
  197. namenumber = "value";
  198. else
  199. namenumber = namenumber + "/value";
  200. }
  201. printf("vor sende Influx_DB_V2 - namenumber. %s, result: %s, timestampt: %s", namenumber.c_str(), result.c_str(), resulttimestamp.c_str());
  202. if (result.length() > 0)
  203. influxdb.InfluxDBPublish(measurement, namenumber, result, resulttimeutc);
  204. // InfluxDB_V2_Publish(measurement, namenumber, result, resulttimeutc);
  205. }
  206. }
  207. OldValue = result;
  208. return true;
  209. }
  210. #endif //ENABLE_INFLUXDB