Create sntp command

This commit is contained in:
Camden Dixie O'Brien 2023-05-16 22:52:43 +01:00
parent 2ca513b98d
commit 06966c2ae6

View File

@ -77,7 +77,7 @@ static int store_time(void)
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
error = nvs_set_u64(nvs, TIMESTAMP_KEY, tv.tv_sec); error = nvs_set_u64(nvs, TIMESTAMP_KEY, tv.tv_sec);
if (error == ESP_OK) if (error == ESP_OK)
ESP_LOGI(TAG, "Stored time"); ESP_LOGI(TAG, "Time stored");
else else
ESP_LOGE(TAG, "Error storing time: %04x", error); ESP_LOGE(TAG, "Error storing time: %04x", error);
@ -92,11 +92,7 @@ static int time_command_func(int argc, char **argv)
printf("%02u:%02u\n", time.hour, time.minute); printf("%02u:%02u\n", time.hour, time.minute);
return 0; return 0;
} else if (argc == 2) { } else if (argc == 2) {
if (strcmp(argv[1], "sntp-status") == 0) { if (strcmp(argv[1], "store") == 0) {
const sntp_sync_status_t status = sntp_get_sync_status();
printf("%s\n", sync_status_description(status));
return 0;
} else if (strcmp(argv[1], "store") == 0) {
return store_time(); return store_time();
} else { } else {
Time time; Time time;
@ -138,6 +134,32 @@ static int date_command_func(int argc, char **argv)
} }
} }
static int sntp_command_func(int argc, char **argv)
{
if (argc == 2) {
if (strcmp(argv[1], "status") == 0) {
if (esp_sntp_enabled()) {
const sntp_sync_status_t status = sntp_get_sync_status();
printf("%s\n", sync_status_description(status));
} else {
printf("Disabled\n");
}
return 0;
} else if (strcmp(argv[1], "restart") == 0) {
return sntp_restart() ? 0 : 1;
} else if (strcmp(argv[1], "stop") == 0) {
sntp_stop();
return 0;
} else {
printf("Unrecognised subcommand\n");
return 1;
}
} else {
printf("Invalid number of arguments\n");
return 1;
}
}
static void time_saver_func(void *arg) static void time_saver_func(void *arg)
{ {
(void)arg; (void)arg;
@ -165,11 +187,14 @@ void time_manager_init(void)
settings_add_sntp_server_callback(&handle_sntp_server_update); settings_add_sntp_server_callback(&handle_sntp_server_update);
console_register( console_register(
"time", "Get, set or store the time, or get SNTP status", "time", "Get, set or store the time", "time [HH:MM] OR time store",
"time [HH:MM] OR time <sntp-status|store>", time_command_func); time_command_func);
console_register( console_register(
"date", "Get or set the date", "date [yyyy-mm-dd]", "date", "Get or set the date", "date [yyyy-mm-dd]",
date_command_func); date_command_func);
console_register(
"sntp", "Manage SNTP", "sntp <status|restart|stop>",
sntp_command_func);
// Attempt to load time from storage // Attempt to load time from storage
esp_err_t error; esp_err_t error;