ClassFlowWebhook.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifdef ENABLE_WEBHOOK
  2. #include <sstream>
  3. #include "ClassFlowWebhook.h"
  4. #include "Helper.h"
  5. #include "connect_wlan.h"
  6. #include "time_sntp.h"
  7. #include "interface_webhook.h"
  8. #include "ClassFlowPostProcessing.h"
  9. #include "ClassFlowAlignment.h"
  10. #include "esp_log.h"
  11. #include "../../include/defines.h"
  12. #include "ClassLogFile.h"
  13. #include <time.h>
  14. static const char* TAG = "WEBHOOK";
  15. void ClassFlowWebhook::SetInitialParameter(void)
  16. {
  17. uri = "";
  18. flowpostprocessing = NULL;
  19. flowAlignment = NULL;
  20. previousElement = NULL;
  21. ListFlowControll = NULL;
  22. disabled = false;
  23. WebhookEnable = false;
  24. WebhookUploadImg = 0;
  25. }
  26. ClassFlowWebhook::ClassFlowWebhook()
  27. {
  28. SetInitialParameter();
  29. }
  30. ClassFlowWebhook::ClassFlowWebhook(std::vector<ClassFlow*>* lfc)
  31. {
  32. SetInitialParameter();
  33. ListFlowControll = lfc;
  34. for (int i = 0; i < ListFlowControll->size(); ++i)
  35. {
  36. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  37. {
  38. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  39. }
  40. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  41. {
  42. flowAlignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
  43. }
  44. }
  45. }
  46. ClassFlowWebhook::ClassFlowWebhook(std::vector<ClassFlow*>* lfc, ClassFlow *_prev)
  47. {
  48. SetInitialParameter();
  49. previousElement = _prev;
  50. ListFlowControll = lfc;
  51. for (int i = 0; i < ListFlowControll->size(); ++i)
  52. {
  53. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  54. {
  55. flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
  56. }
  57. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  58. {
  59. flowAlignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
  60. }
  61. }
  62. }
  63. bool ClassFlowWebhook::ReadParameter(FILE* pfile, string& aktparamgraph)
  64. {
  65. std::vector<string> splitted;
  66. aktparamgraph = trim(aktparamgraph);
  67. printf("akt param: %s\n", aktparamgraph.c_str());
  68. if (aktparamgraph.size() == 0)
  69. if (!this->GetNextParagraph(pfile, aktparamgraph))
  70. return false;
  71. if (toUpper(aktparamgraph).compare("[WEBHOOK]") != 0)
  72. return false;
  73. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  74. {
  75. ESP_LOGD(TAG, "while loop reading line: %s", aktparamgraph.c_str());
  76. splitted = ZerlegeZeile(aktparamgraph);
  77. std::string _param = GetParameterName(splitted[0]);
  78. if ((toUpper(_param) == "URI") && (splitted.size() > 1))
  79. {
  80. this->uri = splitted[1];
  81. }
  82. if (((toUpper(_param) == "APIKEY")) && (splitted.size() > 1))
  83. {
  84. this->apikey = splitted[1];
  85. }
  86. if (((toUpper(_param) == "UPLOADIMG")) && (splitted.size() > 1))
  87. {
  88. if (toUpper(splitted[1]) == "1")
  89. {
  90. this->WebhookUploadImg = 1;
  91. } else if (toUpper(splitted[1]) == "2")
  92. {
  93. this->WebhookUploadImg = 2;
  94. }
  95. }
  96. }
  97. WebhookInit(uri,apikey);
  98. WebhookEnable = true;
  99. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Webhook Enabled for Uri " + uri);
  100. printf("uri: %s\n", uri.c_str());
  101. return true;
  102. }
  103. void ClassFlowWebhook::handleMeasurement(string _decsep, string _value)
  104. {
  105. string _digit, _decpos;
  106. int _pospunkt = _decsep.find_first_of(".");
  107. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  108. if (_pospunkt > -1)
  109. _digit = _decsep.substr(0, _pospunkt);
  110. else
  111. _digit = "default";
  112. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  113. {
  114. if (_digit == "default") // Set to default first (if nothing else is set)
  115. {
  116. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  117. }
  118. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  119. {
  120. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  121. }
  122. }
  123. }
  124. bool ClassFlowWebhook::doFlow(string zwtime)
  125. {
  126. if (!WebhookEnable)
  127. return true;
  128. if (flowpostprocessing)
  129. {
  130. printf("vor sende WebHook");
  131. bool numbersWithError = WebhookPublish(flowpostprocessing->GetNumbers());
  132. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  133. if ((WebhookUploadImg == 1 || (WebhookUploadImg != 0 && numbersWithError)) && flowAlignment && flowAlignment->AlgROI) {
  134. WebhookUploadPic(flowAlignment->AlgROI);
  135. }
  136. #endif
  137. }
  138. return true;
  139. }
  140. #endif //ENABLE_WEBHOOK