ClassFlowInfluxDB.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "defines.h"
  2. #include <sstream>
  3. #include <time.h>
  4. #include "ClassFlowInfluxDB.h"
  5. #include "Helper.h"
  6. #include "connect_wifi_sta.h"
  7. #include "time_sntp.h"
  8. #include "interface_influxdb.h"
  9. #include "ClassFlowPostProcessing.h"
  10. #include "esp_log.h"
  11. #include "ClassLogFile.h"
  12. static const char *TAG = "INFLUXDB";
  13. influxDBv1_controll_config_t influxDBv1_controll_config;
  14. void ClassFlowInfluxDB::SetInitialParameter(void)
  15. {
  16. influxDBv1_controll_config.enabled = false;
  17. influxDBv1_controll_config.uri = "";
  18. influxDBv1_controll_config.database = "";
  19. influxDBv1_controll_config.user = "";
  20. influxDBv1_controll_config.password = "";
  21. influxDBv1_controll_config.oldValue = "";
  22. flowpostprocessing = NULL;
  23. previousElement = NULL;
  24. ListFlowControll = NULL;
  25. disabled = 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, std::string &aktparamgraph)
  57. {
  58. aktparamgraph = trim_string_left_right(aktparamgraph);
  59. if (aktparamgraph.size() == 0)
  60. {
  61. if (!GetNextParagraph(pFile, aktparamgraph))
  62. {
  63. return false;
  64. }
  65. }
  66. if ((to_upper(aktparamgraph).compare("[INFLUXDB]") != 0) && (to_upper(aktparamgraph).compare(";[INFLUXDB]") != 0))
  67. {
  68. return false;
  69. }
  70. if (aktparamgraph[0] == ';')
  71. {
  72. influxDBv1_controll_config.enabled = false;
  73. while (getNextLine(pFile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  74. ESP_LOGD(TAG, "InfluxDBv1 is disabled!");
  75. return true;
  76. }
  77. std::vector<std::string> splitted;
  78. while (getNextLine(pFile, &aktparamgraph) && !isNewParagraph(aktparamgraph))
  79. {
  80. splitted = split_line(aktparamgraph);
  81. if (splitted.size() > 1)
  82. {
  83. std::string _param = to_upper(GetParameterName(splitted[0]));
  84. if (_param == "USER")
  85. {
  86. influxDBv1_controll_config.user = splitted[1];
  87. }
  88. else if (_param == "PASSWORD")
  89. {
  90. influxDBv1_controll_config.password = splitted[1];
  91. }
  92. else if (_param == "URI")
  93. {
  94. influxDBv1_controll_config.uri = splitted[1];
  95. }
  96. else if (_param == "DATABASE")
  97. {
  98. influxDBv1_controll_config.database = splitted[1];
  99. }
  100. else if (_param == "MEASUREMENT")
  101. {
  102. handleMeasurement(splitted[0], splitted[1]);
  103. }
  104. else if (_param == "FIELD")
  105. {
  106. handleFieldname(splitted[0], splitted[1]);
  107. }
  108. }
  109. }
  110. if ((influxDBv1_controll_config.uri.length() > 0) && (influxDBv1_controll_config.database.length() > 0))
  111. {
  112. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init InfluxDBv1 with uri: " + influxDBv1_controll_config.uri + ", user: " + influxDBv1_controll_config.user + ", password: " + influxDBv1_controll_config.password);
  113. influxDB.InfluxDBInitV1(influxDBv1_controll_config.uri, influxDBv1_controll_config.database, influxDBv1_controll_config.user, influxDBv1_controll_config.password);
  114. influxDBv1_controll_config.enabled = true;
  115. }
  116. else
  117. {
  118. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBv1 init skipped as we are missing some parameters");
  119. }
  120. return true;
  121. }
  122. bool ClassFlowInfluxDB::doFlow(std::string temp_time)
  123. {
  124. if (!influxDBv1_controll_config.enabled)
  125. {
  126. return true;
  127. }
  128. std::string result = "";
  129. std::string measurement = "";
  130. long int result_timeutc = 0;
  131. std::string name_number = "";
  132. if (flowpostprocessing)
  133. {
  134. std::vector<NumberPost *> *NUMBERS = flowpostprocessing->GetNumbers();
  135. for (int i = 0; i < (*NUMBERS).size(); ++i)
  136. {
  137. measurement = (*NUMBERS)[i]->MeasurementV1;
  138. result = (*NUMBERS)[i]->ReturnValue;
  139. result_timeutc = (*NUMBERS)[i]->timeStampTimeUTC;
  140. if ((*NUMBERS)[i]->FieldV1.length() > 0)
  141. {
  142. name_number = (*NUMBERS)[i]->FieldV1;
  143. }
  144. else
  145. {
  146. name_number = (*NUMBERS)[i]->name;
  147. if (name_number == "default")
  148. {
  149. name_number = "value";
  150. }
  151. else
  152. {
  153. name_number = name_number + "/value";
  154. }
  155. }
  156. if (result.length() > 0)
  157. {
  158. influxDB.InfluxDBPublish(measurement, name_number, result, result_timeutc);
  159. }
  160. }
  161. }
  162. influxDBv1_controll_config.oldValue = result;
  163. return true;
  164. }
  165. void ClassFlowInfluxDB::handleMeasurement(std::string _decsep, std::string _value)
  166. {
  167. std::string _digit;
  168. int _pospunkt = _decsep.find_first_of(".");
  169. if (_pospunkt > -1)
  170. {
  171. _digit = _decsep.substr(0, _pospunkt);
  172. }
  173. else
  174. {
  175. _digit = "default";
  176. }
  177. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  178. {
  179. // Set to default first (if nothing else is set)
  180. if ((_digit == "default") || (flowpostprocessing->NUMBERS[j]->name == _digit))
  181. {
  182. flowpostprocessing->NUMBERS[j]->MeasurementV1 = _value;
  183. }
  184. }
  185. }
  186. void ClassFlowInfluxDB::handleFieldname(std::string _decsep, std::string _value)
  187. {
  188. std::string _digit;
  189. int _pospunkt = _decsep.find_first_of(".");
  190. if (_pospunkt > -1)
  191. {
  192. _digit = _decsep.substr(0, _pospunkt);
  193. }
  194. else
  195. {
  196. _digit = "default";
  197. }
  198. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  199. {
  200. // Set to default first (if nothing else is set)
  201. if ((_digit == "default") || (flowpostprocessing->NUMBERS[j]->name == _digit))
  202. {
  203. flowpostprocessing->NUMBERS[j]->FieldV1 = _value;
  204. }
  205. }
  206. }