NTPClientPlus rework, minor improvements.

This commit is contained in:
2023-09-03 14:54:14 +02:00
parent 8598e019d4
commit f402d31be5
3 changed files with 247 additions and 287 deletions

View File

@@ -16,7 +16,7 @@ typedef enum
NTP_UPDATE_SUCCESS = 0,
NTP_UPDATE_DIFFTOOHIGH = 1,
NTP_UPDATE_TIME_INVALID = 2
} ntp_return_values;
} NtpReturnValue;
/**
* @brief Own NTP Client library for Arduino with code from:
@@ -27,64 +27,64 @@ typedef enum
class NTPClientPlus
{
public:
NTPClientPlus(UDP &udp, const char *poolServerName, int utcx, bool _swChange);
void setupNTPClient();
int updateNTP();
NTPClientPlus(UDP &udp, const char *pool_server_name, int utcx, bool sw_change);
bool is_leap_year(unsigned int year);
bool update_sw_change();
int get_hours_12() const;
int get_hours_24() const;
int get_minutes() const;
int get_month(int dayOfYear);
int get_seconds() const;
int update_ntp();
long get_time_offset();
String get_formatted_date();
String get_formatted_time() const;
unsigned int get_day_of_week();
unsigned int get_year();
unsigned long get_epoch_time() const;
unsigned long get_secs_since_1900() const;
void calc_date();
void end();
void setTimeOffset(int timeOffset);
void setPoolServerName(const char *poolServerName);
unsigned long getSecsSince1900() const;
unsigned long getEpochTime() const;
int getHours24() const;
int getHours12() const;
int getMinutes() const;
int getSeconds() const;
String getFormattedTime() const;
String getFormattedDate();
void calcDate();
unsigned int getDayOfWeek();
unsigned int getYear();
bool isLeapYear(unsigned int year);
int getMonth(int dayOfYear);
long getTimeOffset();
bool updateSWChange();
void set_pool_server_name(const char *pool_server_name);
void set_time_offset(int time_offset);
void setup_ntp_client();
private:
UDP *_udp;
bool _udpSetup = false;
bool _udp_setup = false;
bool _swChange = 1;
const char *_poolServerName = "pool.ntp.org"; // Default time server
bool _sw_change = 1;
const char *_pool_server_name = "pool.ntp.org"; // Default time server
int _utcx = 0;
IPAddress _poolServerIP;
long _timeOffset = 0;
IPAddress _pool_server_ip;
long _time_offset = 0;
unsigned int _port = NTP_DEFAULT_LOCAL_PORT;
unsigned long _updateInterval = 60000; // In ms
unsigned long _update_interval = 60000; // In ms
unsigned long _currentEpoc = 0; // In s
unsigned long _lastUpdate = 0; // In ms
unsigned long _secsSince1900 = 0; // seconds since 1. Januar 1900, 00:00:00
unsigned long _lastSecsSince1900 = 0;
unsigned int _dateYear = 0;
unsigned int _dateMonth = 0;
unsigned int _dateDay = 0;
unsigned int _dayOfWeek = 0;
unsigned int _date_day = 0;
unsigned int _date_month = 0;
unsigned int _date_year = 0;
unsigned int _day_of_week = 0;
unsigned long _current_epoc = 0; // In s
unsigned long _last_secs_since_1900 = 0;
unsigned long _last_update = 0; // In ms
unsigned long _secs_since_1900 = 0; // seconds since 1. Januar 1900, 00:00:00
unsigned char _packetBuffer[NTP_PACKET_SIZE] = {0};
void sendNTPPacket();
void setSummertime(bool summertime);
unsigned char _packet_buffer[NTP_PACKET_SIZE] = {0};
void send_ntp_packet();
void set_summertime(bool summertime);
static const unsigned long secondperday = 86400;
static const unsigned long secondperhour = 3600;
static const unsigned long secondperminute = 60;
static const unsigned long minuteperhour = 60;
static const unsigned long millisecondpersecond = 1000;
static const unsigned long milliseconds_per_second = 1000;
static const unsigned long minutes_per_hour = 60;
static const unsigned long seconds_per_day = 86400;
static const unsigned long seconds_per_hour = 3600;
static const unsigned long seconds_per_minute = 60;
// number of days in months
unsigned int daysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
unsigned int _days_per_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
};
void wait(unsigned long time);
void wait(unsigned long time_ms);
#endif /* NTPCLIENTPLUS_H */