interface_webhook.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #ifdef ENABLE_WEBHOOK
  2. #include "interface_webhook.h"
  3. #include "esp_log.h"
  4. #include <time.h>
  5. #include "ClassLogFile.h"
  6. #include "esp_http_client.h"
  7. #include "time_sntp.h"
  8. #include "../../include/defines.h"
  9. #include <cJSON.h>
  10. #include <ClassFlowDefineTypes.h>
  11. static const char *TAG = "WEBHOOK";
  12. std::string _webhookURI;
  13. std::string _webhookApiKey;
  14. static esp_err_t http_event_handler(esp_http_client_event_t *evt);
  15. void WebhookInit(std::string _uri, std::string _apiKey)
  16. {
  17. _webhookURI = _uri;
  18. _webhookApiKey = _apiKey;
  19. }
  20. void WebhookPublish(std::vector<NumberPost*>* numbers)
  21. {
  22. cJSON *jsonArray = cJSON_CreateArray();
  23. for (int i = 0; i < (*numbers).size(); ++i)
  24. {
  25. string timezw = "";
  26. char buffer[80];
  27. struct tm* timeinfo = localtime(&(*numbers)[i]->lastvalue);
  28. strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
  29. timezw = std::string(buffer);
  30. cJSON *json = cJSON_CreateObject();
  31. cJSON_AddStringToObject(json, "timestamp", timezw.c_str());
  32. cJSON_AddStringToObject(json, "name", (*numbers)[i]->name.c_str());
  33. cJSON_AddStringToObject(json, "rawValue", (*numbers)[i]->ReturnRawValue.c_str());
  34. cJSON_AddStringToObject(json, "value", (*numbers)[i]->ReturnValue.c_str());
  35. cJSON_AddStringToObject(json, "preValue", (*numbers)[i]->ReturnPreValue.c_str());
  36. cJSON_AddStringToObject(json, "rate", (*numbers)[i]->ReturnRateValue.c_str());
  37. cJSON_AddStringToObject(json, "changeAbsolute", (*numbers)[i]->ReturnChangeAbsolute.c_str());
  38. cJSON_AddStringToObject(json, "error", (*numbers)[i]->ErrorMessageText.c_str());
  39. cJSON_AddItemToArray(jsonArray, json);
  40. }
  41. char *jsonString = cJSON_PrintUnformatted(jsonArray);
  42. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "sending webhook");
  43. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "sending JSON: " + std::string(jsonString));
  44. char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
  45. esp_http_client_config_t http_config = {
  46. .url = _webhookURI.c_str(),
  47. .user_agent = "ESP32 Meter reader",
  48. .method = HTTP_METHOD_POST,
  49. .event_handler = http_event_handler,
  50. .buffer_size = MAX_HTTP_OUTPUT_BUFFER,
  51. .user_data = response_buffer
  52. };
  53. esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
  54. esp_http_client_set_header(http_client, "Content-Type", "application/json");
  55. esp_http_client_set_header(http_client, "APIKEY", _webhookApiKey.c_str());
  56. ESP_ERROR_CHECK(esp_http_client_set_post_field(http_client, jsonString, strlen(jsonString)));
  57. esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
  58. if(err == ESP_OK) {
  59. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
  60. int status_code = esp_http_client_get_status_code(http_client);
  61. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code: " + std::to_string(status_code));
  62. } else {
  63. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP request failed");
  64. }
  65. esp_http_client_cleanup(http_client);
  66. cJSON_Delete(jsonArray);
  67. free(jsonString);
  68. }
  69. static esp_err_t http_event_handler(esp_http_client_event_t *evt)
  70. {
  71. switch(evt->event_id)
  72. {
  73. case HTTP_EVENT_ERROR:
  74. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
  75. break;
  76. case HTTP_EVENT_ON_CONNECTED:
  77. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client connected");
  78. ESP_LOGI(TAG, "HTTP Client Connected");
  79. break;
  80. case HTTP_EVENT_HEADERS_SENT:
  81. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
  82. break;
  83. case HTTP_EVENT_ON_HEADER:
  84. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
  85. break;
  86. case HTTP_EVENT_ON_DATA:
  87. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
  88. break;
  89. case HTTP_EVENT_ON_FINISH:
  90. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
  91. break;
  92. case HTTP_EVENT_DISCONNECTED:
  93. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
  94. break;
  95. case HTTP_EVENT_REDIRECT:
  96. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Redirect");
  97. break;
  98. }
  99. return ESP_OK;
  100. }
  101. #endif //ENABLE_WEBHOOK