per-source polling, initial client state, webhook, preview, client tracking

- per-source poll intervals: spawn task per source with own interval,
  manager re-checks sources every 30s for add/remove
- initial screen update on TCP connect: send layout + widget states
- client tracking: ClientRegistry port, GET /api/clients, dashboard list
- webhook adapter: POST /api/webhook/{source_id} feeds data into projection
- widget preview: GET /api/widgets/{id}/preview returns current state
- serve SPA from Axum: ServeDir + index.html fallback via KFRAME_SPA_DIR
- layout builder delete confirmation with AlertDialog
- form validation: required fields disable save button
- guide page at /guide
- fix architecture: ClientDto to api-types, ClientRegistry + WidgetStateReader
  ports in domain, DataProjection has internal Mutex, no adapter cross-deps
- ESP32: full screen clear on layout change (stale pixel fix)
This commit is contained in:
2026-06-19 00:42:31 +02:00
parent 26ebfad3a2
commit 1d7b5324d6
39 changed files with 1232 additions and 158 deletions

View File

@@ -6,6 +6,7 @@ edition = "2024"
[dependencies]
domain.workspace = true
thiserror.workspace = true
tokio.workspace = true
[dev-dependencies]
tokio = { workspace = true }

View File

@@ -1,9 +1,17 @@
use domain::{DataSourceId, Value, WidgetConfig, WidgetId, WidgetState};
use domain::{DataSourceId, Value, WidgetConfig, WidgetId, WidgetState, WidgetStateReader};
use std::collections::HashMap;
use tokio::sync::Mutex;
#[derive(Default)]
pub struct DataProjection {
current: HashMap<WidgetId, WidgetState>,
current: Mutex<HashMap<WidgetId, WidgetState>>,
}
impl Default for DataProjection {
fn default() -> Self {
Self {
current: Mutex::new(HashMap::new()),
}
}
}
impl DataProjection {
@@ -11,16 +19,17 @@ impl DataProjection {
Self::default()
}
pub fn get_state(&self, widget_id: WidgetId) -> Option<&WidgetState> {
self.current.get(&widget_id)
pub async fn get_state(&self, widget_id: WidgetId) -> Option<WidgetState> {
self.current.lock().await.get(&widget_id).cloned()
}
pub fn apply_poll_result(
&mut self,
pub async fn apply_poll_result(
&self,
data_source_id: DataSourceId,
raw: &Value,
widget_configs: &[WidgetConfig],
) -> Vec<(WidgetId, WidgetState)> {
let mut current = self.current.lock().await;
let mut changed = Vec::new();
for config in widget_configs {
@@ -30,13 +39,12 @@ impl DataProjection {
let new_state = config.extract(raw);
let is_changed = self
.current
let is_changed = current
.get(&config.id)
.is_none_or(|prev| *prev != new_state);
if is_changed {
self.current.insert(config.id, new_state.clone());
current.insert(config.id, new_state.clone());
changed.push((config.id, new_state));
}
}
@@ -44,3 +52,18 @@ impl DataProjection {
changed
}
}
impl WidgetStateReader for DataProjection {
async fn get_widget_state(&self, id: WidgetId) -> Option<WidgetState> {
self.get_state(id).await
}
async fn apply_raw_data(
&self,
source_id: u16,
raw: &Value,
widgets: &[WidgetConfig],
) -> Vec<(WidgetId, WidgetState)> {
self.apply_poll_result(source_id, raw, widgets).await
}
}

View File

@@ -1,5 +1,5 @@
use application::DataProjection;
use domain::{DisplayHint, KeyMapping, Value, WidgetConfig, WidgetId, WidgetState};
use domain::{DisplayHint, KeyMapping, Value, WidgetConfig};
use std::collections::BTreeMap;
fn weather_widget() -> WidgetConfig {
@@ -28,12 +28,14 @@ fn weather_response(temp: f64) -> Value {
]))
}
#[test]
fn apply_poll_result_detects_new_widget_state() {
let mut projection = DataProjection::new();
#[tokio::test]
async fn apply_poll_result_detects_new_widget_state() {
let projection = DataProjection::new();
let widgets = vec![weather_widget()];
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
let changed = projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].0, 1);
@@ -43,24 +45,32 @@ fn apply_poll_result_detects_new_widget_state() {
);
}
#[test]
fn apply_poll_result_returns_empty_when_nothing_changed() {
let mut projection = DataProjection::new();
#[tokio::test]
async fn apply_poll_result_returns_empty_when_nothing_changed() {
let projection = DataProjection::new();
let widgets = vec![weather_widget()];
projection.apply_poll_result(10, &weather_response(5.4), &widgets);
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
let changed = projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
assert!(changed.is_empty());
}
#[test]
fn apply_poll_result_detects_changed_value() {
let mut projection = DataProjection::new();
#[tokio::test]
async fn apply_poll_result_detects_changed_value() {
let projection = DataProjection::new();
let widgets = vec![weather_widget()];
projection.apply_poll_result(10, &weather_response(5.4), &widgets);
let changed = projection.apply_poll_result(10, &weather_response(6.1), &widgets);
projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
let changed = projection
.apply_poll_result(10, &weather_response(6.1), &widgets)
.await;
assert_eq!(changed.len(), 1);
assert_eq!(
@@ -69,9 +79,9 @@ fn apply_poll_result_detects_changed_value() {
);
}
#[test]
fn apply_poll_result_only_updates_widgets_bound_to_source() {
let mut projection = DataProjection::new();
#[tokio::test]
async fn apply_poll_result_only_updates_widgets_bound_to_source() {
let projection = DataProjection::new();
let widgets = vec![
weather_widget(),
WidgetConfig::new(
@@ -86,7 +96,9 @@ fn apply_poll_result_only_updates_widgets_bound_to_source() {
),
];
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
let changed = projection
.apply_poll_result(10, &weather_response(5.4), &widgets)
.await;
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].0, 1);