diff --git a/src/Network/TimeClient.cpp b/src/Network/TimeClient.cpp index 76cc67d..d8d18e4 100644 --- a/src/Network/TimeClient.cpp +++ b/src/Network/TimeClient.cpp @@ -1,8 +1,23 @@ #include "TimeClient.h" TimeClient::TimeClient(float utcOffset, DebugController * debugController) { - this->myUtcOffset = utcOffset; - this->debugController = debugController; + this->myUtcOffset = utcOffset; + this->debugController = debugController; +} + +void TimeClient::handleSync(int snycDelayMinutes) { + //Get Time Update + if((this->getMinutesFromLastRefresh() >= snycDelayMinutes) || this->lastEpoch == 0) { + this->debugController->printLn("Updating Time..."); + this->updateTime(); + this->lastEpoch = this->getCurrentEpoch(); + this->debugController->printLn("Local time: " + this->getAmPmFormattedTime()); + } +} + +int TimeClient::getMinutesFromLastRefresh() { + int minutes = (this->getCurrentEpoch() - this->lastEpoch) / 60; + return minutes; } void TimeClient::updateTime() { diff --git a/src/Network/TimeClient.h b/src/Network/TimeClient.h index 62f3f08..b2967f0 100644 --- a/src/Network/TimeClient.h +++ b/src/Network/TimeClient.h @@ -14,10 +14,15 @@ private: byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets DebugController * debugController; + long lastEpoch = 0; + long firstEpoch = 0; + public: TimeClient(float utcOffset, DebugController * debugController); void updateTime(); - + void handleSync(int snycDelayMinutes); + int getMinutesFromLastRefresh(); + void setUtcOffset(float utcOffset); String getHours(); String getAmPmHours(); diff --git a/src/main.cpp b/src/main.cpp index 48180a4..b440d53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,8 @@ #include + + void configModeCallback(WiFiManager *myWiFiManager); @@ -75,6 +77,12 @@ void setup() { } void loop() { + + // Handle update of time + timeClient.handleSync(globalDataController.getClockResyncMinutes()); + + + // put your main code here, to run repeatedly: