domotic/main/obtain_time.c
2025-05-08 19:56:56 +02:00

70 lines
2.5 KiB
C

#include "esp_log.h"
#include "esp_netif_sntp.h"
#include "esp_sntp.h"
#include "obtain_time.h"
#include "esp_lvgl_port.h"
#include "ihm.h"
#include "main.h"
static const char *TAG = "sntp";
extern lv_subject_t dateHeureSubj;
char *days[7]={"Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"};
char *months[12]={"Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"};
void time_sync_notification_cb(struct timeval *tv)
{
ESP_LOGI(TAG, "Notification of a time synchronization event");
char strftime_buf[64];
// Set timezone to Eastern Standard Time and print local time
time_t now;
struct tm timeinfo;
time(&now);
localtime_r(&now, &timeinfo);
//strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
sprintf(strftime_buf, "%s %d %s %02d:%02d", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min);
localtime_r(&now, &timeinfo);
send_event(EVT_TIME_SETTED,&strftime_buf);
}
void obtain_time()
{
}
void updateTime(void *pvParameter)
{
// Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
// number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above)
EventBits_t bits = xEventGroupWaitBits(domotic_event_group,
WIFI_CONNECTED_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT)
{
ESP_LOGI(TAG, "Initializing and starting SNTP");
esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org");
config.sync_cb = time_sync_notification_cb; // Note: This is only needed if we want
esp_netif_sntp_init(&config);
}
char strftime_buf[64];
time_t now = 0;
while (1)
{
time(&now);
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
tzset();
struct tm timeinfo = {0};
localtime_r(&now, &timeinfo);
sprintf(strftime_buf, "%s %d %s %02d:%02d", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min);
send_event(EVT_TIME_SETTED,&strftime_buf);
vTaskDelay(60000 / portTICK_PERIOD_MS);
}
}