|
|
@@ -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<NumberPost*>* numbers)
|
|
|
+bool WebhookPublish(std::vector<NumberPost*>* 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<long>(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<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);
|
|
|
@@ -79,13 +89,51 @@ 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");
|
|
|
+
|
|
|
+ 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)
|