Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional ImageUpload for Webhook #3174

Merged
merged 25 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e8c5ad1
WIP add Webhook
RaHehl Aug 6, 2024
260ef21
fix config html for webhook
RaHehl Aug 7, 2024
1ce6010
webhook: fix not enabling webhook
RaHehl Aug 7, 2024
a9d5f08
send webhook as json
RaHehl Aug 7, 2024
e642b0a
Update ApiKey.md
caco3 Aug 8, 2024
e38c9ea
webhook: fix only sending last "Number"
RaHehl Aug 9, 2024
6529252
webhook JSON is now closer to the data log in CSV format
RaHehl Aug 9, 2024
5d571fd
Merge branch 'jomjol:rolling' into webhook
RaHehl Aug 9, 2024
c335d0d
webhook: add img upload
RaHehl Aug 9, 2024
67ab7fc
webhoop added config for imgupload
RaHehl Aug 9, 2024
d18afe9
webhook html fixes
RaHehl Aug 9, 2024
4e51786
webhook: drop timeStampTimeUTC and switch from timeStamp to lastvalue…
RaHehl Aug 9, 2024
7cb308b
Merge branch 'webhook' into webhook-withImgUpload
RaHehl Aug 9, 2024
f5a02ff
Merge branch 'rolling' into webhook
RaHehl Aug 23, 2024
599fafd
Merge branch 'webhook' into webhook-withImgUpload
RaHehl Aug 23, 2024
19a7654
Merge branch 'rolling' into webhook-withImgUpload
RaHehl Aug 23, 2024
8b16d24
Merge branch 'rolling' into webhook-withImgUpload
RaHehl Aug 25, 2024
524d96c
add checkbox for Webhook_UploadImg
caco3 Aug 25, 2024
086e1dd
Update sd-card/html/edit_config_template.html
caco3 Aug 25, 2024
0929ee1
Update edit_config_template.html
caco3 Aug 25, 2024
78d8100
Update edit_config_template.html
caco3 Aug 25, 2024
c6e1e6f
Update edit_config_template.html
caco3 Aug 25, 2024
e83d515
Merge branch 'jomjol:rolling' into webhook-withImgUpload
RaHehl Aug 27, 2024
6b88121
added a long timestamp to both webhook requests
RaHehl Aug 30, 2024
a795dfa
Merge branch 'jomjol:rolling' into webhook-withImgUpload
RaHehl Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions code/components/jomjol_flowcontroll/ClassFlowWebhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "interface_webhook.h"

#include "ClassFlowPostProcessing.h"
#include "ClassFlowAlignment.h"
#include "esp_log.h"
#include "../../include/defines.h"

Expand All @@ -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()
Expand All @@ -43,6 +46,11 @@ ClassFlowWebhook::ClassFlowWebhook(std::vector<ClassFlow*>* lfc)
{
flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
}
if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
{
flowAlignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
}

}
}

Expand All @@ -59,6 +67,10 @@ ClassFlowWebhook::ClassFlowWebhook(std::vector<ClassFlow*>* lfc, ClassFlow *_pre
{
flowpostprocessing = (ClassFlowPostProcessing*) (*ListFlowControll)[i];
}
if (((*ListFlowControll)[i])->name().compare("ClassFlowAlignment") == 0)
{
flowAlignment = (ClassFlowAlignment*) (*ListFlowControll)[i];
}
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion code/components/jomjol_flowcontroll/ClassFlowWebhook.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ClassFlow.h"

#include "ClassFlowPostProcessing.h"
#include "ClassFlowAlignment.h"

#include <string>

Expand All @@ -16,8 +17,11 @@ class ClassFlowWebhook :
{
protected:
std::string uri, apikey;
ClassFlowPostProcessing* flowpostprocessing;
ClassFlowPostProcessing* flowpostprocessing;
ClassFlowAlignment* flowAlignment;

bool WebhookEnable;
int WebhookUploadImg;

void SetInitialParameter(void);

Expand Down
46 changes: 44 additions & 2 deletions code/components/jomjol_webhook/interface_webhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ void WebhookInit(std::string _uri, std::string _apiKey)
_webhookApiKey = _apiKey;
}

void WebhookPublish(std::vector<NumberPost*>* numbers)
bool WebhookPublish(std::vector<NumberPost*>* numbers)
{
bool numbersWithError = false;
cJSON *jsonArray = cJSON_CreateArray();

for (int i = 0; i < (*numbers).size(); ++i)
Expand All @@ -47,6 +48,10 @@ void WebhookPublish(std::vector<NumberPost*>* 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);
Expand Down Expand Up @@ -79,13 +84,50 @@ void WebhookPublish(std::vector<NumberPost*>* 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");

char response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
esp_http_client_config_t http_config = {
.url = _webhookURI.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)
Expand Down
3 changes: 2 additions & 1 deletion code/components/jomjol_webhook/interface_webhook.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#include <ClassFlowDefineTypes.h>

void WebhookInit(std::string _webhookURI, std::string _apiKey);
void WebhookPublish(std::vector<NumberPost*>* numbers);
bool WebhookPublish(std::vector<NumberPost*>* numbers);
void WebhookUploadPic(ImageData *Img);

#endif //INTERFACE_WEBHOOK_H
#endif //ENABLE_WEBHOOK
8 changes: 8 additions & 0 deletions param-docs/parameter-pages/Webhook/UploadImg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Parameter `UploadImg`
Default Value: `0` (`NEVER`)

Available options:

- `0`: `NEVER`
- `1`: `ALWAYS`
- `2`: `ON_ERROR`
1 change: 1 addition & 0 deletions sd-card/config/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ HomeassistantDiscovery = false
;[Webhook]
;Uri = undefined
;ApiKey = undefined
;UploadImg = 0

;[GPIO]
;MainTopicMQTT = wasserzaehler/GPIO
Expand Down
20 changes: 19 additions & 1 deletion sd-card/html/edit_config_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,8 @@ <h4>
<td colspan="3" style="padding-left: 0px; padding-bottom: 3px;">
<h4>
<input type="checkbox" id="Category_Webhook_enabled" value="1" onclick = 'UpdateAfterCategoryCheck()' unchecked >
<label for=Category_Webhook_enabled>Webhook</label></h4>
<label for=Category_Webhook_enabled>Webhook</label>
</h4>
</td>
</tr>

Expand All @@ -1355,6 +1356,20 @@ <h4>
<td>$TOOLTIP_Webhook_ApiKey</td>
</tr>

<tr class="WebhookItem">
<td class="indent1">
<input type="checkbox" id="Webhook_UploadImg_enabled" value="1" onclick = 'InvertEnableItem("Webhook", "Webhook_UploadImg")' unchecked>
<label><class id="Webhook_UploadImg_text" style="color:black;">Upload Image</class></label>
</td>
<td>
<select id="Webhook_UploadImg_value1">
<option value="0" selected>NEVER</option>
<option value="1">ALWAYS</option>
<option value="2">ON_ERROR</option>
</select>
</td>
<td>$TOOLTIP_Webhook_UploadImg</td>
</tr>

<!------------- GPIO ------------------>
<tr style="border-bottom: 2px solid lightgray;">
Expand Down Expand Up @@ -2283,6 +2298,7 @@ <h4><input type="checkbox" id="Category_GPIO_enabled" value="1" onclick='Update

WriteParameter(param, category, "Webhook", "Uri", true);
WriteParameter(param, category, "Webhook", "ApiKey", true);
WriteParameter(param, category, "Webhook", "UploadImg", false);
caco3 marked this conversation as resolved.
Show resolved Hide resolved

WriteParameter(param, category, "GPIO", "IO0", true);
WriteParameter(param, category, "GPIO", "IO1", true);
Expand Down Expand Up @@ -2364,6 +2380,7 @@ <h4><input type="checkbox" id="Category_GPIO_enabled" value="1" onclick='Update
category["MQTT"]["enabled"] = document.getElementById("Category_MQTT_enabled").checked;
category["InfluxDB"]["enabled"] = document.getElementById("Category_InfluxDB_enabled").checked;
category["InfluxDBv2"]["enabled"] = document.getElementById("Category_InfluxDBv2_enabled").checked;
category["Webhook"]["enabled"] = document.getElementById("Category_Webhook_enabled").checked;
category["GPIO"]["enabled"] = document.getElementById("Category_GPIO_enabled").checked;

ReadParameter(param, "TakeImage", "RawImagesLocation", true);
Expand Down Expand Up @@ -2446,6 +2463,7 @@ <h4><input type="checkbox" id="Category_GPIO_enabled" value="1" onclick='Update

ReadParameter(param, "Webhook", "Uri", true);
ReadParameter(param, "Webhook", "ApiKey", true);
ReadParameter(param, "Webhook", "UploadImg", false);

ReadParameter(param, "GPIO", "IO0", true);
ReadParameter(param, "GPIO", "IO1", true);
Expand Down
1 change: 1 addition & 0 deletions sd-card/html/readconfigparam.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ function ParseConfig() {
param[catname] = new Object();
ParamAddValue(param, catname, "Uri");
ParamAddValue(param, catname, "ApiKey");
ParamAddValue(param, catname, "UploadImg");

var catname = "GPIO";
category[catname] = new Object();
Expand Down