ClassFlowInfluxDB.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <sstream>
  2. #include "ClassFlowInfluxDB.h"
  3. #include "Helper.h"
  4. #include "connect_wlan.h"
  5. #include "time_sntp.h"
  6. #include "interface_influxdb.h"
  7. #include "ClassFlowPostProcessing.h"
  8. #include <time.h>
  9. void ClassFlowInfluxDB::SetInitialParameter(void)
  10. {
  11. uri = "";
  12. database = "";
  13. measurement = "";
  14. OldValue = "";
  15. flowpostprocessing = NULL;
  16. user = "";
  17. password = "";
  18. previousElement = NULL;
  19. ListFlowControll = NULL;
  20. disabled = false;
  21. InfluxDBenable = false;
  22. }
  23. ClassFlowInfluxDB::ClassFlowInfluxDB()
  24. {
  25. SetInitialParameter();
  26. }
  27. ClassFlowInfluxDB::ClassFlowInfluxDB(std::vector<ClassFlow*>* lfc)
  28. {
  29. SetInitialParameter();
  30. ListFlowControll = lfc;
  31. for (int i = 0; i < ListFlowControll->size(); ++i)
  32. {
  33. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  34. {
  35. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  36. }
  37. }
  38. }
  39. ClassFlowInfluxDB::ClassFlowInfluxDB(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  40. {
  41. SetInitialParameter();
  42. previousElement = _prev;
  43. ListFlowControll = lfc;
  44. for (int i = 0; i < ListFlowControll->size(); ++i)
  45. {
  46. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  47. {
  48. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  49. }
  50. }
  51. }
  52. bool ClassFlowInfluxDB::ReadParameter(FILE* pfile, string& aktparamgraph)
  53. {
  54. std::vector<string> zerlegt;
  55. aktparamgraph = trim(aktparamgraph);
  56. if (aktparamgraph.size() == 0)
  57. if (!this->GetNextParagraph(pfile, aktparamgraph))
  58. return false;
  59. if (toUpper(aktparamgraph).compare("[INFLUXDB]") != 0)
  60. return false;
  61. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  62. {
  63. printf("while loop reading line: %s\n", aktparamgraph.c_str());
  64. zerlegt = this->ZerlegeZeile(aktparamgraph);
  65. if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1))
  66. {
  67. this->user = zerlegt[1];
  68. }
  69. if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1))
  70. {
  71. this->password = zerlegt[1];
  72. }
  73. if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1))
  74. {
  75. this->uri = zerlegt[1];
  76. }
  77. if (((toUpper(zerlegt[0]) == "MEASUREMENT")) && (zerlegt.size() > 1))
  78. {
  79. this->measurement = zerlegt[1];
  80. }
  81. if (((toUpper(zerlegt[0]) == "DATABASE")) && (zerlegt.size() > 1))
  82. {
  83. this->database = zerlegt[1];
  84. }
  85. }
  86. if ((uri.length() > 0) && (database.length() > 0) && (measurement.length() > 0))
  87. {
  88. printf("Init InfluxDB with uri: %s, measurement: %s, user: %s, password: %s\n", uri.c_str(), measurement.c_str(), user.c_str(), password.c_str());
  89. InfluxDBInit(uri, database, measurement, user, password);
  90. InfluxDBenable = true;
  91. } else {
  92. printf("InfluxDB init skipped as we are missing some parameters");
  93. }
  94. return true;
  95. }
  96. string ClassFlowInfluxDB::GetInfluxDBMeasurement()
  97. {
  98. return measurement;
  99. }
  100. bool ClassFlowInfluxDB::doFlow(string zwtime)
  101. {
  102. if (!InfluxDBenable)
  103. return true;
  104. std::string result;
  105. std::string resulterror = "";
  106. std::string resultraw = "";
  107. std::string resultrate = "";
  108. std::string resulttimestamp = "";
  109. string zw = "";
  110. string namenumber = "";
  111. if (flowpostprocessing)
  112. {
  113. std::vector<NumberPost*>* NUMBERS = flowpostprocessing->GetNumbers();
  114. for (int i = 0; i < (*NUMBERS).size(); ++i)
  115. {
  116. result = (*NUMBERS)[i]->ReturnValue;
  117. resultraw = (*NUMBERS)[i]->ReturnRawValue;
  118. resulterror = (*NUMBERS)[i]->ErrorMessageText;
  119. resultrate = (*NUMBERS)[i]->ReturnRateValue;
  120. resulttimestamp = (*NUMBERS)[i]->timeStamp;
  121. namenumber = (*NUMBERS)[i]->name;
  122. if (namenumber == "default")
  123. namenumber = "value";
  124. else
  125. namenumber = namenumber + "/value";
  126. if (result.length() > 0 && resulttimestamp.length() > 0)
  127. InfluxDBPublish(namenumber, result, resulttimestamp);
  128. }
  129. }
  130. OldValue = result;
  131. return true;
  132. }