feat: implement system time synchronization, web server handlers, and clock application components
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
#include "os_cli.h"
|
||||
#include "os_cli.h"
|
||||
#include "os_app.h"
|
||||
#include "os_input.h"
|
||||
#include "os_gfx.h"
|
||||
#include "os_oled.h"
|
||||
#include "os_status.h"
|
||||
#include "sys_uart.h"
|
||||
#include "sys_time.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "esp_console.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
@@ -16,6 +18,57 @@
|
||||
|
||||
static const char *TAG = "SYS_CLI";
|
||||
|
||||
static int cmd_time(int argc, char **argv)
|
||||
{
|
||||
if (argc >= 2 && strcmp(argv[1], "sync") == 0) {
|
||||
printf("Triggering SNTP network time sync...\n");
|
||||
sys_time_start_sntp();
|
||||
return 0;
|
||||
} else if (argc >= 3 && strcmp(argv[1], "set") == 0) {
|
||||
time_t epoch = (time_t)strtoull(argv[2], NULL, 10);
|
||||
if (sys_time_set_epoch(epoch) == ESP_OK) {
|
||||
printf("System time successfully set to epoch: %lld\n", (long long)epoch);
|
||||
return 0;
|
||||
} else {
|
||||
printf("Failed to set time. Usage: time set <epoch_seconds>\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
struct tm tm_info;
|
||||
bool synced = sys_time_get_local(&tm_info);
|
||||
time_t now = sys_time_get_epoch();
|
||||
char strftime_buf[64];
|
||||
strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d %H:%M:%S (%A)", &tm_info);
|
||||
|
||||
printf("\n=== System Wall Clock ===\n");
|
||||
printf("Status: %s\n", synced ? "SYNCHRONIZED" : "UNSYNCHRONIZED (Uptime only)");
|
||||
printf("Local: %s\n", strftime_buf);
|
||||
printf("Epoch: %lld\n", (long long)now);
|
||||
printf("Timezone: %s\n", sys_time_get_timezone());
|
||||
printf("\nCommands: time sync | time set <epoch>\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_tz(int argc, char **argv)
|
||||
{
|
||||
if (argc >= 2) {
|
||||
if (sys_time_set_timezone(argv[1]) == ESP_OK) {
|
||||
printf("Timezone set to '%s'\n", argv[1]);
|
||||
return 0;
|
||||
} else {
|
||||
printf("Failed to set timezone\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
printf("Current Timezone: %s\n", sys_time_get_timezone());
|
||||
printf("Examples: tz \"GMT0BST,M3.5.0/1,M10.5.0\" (UK)\n");
|
||||
printf(" tz \"EST5EDT,M3.2.0,M11.1.0\" (US Eastern)\n");
|
||||
printf(" tz \"CST-8\" (China / Asia)\n");
|
||||
printf(" tz \"UTC0\" (UTC)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_uart(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
@@ -170,6 +223,22 @@ esp_err_t sys_cli_init(void)
|
||||
|
||||
esp_console_register_help_command();
|
||||
|
||||
const esp_console_cmd_t time_cmd = {
|
||||
.command = "time",
|
||||
.help = "View or configure real-time clock (time | time sync | time set <epoch>)",
|
||||
.hint = "<sync|set <epoch>>",
|
||||
.func = &cmd_time,
|
||||
};
|
||||
esp_console_cmd_register(&time_cmd);
|
||||
|
||||
const esp_console_cmd_t tz_cmd = {
|
||||
.command = "tz",
|
||||
.help = "View or configure timezone string",
|
||||
.hint = "<tz_string>",
|
||||
.func = &cmd_tz,
|
||||
};
|
||||
esp_console_cmd_register(&tz_cmd);
|
||||
|
||||
const esp_console_cmd_t ps_cmd = {
|
||||
.command = "ps",
|
||||
.help = "List active FreeRTOS tasks and memory",
|
||||
|
||||
Reference in New Issue
Block a user