Skip to content

Commit 90354df

Browse files
committed
esp_rmaker_console: Added commands for time get and timezone set
Examples: tz-set posix CST-8 I (25752) esp_rmaker_time_sync: The current time is: Tue Sep 1 21:48:13 2020 +0800[CST], DST: No. tz-set America/Los_Angeles I (70892) esp_rmaker_time_sync: The current time is: Tue Sep 1 06:48:58 2020 -0700[PDT], DST: Yes. local-time esp_rmaker_commands: Current local time: Tue Sep 1 06:49:21 2020 -0700[PDT], DST: Yes
1 parent bf9de50 commit 90354df

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

components/esp_rainmaker/src/console/esp_rmaker_commands.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <esp_rmaker_core.h>
3535
#include <esp_rmaker_user_mapping.h>
36+
#include <esp_rmaker_utils.h>
3637

3738
#include <esp_rmaker_console_internal.h>
3839

@@ -352,11 +353,61 @@ static void register_wifi_prov()
352353
esp_console_cmd_register(&cmd);
353354
}
354355

356+
static int local_time_cli_handler(int argc, char *argv[])
357+
{
358+
char local_time[64];
359+
if (esp_rmaker_get_local_time_str(local_time, sizeof(local_time)) == ESP_OK) {
360+
printf("%s: Current local time: %s\n", TAG, local_time);
361+
} else {
362+
printf("%s: Current local time (truncated): %s\n", TAG, local_time);
363+
}
364+
return ESP_OK;
365+
}
366+
367+
static int tz_set_cli_handler(int argc, char *argv[])
368+
{
369+
if (argc < 2) {
370+
printf("%s: Invalid Usage.\n", TAG);
371+
return ESP_ERR_INVALID_ARG;
372+
}
373+
if (strcmp(argv[1], "posix") == 0) {
374+
if (argv[2]) {
375+
esp_rmaker_time_set_timezone_posix(argv[2]);
376+
} else {
377+
printf("%s: Invalid Usage.\n", TAG);
378+
return ESP_ERR_INVALID_ARG;
379+
}
380+
} else {
381+
esp_rmaker_time_set_timezone(argv[1]);
382+
}
383+
return ESP_OK;
384+
}
385+
386+
static void register_time_commands()
387+
{
388+
const esp_console_cmd_t local_time_cmd = {
389+
.command = "local-time",
390+
.help = "Get the local time of device.",
391+
.func = &local_time_cli_handler,
392+
};
393+
ESP_LOGI(TAG, "Registering command: %s", local_time_cmd.command);
394+
esp_console_cmd_register(&local_time_cmd);
395+
396+
const esp_console_cmd_t tz_set_cmd = {
397+
.command = "tz-set",
398+
.help = "Set Timezone. Usage: tz-set [posix] <tz_string>.",
399+
.func = &tz_set_cli_handler,
400+
};
401+
ESP_LOGI(TAG, "Registering command: %s", tz_set_cmd.command);
402+
esp_console_cmd_register(&tz_set_cmd);
403+
}
404+
355405
void register_commands()
356406
{
357407
register_generic_debug_commands();
358408
register_reset_to_factory();
359409
register_user_node_mapping();
360410
register_get_node_id();
361411
register_wifi_prov();
412+
register_time_commands();
362413
}

0 commit comments

Comments
 (0)