mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 15:08:10 +01:00
http_request: add request duration logging (#4272)
This commit is contained in:
parent
74556b28a8
commit
c16c0b11cb
3 changed files with 15 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -66,6 +66,9 @@ void HttpRequestComponent::send(const std::vector<HttpRequestResponseTrigger *>
|
|||
}
|
||||
|
||||
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<HttpRequestResponseTrigger *>
|
|||
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
|
||||
|
|
|
@ -138,9 +138,9 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
|
|||
std::vector<HttpRequestResponseTrigger *> response_triggers_;
|
||||
};
|
||||
|
||||
class HttpRequestResponseTrigger : public Trigger<int> {
|
||||
class HttpRequestResponseTrigger : public Trigger<int, uint32_t> {
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue