From f0a773b53f345415ef48a026169e36157b067263 Mon Sep 17 00:00:00 2001 From: bogd Date: Mon, 13 May 2024 12:47:40 +0200 Subject: [PATCH] added manual time set, fixed SNTP client disable --- include/NetTransport.h | 1 + src/RestApiHandler.c | 20 +++++++++++++++++--- src/SNTP.c | 7 +++++++ src/SysConfiguration.c | 16 ++++++---------- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/include/NetTransport.h b/include/NetTransport.h index 9ed3856..69603d4 100644 --- a/include/NetTransport.h +++ b/include/NetTransport.h @@ -118,6 +118,7 @@ void GetRFC3339Time(char *t); void GetISO8601Time(char *t); void StartTimeGet(void); void SetSystemTime(struct tm *time, const char* source); +void SetSystemTimeVal(struct timeval *tv, const char* source); esp_err_t StartOTA(bool isManual); char* GetAvailVersion(); diff --git a/src/RestApiHandler.c b/src/RestApiHandler.c index f19a0a3..fa413da 100644 --- a/src/RestApiHandler.c +++ b/src/RestApiHandler.c @@ -106,6 +106,20 @@ static void funct_time(char *argres, int rw) snprintf(argres, VAR_MAX_VALUE_LENGTH, "%d", (int) now); } +static void funct_time_set(char *argres, int rw) +{ + time_t unix = atoi(argres); + if (unix == 0) + { + snprintf(argres, VAR_MAX_VALUE_LENGTH, "%s", "\"ERROR_UNIX_TIME_NULL\""); + return; + } + struct timeval tv; + tv.tv_sec = unix; + SetSystemTimeVal(&tv, "Time set from user API"); + snprintf(argres, VAR_MAX_VALUE_LENGTH, "%s", "\"TIME_SET_OK\""); +} + static void funct_uptime(char *argres, int rw) { snprintf(argres, VAR_MAX_VALUE_LENGTH, "%d", (int) GetUpTime()); @@ -406,6 +420,7 @@ const rest_var_t SystemVariables[] = { 0, "exec", &funct_exec, VAR_FUNCT, RW, 0, 0 }, { 0, "time", &funct_time, VAR_FUNCT, R, 0, 0 }, + { 0, "time_set", &funct_time_set, VAR_FUNCT, RW, 0, 0 }, { 0, "uptime", &funct_uptime, VAR_FUNCT, R, 0, 0 }, { 0, "free_ram", &funct_fram, VAR_FUNCT, R, 0, 0 }, { 0, "free_ram_min", &funct_fram_min, VAR_FUNCT, R, 0, 0 }, @@ -577,14 +592,13 @@ const rest_var_t SystemVariables[] = { 0, "file_list", &funct_file_list, VAR_FUNCT, R, 0, 0 }, { 0, "file_block", &funct_file_block, VAR_FUNCT, R, 0, 0 }, -#if CONFIG_SDCARD_ENABLE + #if CONFIG_SDCARD_ENABLE { 0, "sd_list", &funct_sd_list, VAR_FUNCT, R, 0, 0 }, { 0, "sd_block", &funct_sd_block, VAR_FUNCT, R, 0, 0 }, { 0, "sd_visible", (bool*) (&VAR_TRUE), VAR_BOOL, R, 0, 1 } #else { 0, "sd_visible", (bool*) (&VAR_FALSE), VAR_BOOL, R, 0, 1 }, -#endif - + #endif }; diff --git a/src/SNTP.c b/src/SNTP.c index 380a487..60e815d 100644 --- a/src/SNTP.c +++ b/src/SNTP.c @@ -143,6 +143,13 @@ void SetSystemTime(struct tm *time, const char* source) TimeObtainHandler(&now); } +void SetSystemTimeVal(struct timeval *tv, const char* source) +{ + ESP_LOGI("SNTP","Setting time: %d from the source %s", (int)(tv->tv_sec), source); + settimeofday(tv, NULL); + TimeObtainHandler(tv); +} + void SecondTickSystem(void *param) { ++UpTime; diff --git a/src/SysConfiguration.c b/src/SysConfiguration.c index 3714088..7021fdf 100644 --- a/src/SysConfiguration.c +++ b/src/SysConfiguration.c @@ -103,7 +103,6 @@ esp_err_t WebGuiAppInit(void) InitSysSDCard(); #endif - esp_err_t err = nvs_flash_init(); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -161,7 +160,8 @@ esp_err_t WebGuiAppInit(void) /*Start services depends on client connection*/ #if CONFIG_WEBGUIAPP_GPRS_ENABLE || CONFIG_WEBGUIAPP_ETHERNET_ENABLE || CONFIG_WEBGUIAPP_WIFI_ENABLE ESP_ERROR_CHECK(start_file_server()); - StartTimeGet(); + if (GetSysConf()->sntpClient.Flags1.bIsGlobalEnabled) + StartTimeGet(); //regTimeSyncCallback(&TimeObtainHandler); //mDNSServiceStart(); @@ -178,7 +178,6 @@ esp_err_t WebGuiAppInit(void) InitSerialPort(); #endif - return ESP_OK; } @@ -253,7 +252,7 @@ static void ResetSysConfig(SYS_CONFIG *Conf) UINT32_VAL d; GetChipId((uint8_t*) d.v); - snprintf(Conf->SN, 11, "%010u", (unsigned int)swap(d.Val)); + snprintf(Conf->SN, 11, "%010u", (unsigned int) swap(d.Val)); Conf->ColorSheme = CONFIG_WEBGUIAPP_DEFAULT_COLOR_SCHEME; @@ -371,8 +370,6 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C strcat(Conf->mqttStation[0].ClientID, "-"); strcat(Conf->mqttStation[0].ClientID, Conf->ID); - - memcpy(Conf->mqttStation[0].UserName, CONFIG_WEBGUIAPP_MQTT_USERNAME, sizeof(CONFIG_WEBGUIAPP_MQTT_USERNAME)); memcpy(Conf->mqttStation[0].UserPass, CONFIG_WEBGUIAPP_MQTT_PASSWORD, @@ -393,7 +390,6 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C strcat(Conf->mqttStation[1].ClientID, "-"); strcat(Conf->mqttStation[1].ClientID, Conf->ID); - memcpy(Conf->mqttStation[1].UserName, CONFIG_WEBGUIAPP_MQTT_USERNAME, sizeof(CONFIG_WEBGUIAPP_MQTT_USERNAME)); memcpy(Conf->mqttStation[1].UserPass, CONFIG_WEBGUIAPP_MQTT_PASSWORD, sizeof(CONFIG_WEBGUIAPP_MQTT_PASSWORD)); #endif @@ -440,9 +436,9 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C #endif Conf->modbusSettings.ModbusTCPPort = CONFIG_WEBGUIAPP_MBTCP_SERVER_PORT; #endif - for (int i = 0; i < CONFIG_WEBGUIAPP_CRON_NUMBER; i++ ) + for (int i = 0; i < CONFIG_WEBGUIAPP_CRON_NUMBER; i++) { - Conf->Timers[i].num = i+1; + Conf->Timers[i].num = i + 1; Conf->Timers[i].del = true; Conf->Timers[i].enab = false; Conf->Timers[i].prev = false; @@ -578,7 +574,7 @@ void DelayedRestartTask(void *pvParameter) void DelayedRestart(void) { xTaskCreate(DelayedRestartTask, "RestartTask", 1024 * 4, (void*) 0, 3, - NULL); + NULL); } bool GetUserAppNeedReset(void)