Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: standardize whitespace and formatting #524

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 72 additions & 79 deletions src/Ntp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,127 +19,120 @@ byte NtpClient::NTPpacket[NTP_PACKET_SIZE];

void ICACHE_FLASH_ATTR NtpClient::Ntp(const char *server, int8_t tz, time_t syncSecs)
{
TimeServerName = strdup(server);
timezone = tz;
syncInterval = syncSecs;
WiFi.hostByName(TimeServerName, timeServer);
setSyncProvider(getNtpTime);
setSyncInterval(syncInterval);
TimeServerName = strdup(server);
timezone = tz;
syncInterval = syncSecs;
WiFi.hostByName(TimeServerName, timeServer);
setSyncProvider(getNtpTime);
setSyncInterval(syncInterval);
}

ICACHE_FLASH_ATTR NtpClient::~NtpClient()
{
udpListener.close();
udpListener.close();
}

// send an NTP request to the time server at the given address
time_t ICACHE_FLASH_ATTR NtpClient::getNtpTime()
{
memset(NTPpacket, 0, sizeof(NTPpacket));
NTPpacket[0] = 0b11100011;
NTPpacket[1] = 0;
NTPpacket[2] = 6;
NTPpacket[3] = 0xEC;
NTPpacket[12] = 49;
NTPpacket[13] = 0x4E;
NTPpacket[14] = 49;
NTPpacket[15] = 52;
if (udpListener.connect(timeServer, 123))
{
udpListener.onPacket([](AsyncUDPPacket packet)
{
unsigned long highWord = word(packet.data()[40], packet.data()[41]);
unsigned long lowWord = word(packet.data()[42], packet.data()[43]);
time_t UnixUTCtime = (highWord << 16 | lowWord) - 2208988800UL;
setTime(UnixUTCtime);
});
}
else
{
}
udpListener.write(NTPpacket, sizeof(NTPpacket));
// ugly
return 0;
memset(NTPpacket, 0, sizeof(NTPpacket));
NTPpacket[0] = 0b11100011;
NTPpacket[1] = 0;
NTPpacket[2] = 6;
NTPpacket[3] = 0xEC;
NTPpacket[12] = 49;
NTPpacket[13] = 0x4E;
NTPpacket[14] = 49;
NTPpacket[15] = 52;
if (udpListener.connect(timeServer, 123)) {
udpListener.onPacket([](AsyncUDPPacket packet)
{
unsigned long highWord = word(packet.data()[40], packet.data()[41]);
unsigned long lowWord = word(packet.data()[42], packet.data()[43]);
time_t UnixUTCtime = (highWord << 16 | lowWord) - 2208988800UL;
setTime(UnixUTCtime);
});
} else {
}
udpListener.write(NTPpacket, sizeof(NTPpacket));
// ugly
return 0;
}

bool ICACHE_FLASH_ATTR NtpClient::processTime()
{

timeStatus_t ts = timeStatus();

switch (ts)
{
case timeNeedsSync:
return false;
break;
case timeSet:
return true;
break;
default:
return false;
}
timeStatus_t ts = timeStatus();

switch (ts) {
case timeNeedsSync:
return false;
break;
case timeSet:
return true;
break;
default:
return false;
}
}

String ICACHE_FLASH_ATTR NtpClient::zeroPaddedIntVal(int val)
{
if (val < 10)
return "0" + String(val);
else
return String(val);
if (val < 10) {
return "0" + String(val);
} else {
return String(val);
}
}

//returns the current date/time as a string in iso8601 format
String ICACHE_FLASH_ATTR NtpClient::iso8601DateTime()
{

String hyphen = "-";
String colon = ":";

return String(year()) + hyphen +
zeroPaddedIntVal(month()) + hyphen +
zeroPaddedIntVal(day()) + "T" +
zeroPaddedIntVal(hour()) + colon +
zeroPaddedIntVal(minute()) + colon +
zeroPaddedIntVal(second()) +
(timezone == 0 ? "Z" : String(timezone));
String hyphen = "-";
String colon = ":";

return String(year()) + hyphen +
zeroPaddedIntVal(month()) + hyphen +
zeroPaddedIntVal(day()) + "T" +
zeroPaddedIntVal(hour()) + colon +
zeroPaddedIntVal(minute()) + colon +
zeroPaddedIntVal(second()) +
(timezone == 0 ? "Z" : String(timezone));
}

time_t NtpClient::getUptimeSec()
{
_uptimesec = _uptimesec + (millis() - _uptimesec);
return _uptimesec / 1000;
_uptimesec = _uptimesec + (millis() - _uptimesec);
return _uptimesec / 1000;
}

deviceUptime ICACHE_FLASH_ATTR NtpClient::getDeviceUptime()
{
unsigned long currentmillis = millis();

unsigned long currentmillis = millis();
deviceUptime uptime;

deviceUptime uptime;
uptime.secs = (long)((currentmillis / 1000) % 60);
uptime.mins = (long)((currentmillis / 60000) % 60);
uptime.hours = (long)((currentmillis / 3600000) % 24);
uptime.days = (long)((currentmillis / 86400000) % 10);
uptime.secs = (long)((currentmillis / 1000) % 60);
uptime.mins = (long)((currentmillis / 60000) % 60);
uptime.hours = (long)((currentmillis / 3600000) % 24);
uptime.days = (long)((currentmillis / 86400000) % 10);

return uptime;
return uptime;
}

String ICACHE_FLASH_ATTR NtpClient::getDeviceUptimeString()
{
deviceUptime uptime = getDeviceUptime();

deviceUptime uptime = getDeviceUptime();

return String(uptime.days) + " days, " +
String(uptime.hours) + " hours, " +
String(uptime.mins) + " mins, " +
String(uptime.secs) + " secs";
return String(uptime.days) + " days, " +
String(uptime.hours) + " hours, " +
String(uptime.mins) + " mins, " +
String(uptime.secs) + " secs";
}

/*
* returns the current time as UTC (timezone offset removed)
*/
ICACHE_FLASH_ATTR time_t NtpClient::getUtcTimeNow()
{

return now() - timezone;
}
return now() - timezone;
}
45 changes: 22 additions & 23 deletions src/Ntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,41 @@

#define NTP_PACKET_SIZE 48 // NTP time is in the first 48 bytes of message

struct deviceUptime
{
long days;
long hours;
long mins;
long secs;
struct deviceUptime {
long days;
long hours;
long mins;
long secs;
};

class NtpClient
{
public:
void ICACHE_FLASH_ATTR Ntp(const char *server, int8_t tz, time_t syncSecs);
ICACHE_FLASH_ATTR virtual ~NtpClient();
void ICACHE_FLASH_ATTR Ntp(const char *server, int8_t tz, time_t syncSecs);
ICACHE_FLASH_ATTR virtual ~NtpClient();

static char *TimeServerName;
static IPAddress timeServer;
static int8_t timezone;
static time_t syncInterval;
static char *TimeServerName;
static IPAddress timeServer;
static int8_t timezone;
static time_t syncInterval;

static AsyncUDP udpListener;
static AsyncUDP udpListener;

static byte NTPpacket[NTP_PACKET_SIZE];
static byte NTPpacket[NTP_PACKET_SIZE];

static ICACHE_FLASH_ATTR String iso8601DateTime();
static ICACHE_FLASH_ATTR deviceUptime getDeviceUptime();
static ICACHE_FLASH_ATTR String getDeviceUptimeString();
static ICACHE_FLASH_ATTR time_t getUtcTimeNow();
bool ICACHE_FLASH_ATTR processTime();
time_t getUptimeSec();
static ICACHE_FLASH_ATTR String iso8601DateTime();
static ICACHE_FLASH_ATTR deviceUptime getDeviceUptime();
static ICACHE_FLASH_ATTR String getDeviceUptimeString();
static ICACHE_FLASH_ATTR time_t getUtcTimeNow();
bool ICACHE_FLASH_ATTR processTime();
time_t getUptimeSec();

private:
static ICACHE_FLASH_ATTR String zeroPaddedIntVal(int val);
static ICACHE_FLASH_ATTR time_t getNtpTime();
static ICACHE_FLASH_ATTR String zeroPaddedIntVal(int val);
static ICACHE_FLASH_ATTR time_t getNtpTime();

protected:
time_t _uptimesec = 0;
time_t _uptimesec = 0;
};

#endif /* NTP_H_ */
Loading