ClassFlowInfluxDB.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = "INFLUXDB";
  14. void ClassFlowInfluxDB::SetInitialParameter(void)
  15. {
  16. uri = "";
  17. database = "";
  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. std::string _param = GetParameterName(splitted[0]);
  70. if ((toUpper(_param) == "USER") && (splitted.size() > 1))
  71. {
  72. this->user = splitted[1];
  73. }
  74. if ((toUpper(_param) == "PASSWORD") && (splitted.size() > 1))
  75. {
  76. this->password = splitted[1];
  77. }
  78. if ((toUpper(_param) == "URI") && (splitted.size() > 1))
  79. {
  80. this->uri = splitted[1];
  81. }
  82. if (((toUpper(_param) == "DATABASE")) && (splitted.size() > 1))
  83. {
  84. this->database = splitted[1];
  85. }
  86. if (((toUpper(_param) == "MEASUREMENT")) && (splitted.size() > 1))
  87. {
  88. handleMeasurement(splitted[0], splitted[1]);
  89. }
  90. if (((toUpper(_param) == "FIELD")) && (splitted.size() > 1))
  91. {
  92. handleFieldname(splitted[0], splitted[1]);
  93. }
  94. }
  95. if ((uri.length() > 0) && (database.length() > 0))
  96. {
  97. // 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());
  98. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init InfluxDB with uri: " + uri + ", user: " + user + ", password: " + password);
  99. InfluxDBInit(uri, database, user, password);
  100. InfluxDBenable = true;
  101. } else {
  102. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB init skipped as we are missing some parameters");
  103. }
  104. return true;
  105. }
  106. bool ClassFlowInfluxDB::doFlow(string zwtime)
  107. {
  108. if (!InfluxDBenable)
  109. return true;
  110. std::string result;
  111. std::string measurement;
  112. std::string resulterror = "";
  113. std::string resultraw = "";
  114. std::string resultrate = "";
  115. std::string resulttimestamp = "";
  116. string zw = "";
  117. string namenumber = "";
  118. if (flowpostprocessing)
  119. {
  120. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  121. for (int i = 0; i < (*NUMBERS).size(); ++i)
  122. {
  123. measurement = (*NUMBERS)[i]->MeasurementV1;
  124. result = (*NUMBERS)[i]->ReturnValue;
  125. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  126. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  127. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  128. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  129. if ((*NUMBERS)[i]->FieldV1.length() > 0)
  130. {
  131. namenumber = (*NUMBERS)[i]->FieldV1;
  132. }
  133. else
  134. {
  135. namenumber = (*NUMBERS)[i]->name;
  136. if (namenumber == "default")
  137. namenumber = "value";
  138. else
  139. namenumber = namenumber + "/value";
  140. }
  141. if (result.length() > 0)
  142. InfluxDBPublish(measurement, namenumber, result, resulttimestamp);
  143. }
  144. }
  145. OldValue = result;
  146. return true;
  147. }
  148. void ClassFlowInfluxDB::handleMeasurement(string _decsep, string _value)
  149. {
  150. string _digit, _decpos;
  151. int _pospunkt = _decsep.find_first_of(".");
  152. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  153. if (_pospunkt > -1)
  154. _digit = _decsep.substr(0, _pospunkt);
  155. else
  156. _digit = "default";
  157. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  158. {
  159. if (_digit == "default") // Set to default first (if nothing else is set)
  160. {
  161. flowpostprocessing->NUMBERS[j]->MeasurementV1 = _value;
  162. }
  163. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  164. {
  165. flowpostprocessing->NUMBERS[j]->MeasurementV1 = _value;
  166. }
  167. }
  168. }
  169. void ClassFlowInfluxDB::handleFieldname(string _decsep, string _value)
  170. {
  171. string _digit, _decpos;
  172. int _pospunkt = _decsep.find_first_of(".");
  173. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  174. if (_pospunkt > -1)
  175. _digit = _decsep.substr(0, _pospunkt);
  176. else
  177. _digit = "default";
  178. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  179. {
  180. if (_digit == "default") // Set to default first (if nothing else is set)
  181. {
  182. flowpostprocessing->NUMBERS[j]->FieldV1 = _value;
  183. }
  184. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  185. {
  186. flowpostprocessing->NUMBERS[j]->FieldV1 = _value;
  187. }
  188. }
  189. }
  190. #endif //ENABLE_INFLUXDB