ClassFlowInfluxDB.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /////////////////////// NEW //////////////////////////
  100. // InfluxDBInit(uri, database, user, password);
  101. influxDB.InfluxDBInitV1(uri, database, user, password);
  102. /////////////////////// NEW //////////////////////////
  103. InfluxDBenable = true;
  104. } else {
  105. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB init skipped as we are missing some parameters");
  106. }
  107. return true;
  108. }
  109. bool ClassFlowInfluxDB::doFlow(string zwtime)
  110. {
  111. if (!InfluxDBenable)
  112. return true;
  113. std::string result;
  114. std::string measurement;
  115. std::string resulterror = "";
  116. std::string resultraw = "";
  117. std::string resultrate = "";
  118. std::string resulttimestamp = "";
  119. long int timeutc;
  120. string zw = "";
  121. string namenumber = "";
  122. if (flowpostprocessing)
  123. {
  124. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  125. for (int i = 0; i < (*NUMBERS).size(); ++i)
  126. {
  127. measurement = (*NUMBERS)[i]->MeasurementV1;
  128. result = (*NUMBERS)[i]->ReturnValue;
  129. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  130. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  131. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  132. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  133. timeutc = (*NUMBERS)[i]->timeStampTimeUTC;
  134. if ((*NUMBERS)[i]->FieldV1.length() > 0)
  135. {
  136. namenumber = (*NUMBERS)[i]->FieldV1;
  137. }
  138. else
  139. {
  140. namenumber = (*NUMBERS)[i]->name;
  141. if (namenumber == "default")
  142. namenumber = "value";
  143. else
  144. namenumber = namenumber + "/value";
  145. }
  146. if (result.length() > 0)
  147. //////////////////////// NEW //////////////////////////
  148. // InfluxDBPublish(measurement, namenumber, result, timeutc);
  149. influxDB.InfluxDBPublish(measurement, namenumber, result, timeutc);
  150. //////////////////////// NEW //////////////////////////
  151. }
  152. }
  153. OldValue = result;
  154. return true;
  155. }
  156. void ClassFlowInfluxDB::handleMeasurement(string _decsep, string _value)
  157. {
  158. string _digit, _decpos;
  159. int _pospunkt = _decsep.find_first_of(".");
  160. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  161. if (_pospunkt > -1)
  162. _digit = _decsep.substr(0, _pospunkt);
  163. else
  164. _digit = "default";
  165. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  166. {
  167. if (_digit == "default") // Set to default first (if nothing else is set)
  168. {
  169. flowpostprocessing->NUMBERS[j]->MeasurementV1 = _value;
  170. }
  171. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  172. {
  173. flowpostprocessing->NUMBERS[j]->MeasurementV1 = _value;
  174. }
  175. }
  176. }
  177. void ClassFlowInfluxDB::handleFieldname(string _decsep, string _value)
  178. {
  179. string _digit, _decpos;
  180. int _pospunkt = _decsep.find_first_of(".");
  181. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  182. if (_pospunkt > -1)
  183. _digit = _decsep.substr(0, _pospunkt);
  184. else
  185. _digit = "default";
  186. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  187. {
  188. if (_digit == "default") // Set to default first (if nothing else is set)
  189. {
  190. flowpostprocessing->NUMBERS[j]->FieldV1 = _value;
  191. }
  192. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  193. {
  194. flowpostprocessing->NUMBERS[j]->FieldV1 = _value;
  195. }
  196. }
  197. }
  198. #endif //ENABLE_INFLUXDB