diff --git a/code/components/jomjol_flowcontroll/ClassFlowWebhook.cpp b/code/components/jomjol_flowcontroll/ClassFlowWebhook.cpp index a60919bda..5194c82b4 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowWebhook.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowWebhook.cpp @@ -8,6 +8,7 @@ #include "interface_webhook.h" #include "ClassFlowPostProcessing.h" +#include "ClassFlowAlignment.h" #include "esp_log.h" #include "../../include/defines.h" @@ -20,11 +21,13 @@ static const char* TAG = "WEBHOOK"; void ClassFlowWebhook::SetInitialParameter(void) { uri = ""; - flowpostprocessing = NULL; + flowpostprocessing = NULL; + flowAlignment = NULL; previousElement = NULL; ListFlowControll = NULL; disabled = false; WebhookEnable = false; + WebhookUploadImg = 0; } ClassFlowWebhook::ClassFlowWebhook() @@ -43,6 +46,11 @@ ClassFlowWebhook::ClassFlowWebhook(std::vector* lfc) { flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i]; } + if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0) + { + flowAlignment = (ClassFlowAlignment*) (*ListFlowControll)[i]; + } + } } @@ -59,6 +67,10 @@ ClassFlowWebhook::ClassFlowWebhook(std::vector* lfc, ClassFlow *_pre { flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i]; } + if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0) + { + flowAlignment = (ClassFlowAlignment*) (*ListFlowControll)[i]; + } } } @@ -93,6 +105,16 @@ bool ClassFlowWebhook::ReadParameter(FILE* pfile, string& aktparamgraph) { this->apikey = splitted[1]; } + if (((toUpper(_param) == "UPLOADIMG")) && (splitted.size() > 1)) + { + if (toUpper(splitted[1]) == "1") + { + this->WebhookUploadImg = 1; + } else if (toUpper(splitted[1]) == "2") + { + this->WebhookUploadImg = 2; + } + } } WebhookInit(uri,apikey); @@ -135,7 +157,13 @@ bool ClassFlowWebhook::doFlow(string zwtime) if (flowpostprocessing) { printf("vor sende WebHook"); - WebhookPublish(flowpostprocessing->GetNumbers()); + bool numbersWithError = WebhookPublish(flowpostprocessing->GetNumbers()); + + #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG + if ((WebhookUploadImg == 1 || (WebhookUploadImg != 0 && numbersWithError)) && flowAlignment && flowAlignment->AlgROI) { + WebhookUploadPic(flowAlignment->AlgROI); + } + #endif } return true; diff --git a/code/components/jomjol_flowcontroll/ClassFlowWebhook.h b/code/components/jomjol_flowcontroll/ClassFlowWebhook.h index 3900fc092..8f679d42f 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowWebhook.h +++ b/code/components/jomjol_flowcontroll/ClassFlowWebhook.h @@ -8,6 +8,7 @@ #include "ClassFlow.h" #include "ClassFlowPostProcessing.h" +#include "ClassFlowAlignment.h" #include @@ -16,8 +17,11 @@ class ClassFlowWebhook : { protected: std::string uri, apikey; - ClassFlowPostProcessing* flowpostprocessing; + ClassFlowPostProcessing* flowpostprocessing; + ClassFlowAlignment* flowAlignment; + bool WebhookEnable; + int WebhookUploadImg; void SetInitialParameter(void); diff --git a/code/components/jomjol_webhook/interface_webhook.cpp b/code/components/jomjol_webhook/interface_webhook.cpp index abeba657b..54d4c48ed 100644 --- a/code/components/jomjol_webhook/interface_webhook.cpp +++ b/code/components/jomjol_webhook/interface_webhook.cpp @@ -15,6 +15,7 @@ static const char *TAG = "WEBHOOK"; std::string _webhookURI; std::string _webhookApiKey; +long _lastTimestamp; static esp_err_t http_event_handler(esp_http_client_event_t *evt); @@ -22,22 +23,27 @@ void WebhookInit(std::string _uri, std::string _apiKey) { _webhookURI = _uri; _webhookApiKey = _apiKey; + _lastTimestamp = 0L; } -void WebhookPublish(std::vector* numbers) +bool WebhookPublish(std::vector* numbers) { + bool numbersWithError = false; cJSON *jsonArray = cJSON_CreateArray(); for (int i = 0; i < (*numbers).size(); ++i) { string timezw = ""; char buffer[80]; - struct tm* timeinfo = localtime(&(*numbers)[i]->timeStampLastPreValue); + time_t &lastPreValue = (*numbers)[i]->timeStampLastPreValue; + struct tm* timeinfo = localtime(&lastPreValue); + _lastTimestamp = static_cast(lastPreValue); strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo); timezw = std::string(buffer); cJSON *json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "timestamp", timezw.c_str()); + cJSON_AddStringToObject(json, "timestampLong", std::to_string(_lastTimestamp).c_str()); cJSON_AddStringToObject(json, "name", (*numbers)[i]->name.c_str()); cJSON_AddStringToObject(json, "rawValue", (*numbers)[i]->ReturnRawValue.c_str()); cJSON_AddStringToObject(json, "value", (*numbers)[i]->ReturnValue.c_str()); @@ -47,6 +53,10 @@ void WebhookPublish(std::vector* numbers) cJSON_AddStringToObject(json, "error", (*numbers)[i]->ErrorMessageText.c_str()); cJSON_AddItemToArray(jsonArray, json); + + if ((*numbers)[i]->ErrorMessage) { + numbersWithError = true; + } } char *jsonString = cJSON_PrintUnformatted(jsonArray); @@ -79,13 +89,51 @@ void WebhookPublish(std::vector* numbers) LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code: " + std::to_string(status_code)); } else { LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP request failed"); - } + } esp_http_client_cleanup(http_client); cJSON_Delete(jsonArray); free(jsonString); + return numbersWithError; } +void WebhookUploadPic(ImageData *Img) { + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting WebhookUploadPic"); + + std::string fullURI = _webhookURI + "?timestamp=" + std::to_string(_lastTimestamp); + char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; + esp_http_client_config_t http_config = { + .url = fullURI.c_str(), + .user_agent = "ESP32 Meter reader", + .method = HTTP_METHOD_PUT, + .event_handler = http_event_handler, + .buffer_size = MAX_HTTP_OUTPUT_BUFFER, + .user_data = response_buffer + }; + + esp_http_client_handle_t http_client = esp_http_client_init(&http_config); + + esp_http_client_set_header(http_client, "Content-Type", "image/jpeg"); + esp_http_client_set_header(http_client, "APIKEY", _webhookApiKey.c_str()); + + esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_set_post_field(http_client, (const char *)Img->data, Img->size)); + + err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client)); + + if (err == ESP_OK) { + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP PUT request was performed successfully"); + int status_code = esp_http_client_get_status_code(http_client); + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP status code: " + std::to_string(status_code)); + } else { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP PUT request failed"); + } + + esp_http_client_cleanup(http_client); + + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "WebhookUploadPic finished"); +} + + static esp_err_t http_event_handler(esp_http_client_event_t *evt) { switch(evt->event_id) diff --git a/code/components/jomjol_webhook/interface_webhook.h b/code/components/jomjol_webhook/interface_webhook.h index 0d1cb0dd1..d7033be09 100644 --- a/code/components/jomjol_webhook/interface_webhook.h +++ b/code/components/jomjol_webhook/interface_webhook.h @@ -10,7 +10,8 @@ #include void WebhookInit(std::string _webhookURI, std::string _apiKey); -void WebhookPublish(std::vector* numbers); +bool WebhookPublish(std::vector* numbers); +void WebhookUploadPic(ImageData *Img); #endif //INTERFACE_WEBHOOK_H #endif //ENABLE_WEBHOOK \ No newline at end of file diff --git a/param-docs/parameter-pages/Webhook/UploadImg.md b/param-docs/parameter-pages/Webhook/UploadImg.md new file mode 100644 index 000000000..fb375642f --- /dev/null +++ b/param-docs/parameter-pages/Webhook/UploadImg.md @@ -0,0 +1,8 @@ +# Parameter `UploadImg` +Default Value: `0` (`NEVER`) + +Available options: + +- `0`: `NEVER` +- `1`: `ALWAYS` +- `2`: `ON_ERROR` \ No newline at end of file diff --git a/sd-card/config/config.ini b/sd-card/config/config.ini index eb8a3316a..2c58dfba1 100644 --- a/sd-card/config/config.ini +++ b/sd-card/config/config.ini @@ -108,6 +108,7 @@ HomeassistantDiscovery = false ;[Webhook] ;Uri = undefined ;ApiKey = undefined +;UploadImg = 0 ;[GPIO] ;MainTopicMQTT = wasserzaehler/GPIO diff --git a/sd-card/html/edit_config_template.html b/sd-card/html/edit_config_template.html index 70f2f29e7..6861843e0 100644 --- a/sd-card/html/edit_config_template.html +++ b/sd-card/html/edit_config_template.html @@ -1353,7 +1353,8 @@

-

+ + @@ -1379,6 +1380,20 @@

$TOOLTIP_Webhook_ApiKey + + + + + + + + + $TOOLTIP_Webhook_UploadImg + @@ -2310,6 +2325,7 @@