mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2026-01-27 12:50:39 +03:00
test1
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*)
|
||||
|
||||
idf_component_register(SRCS ${app_sources}
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_http_client jomjol_logfile jomjol_flowcontroll json)
|
||||
INCLUDE_DIRS "." "../../include"
|
||||
REQUIRES esp_http_client jomjol_logfile jomjol_flowcontroll json jomjol_helper)
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#ifdef ENABLE_WEBHOOK
|
||||
#include "defines.h"
|
||||
|
||||
#include "interface_webhook.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
@@ -6,11 +7,9 @@
|
||||
#include "ClassLogFile.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "time_sntp.h"
|
||||
#include "../../include/defines.h"
|
||||
#include <cJSON.h>
|
||||
#include <ClassFlowDefineTypes.h>
|
||||
|
||||
|
||||
static const char *TAG = "WEBHOOK";
|
||||
|
||||
std::string _webhookURI;
|
||||
@@ -26,7 +25,7 @@ void WebhookInit(std::string _uri, std::string _apiKey)
|
||||
_lastTimestamp = 0L;
|
||||
}
|
||||
|
||||
bool WebhookPublish(std::vector<NumberPost*>* numbers)
|
||||
bool WebhookPublish(std::vector<NumberPost *> *numbers)
|
||||
{
|
||||
bool numbersWithError = false;
|
||||
cJSON *jsonArray = cJSON_CreateArray();
|
||||
@@ -36,7 +35,7 @@ bool WebhookPublish(std::vector<NumberPost*>* numbers)
|
||||
string timezw = "";
|
||||
char buffer[80];
|
||||
time_t &lastPreValue = (*numbers)[i]->timeStampLastPreValue;
|
||||
struct tm* timeinfo = localtime(&lastPreValue);
|
||||
struct tm *timeinfo = localtime(&lastPreValue);
|
||||
_lastTimestamp = static_cast<long>(lastPreValue);
|
||||
strftime(buffer, 80, PREVALUE_TIME_FORMAT_OUTPUT, timeinfo);
|
||||
timezw = std::string(buffer);
|
||||
@@ -51,10 +50,11 @@ bool WebhookPublish(std::vector<NumberPost*>* numbers)
|
||||
cJSON_AddStringToObject(json, "rate", (*numbers)[i]->ReturnRateValue.c_str());
|
||||
cJSON_AddStringToObject(json, "changeAbsolute", (*numbers)[i]->ReturnChangeAbsolute.c_str());
|
||||
cJSON_AddStringToObject(json, "error", (*numbers)[i]->ErrorMessageText.c_str());
|
||||
|
||||
|
||||
cJSON_AddItemToArray(jsonArray, json);
|
||||
|
||||
if ((*numbers)[i]->ErrorMessage) {
|
||||
if ((*numbers)[i]->ErrorMessage)
|
||||
{
|
||||
numbersWithError = true;
|
||||
}
|
||||
}
|
||||
@@ -71,8 +71,7 @@ bool WebhookPublish(std::vector<NumberPost*>* numbers)
|
||||
.method = HTTP_METHOD_POST,
|
||||
.event_handler = http_event_handler,
|
||||
.buffer_size = MAX_HTTP_OUTPUT_BUFFER,
|
||||
.user_data = response_buffer
|
||||
};
|
||||
.user_data = response_buffer};
|
||||
|
||||
esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
|
||||
|
||||
@@ -83,21 +82,25 @@ bool WebhookPublish(std::vector<NumberPost*>* numbers)
|
||||
|
||||
esp_err_t err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
|
||||
|
||||
if(err == ESP_OK) {
|
||||
if (err == ESP_OK)
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP request was performed");
|
||||
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 {
|
||||
}
|
||||
else
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP request failed");
|
||||
}
|
||||
}
|
||||
|
||||
esp_http_client_cleanup(http_client);
|
||||
cJSON_Delete(jsonArray);
|
||||
free(jsonString);
|
||||
return numbersWithError;
|
||||
return numbersWithError;
|
||||
}
|
||||
|
||||
void WebhookUploadPic(ImageData *Img) {
|
||||
void WebhookUploadPic(ImageData *Img)
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Starting WebhookUploadPic");
|
||||
|
||||
std::string fullURI = _webhookURI + "?timestamp=" + std::to_string(_lastTimestamp);
|
||||
@@ -108,8 +111,7 @@ void WebhookUploadPic(ImageData *Img) {
|
||||
.method = HTTP_METHOD_PUT,
|
||||
.event_handler = http_event_handler,
|
||||
.buffer_size = MAX_HTTP_OUTPUT_BUFFER,
|
||||
.user_data = response_buffer
|
||||
};
|
||||
.user_data = response_buffer};
|
||||
|
||||
esp_http_client_handle_t http_client = esp_http_client_init(&http_config);
|
||||
|
||||
@@ -120,11 +122,14 @@ void WebhookUploadPic(ImageData *Img) {
|
||||
|
||||
err = ESP_ERROR_CHECK_WITHOUT_ABORT(esp_http_client_perform(http_client));
|
||||
|
||||
if (err == ESP_OK) {
|
||||
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 {
|
||||
}
|
||||
else
|
||||
{
|
||||
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "HTTP PUT request failed");
|
||||
}
|
||||
|
||||
@@ -133,38 +138,35 @@ void WebhookUploadPic(ImageData *Img) {
|
||||
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)
|
||||
switch (evt->event_id)
|
||||
{
|
||||
case HTTP_EVENT_ERROR:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
|
||||
break;
|
||||
case HTTP_EVENT_ON_CONNECTED:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client connected");
|
||||
ESP_LOGI(TAG, "HTTP Client Connected");
|
||||
break;
|
||||
case HTTP_EVENT_HEADERS_SENT:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
|
||||
break;
|
||||
case HTTP_EVENT_ON_HEADER:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
|
||||
break;
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
|
||||
break;
|
||||
case HTTP_EVENT_REDIRECT:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Redirect");
|
||||
break;
|
||||
case HTTP_EVENT_ERROR:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Error encountered");
|
||||
break;
|
||||
case HTTP_EVENT_ON_CONNECTED:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client connected");
|
||||
ESP_LOGI(TAG, "HTTP Client Connected");
|
||||
break;
|
||||
case HTTP_EVENT_HEADERS_SENT:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client sent all request headers");
|
||||
break;
|
||||
case HTTP_EVENT_ON_HEADER:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Header: key=" + std::string(evt->header_key) + ", value=" + std::string(evt->header_value));
|
||||
break;
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client data recevied: len=" + std::to_string(evt->data_len));
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client finished");
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Client Disconnected");
|
||||
break;
|
||||
case HTTP_EVENT_REDIRECT:
|
||||
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "HTTP Redirect");
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
#endif //ENABLE_WEBHOOK
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#ifdef ENABLE_WEBHOOK
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef INTERFACE_WEBHOOK_H
|
||||
#define INTERFACE_WEBHOOK_H
|
||||
|
||||
@@ -10,8 +9,7 @@
|
||||
#include <ClassFlowDefineTypes.h>
|
||||
|
||||
void WebhookInit(std::string _webhookURI, std::string _apiKey);
|
||||
bool WebhookPublish(std::vector<NumberPost*>* numbers);
|
||||
bool WebhookPublish(std::vector<NumberPost *> *numbers);
|
||||
void WebhookUploadPic(ImageData *Img);
|
||||
|
||||
#endif //INTERFACE_WEBHOOK_H
|
||||
#endif //ENABLE_WEBHOOK
|
||||
#endif // INTERFACE_WEBHOOK_H
|
||||
|
||||
Reference in New Issue
Block a user