From c16c0b11cb5c27159e651429139c1f02fff78d68 Mon Sep 17 00:00:00 2001 From: Stas Date: Tue, 10 Jan 2023 07:40:42 +0300 Subject: [PATCH] http_request: add request duration logging (#4272) --- esphome/components/http_request/__init__.py | 4 +++- esphome/components/http_request/http_request.cpp | 15 ++++++++++----- esphome/components/http_request/http_request.h | 4 ++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/esphome/components/http_request/__init__.py b/esphome/components/http_request/__init__.py index c8c0ca5369..0c3e249512 100644 --- a/esphome/components/http_request/__init__.py +++ b/esphome/components/http_request/__init__.py @@ -195,6 +195,8 @@ async def http_request_action_to_code(config, action_id, template_arg, args): for conf in config.get(CONF_ON_RESPONSE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) cg.add(var.register_response_trigger(trigger)) - await automation.build_automation(trigger, [(int, "status_code")], conf) + await automation.build_automation( + trigger, [(int, "status_code"), (cg.uint32, "duration_ms")], conf + ) return var diff --git a/esphome/components/http_request/http_request.cpp b/esphome/components/http_request/http_request.cpp index 4e1cfe94b3..46894a9afd 100644 --- a/esphome/components/http_request/http_request.cpp +++ b/esphome/components/http_request/http_request.cpp @@ -66,6 +66,9 @@ void HttpRequestComponent::send(const std::vector } this->client_.setTimeout(this->timeout_); +#if defined(USE_ESP32) + this->client_.setConnectTimeout(this->timeout_); +#endif if (this->useragent_ != nullptr) { this->client_.setUserAgent(this->useragent_); } @@ -73,25 +76,27 @@ void HttpRequestComponent::send(const std::vector this->client_.addHeader(header.name, header.value, false, true); } + uint32_t start_time = millis(); int http_code = this->client_.sendRequest(this->method_, this->body_.c_str()); + uint32_t duration = millis() - start_time; for (auto *trigger : response_triggers) - trigger->process(http_code); + trigger->process(http_code, duration); if (http_code < 0) { - ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", this->url_.c_str(), - HTTPClient::errorToString(http_code).c_str()); + ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s; Duration: %u ms", this->url_.c_str(), + HTTPClient::errorToString(http_code).c_str(), duration); this->status_set_warning(); return; } if (http_code < 200 || http_code >= 300) { - ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Code: %d", this->url_.c_str(), http_code); + ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Code: %d; Duration: %u ms", this->url_.c_str(), http_code, duration); this->status_set_warning(); return; } this->status_clear_warning(); - ESP_LOGD(TAG, "HTTP Request completed; URL: %s; Code: %d", this->url_.c_str(), http_code); + ESP_LOGD(TAG, "HTTP Request completed; URL: %s; Code: %d; Duration: %u ms", this->url_.c_str(), http_code, duration); } #ifdef USE_ESP8266 diff --git a/esphome/components/http_request/http_request.h b/esphome/components/http_request/http_request.h index eab3045fdc..3b15a6c89a 100644 --- a/esphome/components/http_request/http_request.h +++ b/esphome/components/http_request/http_request.h @@ -138,9 +138,9 @@ template class HttpRequestSendAction : public Action { std::vector response_triggers_; }; -class HttpRequestResponseTrigger : public Trigger { +class HttpRequestResponseTrigger : public Trigger { public: - void process(int status_code) { this->trigger(status_code); } + void process(int status_code, uint32_t duration_ms) { this->trigger(status_code, duration_ms); } }; } // namespace http_request