format check in ntp client

pull/293/head
Peter Buchegger 2023-05-17 21:34:25 +02:00
rodzic 9d6ce8dfac
commit 74e01a76a7
3 zmienionych plików z 124 dodań i 122 usunięć

Wyświetl plik

@ -44,7 +44,7 @@ jobs:
- 'lib/BoardFinder' - 'lib/BoardFinder'
- 'lib/ConfigurationManagement' - 'lib/ConfigurationManagement'
#- 'lib/Display' #- 'lib/Display'
#- 'lib/NTPClient' - 'lib/NTPClient'
- 'lib/PowerManagement' - 'lib/PowerManagement'
- 'lib/System' - 'lib/System'
#- 'lib/TimeLib' #- 'lib/TimeLib'

Wyświetl plik

@ -28,7 +28,7 @@ NTPClient::NTPClient(long timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
} }
NTPClient::NTPClient(const char* poolServerName) { NTPClient::NTPClient(const char *poolServerName) {
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }
@ -37,18 +37,18 @@ NTPClient::NTPClient(IPAddress poolServerIP) {
this->_poolServerName = NULL; this->_poolServerName = NULL;
} }
NTPClient::NTPClient(const char* poolServerName, long timeOffset) { NTPClient::NTPClient(const char *poolServerName, long timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }
NTPClient::NTPClient(IPAddress poolServerIP, long timeOffset){ NTPClient::NTPClient(IPAddress poolServerIP, long timeOffset) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerIP = poolServerIP; this->_poolServerIP = poolServerIP;
this->_poolServerName = NULL; this->_poolServerName = NULL;
} }
NTPClient::NTPClient(const char* poolServerName, long timeOffset, unsigned long updateInterval) { NTPClient::NTPClient(const char *poolServerName, long timeOffset, unsigned long updateInterval) {
this->_timeOffset = timeOffset; this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
this->_updateInterval = updateInterval; this->_updateInterval = updateInterval;
@ -74,12 +74,12 @@ void NTPClient::begin(unsigned int port) {
} }
bool NTPClient::forceUpdate() { bool NTPClient::forceUpdate() {
#ifdef DEBUG_NTPClient #ifdef DEBUG_NTPClient
Serial.println("Update from NTP Server"); Serial.println("Update from NTP Server");
#endif #endif
// flush any existing packets // flush any existing packets
while(this->_udp.parsePacket() != 0) while (this->_udp.parsePacket() != 0)
this->_udp.flush(); this->_udp.flush();
this->sendNTPPacket(); this->sendNTPPacket();
@ -88,9 +88,10 @@ bool NTPClient::forceUpdate() {
byte timeout = 0; byte timeout = 0;
int cb = 0; int cb = 0;
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)
return false; // timeout after 1000 ms
timeout++; timeout++;
} while (cb == 0); } while (cb == 0);
@ -112,7 +113,8 @@ bool NTPClient::forceUpdate() {
bool NTPClient::update() { bool NTPClient::update() {
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval
|| this->_lastUpdate == 0) { // Update if there was no update yet. || this->_lastUpdate == 0) { // Update if there was no update yet.
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 false; // return false if update does not occur
@ -125,7 +127,7 @@ unsigned long NTPClient::getEpochTime() const {
} }
int NTPClient::getDay() const { int NTPClient::getDay() const {
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday return (((this->getEpochTime() / 86400L) + 4) % 7); // 0 is Sunday
} }
int NTPClient::getHours() const { int NTPClient::getHours() const {
return ((this->getEpochTime() % 86400L) / 3600); return ((this->getEpochTime() % 86400L) / 3600);
@ -165,7 +167,7 @@ void NTPClient::setUpdateInterval(unsigned long updateInterval) {
this->_updateInterval = updateInterval; this->_updateInterval = updateInterval;
} }
void NTPClient::setPoolServerName(const char* poolServerName) { void NTPClient::setPoolServerName(const char *poolServerName) {
this->_poolServerName = poolServerName; this->_poolServerName = poolServerName;
} }

Wyświetl plik

@ -8,11 +8,11 @@
#define NTP_DEFAULT_LOCAL_PORT 1337 #define NTP_DEFAULT_LOCAL_PORT 1337
class NTPClient { class NTPClient {
private: private:
WiFiUDP _udp; WiFiUDP _udp;
bool _udpSetup = false; bool _udpSetup = false;
const char* _poolServerName = "pool.ntp.org"; // Default time server const char *_poolServerName = "pool.ntp.org"; // Default time server
IPAddress _poolServerIP; IPAddress _poolServerIP;
unsigned int _port = NTP_DEFAULT_LOCAL_PORT; unsigned int _port = NTP_DEFAULT_LOCAL_PORT;
long _timeOffset = 0; long _timeOffset = 0;
@ -26,12 +26,12 @@ class NTPClient {
void sendNTPPacket(); void sendNTPPacket();
public: public:
NTPClient(); NTPClient();
explicit NTPClient(long timeOffset); explicit NTPClient(long timeOffset);
explicit NTPClient(const char* poolServerName); explicit NTPClient(const char *poolServerName);
NTPClient(const char* poolServerName, long timeOffset); NTPClient(const char *poolServerName, long timeOffset);
NTPClient(const char* poolServerName, long timeOffset, unsigned long updateInterval); NTPClient(const char *poolServerName, long timeOffset, unsigned long updateInterval);
explicit NTPClient(IPAddress poolServerIP); explicit NTPClient(IPAddress poolServerIP);
NTPClient(IPAddress poolServerIP, long timeOffset); NTPClient(IPAddress poolServerIP, long timeOffset);
NTPClient(IPAddress poolServerIP, long timeOffset, unsigned long updateInterval); NTPClient(IPAddress poolServerIP, long timeOffset, unsigned long updateInterval);
@ -41,7 +41,7 @@ class NTPClient {
* *
* @param poolServerName * @param poolServerName
*/ */
void setPoolServerName(const char* poolServerName); void setPoolServerName(const char *poolServerName);
/** /**
* Set random local port * Set random local port