feat: implement system time synchronization, web server handlers, and clock application components

This commit is contained in:
dark98
2026-09-25 20:57:19 +01:00
parent 4bf31f908a
commit 59b0318579
11 changed files with 442 additions and 33 deletions
+41 -2
View File
@@ -1,11 +1,42 @@
#include "web_server.h"
#include "web_server.h"
#include "web_server_pages.h"
#include "sys_time.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
static const char *TAG = "SYS_WEB";
static httpd_handle_t s_server;
static esp_err_t time_sync_handler(httpd_req_t *req)
{
char query[128] = {0};
if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK) {
char val[32] = {0};
if (httpd_query_key_value(query, "epoch", val, sizeof(val)) == ESP_OK) {
time_t epoch = (time_t)strtoull(val, NULL, 10);
if (epoch >= 1704067200ULL) {
sys_time_set_epoch(epoch);
}
}
}
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", &tm_info);
char resp[160];
snprintf(resp, sizeof(resp), "{\"synced\":%s,\"epoch\":%lld,\"time\":\"%s\",\"tz\":\"%s\"}",
synced ? "true" : "false", (long long)now, strftime_buf, sys_time_get_timezone());
httpd_resp_set_type(req, "application/json");
return httpd_resp_sendstr(req, resp);
}
esp_err_t web_server_start(void)
{
if (s_server) return ESP_OK;
@@ -19,6 +50,14 @@ esp_err_t web_server_start(void)
web_server_register_wifi_pages(s_server);
web_server_register_ota_page(s_server);
web_server_register_serial_pages(s_server);
ESP_LOGI(TAG, "Web server started");
const httpd_uri_t time_uri = {
.uri = "/time/sync",
.method = HTTP_GET,
.handler = time_sync_handler
};
httpd_register_uri_handler(s_server, &time_uri);
ESP_LOGI(TAG, "Web server started with real-time sync service");
return ESP_OK;
}