NTP disabled if no host set

develop
Kenneth Barbour 2020-07-04 08:27:37 -04:00
parent 7e748d24e9
commit ee75db5a4d
1 changed files with 8 additions and 6 deletions

View File

@ -9,7 +9,7 @@
#define DEFAULT_NTP_HOST "time.nist.gov"
#define NTP_PACKET_SIZE 48
enum state { INIT, SENT, IDLE, NO_HOST} ntp_state;
enum state { INIT, SENT, IDLE, NO_HOST, DISABLED} ntp_state;
byte packet_buffer[NTP_PACKET_SIZE]; // NTP timestamp is in the first 48 bytes
WiFiUDP udp;
unsigned long time_offset = 0;
@ -19,11 +19,7 @@ IPAddress host;
const char* ntp_get_hostname()
{
const char* host_in_cfg = (const char*) Config._data.ntp_host;
if (host_in_cfg[0] == '\0') {
return DEFAULT_NTP_HOST;
}
return host_in_cfg;
return (const char*) Config._data.ntp_host;
}
void uweather_time_init()
@ -38,6 +34,8 @@ void uweather_time_init()
void uweather_time_handle()
{
switch (ntp_state) {
case state::DISABLED:
return;
case state::INIT:
{
if (!WiFi.isConnected())
@ -45,6 +43,10 @@ void uweather_time_handle()
// resolve ntp hostname
const char* hostname = ntp_get_hostname();
if (hostname[0] == '\0') {
ntp_state = state::DISABLED;
return;
}
if (!WiFi.hostByName(hostname, host)) {
ntp_state = state::NO_HOST;
Serial.printf("Unable to resolve ntp host: %s\n", hostname);