Move printing out to loop

This commit is contained in:
Anton Sergunov 2024-05-09 02:44:40 +00:00
parent c13dc929c9
commit 87659ef22b
2 changed files with 18 additions and 15 deletions

View file

@ -53,21 +53,7 @@ void SNTPComponent::setup() {
#endif
g_sync_callback = [this](struct timeval *tv) {
static struct timeval time_val;
switch (sntp_get_sync_status()) {
case SNTP_SYNC_STATUS_RESET:
ESP_LOGD(TAG, "Time sync reset");
break;
case SNTP_SYNC_STATUS_COMPLETED:
ESP_LOGD(TAG, "Time sync completed. Delta is %ds", tv->tv_sec - time_val.tv_sec);
this->time_sync_callback_.call();
break;
case SNTP_SYNC_STATUS_IN_PROGRESS:
ESP_LOGD(TAG, "Time sync in progress");
break;
}
if (tv)
time_val = *tv;
this->callback_args_.push_back({tv ? *tv : {}, sntp_get_sync_status()});
};
ESP_LOGD(TAG, "Set notification callback");
sntp_set_time_sync_notification_cb(sntp_sync_time_cb);
@ -93,6 +79,22 @@ void SNTPComponent::update() {
#endif
}
void SNTPComponent::loop() {
for (const auto &item : this->callback_args_) {
switch (item.second) {
case SNTP_SYNC_STATUS_RESET:
ESP_LOGD(TAG, "Time sync reset");
break;
case SNTP_SYNC_STATUS_COMPLETED:
ESP_LOGD(TAG, "Time sync completed");
this->time_sync_callback_.call();
break;
case SNTP_SYNC_STATUS_IN_PROGRESS:
ESP_LOGD(TAG, "Time sync in progress");
break;
}
}
callback_args_.clear();
if (this->has_time_)
return;

View file

@ -32,6 +32,7 @@ class SNTPComponent : public time::RealTimeClock {
std::string server_2_;
std::string server_3_;
bool has_time_{false};
std::vector<std::pair<optional<struct timeval>, sntp_sync_status_t>> callback_args_;
};
} // namespace sntp