ClassFlowWebhook.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if (uri.empty())
  99. {
  100. WebhookEnable = false;
  101. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Webhook not enabled: no Uri configured");
  102. }
  103. else
  104. {
  105. WebhookEnable = true;
  106. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Webhook Enabled for Uri " + uri);
  107. }
  108. printf("uri: %s\n", uri.c_str());
  109. return true;
  110. }
  111. void ClassFlowWebhook::handleMeasurement(string _decsep, string _value)
  112. {
  113. string _digit, _decpos;
  114. int _pospunkt = _decsep.find_first_of(".");
  115. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  116. if (_pospunkt > -1)
  117. _digit = _decsep.substr(0, _pospunkt);
  118. else
  119. _digit = "default";
  120. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  121. {
  122. if (_digit == "default") // Set to default first (if nothing else is set)
  123. {
  124. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  125. }
  126. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  127. {
  128. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  129. }
  130. }
  131. }
  132. bool ClassFlowWebhook::doFlow(string zwtime)
  133. {
  134. if (!WebhookEnable)
  135. return true;
  136. if (flowpostprocessing)
  137. {
  138. printf("vor sende WebHook");
  139. bool numbersWithError = WebhookPublish(flowpostprocessing->GetNumbers());
  140. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  141. if ((WebhookUploadImg == 1 || (WebhookUploadImg != 0 && numbersWithError)) && flowAlignment) {
  142. ImageData *roiImage = flowAlignment->GetAlgROICopy();
  143. if (roiImage != NULL) {
  144. WebhookUploadPic(roiImage);
  145. delete roiImage;
  146. }
  147. }
  148. #endif
  149. }
  150. return true;
  151. }
  152. #endif //ENABLE_WEBHOOK