pi server realised

This commit is contained in:
2026-02-15 12:57:05 +09:00
parent 89f75c23ef
commit 9ca0227214
7 changed files with 197 additions and 1 deletions

View File

@@ -89,6 +89,33 @@ void UserApp_TaskInit(void)
/* ---------- WebSocket callbacks ---------- */
static void rtc_sync_if_needed(const pi_stats_t *stats)
{
if (!stats->time_valid) return;
rtcTimeStruct_t rtc = {};
Rtc_GetTime(&rtc);
/* Convert both to seconds-since-midnight for comparison */
int pi_secs = stats->time_hour * 3600 + stats->time_minute * 60 + stats->time_second;
int rtc_secs = rtc.hour * 3600 + rtc.minute * 60 + rtc.second;
int delta = pi_secs - rtc_secs;
if (delta < 0) delta = -delta;
/* Also check date mismatch as an immediate trigger */
bool date_mismatch = (rtc.year != stats->time_year ||
rtc.month != stats->time_month ||
rtc.day != stats->time_day);
if (date_mismatch || delta > 60) {
Rtc_SetTime(stats->time_year, stats->time_month, stats->time_day,
stats->time_hour, stats->time_minute, stats->time_second);
ESP_LOGI(TAG, "RTC synced from Pi: %04d-%02d-%02d %02d:%02d:%02d (drift: %ds)",
stats->time_year, stats->time_month, stats->time_day,
stats->time_hour, stats->time_minute, stats->time_second, delta);
}
}
static void ws_data_cb(const pi_stats_t *stats)
{
/* Check alert conditions */
@@ -103,6 +130,9 @@ static void ws_data_cb(const pi_stats_t *stats)
}
}
/* Sync RTC if Pi time drifts from board clock */
rtc_sync_if_needed(stats);
/* Update UI under LVGL lock */
if (Lvgl_lock(100)) {
dashboard_ui_update_stats(stats);