update() return last sync state

As the update() function return false both if the forceUpdate fail, and also if the timeout isn't expired, we are not able to know if the last ntp update was fine.
Wouldn't be better to return true if the last sync was gone ok, and false if wasn't good?
This commit is contained in:
masterx1981 2020-12-30 11:09:08 +01:00
parent b5a611a270
commit be86f1b324
2 changed files with 7 additions and 2 deletions

View File

@ -98,11 +98,15 @@ bool NTPClient::forceUpdate() {
do { do {
delay ( 10 ); delay ( 10 );
cb = this->_udp->parsePacket(); cb = this->_udp->parsePacket();
if (timeout > 100) return false; // timeout after 1000 ms if (timeout > 100) {
this->_lastUpdateOk = false;
return false; // timeout after 1000 ms
}
timeout++; timeout++;
} while (cb == 0); } while (cb == 0);
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
this->_lastUpdateOk = true;
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
@ -123,7 +127,7 @@ bool NTPClient::update() {
if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // setup the UDP client if needed if (!this->_udpSetup || this->_port != NTP_DEFAULT_LOCAL_PORT) this->begin(this->_port); // setup the UDP client if needed
return this->forceUpdate(); return this->forceUpdate();
} }
return false; // return false if update does not occur return this->_lastUpdateOk; // return last sync state
} }
unsigned long NTPClient::getEpochTime() const { unsigned long NTPClient::getEpochTime() const {

View File

@ -22,6 +22,7 @@ class NTPClient {
unsigned long _currentEpoc = 0; // In s unsigned long _currentEpoc = 0; // In s
unsigned long _lastUpdate = 0; // In ms unsigned long _lastUpdate = 0; // In ms
bool _lastUpdateOk = false; //
byte _packetBuffer[NTP_PACKET_SIZE]; byte _packetBuffer[NTP_PACKET_SIZE];