Add timeout while waiting for answer

This commit is contained in:
sqozz 2024-09-17 02:14:16 +02:00
parent 189fd539f1
commit f72e0ea591

View file

@ -9,6 +9,7 @@ namespace esphome {
namespace tw7100 {
bool waiting_for_answer = false;
unsigned long waiting_for_answer_since = 0;
std::deque<std::string> cmd_queue;
unsigned long last_set_cmd_timestamp;
@ -138,10 +139,15 @@ void tw7100Component::loop() {
flush();
if (cmd.find("?") != std::string::npos) { // only wait for response on CMD? requests
waiting_for_answer = true;
waiting_for_answer_since = millis();
} else {
last_set_cmd_timestamp = millis();
}
}
if (waiting_for_answer && (millis() - waiting_for_answer_since > 1000)) {
ESP_LOGE(TAG, "no response for last request since 1000ms, timing out!");
waiting_for_answer = false;
}
} else { // response found, handle eventual errors
ESP_LOGV(TAG, "buffer content: %s", response.c_str());
std::pair<std::string, std::string> parsed_response = parse_response(response);