ClassFlowWebhook.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "defines.h"
  2. #include "Helper.h"
  3. #include <time.h>
  4. #include <sstream>
  5. #include "ClassFlowWebhook.h"
  6. #include "connect_wifi_sta.h"
  7. #include "time_sntp.h"
  8. #include "interface_webhook.h"
  9. #include "ClassFlowPostProcessing.h"
  10. #include "ClassFlowAlignment.h"
  11. #include "esp_log.h"
  12. #include "ClassLogFile.h"
  13. static const char *TAG = "WEBHOOK";
  14. Webhook_controll_config_t Webhook_controll_config;
  15. void ClassFlowWebhook::SetInitialParameter(void)
  16. {
  17. Webhook_controll_config.enabled = false;
  18. Webhook_controll_config.uri = "";
  19. Webhook_controll_config.apikey = "";
  20. Webhook_controll_config.uploadImg = 0;
  21. Webhook_controll_config.oldValue = "";
  22. flowpostprocessing = NULL;
  23. flowAlignment = NULL;
  24. previousElement = NULL;
  25. ListFlowControll = NULL;
  26. disabled = false;
  27. }
  28. ClassFlowWebhook::ClassFlowWebhook()
  29. {
  30. SetInitialParameter();
  31. }
  32. ClassFlowWebhook::ClassFlowWebhook(std::vector<ClassFlow *> *lfc)
  33. {
  34. SetInitialParameter();
  35. ListFlowControll = lfc;
  36. for (int i = 0; i < ListFlowControll->size(); ++i)
  37. {
  38. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  39. {
  40. flowpostprocessing = (ClassFlowPostProcessing *)(*ListFlowControll)[i];
  41. }
  42. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  43. {
  44. flowAlignment = (ClassFlowAlignment *)(*ListFlowControll)[i];
  45. }
  46. }
  47. }
  48. ClassFlowWebhook::ClassFlowWebhook(std::vector<ClassFlow *> *lfc, ClassFlow *_prev)
  49. {
  50. SetInitialParameter();
  51. previousElement = _prev;
  52. ListFlowControll = lfc;
  53. for (int i = 0; i < ListFlowControll->size(); ++i)
  54. {
  55. if (((*ListFlowControll)[i])->name().compare("ClassFlowPostProcessing") == 0)
  56. {
  57. flowpostprocessing = (ClassFlowPostProcessing *)(*ListFlowControll)[i];
  58. }
  59. if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
  60. {
  61. flowAlignment = (ClassFlowAlignment *)(*ListFlowControll)[i];
  62. }
  63. }
  64. }
  65. bool ClassFlowWebhook::ReadParameter(FILE *pFile, std::string &aktparamgraph)
  66. {
  67. aktparamgraph = trim_string_left_right(aktparamgraph);
  68. if (aktparamgraph.size() == 0)
  69. {
  70. if (!GetNextParagraph(pFile, aktparamgraph))
  71. {
  72. return false;
  73. }
  74. }
  75. if ((to_upper(aktparamgraph).compare("[WEBHOOK]") != 0) && (to_upper(aktparamgraph).compare(";[WEBHOOK]") != 0))
  76. {
  77. return false;
  78. }
  79. if (aktparamgraph[0] == ';')
  80. {
  81. Webhook_controll_config.enabled = false;
  82. while (getNextLine(pFile, &aktparamgraph) && !isNewParagraph(aktparamgraph));
  83. ESP_LOGD(TAG, "Webhook is disabled!");
  84. return true;
  85. }
  86. std::vector<std::string> splitted;
  87. while (getNextLine(pFile, &aktparamgraph) && !isNewParagraph(aktparamgraph))
  88. {
  89. splitted = split_line(aktparamgraph);
  90. if (splitted.size() > 1)
  91. {
  92. std::string _param = to_upper(GetParameterName(splitted[0]));
  93. if (_param == "URI")
  94. {
  95. Webhook_controll_config.uri = splitted[1];
  96. }
  97. else if (_param == "APIKEY")
  98. {
  99. Webhook_controll_config.apikey = splitted[1];
  100. }
  101. else if (_param == "UPLOADIMG")
  102. {
  103. if (to_upper(splitted[1]) == "1")
  104. {
  105. Webhook_controll_config.uploadImg = 1;
  106. }
  107. else if (to_upper(splitted[1]) == "2")
  108. {
  109. Webhook_controll_config.uploadImg = 2;
  110. }
  111. }
  112. }
  113. }
  114. if ((Webhook_controll_config.uri.length() > 0) && (Webhook_controll_config.apikey.length() > 0))
  115. {
  116. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init Webhook with uri: " + Webhook_controll_config.uri + ", apikey: *****");
  117. WebhookInit(Webhook_controll_config.uri, Webhook_controll_config.apikey);
  118. Webhook_controll_config.enabled = true;
  119. }
  120. else
  121. {
  122. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Webhook init skipped as we are missing some parameters");
  123. Webhook_controll_config.enabled = false;
  124. }
  125. return true;
  126. }
  127. void ClassFlowWebhook::handleMeasurement(std::string _decsep, std::string _value)
  128. {
  129. std::string _digit;
  130. int _pospunkt = _decsep.find_first_of(".");
  131. if (_pospunkt > -1)
  132. {
  133. _digit = _decsep.substr(0, _pospunkt);
  134. }
  135. else
  136. {
  137. _digit = "default";
  138. }
  139. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  140. {
  141. // Set to default first (if nothing else is set)
  142. if ((_digit == "default") || (flowpostprocessing->NUMBERS[j]->name == _digit))
  143. {
  144. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  145. }
  146. }
  147. }
  148. bool ClassFlowWebhook::doFlow(std::string temp_time)
  149. {
  150. if (!Webhook_controll_config.enabled)
  151. {
  152. return true;
  153. }
  154. if (flowpostprocessing)
  155. {
  156. printf("vor sende WebHook");
  157. bool numbersWithError = WebhookPublish(flowpostprocessing->GetNumbers());
  158. #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
  159. if ((Webhook_controll_config.uploadImg == 1 || (Webhook_controll_config.uploadImg != 0 && numbersWithError)) && flowAlignment && flowAlignment->AlgROI)
  160. {
  161. WebhookUploadPic(flowAlignment->AlgROI);
  162. }
  163. #endif
  164. }
  165. return true;
  166. }