add timesync and timezone setup
This commit is contained in:
112
src/hardware/timesync.cpp
Normal file
112
src/hardware/timesync.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "config.h"
|
||||
#include "timesync.h"
|
||||
#include <WiFi.h>
|
||||
|
||||
EventGroupHandle_t time_event_handle = NULL;
|
||||
TaskHandle_t _timesync_Task;
|
||||
void timesync_Task( void * pvParameters );
|
||||
|
||||
timesync_config_t timesync_config;
|
||||
|
||||
void timesync_setup( TTGOClass *ttgo ) {
|
||||
|
||||
timesync_read_config();
|
||||
|
||||
WiFi.onEvent( [](WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
xEventGroupSetBits( time_event_handle, TIME_SYNC_REQUEST );
|
||||
vTaskResume( _timesync_Task );
|
||||
}, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP );
|
||||
|
||||
time_event_handle = xEventGroupCreate();
|
||||
xEventGroupClearBits( time_event_handle, TIME_SYNC_REQUEST );
|
||||
|
||||
xTaskCreate(
|
||||
timesync_Task, /* Function to implement the task */
|
||||
"timesync Task", /* Name of the task */
|
||||
10000, /* Stack size in words */
|
||||
NULL, /* Task input parameter */
|
||||
1, /* Priority of the task */
|
||||
&_timesync_Task ); /* Task handle. */
|
||||
|
||||
}
|
||||
|
||||
void timesync_save_config( void ) {
|
||||
fs::File file = SPIFFS.open( TIMESYNC_CONFIG_FILE, FILE_WRITE );
|
||||
|
||||
if ( !file ) {
|
||||
Serial.printf("Can't save file: %s\r\n", TIMESYNC_CONFIG_FILE );
|
||||
}
|
||||
else {
|
||||
file.write( (uint8_t *)×ync_config, sizeof( timesync_config ) );
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
void timesync_read_config( void ) {
|
||||
fs::File file = SPIFFS.open( TIMESYNC_CONFIG_FILE, FILE_READ );
|
||||
|
||||
if (!file) {
|
||||
Serial.printf("Can't open file: %s!\r\n", TIMESYNC_CONFIG_FILE );
|
||||
}
|
||||
else {
|
||||
int filesize = file.size();
|
||||
if ( filesize > sizeof( timesync_config ) ) {
|
||||
Serial.printf("Failed to read configfile. Wrong filesize!\r\n" );
|
||||
}
|
||||
else {
|
||||
file.read( (uint8_t *)×ync_config, filesize );
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
bool timesync_get_timesync( void ) {
|
||||
return( timesync_config.timesync );
|
||||
}
|
||||
|
||||
void timesync_set_timesync( bool timesync ) {
|
||||
timesync_config.timesync = timesync;
|
||||
timesync_save_config();
|
||||
}
|
||||
|
||||
bool timesync_get_daylightsave( void ) {
|
||||
return( timesync_config.daylightsave );
|
||||
}
|
||||
|
||||
void timesync_set_daylightsave( bool daylightsave ) {
|
||||
timesync_config.daylightsave = daylightsave;
|
||||
timesync_save_config();
|
||||
}
|
||||
|
||||
int32_t timesync_get_timezone( void ) {
|
||||
return( timesync_config.timezone );
|
||||
}
|
||||
|
||||
void timesync_set_timezone( int32_t timezone ) {
|
||||
timesync_config.timezone = timezone;
|
||||
timesync_save_config();
|
||||
}
|
||||
|
||||
void timesync_Task( void * pvParameters ) {
|
||||
|
||||
while( true ) {
|
||||
vTaskDelay( 50 );
|
||||
|
||||
if ( xEventGroupGetBits( time_event_handle ) & TIME_SYNC_REQUEST ) {
|
||||
struct tm timeinfo;
|
||||
TTGOClass *ttgo = TTGOClass::getWatch();
|
||||
|
||||
long gmtOffset_sec = timesync_config.timezone * 3600;
|
||||
|
||||
configTime( gmtOffset_sec, 0, "pool.ntp.org" );
|
||||
|
||||
if( !getLocalTime( &timeinfo ) ) {
|
||||
Serial.println( "Failed to obtain time\r\n" );
|
||||
}
|
||||
ttgo->rtc->syncToRtc();
|
||||
xEventGroupClearBits( time_event_handle, TIME_SYNC_REQUEST );
|
||||
}
|
||||
|
||||
vTaskSuspend( _timesync_Task );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user