From a876518db2bd436fcb7aa228f195634f2d50f047 Mon Sep 17 00:00:00 2001 From: Staszek Salik Date: Sat, 20 Oct 2018 15:30:00 +0200 Subject: [PATCH] Create event when NTP client starts and finishes update. --- NTPClient.cpp | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 87b7a53..f25441a 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -21,27 +21,42 @@ #include "NTPClient.h" -NTPClient::NTPClient(UDP& udp) { +NTPClient::NTPClient(UDP& udp) + :_updateStartHandler(NULL), + _updateEndHandler(NULL) +{ this->_udp = &udp; } -NTPClient::NTPClient(UDP& udp, long timeOffset) { +NTPClient::NTPClient(UDP& udp, long timeOffset) + :_updateStartHandler(NULL), + _updateEndHandler(NULL) +{ this->_udp = &udp; this->_timeOffset = timeOffset; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName) + :_updateStartHandler(NULL), + _updateEndHandler(NULL) +{ this->_udp = &udp; this->_poolServerName = poolServerName; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) + :_updateStartHandler(NULL), + _updateEndHandler(NULL) +{ this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) + :_updateStartHandler(NULL), + _updateEndHandler(NULL) +{ this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; @@ -65,6 +80,10 @@ bool NTPClient::forceUpdate() { Serial.println("Update from NTP Server"); #endif + if (this->_updateStartHandler) { + this->_updateStartHandler(); + } + this->sendNTPPacket(); // Wait till data is there or timeout... @@ -89,6 +108,10 @@ bool NTPClient::forceUpdate() { this->_currentEpoc = secsSince1900 - SEVENZYYEARS; + if (this->_updateEndHandler) { + this->_updateEndHandler(); + } + return true; }