33 Commits

Author SHA1 Message Date
436e55a0c5 alarm image persist for 1 second longer 2026-02-16 23:42:16 +09:00
Mikkeli Matlock
776aee18fc pi gitignore 2026-02-16 23:35:09 +09:00
Mikkeli Matlock
41e0a6b81d ui tweaks 2026-02-16 23:33:42 +09:00
Mikkeli Matlock
e4da3687b4 pi gitignore 2026-02-16 23:33:18 +09:00
Mikkeli Matlock
05cd085b89 untracked alarm config 2026-02-16 23:32:32 +09:00
Mikkeli Matlock
d0d0b4dc39 untracked vscode settings 2026-02-16 22:38:07 +09:00
Mikkeli Matlock
25420d57b3 gitignore 2026-02-16 22:37:38 +09:00
Mikkeli Matlock
18984c29a3 smooth scrolling 2026-02-16 22:28:26 +09:00
Mikkeli Matlock
7555efcba9 Merge 2026-02-16 22:17:23 +09:00
379f8e105b fix? 2026-02-16 22:14:55 +09:00
3b4d61c56d process order fix 2026-02-16 22:00:23 +09:00
5ae0c64ba9 new client connection logic
- esp32 requests for image when ready to receive
- server serves initial image on request
2026-02-16 21:56:28 +09:00
Mikkeli Matlock
7f644652bb minor visual change 2026-02-16 21:31:43 +09:00
5c16e6deb7 pi status server update 2026-02-16 21:08:40 +09:00
Mikkeli Matlock
706c7ac21b merged changes 2026-02-16 20:55:12 +09:00
b33c658885 docker services real 2026-02-16 20:47:44 +09:00
Mikkeli Matlock
2e5ad58978 changed rx/tx to kByte/s 2026-02-16 16:40:28 +09:00
Mikkeli Matlock
8ffe8cdffb fixed charging heuristic threshold 2026-02-16 16:17:26 +09:00
Mikkeli Matlock
b0a5e05e4a changed charging heuristic voltage threshold 2026-02-16 15:34:26 +09:00
Mikkeli Matlock
4e2c714e1f new layout + 200px status images 2026-02-16 14:52:20 +09:00
Mikkeli Matlock
7165f56464 cosmetic: battery related stuff 2026-02-16 14:39:21 +09:00
Mikkeli Matlock
a25e4b2cb3 alarm audio normalisation 2026-02-15 23:12:25 +09:00
Mikkeli Matlock
76bf8c966d docs cleanup and disk usage target change 2026-02-15 23:00:30 +09:00
Mikkeli Matlock
d63322d304 esp32 improvements
- better clock seconds
- better alarm (image before audio, supposedly)
2026-02-15 22:55:53 +09:00
Mikkeli Matlock
0b2f274719 changed alarm time 2026-02-15 22:55:22 +09:00
Mikkeli Matlock
22c32f3538 config format fix and handling fix
multiple alarms supported
2026-02-15 22:46:59 +09:00
Mikkeli Matlock
89c975bf17 new alarm mechanism 2026-02-15 22:44:36 +09:00
Mikkeli Matlock
e5cc124dd3 monochrome threshold 2026-02-15 22:24:29 +09:00
Mikkeli Matlock
a0cec91845 preprocessed images 2026-02-15 22:23:28 +09:00
Mikkeli Matlock
1140de0fd4 removed docs folder: contents lives in respective scopes directly now 2026-02-15 22:14:35 +09:00
Mikkeli Matlock
37bdc8bb1b pi features docs 2026-02-15 22:14:06 +09:00
Mikkeli Matlock
33936650c6 image alarm 2026-02-15 21:46:18 +09:00
Mikkeli Matlock
7eb05ea983 audio_client.cpp fix 2026-02-15 21:36:56 +09:00
33 changed files with 1050 additions and 294 deletions

View File

@@ -5,7 +5,18 @@
"Bash(idf.py build:*)", "Bash(idf.py build:*)",
"Bash(for f in DSEG14C_BI_50px.c InziuIosevka_Slab_CC_12px.c InziuIosevka_Slab_CC_16px.c InziuIosevka_Slab_CC_20px.c InziuIosevka_Slab_CC_24px.c InziuIosevka_Slab_CC_32px.c)", "Bash(for f in DSEG14C_BI_50px.c InziuIosevka_Slab_CC_12px.c InziuIosevka_Slab_CC_16px.c InziuIosevka_Slab_CC_20px.c InziuIosevka_Slab_CC_24px.c InziuIosevka_Slab_CC_32px.c)",
"Bash(do sed -i '/\\\\.static_bitmap = 0,/d' \"$f\")", "Bash(do sed -i '/\\\\.static_bitmap = 0,/d' \"$f\")",
"Bash(done)" "Bash(done)",
"Bash(file:*)",
"mcp__ide__getDiagnostics",
"Bash(python -m py_compile:*)",
"WebSearch",
"WebFetch(domain:docs.waveshare.com)",
"WebFetch(domain:www.waveshare.com)",
"WebFetch(domain:raw.githubusercontent.com)",
"Bash(npm view:*)",
"WebFetch(domain:raw.githubusercontent.com)",
"Bash(docker ps:*)",
"Bash(python3:*)"
] ]
}, },
"outputStyle": "iseri", "outputStyle": "iseri",

3
.gitignore vendored
View File

@@ -3,3 +3,6 @@ managed_components/
sdkconfig sdkconfig
sdkconfig.old sdkconfig.old
dependencies.lock dependencies.lock
# vscode local settings
.vscode/

12
.vscode/settings.json vendored
View File

@@ -1,12 +0,0 @@
{
"idf.currentSetup": "J:\\esp\\.espressif\\v5.5.2\\esp-idf",
"idf.flashType": "UART",
"idf.portWin": "COM7",
"idf.openOcdConfigs": [
"interface/ftdi/esp_ftdi.cfg",
"target/esp32s3.cfg"
],
"idf.customExtraVars": {
"IDF_TARGET": "esp32s3"
}
}

View File

@@ -19,7 +19,7 @@ stats_server.py --WS/JSON--> ws_client --> dashboard_ui (LVGL)
The Pi runs a WebSocket server that pushes system stats (CPU, memory, disk, temperature, network, services) as JSON every 2 seconds. The ESP32 parses the JSON and updates LVGL widgets. A data staleness watchdog forces reconnection if the server goes silent. The Pi runs a WebSocket server that pushes system stats (CPU, memory, disk, temperature, network, services) as JSON every 2 seconds. The ESP32 parses the JSON and updates LVGL widgets. A data staleness watchdog forces reconnection if the server goes silent.
The display uses a two-column layout: left half shows Pi stats (CPU/RAM/DISK bars, CPU temp) and a services table; right half shows a large HH:MM:SS clock (montserrat_36), date with day-of-week, and local sensor readings (room temp, humidity). The services table auto-scrolls when more than 4 services are present. The clock updates every second from the on-board RTC, which syncs from the Pi's time when drift exceeds 60 seconds. The display uses a two-column layout: left half shows Pi stats (CPU/RAM/DISK bars, CPU temp) and a services table; right half shows a large HH:MM:SS clock (montserrat_36), date with day-of-week, and local sensor readings (room temp, humidity). The services table auto-scrolls when services exceed the visible area; row height and visible row count are measured from LVGL at runtime, so the scroll loop adapts automatically to font, padding, or border changes. The clock updates every second from the on-board RTC, which syncs from the Pi's time when drift exceeds 60 seconds.
## Configuration ## Configuration

View File

@@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "audio_client.cpp" SRCS "audio_client.cpp"
REQUIRES espressif__esp_websocket_client port_bsp REQUIRES espressif__esp_websocket_client port_bsp codec_board lvgl__lvgl
PRIV_REQUIRES esp_event json PRIV_REQUIRES esp_event json
INCLUDE_DIRS "./") INCLUDE_DIRS "./")

View File

@@ -18,6 +18,11 @@ static const char *TAG = "audio_client";
#define PLAYBACK_PRIORITY 4 #define PLAYBACK_PRIORITY 4
#define WS_BUFFER_SIZE 8192 #define WS_BUFFER_SIZE 8192
/* Status image constants */
#define STATUS_IMG_W 200
#define STATUS_IMG_H 200
#define STATUS_IMG_BYTES (STATUS_IMG_W * STATUS_IMG_H / 8) /* 5000 */
static esp_websocket_client_handle_t s_client = NULL; static esp_websocket_client_handle_t s_client = NULL;
static CodecPort *s_codec = NULL; static CodecPort *s_codec = NULL;
static QueueHandle_t s_pcm_queue = NULL; static QueueHandle_t s_pcm_queue = NULL;
@@ -25,6 +30,14 @@ static TaskHandle_t s_playback_task = NULL;
static volatile audio_state_t s_state = AUDIO_IDLE; static volatile audio_state_t s_state = AUDIO_IDLE;
static volatile bool s_playing = false; static volatile bool s_playing = false;
/* Status image state */
static uint8_t s_img_buf[STATUS_IMG_BYTES];
static lv_img_dsc_t s_img_dsc;
static volatile bool s_img_pending = false; /* expecting binary frame with image data */
static volatile bool s_img_updated = false; /* new image ready for UI consumption */
static volatile bool s_need_request_image = false; /* deferred image request on connect */
static TaskHandle_t s_img_notify_task = NULL; /* task to wake on new image */
/* Forward declarations */ /* Forward declarations */
static void playback_task(void *arg); static void playback_task(void *arg);
static void ws_event_handler(void *arg, esp_event_base_t event_base, static void ws_event_handler(void *arg, esp_event_base_t event_base,
@@ -56,6 +69,13 @@ static void handle_text_frame(const char *data, int len)
return; return;
} }
if (strcmp(type->valuestring, "status_image") == 0) {
ESP_LOGI(TAG, "Status image header received");
s_img_pending = true;
cJSON_Delete(root);
return;
}
if (strcmp(type->valuestring, "alarm_start") == 0) { if (strcmp(type->valuestring, "alarm_start") == 0) {
int sr = 24000; int sr = 24000;
int ch = 2; int ch = 2;
@@ -98,9 +118,25 @@ static void handle_text_frame(const char *data, int len)
static void handle_binary_frame(const uint8_t *data, int len) static void handle_binary_frame(const uint8_t *data, int len)
{ {
/* Status image binary payload */
if (s_img_pending) {
if (len == STATUS_IMG_BYTES) {
memcpy(s_img_buf, data, STATUS_IMG_BYTES);
s_img_updated = true;
if (s_img_notify_task) {
xTaskNotifyGive(s_img_notify_task);
}
ESP_LOGI(TAG, "Status image received (%d bytes)", len);
} else {
ESP_LOGW(TAG, "Status image size mismatch: got %d, expected %d", len, STATUS_IMG_BYTES);
}
s_img_pending = false;
return;
}
if (!s_playing) return; if (!s_playing) return;
uint8_t *chunk = heap_caps_malloc(len, MALLOC_CAP_SPIRAM); uint8_t *chunk = (uint8_t *)heap_caps_malloc(len, MALLOC_CAP_SPIRAM);
if (!chunk) { if (!chunk) {
ESP_LOGW(TAG, "PSRAM alloc failed (%d bytes)", len); ESP_LOGW(TAG, "PSRAM alloc failed (%d bytes)", len);
return; return;
@@ -122,11 +158,17 @@ static void ws_event_handler(void *arg, esp_event_base_t event_base,
case WEBSOCKET_EVENT_CONNECTED: case WEBSOCKET_EVENT_CONNECTED:
ESP_LOGI(TAG, "Audio WS connected"); ESP_LOGI(TAG, "Audio WS connected");
s_state = AUDIO_CONNECTED; s_state = AUDIO_CONNECTED;
s_img_pending = false;
s_need_request_image = true;
if (s_img_notify_task) {
xTaskNotifyGive(s_img_notify_task);
}
break; break;
case WEBSOCKET_EVENT_DISCONNECTED: case WEBSOCKET_EVENT_DISCONNECTED:
ESP_LOGW(TAG, "Audio WS disconnected"); ESP_LOGW(TAG, "Audio WS disconnected");
s_playing = false; s_playing = false;
s_img_pending = false;
flush_queue(); flush_queue();
s_state = AUDIO_IDLE; s_state = AUDIO_IDLE;
break; break;
@@ -172,6 +214,14 @@ void audio_client_init(const char *uri, void *codec)
{ {
s_codec = (CodecPort *)codec; s_codec = (CodecPort *)codec;
/* Initialize status image descriptor */
memset(&s_img_dsc, 0, sizeof(s_img_dsc));
s_img_dsc.header.cf = LV_IMG_CF_ALPHA_1BIT;
s_img_dsc.header.w = STATUS_IMG_W;
s_img_dsc.header.h = STATUS_IMG_H;
s_img_dsc.data_size = STATUS_IMG_BYTES;
s_img_dsc.data = s_img_buf;
s_pcm_queue = xQueueCreate(PCM_QUEUE_DEPTH, sizeof(uint8_t *)); s_pcm_queue = xQueueCreate(PCM_QUEUE_DEPTH, sizeof(uint8_t *));
if (!s_pcm_queue) { if (!s_pcm_queue) {
ESP_LOGE(TAG, "Failed to create PCM queue"); ESP_LOGE(TAG, "Failed to create PCM queue");
@@ -222,3 +272,35 @@ audio_state_t audio_client_get_state(void)
{ {
return s_state; return s_state;
} }
void audio_client_set_image_notify_task(TaskHandle_t task)
{
s_img_notify_task = task;
}
const lv_img_dsc_t *audio_client_get_status_image(bool *updated)
{
if (updated) {
*updated = s_img_updated;
}
return &s_img_dsc;
}
void audio_client_ack_status_image(void)
{
s_img_updated = false;
}
bool audio_client_send_pending_request(void)
{
if (!s_need_request_image || !s_client) return false;
s_need_request_image = false;
static const char REQUEST_IMG_JSON[] = "{\"type\":\"request_image\"}";
int ret = esp_websocket_client_send_text(s_client, REQUEST_IMG_JSON, strlen(REQUEST_IMG_JSON), pdMS_TO_TICKS(1000));
if (ret < 0) {
ESP_LOGE(TAG, "Failed to send image request: %d", ret);
return false;
}
ESP_LOGI(TAG, "Sent image request to server");
return true;
}

View File

@@ -1,6 +1,9 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "lvgl.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -29,6 +32,32 @@ void audio_client_stop(void);
/** Get current audio client state. */ /** Get current audio client state. */
audio_state_t audio_client_get_state(void); audio_state_t audio_client_get_state(void);
/**
* Register a task to be notified (via xTaskNotifyGive) when a new status
* image arrives. Call before audio_client_start().
*/
void audio_client_set_image_notify_task(TaskHandle_t task);
/**
* Get the latest status image descriptor.
* @param updated Set to true if a new image arrived since last call.
* @return Pointer to the static image descriptor (always valid).
*/
const lv_img_dsc_t *audio_client_get_status_image(bool *updated);
/**
* Acknowledge that the status image was successfully rendered.
* Clears the updated flag so subsequent get_status_image calls return false.
*/
void audio_client_ack_status_image(void);
/**
* Send any pending image request to the server.
* Call from a task context (not from an event handler).
* @return true if a request was sent, false if none pending or send failed.
*/
bool audio_client_send_pending_request(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -19,6 +19,7 @@
/* Top bar */ /* Top bar */
static lv_obj_t *lbl_ip; static lv_obj_t *lbl_ip;
static lv_obj_t *lbl_batt; static lv_obj_t *lbl_batt;
static lv_obj_t *lbl_batt_v;
static lv_obj_t *bar_batt_top; static lv_obj_t *bar_batt_top;
static lv_obj_t *lbl_ws; static lv_obj_t *lbl_ws;
@@ -38,12 +39,16 @@ static lv_obj_t *lbl_cpu_temp;
/* Services table */ /* Services table */
static lv_obj_t *tbl_services; static lv_obj_t *tbl_services;
static int s_service_count; static int s_service_count;
static int s_dup_rows; /* duplicate rows appended for looping */
static lv_coord_t s_row_h; /* measured row height in px */
/* Local sensors */ /* Local sensors (bottom bar) */
static lv_obj_t *lbl_room_temp; static lv_obj_t *lbl_local;
static lv_obj_t *lbl_room_humi;
static lv_obj_t *lbl_uptime; static lv_obj_t *lbl_uptime;
/* Status image placeholder */
static lv_obj_t *img_status;
/* Network */ /* Network */
static lv_obj_t *lbl_net; static lv_obj_t *lbl_net;
@@ -113,21 +118,54 @@ static lv_obj_t *create_label(lv_obj_t *parent, int x, int y, const lv_font_t *f
static void scroll_timer_cb(lv_timer_t *timer) static void scroll_timer_cb(lv_timer_t *timer)
{ {
(void)timer; (void)timer;
if (s_service_count <= 4) { if (s_dup_rows <= 0 || s_row_h <= 0) {
/* Too few services or not yet measured — no scrolling */
lv_obj_scroll_to_y(tbl_services, 0, LV_ANIM_OFF); lv_obj_scroll_to_y(tbl_services, 0, LV_ANIM_OFF);
return; return;
} }
lv_coord_t cur_y = lv_obj_get_scroll_y(tbl_services); lv_coord_t cur_y = lv_obj_get_scroll_y(tbl_services);
/* Each row is ~16px (12px font + 2px pad top + 2px pad bot) */ lv_coord_t wrap_y = s_service_count * s_row_h;
lv_coord_t row_h = 16;
lv_coord_t max_scroll = (s_service_count - 4) * row_h;
if (cur_y >= max_scroll) { if (cur_y >= wrap_y) {
lv_obj_scroll_to_y(tbl_services, 0, LV_ANIM_ON); lv_obj_scroll_to_y(tbl_services, 0, LV_ANIM_OFF);
} else { cur_y = 0;
lv_obj_scroll_to_y(tbl_services, cur_y + row_h, LV_ANIM_ON);
} }
// lv_obj_scroll_to_y(tbl_services, cur_y + s_row_h, LV_ANIM_OFF);
const int delta_y = s_row_h / 8;
lv_obj_scroll_to_y(tbl_services, cur_y + delta_y, LV_ANIM_ON);
}
/* Measure row height, compute visible rows, append duplicates for seamless loop */
static void fill_duplicate_rows(int count)
{
s_dup_rows = 0;
s_row_h = 0;
if (count <= 0) return;
/* Trim table to exactly count rows so measurement is clean */
lv_table_set_row_cnt(tbl_services, count);
lv_obj_update_layout(tbl_services);
lv_coord_t content_h = lv_obj_get_self_height(tbl_services);
lv_coord_t row_h = content_h / count;
if (row_h <= 0) return;
lv_coord_t visible_h = lv_obj_get_content_height(tbl_services);
int visible_rows = (visible_h + row_h - 1) / row_h; /* ceil */
if (count <= visible_rows) return; /* everything fits — no scrolling */
for (int i = 0; i < visible_rows; i++) {
lv_table_set_cell_value(tbl_services, count + i, 0,
lv_table_get_cell_value(tbl_services, i, 0));
lv_table_set_cell_value(tbl_services, count + i, 1,
lv_table_get_cell_value(tbl_services, i, 1));
}
s_dup_rows = visible_rows;
s_row_h = row_h;
} }
/* ---------- Create UI sections ---------- */ /* ---------- Create UI sections ---------- */
@@ -158,6 +196,13 @@ static void create_top_bar(lv_obj_t *parent)
lv_obj_align(lbl_ws, LV_ALIGN_LEFT_MID, 140, 0); lv_obj_align(lbl_ws, LV_ALIGN_LEFT_MID, 140, 0);
lv_label_set_text(lbl_ws, "WS:---"); lv_label_set_text(lbl_ws, "WS:---");
/* Battery voltage — left of bar */
lbl_batt_v = lv_label_create(bar_cont);
lv_obj_set_style_text_color(lbl_batt_v, lv_color_white(), 0);
lv_obj_set_style_text_font(lbl_batt_v, &InziuIosevka_Slab_CC_12px, 0);
lv_obj_align(lbl_batt_v, LV_ALIGN_RIGHT_MID, -72, 0);
lv_label_set_text(lbl_batt_v, "-.--V");
/* Battery bar (24x10) — right */ /* Battery bar (24x10) — right */
bar_batt_top = lv_bar_create(bar_cont); bar_batt_top = lv_bar_create(bar_cont);
lv_obj_set_size(bar_batt_top, 24, 10); lv_obj_set_size(bar_batt_top, 24, 10);
@@ -167,6 +212,17 @@ static void create_top_bar(lv_obj_t *parent)
lv_obj_add_style(bar_batt_top, &style_bar_batt_bg, LV_PART_MAIN); lv_obj_add_style(bar_batt_top, &style_bar_batt_bg, LV_PART_MAIN);
lv_obj_add_style(bar_batt_top, &style_bar_batt_ind, LV_PART_INDICATOR); lv_obj_add_style(bar_batt_top, &style_bar_batt_ind, LV_PART_INDICATOR);
/* Battery positive terminal nub */
lv_obj_t *batt_nub = lv_obj_create(bar_cont);
lv_obj_set_size(batt_nub, 2, 4);
lv_obj_align(batt_nub, LV_ALIGN_RIGHT_MID, -38, 0);
lv_obj_set_style_bg_color(batt_nub, lv_color_white(), 0);
lv_obj_set_style_bg_opa(batt_nub, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(batt_nub, 0, 0);
lv_obj_set_style_radius(batt_nub, 0, 0);
lv_obj_set_style_pad_all(batt_nub, 0, 0);
lv_obj_clear_flag(batt_nub, LV_OBJ_FLAG_SCROLLABLE);
/* Battery text — right of bar */ /* Battery text — right of bar */
lbl_batt = lv_label_create(bar_cont); lbl_batt = lv_label_create(bar_cont);
lv_obj_set_style_text_color(lbl_batt, lv_color_white(), 0); lv_obj_set_style_text_color(lbl_batt, lv_color_white(), 0);
@@ -200,18 +256,18 @@ static void create_time_bar(lv_obj_t *parent)
lbl_date = lv_label_create(bar_cont); lbl_date = lv_label_create(bar_cont);
lv_obj_set_style_text_font(lbl_date, &InziuIosevka_Slab_CC_20px, 0); lv_obj_set_style_text_font(lbl_date, &InziuIosevka_Slab_CC_20px, 0);
lv_obj_set_style_text_color(lbl_date, lv_color_black(), 0); lv_obj_set_style_text_color(lbl_date, lv_color_black(), 0);
lv_obj_align(lbl_date, LV_ALIGN_RIGHT_MID, -10, 0); lv_obj_align(lbl_date, LV_ALIGN_RIGHT_MID, -20, 0);
lv_label_set_text(lbl_date, "----/--/-- ---"); lv_label_set_text(lbl_date, "----/--/-- ---");
} }
static void create_main_section(lv_obj_t *parent) static void create_main_section(lv_obj_t *parent)
{ {
/* === Left column: Services === */ /* === Left column: Services + Pi Vitals === */
create_label(parent, 4, MAIN_Y + 2, &InziuIosevka_Slab_CC_12px, "SERVICES"); create_label(parent, 4, MAIN_Y + 1, &InziuIosevka_Slab_CC_12px, "SERVICES");
tbl_services = lv_table_create(parent); tbl_services = lv_table_create(parent);
lv_obj_set_pos(tbl_services, 4, MAIN_Y + 16); lv_obj_set_pos(tbl_services, 4, MAIN_Y + 16);
lv_obj_set_size(tbl_services, 190, 180); lv_obj_set_size(tbl_services, 190, 82);
lv_table_set_col_cnt(tbl_services, 2); lv_table_set_col_cnt(tbl_services, 2);
lv_table_set_col_width(tbl_services, 0, 110); lv_table_set_col_width(tbl_services, 0, 110);
lv_table_set_col_width(tbl_services, 1, 65); lv_table_set_col_width(tbl_services, 1, 65);
@@ -227,22 +283,22 @@ static void create_main_section(lv_obj_t *parent)
lv_table_set_cell_value(tbl_services, i, 1, "---"); lv_table_set_cell_value(tbl_services, i, 1, "---");
} }
/* Auto-scroll timer: 3 second period */ /* Timer for each shift of 1/8 line's height */
s_scroll_timer = lv_timer_create(scroll_timer_cb, 3000, NULL); s_scroll_timer = lv_timer_create(scroll_timer_cb, 120, NULL);
/* === Right column: Pi Vitals + Local Sensors === */ /* === Left column: Pi Vitals (below services) === */
int rx = RIGHT_COL_X; int rx = 0;
int row_h = 18; /* vertical spacing per stat row */ int row_h = 18; /* vertical spacing per stat row */
int lbl_w = 36; /* width reserved for "CPU " etc */ int lbl_w = 36; /* width reserved for "CPU " etc */
int bar_w = 82; int bar_w = 82;
int bar_h = 12; int bar_h = 12;
int val_x = rx + 4 + lbl_w + bar_w + 4; /* value label after bar */ int val_x = rx + 4 + lbl_w + bar_w + 4; /* value label after bar */
int temp_x = rx + 156; /* TEMP column, right of value labels */ int temp_x = rx + 155; /* TEMP column, right of value labels */
/* Pi Vitals header */ /* Pi Vitals header */
create_label(parent, rx + 4, MAIN_Y + 2, &InziuIosevka_Slab_CC_12px, "PI VITALS"); create_label(parent, rx + 4, 175, &InziuIosevka_Slab_CC_12px, "PI VITALS");
int ry = MAIN_Y + 18; int ry = 192;
/* CPU [========] 12% TEMP */ /* CPU [========] 12% TEMP */
create_label(parent, rx + 4, ry, &InziuIosevka_Slab_CC_12px, "CPU"); create_label(parent, rx + 4, ry, &InziuIosevka_Slab_CC_12px, "CPU");
@@ -267,11 +323,12 @@ static void create_main_section(lv_obj_t *parent)
ry += row_h; ry += row_h;
lbl_uptime = create_label(parent, rx + 4, ry, &InziuIosevka_Slab_CC_12px, "Uptime: --h"); lbl_uptime = create_label(parent, rx + 4, ry, &InziuIosevka_Slab_CC_12px, "Uptime: --h");
/* === Local Sensors (below pi vitals) === */ /* === Right column: Status image (200x200) === */
int sy = ry + row_h + 4; img_status = lv_img_create(parent);
create_label(parent, rx + 4, sy, &InziuIosevka_Slab_CC_12px, "LOCAL SENSORS"); lv_obj_set_pos(img_status, 200, MAIN_Y + 1);
lbl_room_temp = create_label(parent, rx + 4, sy + 16, &InziuIosevka_Slab_CC_16px, "Room: --.-C"); lv_obj_set_size(img_status, 200, 200);
lbl_room_humi = create_label(parent, rx + 4, sy + 42, &InziuIosevka_Slab_CC_16px, "Humi: --%"); lv_obj_set_style_bg_color(img_status, lv_color_white(), 0);
lv_obj_set_style_bg_opa(img_status, LV_OPA_COVER, 0);
} }
static void create_bottom_bar(lv_obj_t *parent) static void create_bottom_bar(lv_obj_t *parent)
@@ -281,7 +338,7 @@ static void create_bottom_bar(lv_obj_t *parent)
lv_obj_t *bot_cont = lv_obj_create(parent); lv_obj_t *bot_cont = lv_obj_create(parent);
lv_obj_set_pos(bot_cont, 0, y0); lv_obj_set_pos(bot_cont, 0, y0);
lv_obj_set_size(bot_cont, SCREEN_W, BOT_H); lv_obj_set_size(bot_cont, SCREEN_W, BOT_H);
lv_obj_set_style_bg_color(bot_cont, lv_color_white(), 0); lv_obj_set_style_bg_color(bot_cont, lv_color_black(), 0);
lv_obj_set_style_bg_opa(bot_cont, LV_OPA_COVER, 0); lv_obj_set_style_bg_opa(bot_cont, LV_OPA_COVER, 0);
lv_obj_set_style_border_color(bot_cont, lv_color_black(), 0); lv_obj_set_style_border_color(bot_cont, lv_color_black(), 0);
lv_obj_set_style_border_width(bot_cont, 1, 0); lv_obj_set_style_border_width(bot_cont, 1, 0);
@@ -292,9 +349,16 @@ static void create_bottom_bar(lv_obj_t *parent)
lbl_net = lv_label_create(bot_cont); lbl_net = lv_label_create(bot_cont);
lv_obj_set_style_text_font(lbl_net, &InziuIosevka_Slab_CC_12px, 0); lv_obj_set_style_text_font(lbl_net, &InziuIosevka_Slab_CC_12px, 0);
lv_obj_set_style_text_color(lbl_net, lv_color_black(), 0); lv_obj_set_style_text_color(lbl_net, lv_color_white(), 0);
lv_obj_align(lbl_net, LV_ALIGN_LEFT_MID, 0, 0); lv_obj_align(lbl_net, LV_ALIGN_LEFT_MID, 0, 0);
lv_label_set_text(lbl_net, "NETWORK RX: ---- kbps TX: ---- kbps"); lv_label_set_text(lbl_net, "NETWORK DOWN: ---- kBps / UP: ---- kBps");
/* Local sensor readings — right-aligned */
lbl_local = lv_label_create(bot_cont);
lv_obj_set_style_text_font(lbl_local, &InziuIosevka_Slab_CC_12px, 0);
lv_obj_set_style_text_color(lbl_local, lv_color_white(), 0);
lv_obj_align(lbl_local, LV_ALIGN_RIGHT_MID, 0, 0);
lv_label_set_text(lbl_local, "T: --.- H: --%");
} }
void dashboard_ui_create(void) void dashboard_ui_create(void)
@@ -340,15 +404,17 @@ void dashboard_ui_update_stats(const pi_stats_t *stats)
/* Services table */ /* Services table */
s_service_count = stats->service_count; s_service_count = stats->service_count;
for (int i = 0; i < stats->service_count && i < WS_MAX_SERVICES; i++) { for (int i = 0; i < stats->service_count && i < WS_MAX_SERVICES; i++) {
const char *tag;
switch (stats->services[i].status) {
case SVC_RUNNING: tag = "[RUN]"; break;
case SVC_WARNING: tag = "[WARN]"; break;
default: tag = "[STOP]"; break;
}
lv_table_set_cell_value(tbl_services, i, 0, stats->services[i].name); lv_table_set_cell_value(tbl_services, i, 0, stats->services[i].name);
lv_table_set_cell_value(tbl_services, i, 1, lv_table_set_cell_value(tbl_services, i, 1, tag);
stats->services[i].running ? "[RUN]" : "[STOP]");
}
/* Clear unused rows */
for (int i = stats->service_count; i < WS_MAX_SERVICES; i++) {
lv_table_set_cell_value(tbl_services, i, 0, "");
lv_table_set_cell_value(tbl_services, i, 1, "");
} }
/* Measure row height, compute visible rows, append duplicates */
fill_duplicate_rows(stats->service_count);
/* Uptime */ /* Uptime */
snprintf(buf, sizeof(buf), "Uptime: %.0fh", stats->uptime_hrs); snprintf(buf, sizeof(buf), "Uptime: %.0fh", stats->uptime_hrs);
@@ -356,23 +422,24 @@ void dashboard_ui_update_stats(const pi_stats_t *stats)
/* Network */ /* Network */
char net_buf[64]; char net_buf[64];
snprintf(net_buf, sizeof(net_buf), "NETWORK RX: %.0f kbps TX: %.0f kbps", snprintf(net_buf, sizeof(net_buf), "NETWORK DOWN: %.0f kBps / UP: %.0f kBps",
stats->net_rx_kbps, stats->net_tx_kbps); stats->net_rx_kbps, stats->net_tx_kbps);
lv_label_set_text(lbl_net, net_buf); lv_label_set_text(lbl_net, net_buf);
} }
void dashboard_ui_update_local(float temp, float humidity, uint8_t battery) void dashboard_ui_update_local(float temp, float humidity, uint8_t battery, bool charging, float voltage)
{ {
char buf[32]; char buf[32];
snprintf(buf, sizeof(buf), "Room: %.1fC", temp); snprintf(buf, sizeof(buf), "T: %.1f H: %.0f%%", temp, humidity);
lv_label_set_text(lbl_room_temp, buf); lv_label_set_text(lbl_local, buf);
snprintf(buf, sizeof(buf), "Humi: %.0f%%", humidity); /* Update top bar battery voltage */
lv_label_set_text(lbl_room_humi, buf); snprintf(buf, sizeof(buf), "%.2fV", voltage);
lv_label_set_text(lbl_batt_v, buf);
/* Update top bar battery text + bar */ /* Update top bar battery text + bar */
snprintf(buf, sizeof(buf), "%d%%", battery); snprintf(buf, sizeof(buf), charging ? "%d%%C" : "%d%%", battery);
lv_label_set_text(lbl_batt, buf); lv_label_set_text(lbl_batt, buf);
lv_bar_set_value(bar_batt_top, battery, LV_ANIM_OFF); lv_bar_set_value(bar_batt_top, battery, LV_ANIM_OFF);
} }
@@ -406,3 +473,10 @@ void dashboard_ui_update_connection(ws_state_t ws_state, bool wifi_connected, co
} }
lv_label_set_text(lbl_ws, ws_str); lv_label_set_text(lbl_ws, ws_str);
} }
void dashboard_ui_update_status_image(const lv_img_dsc_t *dsc)
{
if (dsc) {
lv_img_set_src(img_status, dsc);
}
}

View File

@@ -21,7 +21,7 @@ void dashboard_ui_update_stats(const pi_stats_t *stats);
/** /**
* Update local sensor readings. LVGL lock must be held by caller. * Update local sensor readings. LVGL lock must be held by caller.
*/ */
void dashboard_ui_update_local(float temp, float humidity, uint8_t battery); void dashboard_ui_update_local(float temp, float humidity, uint8_t battery, bool charging, float voltage);
/** /**
* Update clock and date display. LVGL lock must be held by caller. * Update clock and date display. LVGL lock must be held by caller.
@@ -35,6 +35,12 @@ void dashboard_ui_update_time(int h, int m, int s, int year, int month, int day,
*/ */
void dashboard_ui_update_connection(ws_state_t ws_state, bool wifi_connected, const char *ip_str); void dashboard_ui_update_connection(ws_state_t ws_state, bool wifi_connected, const char *ip_str);
/**
* Update the status image widget. LVGL lock must be held by caller.
* @param dsc Image descriptor (1-bit monochrome, 120x120)
*/
void dashboard_ui_update_status_image(const lv_img_dsc_t *dsc);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -48,3 +48,7 @@ uint8_t Adc_GetBatteryLevel() {
//ESP_LOGW("Battery","Voltage: %.3f V, Level: %.1f %%",vol,level); //ESP_LOGW("Battery","Voltage: %.3f V, Level: %.1f %%",vol,level);
return (uint8_t)level; return (uint8_t)level;
} }
bool Adc_IsCharging() {
return Adc_GetBatteryVoltage() > 4.10f;
}

View File

@@ -5,3 +5,4 @@
void Adc_PortInit(); void Adc_PortInit();
float Adc_GetBatteryVoltage(); float Adc_GetBatteryVoltage();
uint8_t Adc_GetBatteryLevel(); uint8_t Adc_GetBatteryLevel();
bool Adc_IsCharging();

View File

@@ -43,7 +43,7 @@ static const int s_beep_count[ALERT_TYPE_COUNT] = {
0, /* SERVICE_DOWN: triple beep */ /* DISABLED FOR TESTING */ 0, /* SERVICE_DOWN: triple beep */ /* DISABLED FOR TESTING */
2, /* HIGH_TEMP: double beep */ 2, /* HIGH_TEMP: double beep */
0, /* WS_DISCONNECT: single beep */ /* DISABLED FOR TESTING */ 0, /* WS_DISCONNECT: single beep */ /* DISABLED FOR TESTING */
1, /* CONNECT_OK: single short beep */ 0, /* CONNECT_OK: single short beep */ /* DISABLED FOR TESTING */
}; };
static void generate_tone_buffer(void) static void generate_tone_buffer(void)

View File

@@ -82,11 +82,13 @@ void UserApp_TaskInit(void)
/* Start WebSocket client */ /* Start WebSocket client */
ws_client_start(); ws_client_start();
/* Start audio streaming client */
audio_client_start();
/* Sensor polling task - Core 1, 4KB stack */ /* Sensor polling task - Core 1, 4KB stack */
xTaskCreatePinnedToCore(sensor_task, "sensor", 4 * 1024, NULL, 3, NULL, 1); TaskHandle_t sensor_handle = NULL;
xTaskCreatePinnedToCore(sensor_task, "sensor", 4 * 1024, NULL, 3, &sensor_handle, 1);
/* Start audio streaming client, notify sensor task on new images */
audio_client_set_image_notify_task(sensor_handle);
audio_client_start();
/* Button handling task - Core 1 */ /* Button handling task - Core 1 */
xTaskCreatePinnedToCore(button_task, "button", 2 * 1024, NULL, 2, NULL, 1); xTaskCreatePinnedToCore(button_task, "button", 2 * 1024, NULL, 2, NULL, 1);
@@ -131,7 +133,7 @@ static void ws_data_cb(const pi_stats_t *stats)
} }
for (int i = 0; i < stats->service_count; i++) { for (int i = 0; i < stats->service_count; i++) {
if (!stats->services[i].running) { if (stats->services[i].status != SVC_RUNNING) {
alert_trigger(ALERT_SERVICE_DOWN); alert_trigger(ALERT_SERVICE_DOWN);
break; break;
} }
@@ -182,9 +184,36 @@ static void sensor_task(void *arg)
float temp = 0, humidity = 0; float temp = 0, humidity = 0;
rtcTimeStruct_t rtc_time = {}; rtcTimeStruct_t rtc_time = {};
int sensor_divider = 0; int sensor_divider = 0;
TickType_t last_sensor_tick = xTaskGetTickCount();
for (;;) { for (;;) {
/* Read RTC every second */ /*
* Sleep until either:
* - a new status image arrives (xTaskNotifyGive from audio_client), or
* - 1 second elapses (clock refresh cadence)
*/
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000));
/* Send deferred image request if connect just happened */
audio_client_send_pending_request();
/* Check for status image updates immediately */
bool img_updated = false;
const lv_img_dsc_t *img = audio_client_get_status_image(&img_updated);
if (img_updated && Lvgl_lock(100)) {
dashboard_ui_update_status_image(img);
Lvgl_unlock();
audio_client_ack_status_image();
}
/* Sensor + clock updates at ~1s cadence (skip if woken early) */
TickType_t now = xTaskGetTickCount();
if ((now - last_sensor_tick) < pdMS_TO_TICKS(900)) {
continue;
}
last_sensor_tick = now;
/* Read RTC */
Rtc_GetTime(&rtc_time); Rtc_GetTime(&rtc_time);
/* Read SHTC3 + battery every 5 seconds */ /* Read SHTC3 + battery every 5 seconds */
@@ -194,24 +223,24 @@ static void sensor_task(void *arg)
s_shtc3->Shtc3_ReadTempHumi(&temp, &humidity); s_shtc3->Shtc3_ReadTempHumi(&temp, &humidity);
s_shtc3->Shtc3_Sleep(); s_shtc3->Shtc3_Sleep();
float batt_v = Adc_GetBatteryVoltage();
uint8_t batt = Adc_GetBatteryLevel(); uint8_t batt = Adc_GetBatteryLevel();
bool charging = Adc_IsCharging();
if (Lvgl_lock(100)) { if (Lvgl_lock(100)) {
dashboard_ui_update_local(temp, humidity, batt); dashboard_ui_update_local(temp, humidity, batt, charging, batt_v);
Lvgl_unlock(); Lvgl_unlock();
} }
} }
sensor_divider = (sensor_divider + 1) % 5; sensor_divider = (sensor_divider + 1) % 5;
/* Update clock every second */ /* Update clock */
if (Lvgl_lock(100)) { if (Lvgl_lock(100)) {
dashboard_ui_update_time(rtc_time.hour, rtc_time.minute, rtc_time.second, dashboard_ui_update_time(rtc_time.hour, rtc_time.minute, rtc_time.second,
rtc_time.year, rtc_time.month, rtc_time.day, rtc_time.year, rtc_time.month, rtc_time.day,
rtc_time.week); rtc_time.week);
Lvgl_unlock(); Lvgl_unlock();
} }
vTaskDelay(pdMS_TO_TICKS(1000));
} }
} }
@@ -220,10 +249,10 @@ static void sensor_task(void *arg)
static void button_task(void *arg) static void button_task(void *arg)
{ {
for (;;) { for (;;) {
/* Wait for GP18 button event (single click = bit 0) */ /* Wait for GP18 button event (single click = bit 0, long press = bit 2) */
EventBits_t bits = xEventGroupWaitBits( EventBits_t bits = xEventGroupWaitBits(
GP18ButtonGroups, GP18ButtonGroups,
set_bit_button(0), /* single press bit */ set_bit_button(0) | set_bit_button(2),
pdTRUE, /* clear on exit */ pdTRUE, /* clear on exit */
pdFALSE, /* any bit */ pdFALSE, /* any bit */
pdMS_TO_TICKS(500) pdMS_TO_TICKS(500)
@@ -232,7 +261,12 @@ static void button_task(void *arg)
if (bits & set_bit_button(0)) { if (bits & set_bit_button(0)) {
bool muted = !alert_is_muted(); bool muted = !alert_is_muted();
alert_mute(muted); alert_mute(muted);
ESP_LOGI(TAG, "GP18 pressed: alerts %s", muted ? "muted" : "unmuted"); ESP_LOGI(TAG, "GP18 single press: alerts %s", muted ? "muted" : "unmuted");
}
if (bits & set_bit_button(2)) {
ESP_LOGI(TAG, "GP18 long press: forcing WS reconnect");
ws_client_reconnect();
} }
} }
} }

View File

@@ -92,7 +92,13 @@ static void parse_stats_json(const char *data, int len)
s_stats.services[i].name[WS_SERVICE_NAME_LEN - 1] = '\0'; s_stats.services[i].name[WS_SERVICE_NAME_LEN - 1] = '\0';
} }
if (status && status->valuestring) { if (status && status->valuestring) {
s_stats.services[i].running = (strcmp(status->valuestring, "running") == 0); if (strcmp(status->valuestring, "running") == 0) {
s_stats.services[i].status = SVC_RUNNING;
} else if (strcmp(status->valuestring, "warning") == 0) {
s_stats.services[i].status = SVC_WARNING;
} else {
s_stats.services[i].status = SVC_STOPPED;
}
} }
} }
} }
@@ -204,6 +210,14 @@ void ws_client_stop(void)
} }
} }
void ws_client_reconnect(void)
{
if (!s_client) return;
ESP_LOGI(TAG, "Manual WS reconnect triggered");
esp_websocket_client_close(s_client, pdMS_TO_TICKS(2000));
s_last_data_tick = xTaskGetTickCount();
}
ws_state_t ws_client_get_state(void) ws_state_t ws_client_get_state(void)
{ {
return s_state; return s_state;

View File

@@ -8,7 +8,7 @@
extern "C" { extern "C" {
#endif #endif
#define WS_MAX_SERVICES 8 #define WS_MAX_SERVICES 20
#define WS_SERVICE_NAME_LEN 16 #define WS_SERVICE_NAME_LEN 16
typedef enum { typedef enum {
@@ -18,9 +18,15 @@ typedef enum {
WS_STATE_ERROR, WS_STATE_ERROR,
} ws_state_t; } ws_state_t;
typedef enum {
SVC_STOPPED = 0,
SVC_WARNING,
SVC_RUNNING,
} ws_svc_status_t;
typedef struct { typedef struct {
char name[WS_SERVICE_NAME_LEN]; char name[WS_SERVICE_NAME_LEN];
bool running; ws_svc_status_t status;
} ws_service_t; } ws_service_t;
typedef struct { typedef struct {
@@ -53,6 +59,7 @@ typedef void (*ws_state_callback_t)(ws_state_t state);
void ws_client_init(const char *uri); void ws_client_init(const char *uri);
void ws_client_start(void); void ws_client_start(void);
void ws_client_stop(void); void ws_client_stop(void);
void ws_client_reconnect(void);
ws_state_t ws_client_get_state(void); ws_state_t ws_client_get_state(void);
void ws_client_get_stats(pi_stats_t *out); void ws_client_get_stats(pi_stats_t *out);
void ws_client_set_data_callback(ws_data_callback_t cb); void ws_client_set_data_callback(ws_data_callback_t cb);

View File

@@ -1,87 +0,0 @@
# Alarm Audio Streaming Protocol
## Overview
The Raspberry Pi streams alarm audio to the ESP32-S3 over a dedicated WebSocket connection on **port 8766**. The protocol uses mixed text and binary frames — text for control messages, binary for raw PCM data.
## Connection
| Parameter | Value |
|-----------|-------|
| Port | 8766 |
| Transport | WebSocket |
| Direction | Pi (server) → ESP32 (client) |
The ESP32 connects and stays connected. The server initiates alarm playback when needed.
## Message Sequence
```
Pi ESP32
│ │
│◄──── WS connect ──────────│
│ │
│ (idle until alarm fires) │
│ │
├─ alarm_start (text) ──────►│ → open codec
├─ PCM chunk (binary) ──────►│ → queue + play
├─ PCM chunk (binary) ──────►│
│ ... │
├─ alarm_stop (text) ────────►│ → drain + close codec
│ │
```
## Control Messages (Text Frames)
### alarm_start
Sent before the first PCM chunk. The ESP32 uses these parameters to configure the DAC.
```json
{
"type": "alarm_start",
"sample_rate": 24000,
"channels": 2,
"bits": 16
}
```
### alarm_stop
Sent after the last PCM chunk.
```json
{
"type": "alarm_stop"
}
```
## PCM Data (Binary Frames)
- **Format:** Raw signed 16-bit little-endian, interleaved stereo (L, R, L, R, ...)
- **Chunk size:** 4096 bytes (1024 stereo samples at 16-bit)
- **Pacing:** Chunks sent at ~90% real-time speed to maintain buffer without overflow
- **Byte order:** Little-endian (native for both Pi and ESP32)
At 24kHz / 2ch / 16-bit:
- Bytes per second: 96,000
- Chunk duration: ~42.7 ms
- Send interval: ~38.4 ms (90% pacing)
## ESP32 Buffering
- FreeRTOS queue: 10 slots of pointer-to-chunk
- Total buffer capacity: ~427 ms of audio
- Overflow policy: drop chunk and log warning
## Future: Alarm Scheduling
Not yet implemented. Planned JSON format for alarm configuration:
```json
{
"alarm_time": "070000",
"alarm_days": ["Mon", "Tue", "Wed"],
"alarm_audio": "path/to/file.wav"
}
```

4
pi/.gitignore vendored
View File

@@ -3,3 +3,7 @@
__pycache__/ __pycache__/
*.pyo *.pyo
*.pyc *.pyc
# configs
config/
!config/alarms.sample.json

36
pi/PLAN.md Normal file
View File

@@ -0,0 +1,36 @@
# Pi Servers -- Roadmap
## Docker Compose
Containerize the pi servers for easier deployment.
### Options
1. **Single service** -- `run_all.py` as the entrypoint, both servers in one container
2. **Split services** -- separate containers for `stats_server.py` and `contents_server.py`
Single service is simpler. Split services allow independent scaling and restarts.
### Configuration
- Volume mount `assets/` and `config/alarms.json` so they're editable without rebuilding
- Expose ports 8765 and 8766
- Network mode `host` or a bridge with known IPs for ESP32 discovery
- Restart policy: `unless-stopped`
## Repository Extraction
The `pi/` directory will become its own git repository.
### Steps
1. Extract `pi/` into a standalone repo with its own `README.md`, `requirements.txt`, and CI
2. Add it back to this project as a git submodule
3. The interface contract between the two repos is the WebSocket protocol -- JSON schemas and binary frame formats documented in `README.md`
### Benefits
- Independent versioning and release cycle
- Pi-side contributors don't need the ESP-IDF toolchain
- CI can test the Python servers in isolation
- Cleaner separation of concerns between embedded firmware and host services

132
pi/README.md Normal file
View File

@@ -0,0 +1,132 @@
# Pi Dashboard Servers
WebSocket servers that feed system stats, alarm audio, and status images to the ESP32-S3 RLCD dashboard.
## File Structure
```
pi/
run_all.py # Launches both servers as child processes
stats_server.py # Real system stats over WebSocket (port 8765)
contents_server.py # Alarm audio + status images over WebSocket (port 8766)
mock_server.py # Drop-in replacement for stats_server with random data
audio_handler.py # WAV loading, PCM chunking, alarm streaming
image_handler.py # PNG to 1-bit monochrome conversion, alpha compositing
alarm_scheduler.py # Loads and validates alarm config, checks firing schedule
requirements.txt
config/
alarms.json # Alarm schedule configuration
assets/
alarm/ # WAV files for alarm audio
img/ # Status images (idle.png, on_alarm.png)
```
## Requirements
Python 3.10+
```
pip install -r requirements.txt
```
Dependencies: `websockets`, `psutil`, `Pillow`
## Running
Start both servers:
```
python run_all.py # both servers, default config
python run_all.py --config path/to.json # both servers, custom config
```
Or run individually:
```
python stats_server.py # port 8765 only
python contents_server.py --config path/to.json # port 8766, custom config
python mock_server.py # port 8765, random data (no psutil needed)
```
## Servers
### stats_server.py -- port 8765
Pushes a JSON object every 2 seconds with real system metrics from `psutil`:
- `cpu_pct`, `mem_pct`, `mem_used_mb`, `disk_pct`
- `cpu_temp` (reads `/sys/class/thermal/` as fallback)
- `uptime_hrs`, `net_rx_kbps`, `net_tx_kbps`
- `services` (mocked until systemd integration)
- `local_time` fields for RTC sync (`y`, `mo`, `d`, `h`, `m`, `s`)
### contents_server.py -- port 8766
Serves alarm audio and status images. Protocol:
**Status image:**
1. Text frame: `{"type":"status_image","width":120,"height":120}`
2. Binary frame: 1-bit monochrome bitmap (1800 bytes)
**Alarm audio:**
1. Text frame: `{"type":"alarm_start","sample_rate":N,"channels":N,"bits":N}`
2. Binary frames: raw PCM chunks (4096 bytes each, paced at ~90% real-time)
3. Text frame: `{"type":"alarm_stop"}`
Loads alarm config from `config/alarms.json` (override with `--config`). Checks schedule every 5 seconds, fires once per matched minute. If no config or empty config, sends idle image and blocks forever. On alarm: switches to alarm image, streams audio, switches back to idle.
### mock_server.py -- port 8765
Same JSON schema and 2-second push interval as `stats_server.py`, but all values are randomized. No `psutil` dependency -- useful for development on non-Pi machines.
Does not include `local_time` fields.
## Alarm Configuration
Config file: `config/alarms.json` -- a single alarm object or an array of alarm objects.
Example with two alarms:
```json
[
{
"alarm_time": "0730",
"alarm_days": ["Mon", "Tue", "Wed", "Thu", "Fri"],
"alarm_audio": "assets/alarm/alarm_test.wav",
"alarm_image": "assets/img/on_alarm.png"
},
{
"alarm_time": "2300",
"alarm_audio": "assets/alarm/sleep.wav",
"alarm_image": "assets/img/sleep.png"
}
]
```
| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `alarm_time` | `string` | Yes | 4-digit HHMM, 24-hour. Fires on the matched minute. |
| `alarm_days` | `string[]` | No | 3-letter abbreviations: `Mon``Sun`. If omitted, fires every day. |
| `alarm_dates` | `string[]` | No | `MM/DD` strings. Ignored if `alarm_days` is also set. |
| `alarm_audio` | `string` | No | WAV path, relative to `pi/`. Default: `assets/alarm/alarm_test.wav`. |
| `alarm_image` | `string` | No | Status PNG path, relative to `pi/`. Default: `assets/img/on_alarm.png`. |
If both `alarm_days` and `alarm_dates` are present, `alarm_days` takes priority.
## Modules
### audio_handler.py
- `find_wav(path=None)` -- uses the given path if it exists, otherwise falls back to glob in `assets/alarm/`
- `read_wav(path)` -- reads WAV, returns `(pcm_bytes, sample_rate, channels, bits)`
- `stream_alarm(ws, pcm, sr, ch, bits)` -- streams one alarm cycle over WebSocket
### image_handler.py
- `load_status_image(path)` -- loads PNG, composites transparency onto white, converts to 1-bit 120x120 monochrome bitmap (black=1, MSB-first)
- `send_status_image(ws, img_bytes)` -- sends status image header + binary over WebSocket
### alarm_scheduler.py
- `load_config(path)` -- reads and validates alarm JSON; returns list of alarm dicts or `None`
- `should_fire(config)` -- checks a single alarm entry against current local time

135
pi/alarm_scheduler.py Normal file
View File

@@ -0,0 +1,135 @@
"""Alarm scheduler — load config and check firing schedule."""
import json
import logging
import re
from datetime import datetime
from pathlib import Path
log = logging.getLogger(__name__)
DEFAULT_CONFIG_PATH = Path(__file__).parent / "config" / "alarms.json"
VALID_DAYS = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
TIME_RE = re.compile(r"^([01]\d|2[0-3])[0-5]\d$")
DATE_RE = re.compile(r"^(0[1-9]|1[0-2])/(0[1-9]|[12]\d|3[01])$")
def _validate_entry(entry: dict, index: int) -> dict | None:
"""Validate a single alarm entry. Returns it if valid, None otherwise."""
if not isinstance(entry, dict):
log.warning("Alarm #%d: expected object, got %s", index, type(entry).__name__)
return None
alarm_time = entry.get("alarm_time")
if alarm_time is None:
log.warning("Alarm #%d: missing required field 'alarm_time'", index)
return None
if not isinstance(alarm_time, str) or not TIME_RE.match(alarm_time):
log.warning("Alarm #%d: invalid alarm_time '%s' — must be 4-digit HHMM", index, alarm_time)
return None
alarm_days = entry.get("alarm_days")
if alarm_days is not None:
if not isinstance(alarm_days, list) or not all(isinstance(d, str) for d in alarm_days):
log.warning("Alarm #%d: alarm_days must be a list of strings", index)
return None
bad = [d for d in alarm_days if d not in VALID_DAYS]
if bad:
log.warning("Alarm #%d: invalid day abbreviations: %s", index, bad)
return None
alarm_dates = entry.get("alarm_dates")
if alarm_dates is not None:
if not isinstance(alarm_dates, list) or not all(isinstance(d, str) for d in alarm_dates):
log.warning("Alarm #%d: alarm_dates must be a list of strings", index)
return None
bad = [d for d in alarm_dates if not DATE_RE.match(d)]
if bad:
log.warning("Alarm #%d: invalid date formats (expected MM/DD): %s", index, bad)
return None
log.info("Alarm #%d: time=%s days=%s", index, alarm_time, alarm_days or "(every day)")
return entry
def load_config(path: Path) -> list[dict] | None:
"""Read and validate alarm config JSON.
Accepts either a single alarm object or an array of alarm objects.
Returns a list of valid alarm dicts, or None if the file is missing,
empty, or contains no valid entries.
Never raises — logs warnings and returns None on any problem.
"""
try:
text = path.read_text(encoding="utf-8").strip()
except FileNotFoundError:
log.warning("Config file not found: %s", path)
return None
except OSError as e:
log.warning("Cannot read config %s: %s", path, e)
return None
if not text:
log.warning("Config file is empty: %s", path)
return None
try:
data = json.loads(text)
except json.JSONDecodeError as e:
log.warning("Invalid JSON in %s: %s", path, e)
return None
if not data:
log.info("Config is empty — no alarms configured")
return None
# Normalize to list
if isinstance(data, dict):
entries = [data]
elif isinstance(data, list):
entries = data
else:
log.warning("Config must be a JSON object or array, got %s", type(data).__name__)
return None
valid = []
for i, entry in enumerate(entries):
result = _validate_entry(entry, i)
if result is not None:
valid.append(result)
if not valid:
log.warning("No valid alarm entries in %s", path)
return None
log.info("Loaded %d alarm(s) from %s", len(valid), path)
return valid
def should_fire(config: dict) -> bool:
"""Check if a single alarm entry should fire right now.
Rules:
- alarm_time must match current HHMM
- If alarm_days is present, today's 3-letter abbreviation must be in the list
- If alarm_days is absent but alarm_dates is present, today's MM/DD must match
- If neither alarm_days nor alarm_dates is present, fires every day
- If both are present, alarm_days wins (alarm_dates ignored)
"""
now = datetime.now()
current_hhmm = now.strftime("%H%M")
if config["alarm_time"] != current_hhmm:
return False
alarm_days = config.get("alarm_days")
alarm_dates = config.get("alarm_dates")
if alarm_days is not None:
return now.strftime("%a") in alarm_days
if alarm_dates is not None:
return now.strftime("%m/%d") in alarm_dates
return True

42
pi/assets/alarm/sleep.lab Normal file
View File

@@ -0,0 +1,42 @@
0 850000 s
850000 1580000 u
1580000 2220000 i
2220000 2660000 m
2660000 3370000 i
3370000 4140000 N
4140000 4610000 g
4610000 5780000 a
5780000 13780000 pau
13780000 14350000 k
14350000 15010000 i
15010000 15730000 e
15730000 16160000 t
16160000 16800000 e
16800000 17260000 k
17260000 17840000 u
17840000 18390000 d
18390000 19090000 a
19090000 19700000 s
19700000 20390000 a
20390000 20830000 r
20830000 22120000 i
22120000 23620000 pau
23620000 24390000 a
24390000 24810000 r
24810000 25430000 i
25430000 25860000 g
25860000 26550000 a
26550000 27000000 t
27000000 27780000 o
27780000 28520000 o
28520000 29000000 g
29000000 29740000 o
29740000 30260000 z
30260000 31110000 a
31110000 31790000 i
31790000 32190000 m
32190000 32870000 a
32870000 33480000 sh
33480000 34040000 i
34040000 34520000 t
34520000 35950000 a

BIN
pi/assets/alarm/sleep.wav Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

After

Width:  |  Height:  |  Size: 33 KiB

BIN
pi/assets/img/sleep.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

114
pi/audio_handler.py Normal file
View File

@@ -0,0 +1,114 @@
"""Audio alarm functions — WAV loading and PCM streaming."""
import array
import asyncio
import json
import logging
import math
import wave
from pathlib import Path
log = logging.getLogger(__name__)
CHUNK_SIZE = 4096
AUDIO_DIR = Path(__file__).parent / "assets" / "alarm"
def find_wav(path: Path | None = None) -> Path:
"""Return a WAV file path.
If *path* is given and points to an existing file, use it directly.
Otherwise fall back to the first .wav found in the alarm assets directory.
"""
if path is not None:
p = Path(path)
if p.is_file():
log.info("Using audio file: %s", p)
return p
log.warning("Specified audio path not found: %s — falling back to glob", p)
wavs = list(AUDIO_DIR.glob("*.wav"))
if not wavs:
raise FileNotFoundError(f"No .wav files found in {AUDIO_DIR}")
log.info("Using audio file: %s", wavs[0].name)
return wavs[0]
def _normalize_pcm(pcm: bytes, bits: int) -> bytes:
"""Peak-normalize PCM data to 0 dBFS.
Supports 8-bit (unsigned) and 16-bit (signed) PCM.
Returns the original bytes unchanged if already at 0 dB or silent.
"""
if bits == 16:
samples = array.array("h", pcm) # signed 16-bit
peak = max(abs(s) for s in samples) if samples else 0
if peak == 0 or peak == 32767:
return pcm
scale = 32767 / peak
samples = array.array("h", (min(32767, max(-32768, int(s * scale))) for s in samples))
elif bits == 8:
samples = array.array("B", pcm) # unsigned 8-bit, center=128
peak = max(abs(s - 128) for s in samples) if samples else 0
if peak == 0 or peak == 127:
return pcm
scale = 127 / peak
samples = array.array("B", (max(0, min(255, int((s - 128) * scale) + 128)) for s in samples))
else:
log.warning("Normalization not supported for %d-bit audio, skipping", bits)
return pcm
gain_db = 20 * __import__("math").log10(scale) if scale > 0 else 0
log.info("Normalized: peak %d → 0 dBFS (gain %.1f dB)", peak, gain_db)
return samples.tobytes()
def read_wav(path: Path) -> tuple[bytes, int, int, int]:
"""Read WAV file, normalize to 0 dBFS, return (pcm_data, sample_rate, channels, bits)."""
try:
wf = wave.open(str(path), "rb")
except wave.Error as e:
raise ValueError(
f"{path.name}: unsupported WAV format ({e}). "
"Only 8/16-bit integer PCM is supported — no 32-bit float."
) from e
with wf:
sr = wf.getframerate()
ch = wf.getnchannels()
bits = wf.getsampwidth() * 8
pcm = wf.readframes(wf.getnframes())
log.info("WAV loaded: %dHz %dch %dbit, %.1fs, %d bytes",
sr, ch, bits, len(pcm) / (sr * ch * (bits // 8)), len(pcm))
pcm = _normalize_pcm(pcm, bits)
return pcm, sr, ch, bits
def chunk_bytes(data: bytes, size: int):
"""Yield data in fixed-size chunks."""
for i in range(0, len(data), size):
yield data[i : i + size]
async def stream_alarm(ws, pcm: bytes, sr: int, ch: int, bits: int):
"""Stream one alarm cycle to the connected client."""
bytes_per_sec = sr * ch * (bits // 8)
chunk_duration = CHUNK_SIZE / bytes_per_sec
pace_delay = chunk_duration * 0.9 # 90% real-time to avoid underrun
total_chunks = (len(pcm) + CHUNK_SIZE - 1) // CHUNK_SIZE
start_msg = json.dumps({
"type": "alarm_start",
"sample_rate": sr,
"channels": ch,
"bits": bits,
})
await ws.send(start_msg)
log.info("Sent alarm_start (%d chunks, pace %.1fms)", total_chunks, pace_delay * 1000)
for i, chunk in enumerate(chunk_bytes(pcm, CHUNK_SIZE)):
await ws.send(chunk)
await asyncio.sleep(pace_delay)
await ws.send(json.dumps({"type": "alarm_stop"}))
log.info("Sent alarm_stop")

View File

@@ -1,112 +0,0 @@
"""
Alarm audio streaming test server.
Streams a WAV file as raw PCM chunks over WebSocket on port 8766.
Repeats every 30-60 seconds to exercise the ESP32 audio pipeline.
Protocol:
1. Text frame: {"type":"alarm_start","sample_rate":N,"channels":N,"bits":N}
2. Binary frames: raw PCM chunks (4096 bytes each, paced at ~90% real-time)
3. Text frame: {"type":"alarm_stop"}
"""
import asyncio
import json
import logging
import struct
import wave
from pathlib import Path
from random import randint
import websockets
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("audio_server")
PORT = 8766
CHUNK_SIZE = 4096
AUDIO_DIR = Path(__file__).parent / "assets" / "alarm"
def find_wav() -> Path:
"""Find the first .wav file in the alarm assets directory."""
wavs = list(AUDIO_DIR.glob("*.wav"))
if not wavs:
raise FileNotFoundError(f"No .wav files found in {AUDIO_DIR}")
log.info("Using audio file: %s", wavs[0].name)
return wavs[0]
def read_wav(path: Path) -> tuple[bytes, int, int, int]:
"""Read WAV file and return (pcm_data, sample_rate, channels, bits_per_sample)."""
with wave.open(str(path), "rb") as wf:
sr = wf.getframerate()
ch = wf.getnchannels()
bits = wf.getsampwidth() * 8
pcm = wf.readframes(wf.getnframes())
log.info("WAV loaded: %dHz %dch %dbit, %.1fs, %d bytes",
sr, ch, bits, len(pcm) / (sr * ch * (bits // 8)), len(pcm))
return pcm, sr, ch, bits
def chunk_bytes(data: bytes, size: int):
"""Yield data in fixed-size chunks."""
for i in range(0, len(data), size):
yield data[i : i + size]
async def stream_alarm(ws, pcm: bytes, sr: int, ch: int, bits: int):
"""Stream one alarm cycle to the connected client."""
# Compute pacing: how long each chunk represents in seconds
bytes_per_sec = sr * ch * (bits // 8)
chunk_duration = CHUNK_SIZE / bytes_per_sec
pace_delay = chunk_duration * 0.9 # 90% real-time to avoid underrun
total_chunks = (len(pcm) + CHUNK_SIZE - 1) // CHUNK_SIZE
# Start
start_msg = json.dumps({
"type": "alarm_start",
"sample_rate": sr,
"channels": ch,
"bits": bits,
})
await ws.send(start_msg)
log.info("Sent alarm_start (%d chunks, pace %.1fms)", total_chunks, pace_delay * 1000)
# Stream PCM chunks
for i, chunk in enumerate(chunk_bytes(pcm, CHUNK_SIZE)):
await ws.send(chunk) # bytes → binary frame
await asyncio.sleep(pace_delay)
# Stop
await ws.send(json.dumps({"type": "alarm_stop"}))
log.info("Sent alarm_stop")
async def handler(ws):
"""Handle a single WebSocket connection."""
remote = ws.remote_address
log.info("Client connected: %s:%d", remote[0], remote[1])
wav_path = find_wav()
pcm, sr, ch, bits = read_wav(wav_path)
try:
while True:
delay = randint(30, 60)
log.info("Next alarm in %ds", delay)
await asyncio.sleep(delay)
await stream_alarm(ws, pcm, sr, ch, bits)
except websockets.exceptions.ConnectionClosed:
log.info("Client disconnected: %s:%d", remote[0], remote[1])
async def main():
log.info("Audio server starting on port %d", PORT)
async with websockets.serve(handler, "0.0.0.0", PORT):
await asyncio.Future() # run forever
if __name__ == "__main__":
main_loop = asyncio.run(main())

View File

@@ -0,0 +1,13 @@
[
{
"alarm_time": "0700",
"alarm_days": ["Mon", "Tue", "Wed", "Thu", "Fri"],
"alarm_audio": "assets/alarm/alarm_test.wav",
"alarm_image": "assets/img/on_alarm.png"
},
{
"alarm_time": "2330",
"alarm_audio": "assets/alarm/sleep.wav",
"alarm_image": "assets/img/sleep.png"
}
]

132
pi/contents_server.py Normal file
View File

@@ -0,0 +1,132 @@
"""
Contents server — serves alarm audio and status images over WebSocket.
Streams WAV PCM chunks and pushes 1-bit monochrome status images to the
connected ESP32 dashboard client on port 8766.
Protocol:
Status image:
1. Text frame: {"type":"status_image","width":200,"height":200}
2. Binary frame: 1-bit monochrome bitmap
Alarm audio:
1. Text frame: {"type":"alarm_start","sample_rate":N,"channels":N,"bits":N}
2. Binary frames: raw PCM chunks (4096 bytes each, paced at ~90% real-time)
3. Text frame: {"type":"alarm_stop"}
"""
import argparse
import asyncio
import json
import logging
from datetime import datetime
from pathlib import Path
import websockets
from alarm_scheduler import DEFAULT_CONFIG_PATH, load_config, should_fire
from audio_handler import find_wav, read_wav, stream_alarm
from image_handler import IMG_DIR, load_status_image, send_status_image
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
log = logging.getLogger("contents_server")
PORT = 8766
PI_DIR = Path(__file__).parent
# Set by main(), read by handler()
_config_path: Path = DEFAULT_CONFIG_PATH
TICK_INTERVAL = 5 # seconds between schedule checks
def _resolve_path(relative: str) -> Path:
"""Resolve a config path relative to pi/ directory."""
p = Path(relative)
if not p.is_absolute():
p = PI_DIR / p
return p
def _prepare_alarm(entry: dict) -> dict:
"""Pre-resolve paths and load resources for a single alarm entry."""
audio_path = find_wav(_resolve_path(entry.get("alarm_audio", "assets/alarm/alarm_test.wav")))
alarm_img_path = _resolve_path(entry.get("alarm_image", "assets/img/on_alarm.png"))
pcm, sr, ch, bits = read_wav(audio_path)
img = load_status_image(alarm_img_path)
return {
"config": entry,
"pcm": pcm, "sr": sr, "ch": ch, "bits": bits,
"img": img,
"last_fired": None,
}
async def handler(ws):
"""Handle a single WebSocket connection."""
remote = ws.remote_address
log.info("Client connected: %s:%d", remote[0], remote[1])
configs = load_config(_config_path)
img_idle = load_status_image(IMG_DIR / "idle.png")
current_img = img_idle
alarms = [_prepare_alarm(entry) for entry in configs] if configs else []
async def alarm_ticker():
nonlocal current_img
if not alarms:
log.info("No alarms configured — idling forever")
await asyncio.Future()
return
while True:
for alarm in alarms:
if should_fire(alarm["config"]):
current_minute = datetime.now().strftime("%Y%m%d%H%M")
if current_minute != alarm["last_fired"]:
alarm["last_fired"] = current_minute
log.info("Alarm firing: %s at %s",
alarm["config"]["alarm_time"], current_minute)
current_img = alarm["img"]
await send_status_image(ws, current_img)
await stream_alarm(ws, alarm["pcm"], alarm["sr"],
alarm["ch"], alarm["bits"])
# let the image persist a bit more
await asyncio.sleep(1)
current_img = img_idle
await send_status_image(ws, current_img)
await asyncio.sleep(TICK_INTERVAL)
async def receiver():
async for msg in ws:
try:
data = json.loads(msg)
except (json.JSONDecodeError, TypeError):
continue
if data.get("type") == "request_image":
log.info("Client requested image — sending current (%d bytes)",
len(current_img))
await send_status_image(ws, current_img)
try:
await asyncio.gather(alarm_ticker(), receiver())
except websockets.exceptions.ConnectionClosed:
log.info("Client disconnected: %s:%d", remote[0], remote[1])
async def main():
log.info("Contents server starting on port %d", PORT)
async with websockets.serve(handler, "0.0.0.0", PORT):
await asyncio.Future()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Alarm contents server")
parser.add_argument("--config", type=Path, default=DEFAULT_CONFIG_PATH,
help="Path to alarm config JSON (default: %(default)s)")
args = parser.parse_args()
_config_path = args.config
asyncio.run(main())

52
pi/image_handler.py Normal file
View File

@@ -0,0 +1,52 @@
"""Status image functions — loading, alpha compositing, and WS transmission."""
import json
import logging
from pathlib import Path
from PIL import Image
log = logging.getLogger(__name__)
IMG_DIR = Path(__file__).parent / "assets" / "img"
STATUS_IMG_SIZE = 200
MONOCHROME_THRESHOLD = 180
def load_status_image(path: Path) -> bytes:
"""Load a PNG, convert to 1-bit 200x200 monochrome bitmap (MSB-first, black=1).
Transparent pixels are composited onto white so they don't render as black.
"""
img = Image.open(path)
# Composite transparent pixels onto white background
if img.mode in ("RGBA", "LA", "PA"):
bg = Image.new("RGBA", img.size, (255, 255, 255, 255))
bg.paste(img, mask=img.split()[-1])
img = bg
img = img.convert("L")
# Resize to fit within 120x120, preserving aspect ratio
img.thumbnail((STATUS_IMG_SIZE, STATUS_IMG_SIZE), Image.LANCZOS)
# Paste centered onto white canvas
canvas = Image.new("L", (STATUS_IMG_SIZE, STATUS_IMG_SIZE), 255)
x_off = (STATUS_IMG_SIZE - img.width) // 2
y_off = (STATUS_IMG_SIZE - img.height) // 2
canvas.paste(img, (x_off, y_off))
# Threshold to 1-bit: black (< MONOCHROME_THRESHOLD) -> 1, white -> 0
bw = canvas.point(lambda p: 1 if p < MONOCHROME_THRESHOLD else 0, "1")
raw = bw.tobytes()
log.info("Status image loaded: %s -> %d bytes", path.name, len(raw))
return raw
async def send_status_image(ws, img_bytes: bytes):
"""Send a status image over the WebSocket (text header + binary payload)."""
header = json.dumps({"type": "status_image", "width": STATUS_IMG_SIZE, "height": STATUS_IMG_SIZE})
await ws.send(header)
await ws.send(img_bytes)
log.info("Sent status image (%d bytes)", len(img_bytes))

View File

@@ -1,2 +1,3 @@
websockets>=12.0 websockets>=12.0
psutil>=5.9.0 psutil>=5.9.0
Pillow>=10.0

View File

@@ -1,15 +1,19 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Launch stats_server and audio_server as child processes.""" """Launch stats_server and contents_server as child processes."""
import subprocess, sys, signal import subprocess, sys, signal
from pathlib import Path from pathlib import Path
d = Path(__file__).parent d = Path(__file__).parent
# Forward any CLI args (e.g. --config) to contents_server
extra_args = sys.argv[1:]
procs = [ procs = [
subprocess.Popen([sys.executable, d / "stats_server.py"]), subprocess.Popen([sys.executable, d / "stats_server.py"]),
subprocess.Popen([sys.executable, d / "audio_server.py"]), subprocess.Popen([sys.executable, d / "contents_server.py"] + extra_args),
] ]
signal.signal(signal.SIGINT, lambda *_: [p.terminate() for p in procs]) signal.signal(signal.SIGINT, lambda *_: [p.terminate() for p in procs])
signal.signal(signal.SIGTERM, lambda *_: [p.terminate() for p in procs]) signal.signal(signal.SIGTERM, lambda *_: [p.terminate() for p in procs])
print(f"Running stats_server (PID {procs[0].pid}) + audio_server (PID {procs[1].pid})") print(f"Running stats_server (PID {procs[0].pid}) + contents_server (PID {procs[1].pid})")
for p in procs: for p in procs:
p.wait() p.wait()

View File

@@ -7,7 +7,7 @@ same 2s push interval. Services remain mocked until systemd integration is added
import asyncio import asyncio
import json import json
import random import subprocess
import time import time
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
@@ -60,15 +60,52 @@ def _get_net_throughput() -> tuple[float, float]:
return rx_kbps, tx_kbps return rx_kbps, tx_kbps
# only services that matter
SERVICES_ALIASES = {
"gitea": "gitea",
"samba": "samba",
"pihole": "pihole",
"qbittorrent": "qbittorrent",
"frpc-primary": "frpc (ny)",
"pinepods": "pinepods",
"frpc-ssh": "frpc (ssh)",
"jellyfin": "jellyfin",
}
def _get_docker_services() -> list[dict]:
"""Query Docker for real container statuses with ternary status model."""
try:
result = subprocess.run(
["docker", "ps", "-a", "--format", "{{.Names}}\t{{.Status}}"],
capture_output=True, text=True, timeout=5,
)
except (subprocess.TimeoutExpired, FileNotFoundError, OSError):
return []
def _mock_services() -> list[dict]: if result.returncode != 0:
"""Mocked service status — same logic as mock_server.py.""" return []
return [
{"name": "docker", "status": random.choice(["running", "running", "running", "stopped"])}, services = []
{"name": "pihole", "status": random.choice(["running", "running", "running", "stopped"])}, for line in result.stdout.strip().splitlines():
{"name": "nginx", "status": random.choice(["running", "running", "stopped"])}, parts = line.split("\t", 1)
{"name": "sshd", "status": "running"}, if len(parts) != 2:
] continue
name, raw_status = parts
if (name in SERVICES_ALIASES):
if raw_status.startswith("Up"):
if "unhealthy" in raw_status or "Restarting" in raw_status:
status = "warning"
else:
status = "running"
else:
status = "stopped"
services.append({"name": SERVICES_ALIASES[name], "status": status})
# Sort: warnings first, then stopped, then running (problems float to top)
order = {"warning": 0, "stopped": 1, "running": 2}
services.sort(key=lambda s: order.get(s["status"], 3))
return services
def _local_time_fields() -> dict: def _local_time_fields() -> dict:
@@ -86,7 +123,7 @@ def _local_time_fields() -> dict:
def generate_stats() -> dict: def generate_stats() -> dict:
mem = psutil.virtual_memory() mem = psutil.virtual_memory()
disk = psutil.disk_usage("/") disk = psutil.disk_usage("/mnt/buffalo")
rx_kbps, tx_kbps = _get_net_throughput() rx_kbps, tx_kbps = _get_net_throughput()
return { return {
@@ -96,9 +133,9 @@ def generate_stats() -> dict:
"disk_pct": round(disk.percent, 1), "disk_pct": round(disk.percent, 1),
"cpu_temp": _get_cpu_temp(), "cpu_temp": _get_cpu_temp(),
"uptime_hrs": round((time.time() - psutil.boot_time()) / 3600, 1), "uptime_hrs": round((time.time() - psutil.boot_time()) / 3600, 1),
"net_rx_kbps": rx_kbps, "net_rx_kbps": rx_kbps / 8,
"net_tx_kbps": tx_kbps, "net_tx_kbps": tx_kbps / 8, # kByte/s for humans
"services": _mock_services(), "services": _get_docker_services(),
"timestamp": int(time.time()), "timestamp": int(time.time()),
"local_time": _local_time_fields(), "local_time": _local_time_fields(),
} }