Fix time/automation (cron) wdt crash when time jumps ahead too much (#3844)

This commit is contained in:
Javier Peletier 2022-10-05 03:52:45 +02:00 committed by GitHub
parent 05edfd0e82
commit 584b722e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,8 @@ namespace esphome {
namespace time { namespace time {
static const char *const TAG = "automation"; static const char *const TAG = "automation";
static const int MAX_TIMESTAMP_DRIFT = 900; // how far can the clock drift before we consider
// there has been a drastic time synchronization
void CronTrigger::add_second(uint8_t second) { this->seconds_[second] = true; } void CronTrigger::add_second(uint8_t second) { this->seconds_[second] = true; }
void CronTrigger::add_minute(uint8_t minute) { this->minutes_[minute] = true; } void CronTrigger::add_minute(uint8_t minute) { this->minutes_[minute] = true; }
@ -23,12 +25,17 @@ void CronTrigger::loop() {
return; return;
if (this->last_check_.has_value()) { if (this->last_check_.has_value()) {
if (*this->last_check_ > time && this->last_check_->timestamp - time.timestamp > 900) { if (*this->last_check_ > time && this->last_check_->timestamp - time.timestamp > MAX_TIMESTAMP_DRIFT) {
// We went back in time (a lot), probably caused by time synchronization // We went back in time (a lot), probably caused by time synchronization
ESP_LOGW(TAG, "Time has jumped back!"); ESP_LOGW(TAG, "Time has jumped back!");
} else if (*this->last_check_ >= time) { } else if (*this->last_check_ >= time) {
// already handled this one // already handled this one
return; return;
} else if (time > *this->last_check_ && time.timestamp - this->last_check_->timestamp > MAX_TIMESTAMP_DRIFT) {
// We went ahead in time (a lot), probably caused by time synchronization
ESP_LOGW(TAG, "Time has jumped ahead!");
this->last_check_ = time;
return;
} }
while (true) { while (true) {