|
@ -13,7 +13,8 @@ Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com> |
|
|
#include <WiFiClient.h>
|
|
|
#include <WiFiClient.h>
|
|
|
#include <Ticker.h>
|
|
|
#include <Ticker.h>
|
|
|
|
|
|
|
|
|
Ticker _ntp_delay; |
|
|
|
|
|
|
|
|
bool _ntp_update = false; |
|
|
|
|
|
bool _ntp_configure = false; |
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// NTP
|
|
|
// NTP
|
|
@ -31,21 +32,63 @@ void _ntpWebSocketOnSend(JsonObject& root) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void _ntpUpdate() { |
|
|
void _ntpUpdate() { |
|
|
|
|
|
|
|
|
|
|
|
_ntp_update = false; |
|
|
|
|
|
|
|
|
#if WEB_SUPPORT
|
|
|
#if WEB_SUPPORT
|
|
|
wsSend(_ntpWebSocketOnSend); |
|
|
wsSend(_ntpWebSocketOnSend); |
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
|
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str()); |
|
|
DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str()); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void _ntpConfigure() { |
|
|
void _ntpConfigure() { |
|
|
|
|
|
|
|
|
|
|
|
_ntp_configure = false; |
|
|
|
|
|
|
|
|
|
|
|
int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(); |
|
|
|
|
|
int sign = offset > 0 ? 1 : -1; |
|
|
|
|
|
offset = abs(offset); |
|
|
|
|
|
|
|
|
NTP.begin( |
|
|
NTP.begin( |
|
|
getSetting("ntpServer1", NTP_SERVER), |
|
|
|
|
|
getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(), |
|
|
|
|
|
getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1 |
|
|
|
|
|
|
|
|
getSetting("ntpServer", 1, NTP_SERVER).c_str(), |
|
|
|
|
|
sign * (offset / 60), |
|
|
|
|
|
getSetting("ntpDST", NTP_DAY_LIGHT).toInt() == 1, |
|
|
|
|
|
sign * (offset % 60) |
|
|
); |
|
|
); |
|
|
if (getSetting("ntpServer2")) NTP.setNtpServerName(getSetting("ntpServer2"), 1); |
|
|
|
|
|
if (getSetting("ntpServer3")) NTP.setNtpServerName(getSetting("ntpServer3"), 2); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (hasSetting("ntpServer", 2)) NTP.setNtpServerName(getSetting("ntpServer", 2).c_str(), 1); |
|
|
|
|
|
if (hasSetting("ntpServer", 3)) NTP.setNtpServerName(getSetting("ntpServer", 3).c_str(), 2); |
|
|
NTP.setInterval(NTP_UPDATE_INTERVAL); |
|
|
NTP.setInterval(NTP_UPDATE_INTERVAL); |
|
|
|
|
|
|
|
|
|
|
|
_ntp_update = true; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void _ntpLoop() { |
|
|
|
|
|
|
|
|
|
|
|
if (_ntp_configure) _ntpConfigure(); |
|
|
|
|
|
if (_ntp_update) _ntpUpdate(); |
|
|
|
|
|
|
|
|
|
|
|
now(); |
|
|
|
|
|
|
|
|
|
|
|
#if BROKER_SUPPORT
|
|
|
|
|
|
static unsigned char last_minute = 60; |
|
|
|
|
|
if (ntpSynced() && (minute() != last_minute)) { |
|
|
|
|
|
last_minute = minute(); |
|
|
|
|
|
brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str()); |
|
|
|
|
|
} |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void _ntpBackwards() { |
|
|
|
|
|
int offset = getSetting("ntpOffset", NTP_TIME_OFFSET).toInt(); |
|
|
|
|
|
if (-30 < offset && offset < 30) { |
|
|
|
|
|
offset *= 60; |
|
|
|
|
|
setSetting("ntpOffset", offset); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
@ -65,8 +108,12 @@ String ntpDateTime() { |
|
|
return String(buffer); |
|
|
return String(buffer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
void ntpSetup() { |
|
|
void ntpSetup() { |
|
|
|
|
|
|
|
|
|
|
|
_ntpBackwards(); |
|
|
|
|
|
|
|
|
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) { |
|
|
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) { |
|
|
if (error) { |
|
|
if (error) { |
|
|
#if WEB_SUPPORT
|
|
|
#if WEB_SUPPORT
|
|
@ -78,33 +125,21 @@ void ntpSetup() { |
|
|
DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n")); |
|
|
DEBUG_MSG_P(PSTR("[NTP] Error: Invalid NTP server address\n")); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
_ntp_delay.once_ms(100, _ntpUpdate); |
|
|
|
|
|
|
|
|
_ntp_update = true; |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
_ntpConfigure(); |
|
|
|
|
|
|
|
|
wifiRegister([](justwifi_messages_t code, char * parameter) { |
|
|
|
|
|
if (code == MESSAGE_CONNECTED) _ntp_configure = true; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
#if WEB_SUPPORT
|
|
|
#if WEB_SUPPORT
|
|
|
wsOnSendRegister(_ntpWebSocketOnSend); |
|
|
wsOnSendRegister(_ntpWebSocketOnSend); |
|
|
wsOnAfterParseRegister(_ntpConfigure); |
|
|
|
|
|
|
|
|
wsOnAfterParseRegister([]() { _ntp_configure = true; }); |
|
|
#endif
|
|
|
#endif
|
|
|
|
|
|
|
|
|
// Register loop
|
|
|
// Register loop
|
|
|
espurnaRegisterLoop(ntpLoop); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ntpLoop() { |
|
|
|
|
|
|
|
|
|
|
|
now(); |
|
|
|
|
|
|
|
|
|
|
|
#if BROKER_SUPPORT
|
|
|
|
|
|
static unsigned char last_minute = 60; |
|
|
|
|
|
if (ntpSynced() && (minute() != last_minute)) { |
|
|
|
|
|
last_minute = minute(); |
|
|
|
|
|
brokerPublish(MQTT_TOPIC_DATETIME, ntpDateTime().c_str()); |
|
|
|
|
|
} |
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
espurnaRegisterLoop(_ntpLoop); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|