add all crates: domain, protocol, application, client, adapters, ESP32 firmware
Server: domain (entities, value objects, ports), protocol (postcard wire types), application (config service, data projection), adapters (config-memory, tcp-server), bootstrap (composition root with fake data). Client: client-domain (layout engine, render tree, HAL ports), client-application (message handling, repaint commands), adapters (tcp-client, display-terminal), client-desktop (end-to-end working). ESP32: client-esp32 firmware with ILI9341 display over SPI, WiFi networking. Display test verified on hardware — landscape orientation, text rendering works. 60 workspace tests, all passing.
This commit is contained in:
7
crates/adapters/config-memory/Cargo.toml
Normal file
7
crates/adapters/config-memory/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "config-memory"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
domain.workspace = true
|
||||
119
crates/adapters/config-memory/src/lib.rs
Normal file
119
crates/adapters/config-memory/src/lib.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::RwLock;
|
||||
use domain::{
|
||||
ConfigRepository,
|
||||
DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId,
|
||||
WidgetConfig, WidgetId,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MemoryConfigError {
|
||||
LockPoisoned,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for MemoryConfigError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
MemoryConfigError::LockPoisoned => write!(f, "lock poisoned"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemoryConfigStore {
|
||||
widgets: RwLock<HashMap<WidgetId, WidgetConfig>>,
|
||||
data_sources: RwLock<HashMap<DataSourceId, DataSource>>,
|
||||
layout: RwLock<Option<Layout>>,
|
||||
presets: RwLock<HashMap<LayoutPresetId, LayoutPreset>>,
|
||||
}
|
||||
|
||||
impl MemoryConfigStore {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
widgets: RwLock::new(HashMap::new()),
|
||||
data_sources: RwLock::new(HashMap::new()),
|
||||
layout: RwLock::new(None),
|
||||
presets: RwLock::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfigRepository for MemoryConfigStore {
|
||||
type Error = MemoryConfigError;
|
||||
|
||||
async fn get_widget(&self, id: WidgetId) -> Result<Option<WidgetConfig>, Self::Error> {
|
||||
let guard = self.widgets.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
Ok(guard.get(&id).cloned())
|
||||
}
|
||||
|
||||
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, Self::Error> {
|
||||
let guard = self.widgets.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
Ok(guard.values().cloned().collect())
|
||||
}
|
||||
|
||||
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> {
|
||||
let mut guard = self.widgets.write().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
guard.insert(config.id, config.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error> {
|
||||
let mut guard = self.widgets.write().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
guard.remove(&id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_data_source(&self, id: DataSourceId) -> Result<Option<DataSource>, Self::Error> {
|
||||
let guard = self.data_sources.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
Ok(guard.get(&id).cloned())
|
||||
}
|
||||
|
||||
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error> {
|
||||
let guard = self.data_sources.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
Ok(guard.values().cloned().collect())
|
||||
}
|
||||
|
||||
async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error> {
|
||||
let mut guard = self.data_sources.write().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
guard.insert(source.id, source.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error> {
|
||||
let mut guard = self.data_sources.write().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
guard.remove(&id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_layout(&self) -> Result<Option<Layout>, Self::Error> {
|
||||
let guard = self.layout.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
Ok(guard.clone())
|
||||
}
|
||||
|
||||
async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error> {
|
||||
let mut guard = self.layout.write().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
*guard = Some(layout.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_preset(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, Self::Error> {
|
||||
let guard = self.presets.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
Ok(guard.get(&id).cloned())
|
||||
}
|
||||
|
||||
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, Self::Error> {
|
||||
let guard = self.presets.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
Ok(guard.values().cloned().collect())
|
||||
}
|
||||
|
||||
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> {
|
||||
let mut guard = self.presets.write().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
guard.insert(preset.id, preset.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error> {
|
||||
let mut guard = self.presets.write().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
||||
guard.remove(&id);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
7
crates/adapters/display-terminal/Cargo.toml
Normal file
7
crates/adapters/display-terminal/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "display-terminal"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
client-domain.workspace = true
|
||||
38
crates/adapters/display-terminal/src/lib.rs
Normal file
38
crates/adapters/display-terminal/src/lib.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use client_domain::{BoundingBox, DisplayPort};
|
||||
|
||||
pub struct TerminalDisplay;
|
||||
|
||||
impl TerminalDisplay {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl DisplayPort for TerminalDisplay {
|
||||
type Error = std::io::Error;
|
||||
|
||||
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
|
||||
println!("[CLEAR] ({}, {}) {}x{}", bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), Self::Error> {
|
||||
println!("[TEXT] ({x}, {y}) in {}x{}: \"{text}\"", bounds.width, bounds.height);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), Self::Error> {
|
||||
println!("[ICON] ({x}, {y}): {icon}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
|
||||
println!("[BG] ({}, {}) {}x{}", bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
println!("[FLUSH]");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
8
crates/adapters/tcp-client/Cargo.toml
Normal file
8
crates/adapters/tcp-client/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "tcp-client"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
client-domain.workspace = true
|
||||
protocol.workspace = true
|
||||
82
crates/adapters/tcp-client/src/lib.rs
Normal file
82
crates/adapters/tcp-client/src/lib.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
use std::time::Duration;
|
||||
use client_domain::NetworkPort;
|
||||
use protocol::MAX_FRAME_SIZE;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TcpClientError {
|
||||
Io(std::io::Error),
|
||||
NotConnected,
|
||||
FrameTooLarge(usize),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for TcpClientError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
TcpClientError::Io(e) => write!(f, "io: {e}"),
|
||||
TcpClientError::NotConnected => write!(f, "not connected"),
|
||||
TcpClientError::FrameTooLarge(n) => write!(f, "frame too large: {n}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct StdTcpClient {
|
||||
stream: Option<TcpStream>,
|
||||
}
|
||||
|
||||
impl StdTcpClient {
|
||||
pub fn new() -> Self {
|
||||
Self { stream: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl NetworkPort for StdTcpClient {
|
||||
type Error = TcpClientError;
|
||||
|
||||
fn connect(&mut self, addr: &str) -> Result<(), Self::Error> {
|
||||
let stream = TcpStream::connect(addr).map_err(TcpClientError::Io)?;
|
||||
stream.set_nonblocking(true).map_err(TcpClientError::Io)?;
|
||||
stream.set_read_timeout(Some(Duration::from_millis(10))).map_err(TcpClientError::Io)?;
|
||||
self.stream = Some(stream);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn disconnect(&mut self) -> Result<(), Self::Error> {
|
||||
self.stream = None;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send(&mut self, data: &[u8]) -> Result<(), Self::Error> {
|
||||
let stream = self.stream.as_mut().ok_or(TcpClientError::NotConnected)?;
|
||||
stream.write_all(data).map_err(TcpClientError::Io)
|
||||
}
|
||||
|
||||
fn receive(&mut self) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
let stream = self.stream.as_mut().ok_or(TcpClientError::NotConnected)?;
|
||||
|
||||
let mut len_buf = [0u8; 4];
|
||||
match stream.read_exact(&mut len_buf) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => return Ok(None),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::TimedOut => return Ok(None),
|
||||
Err(e) => return Err(TcpClientError::Io(e)),
|
||||
}
|
||||
|
||||
let len = u32::from_be_bytes(len_buf) as usize;
|
||||
if len > MAX_FRAME_SIZE {
|
||||
return Err(TcpClientError::FrameTooLarge(len));
|
||||
}
|
||||
|
||||
let mut payload = vec![0u8; len];
|
||||
stream.set_nonblocking(false).map_err(TcpClientError::Io)?;
|
||||
stream.read_exact(&mut payload).map_err(TcpClientError::Io)?;
|
||||
stream.set_nonblocking(true).map_err(TcpClientError::Io)?;
|
||||
|
||||
Ok(Some(payload))
|
||||
}
|
||||
|
||||
fn is_connected(&self) -> bool {
|
||||
self.stream.is_some()
|
||||
}
|
||||
}
|
||||
10
crates/adapters/tcp-server/Cargo.toml
Normal file
10
crates/adapters/tcp-server/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "tcp-server"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
domain.workspace = true
|
||||
protocol.workspace = true
|
||||
tokio.workspace = true
|
||||
postcard.workspace = true
|
||||
150
crates/adapters/tcp-server/src/lib.rs
Normal file
150
crates/adapters/tcp-server/src/lib.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
use std::sync::Arc;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use domain::{
|
||||
BroadcastPort, EventPublisher, DomainEvent,
|
||||
Layout, WidgetId, WidgetState,
|
||||
};
|
||||
use protocol::{
|
||||
ServerMessage, WidgetDescriptor, WireDisplayHint, WireLayoutNode,
|
||||
encode,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum TcpServerError {
|
||||
Io(std::io::Error),
|
||||
Encode(postcard::Error),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for TcpServerError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
TcpServerError::Io(e) => write!(f, "io: {e}"),
|
||||
TcpServerError::Encode(e) => write!(f, "encode: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TcpBroadcaster {
|
||||
tx: broadcast::Sender<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl TcpBroadcaster {
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let (tx, _) = broadcast::channel(capacity);
|
||||
Self { tx }
|
||||
}
|
||||
|
||||
pub fn subscribe(&self) -> broadcast::Receiver<Vec<u8>> {
|
||||
self.tx.subscribe()
|
||||
}
|
||||
|
||||
fn send_frame(&self, frame: Vec<u8>) -> Result<(), TcpServerError> {
|
||||
let _ = self.tx.send(frame);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl BroadcastPort for TcpBroadcaster {
|
||||
type Error = TcpServerError;
|
||||
|
||||
async fn push_screen_update(
|
||||
&self,
|
||||
layout: &Layout,
|
||||
widgets: &[(WidgetId, WidgetState)],
|
||||
) -> Result<(), Self::Error> {
|
||||
let wire_layout: WireLayoutNode = (&layout.root).into();
|
||||
let wire_widgets: Vec<WidgetDescriptor> = widgets.iter().map(|(id, state)| {
|
||||
WidgetDescriptor {
|
||||
id: *id,
|
||||
display_hint: WireDisplayHint::IconValue,
|
||||
state: state.into(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
let msg = ServerMessage::ScreenUpdate {
|
||||
layout: wire_layout,
|
||||
widgets: wire_widgets,
|
||||
};
|
||||
|
||||
let frame = encode(&msg).map_err(TcpServerError::Encode)?;
|
||||
self.send_frame(frame)
|
||||
}
|
||||
|
||||
async fn push_data_update(
|
||||
&self,
|
||||
updates: &[(WidgetId, WidgetState)],
|
||||
) -> Result<(), Self::Error> {
|
||||
let wire_widgets: Vec<WidgetDescriptor> = updates.iter().map(|(id, state)| {
|
||||
WidgetDescriptor {
|
||||
id: *id,
|
||||
display_hint: WireDisplayHint::IconValue,
|
||||
state: state.into(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
let msg = ServerMessage::DataUpdate {
|
||||
widgets: wire_widgets,
|
||||
};
|
||||
|
||||
let frame = encode(&msg).map_err(TcpServerError::Encode)?;
|
||||
self.send_frame(frame)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TcpEventBus {
|
||||
tx: broadcast::Sender<DomainEvent>,
|
||||
}
|
||||
|
||||
impl TcpEventBus {
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let (tx, _) = broadcast::channel(capacity);
|
||||
Self { tx }
|
||||
}
|
||||
|
||||
pub fn subscribe(&self) -> broadcast::Receiver<DomainEvent> {
|
||||
self.tx.subscribe()
|
||||
}
|
||||
}
|
||||
|
||||
impl EventPublisher for TcpEventBus {
|
||||
type Error = TcpServerError;
|
||||
|
||||
async fn publish(&self, event: DomainEvent) -> Result<(), Self::Error> {
|
||||
let _ = self.tx.send(event);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_tcp_server(
|
||||
addr: &str,
|
||||
broadcaster: Arc<TcpBroadcaster>,
|
||||
) -> Result<(), TcpServerError> {
|
||||
let listener = TcpListener::bind(addr).await.map_err(TcpServerError::Io)?;
|
||||
println!("TCP server listening on {addr}");
|
||||
|
||||
loop {
|
||||
let (mut socket, peer) = listener.accept().await.map_err(TcpServerError::Io)?;
|
||||
println!("Client connected: {peer}");
|
||||
|
||||
let mut rx = broadcaster.subscribe();
|
||||
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
match rx.recv().await {
|
||||
Ok(frame) => {
|
||||
if socket.write_all(&frame).await.is_err() {
|
||||
println!("Client disconnected: {peer}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(broadcast::error::RecvError::Closed) => break,
|
||||
Err(broadcast::error::RecvError::Lagged(n)) => {
|
||||
println!("Client {peer} lagged by {n} messages");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user