Merge pull request #163 from romanmashta/dev

getTime function code refactoring
This commit is contained in:
BitMaker 2023-09-11 23:49:57 +02:00 committed by GitHub
commit 66057d910c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -167,8 +167,7 @@ void getTime(unsigned long* currentHours, unsigned long* currentMinutes, unsigne
//Check if need an NTP call to check current time //Check if need an NTP call to check current time
if((mTriggerUpdate == 0) || (millis() - mTriggerUpdate > UPDATE_PERIOD_h * 60 * 60 * 1000)){ //60 sec. * 60 min * 1000ms if((mTriggerUpdate == 0) || (millis() - mTriggerUpdate > UPDATE_PERIOD_h * 60 * 60 * 1000)){ //60 sec. * 60 min * 1000ms
if(WiFi.status() == WL_CONNECTED) { if(WiFi.status() == WL_CONNECTED) {
timeClient.update(); //NTP call to get current time if(timeClient.update()) mTriggerUpdate = millis(); //NTP call to get current time
mTriggerUpdate = millis();
initialTime = timeClient.getEpochTime(); // Guarda la hora inicial (en segundos desde 1970) initialTime = timeClient.getEpochTime(); // Guarda la hora inicial (en segundos desde 1970)
Serial.print("TimeClient NTPupdateTime "); Serial.print("TimeClient NTPupdateTime ");
} }
@ -181,27 +180,11 @@ void getTime(unsigned long* currentHours, unsigned long* currentMinutes, unsigne
*currentHours = currentTime % 86400 / 3600; *currentHours = currentTime % 86400 / 3600;
*currentMinutes = currentTime % 3600 / 60; *currentMinutes = currentTime % 3600 / 60;
*currentSeconds = currentTime % 60; *currentSeconds = currentTime % 60;
} }
String getTime(void){ String getTime(void){
unsigned long currentHours, currentMinutes, currentSeconds;
//Check if need an NTP call to check current time getTime(&currentHours, &currentMinutes, &currentSeconds);
if((mTriggerUpdate == 0) || (millis() - mTriggerUpdate > UPDATE_PERIOD_h * 60 * 60 * 1000)){ //60 sec. * 60 min * 1000ms
if(WiFi.status() == WL_CONNECTED) {
if(timeClient.update()) mTriggerUpdate = millis();
initialTime = timeClient.getEpochTime(); // Guarda la hora inicial (en segundos desde 1970)
Serial.print("TimeClient NTPupdateTime ");
}
}
unsigned long elapsedTime = (millis() - mTriggerUpdate) / 1000; // Tiempo transcurrido en segundos
unsigned long currentTime = initialTime + elapsedTime; // La hora actual
// convierte la hora actual en horas, minutos y segundos
unsigned long currentHours = currentTime % 86400 / 3600;
unsigned long currentMinutes = currentTime % 3600 / 60;
unsigned long currentSeconds = currentTime % 60;
char LocalHour[10]; char LocalHour[10];
sprintf(LocalHour, "%02d:%02d", currentHours, currentMinutes); sprintf(LocalHour, "%02d:%02d", currentHours, currentMinutes);