2019-04-17 12:06:00 +02:00
|
|
|
#include "sntp_component.h"
|
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP32
|
2019-06-28 11:29:37 +02:00
|
|
|
#include "lwip/apps/sntp.h"
|
2021-10-28 20:57:39 +02:00
|
|
|
#ifdef USE_ESP_IDF
|
|
|
|
#include "esp_sntp.h"
|
|
|
|
#endif
|
2019-04-17 12:06:00 +02:00
|
|
|
#endif
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP8266
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "sntp.h"
|
|
|
|
#endif
|
|
|
|
|
2021-08-09 22:43:18 +02:00
|
|
|
// Yes, the server names are leaked, but that's fine.
|
|
|
|
#ifdef CLANG_TIDY
|
|
|
|
#define strdup(x) (const_cast<char *>(x))
|
|
|
|
#endif
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
namespace esphome {
|
|
|
|
namespace sntp {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "sntp";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
void SNTPComponent::setup() {
|
|
|
|
ESP_LOGCONFIG(TAG, "Setting up SNTP...");
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP32
|
2019-04-17 12:06:00 +02:00
|
|
|
if (sntp_enabled()) {
|
|
|
|
sntp_stop();
|
|
|
|
}
|
|
|
|
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
|
|
|
#endif
|
2021-09-20 11:47:51 +02:00
|
|
|
#ifdef USE_ESP8266
|
2019-04-17 12:06:00 +02:00
|
|
|
sntp_stop();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sntp_setservername(0, strdup(this->server_1_.c_str()));
|
|
|
|
if (!this->server_2_.empty()) {
|
|
|
|
sntp_setservername(1, strdup(this->server_2_.c_str()));
|
|
|
|
}
|
|
|
|
if (!this->server_3_.empty()) {
|
|
|
|
sntp_setservername(2, strdup(this->server_3_.c_str()));
|
|
|
|
}
|
2021-10-28 20:57:39 +02:00
|
|
|
#ifdef USE_ESP_IDF
|
|
|
|
sntp_set_sync_interval(this->get_update_interval());
|
|
|
|
#endif
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
sntp_init();
|
|
|
|
}
|
|
|
|
void SNTPComponent::dump_config() {
|
|
|
|
ESP_LOGCONFIG(TAG, "SNTP Time:");
|
|
|
|
ESP_LOGCONFIG(TAG, " Server 1: '%s'", this->server_1_.c_str());
|
|
|
|
ESP_LOGCONFIG(TAG, " Server 2: '%s'", this->server_2_.c_str());
|
|
|
|
ESP_LOGCONFIG(TAG, " Server 3: '%s'", this->server_3_.c_str());
|
|
|
|
ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str());
|
|
|
|
}
|
2021-10-28 20:57:39 +02:00
|
|
|
void SNTPComponent::update() {
|
|
|
|
#ifndef USE_ESP_IDF
|
|
|
|
// force resync
|
|
|
|
if (sntp_enabled()) {
|
|
|
|
sntp_stop();
|
|
|
|
this->has_time_ = false;
|
|
|
|
sntp_init();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2019-04-17 12:06:00 +02:00
|
|
|
void SNTPComponent::loop() {
|
|
|
|
if (this->has_time_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto time = this->now();
|
|
|
|
if (!time.is_valid())
|
|
|
|
return;
|
|
|
|
|
2021-10-28 20:57:39 +02:00
|
|
|
ESP_LOGD(TAG, "Synchronized time: %d-%d-%d %d:%02d:%02d", time.year, time.month, time.day_of_month, time.hour,
|
2021-08-23 10:43:54 +02:00
|
|
|
time.minute, time.second);
|
2021-01-18 13:34:50 +01:00
|
|
|
this->time_sync_callback_.call();
|
2019-04-17 12:06:00 +02:00
|
|
|
this->has_time_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace sntp
|
|
|
|
} // namespace esphome
|