ClassFlowWebhook.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "esp_log.h"
  10. #include "../../include/defines.h"
  11. #include "ClassLogFile.h"
  12. #include <time.h>
  13. static const char* TAG = "WEBHOOK";
  14. void ClassFlowWebhook::SetInitialParameter(void)
  15. {
  16. uri = "";
  17. flowpostprocessing = NULL;
  18. previousElement = NULL;
  19. ListFlowControll = NULL;
  20. disabled = false;
  21. WebhookEnable = false;
  22. }
  23. ClassFlowWebhook::ClassFlowWebhook()
  24. {
  25. SetInitialParameter();
  26. }
  27. ClassFlowWebhook::ClassFlowWebhook(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. ClassFlowWebhook::ClassFlowWebhook(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 ClassFlowWebhook::ReadParameter(FILE* pfile, string& aktparamgraph)
  53. {
  54. std::vector<string> splitted;
  55. aktparamgraph = trim(aktparamgraph);
  56. printf("akt param: %s\n", aktparamgraph.c_str());
  57. if (aktparamgraph.size() == 0)
  58. if (!this->GetNextParagraph(pfile, aktparamgraph))
  59. return false;
  60. if (toUpper(aktparamgraph).compare("[WEBHOOK]") != 0)
  61. return false;
  62. while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph))
  63. {
  64. ESP_LOGD(TAG, "while loop reading line: %s", aktparamgraph.c_str());
  65. splitted = ZerlegeZeile(aktparamgraph);
  66. std::string _param = GetParameterName(splitted[0]);
  67. if ((toUpper(_param) == "URI") && (splitted.size() > 1))
  68. {
  69. this->uri = splitted[1];
  70. }
  71. if (((toUpper(_param) == "APIKEY")) && (splitted.size() > 1))
  72. {
  73. this->apikey = splitted[1];
  74. }
  75. }
  76. WebhookInit(uri,apikey);
  77. WebhookEnable = true;
  78. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Webhook Enabled for Uri " + uri);
  79. printf("uri: %s\n", uri.c_str());
  80. return true;
  81. }
  82. void ClassFlowWebhook::handleMeasurement(string _decsep, string _value)
  83. {
  84. string _digit, _decpos;
  85. int _pospunkt = _decsep.find_first_of(".");
  86. // ESP_LOGD(TAG, "Name: %s, Pospunkt: %d", _decsep.c_str(), _pospunkt);
  87. if (_pospunkt > -1)
  88. _digit = _decsep.substr(0, _pospunkt);
  89. else
  90. _digit = "default";
  91. for (int j = 0; j < flowpostprocessing->NUMBERS.size(); ++j)
  92. {
  93. if (_digit == "default") // Set to default first (if nothing else is set)
  94. {
  95. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  96. }
  97. if (flowpostprocessing->NUMBERS[j]->name == _digit)
  98. {
  99. flowpostprocessing->NUMBERS[j]->MeasurementV2 = _value;
  100. }
  101. }
  102. }
  103. bool ClassFlowWebhook::doFlow(string zwtime)
  104. {
  105. if (!WebhookEnable)
  106. return true;
  107. if (flowpostprocessing)
  108. {
  109. printf("vor sende WebHook");
  110. WebhookPublish(flowpostprocessing->GetNumbers());
  111. }
  112. return true;
  113. }
  114. #endif //ENABLE_WEBHOOK