mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Fix time/automation (cron) wdt crash when time jumps ahead too much (#3844)
This commit is contained in:
parent
05edfd0e82
commit
584b722e7e
1 changed files with 8 additions and 1 deletions
|
@ -6,6 +6,8 @@ namespace esphome {
|
|||
namespace time {
|
||||
|
||||
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_minute(uint8_t minute) { this->minutes_[minute] = true; }
|
||||
|
@ -23,12 +25,17 @@ void CronTrigger::loop() {
|
|||
return;
|
||||
|
||||
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
|
||||
ESP_LOGW(TAG, "Time has jumped back!");
|
||||
} else if (*this->last_check_ >= time) {
|
||||
// already handled this one
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue