Files
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

127 lines
4.9 KiB
C++

#include "Timer.h"
#include "Trad.h"
#include "common.h"
//Day of the week
const char* weekday_D[] = WEEK_DAYS; //;
//Month
const char* month_M[] = MONTHS;
/////////////////////////////////////////
Timer::Timer(Ecran * _ecran, Connect * _connect, char * _Timezone)
{
ecran = _ecran;
connect = _connect;
Timezone = _Timezone;
}
bool Timer::loadCurrentTime()
{
if (debug) Serial.println("B - loadCurrentTime");
// http://worldtimeapi.org/api/timezone/Europe/Paris.json
String json = connect->callJson("worldtimeapi.org", "80", "/api/timezone/Europe/Paris.json", "");
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, json);
if (error) {
if (debug) Serial.print(F("deserializeJson() failed: "));
if (debug) Serial.println(error.c_str());
return false;
}
//JsonObject obj = doc.as<JsonObject>();
// JsonArray root = doc.as<JsonArray>();
JsonObject root = doc.as<JsonObject>();
String time = root["datetime"].as<const char*>();
if (debug) Serial.println("Main: "+String(time));
// Utiliser sscanf pour extraire les parties de la chaîne
sscanf(time.c_str(), "%d-%d-%dT%d:%d", &year, &month, &day, & CurrentHour, & CurrentMin);
Date_str = String(day) + "/" + String(month) + "/" + String(year);
Time_str = String(CurrentHour) + ":" + String(CurrentMin);
if (debug) Serial.println("E - loadCurrentTime");
return true;
}
//#########################################################################################
boolean Timer::setupTime() {
if (debug) Serial.println("B - Setup time");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer, "time.nist.gov"); //(gmtOffset_sec, daylightOffset_sec, ntpServer)
setenv("TZ", Timezone, 1); //setenv()adds the "TZ" variable to the environment with a value TimeZone, only used if set to 1, 0 means no change
tzset(); // Set the TZ environment variable
delay(100);
bool TimeStatus = updateLocalTime();
if (debug) Serial.println("E - Setup time");
return TimeStatus;
}
boolean Timer::updateLocalTime() {
if (debug) Serial.println("B - UpdateLocalTime");
struct tm timeinfo;
char time_output[30], day_output[30], update_time[30];
while (!getLocalTime(&timeinfo, 10000)) { // Wait for 10-sec for time to synchronise
if (debug) Serial.println("Failed to obtain time");
return false;
}
CurrentHour = timeinfo.tm_hour;
CurrentMin = timeinfo.tm_min;
CurrentSec = timeinfo.tm_sec;
//See http://www.cplusplus.com/reference/ctime/strftime/
//if (debug) Serial.println(&timeinfo, "%a %b %d %Y %H:%M:%S"); // Displays: Saturday, June 24 2017 14:05:49
if (Units == "M") {
if ((Language == "CZ") || (Language == "DE") || (Language == "PL") || (Language == "NL")) {
sprintf(day_output, "%s, %02u. %s %04u", weekday_D[timeinfo.tm_wday], timeinfo.tm_mday, month_M[timeinfo.tm_mon], (timeinfo.tm_year) + 1900); // day_output >> So., 23. Juni 2019 <<
}
else
{
sprintf(day_output, "%s %02u-%s-%04u", weekday_D[timeinfo.tm_wday], timeinfo.tm_mday, month_M[timeinfo.tm_mon], (timeinfo.tm_year) + 1900);
}
strftime(update_time, sizeof(update_time), "%H:%M:%S", &timeinfo); // Creates: '14:05:49'
sprintf(time_output, "%s %s", TXT_UPDATED, update_time);
}
else
{
strftime(day_output, sizeof(day_output), "%a %b-%d-%Y", &timeinfo); // Creates 'Sat May-31-2019'
strftime(update_time, sizeof(update_time), "%r", &timeinfo); // Creates: '02:05:49pm'
sprintf(time_output, "%s %s", TXT_UPDATED, update_time);
}
Date_str = day_output;
Time_str = time_output;
if (debug) Serial.println("E - UpdateLocalTime " + Date_str);
delay(100);
return true;
}
void Timer::sleep()
{
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_ON);
ecran->_display->powerOff();
long SleepTimer = (SleepDuration * 60 - ((CurrentMin % SleepDuration) * 60 + CurrentSec)); //Some ESP32 are too fast to maintain accurate time
esp_sleep_enable_timer_wakeup((SleepTimer + 20) * 1000000LL); // Added extra 20-secs of sleep to allow for slow ESP32 RTC timers
#ifdef BUILTIN_LED
pinMode(BUILTIN_LED, INPUT); // If it's On, turn it off and some boards use GPIO-5 for SPI-SS, which remains low after screen use
digitalWrite(BUILTIN_LED, HIGH);
#endif
if (debug) Serial.println("Entering " + String(SleepTimer) + " seconds of sleep time");
if (debug) Serial.println("Awake for : " + String((millis() - StartTime) / 1000.0, 3) + " seconds");
if (debug) Serial.println("Starting deep-sleep period...");
esp_deep_sleep_start(); // Sleep for e.g. 30 minutes
}
void Timer::shutDown()
{
ecran->_display->powerOff();
if (debug) Serial.println("========");
if (debug) Serial.println("shutDown");
if (debug) Serial.println("========");
esp_sleep_enable_timer_wakeup((31536000) * 1000000LL); // Added extra 20-secs of sleep to allow for slow ESP32 RTC timers
esp_deep_sleep_start(); // Sleep for e.g. 30 minutes
}