Skip to main content

csi_webclient/core/
messages.rs

1use serde_json::Value;
2
3/// Commands sent from the app orchestration layer to the core worker.
4#[derive(Debug, Clone)]
5pub enum CoreCommand {
6    /// Execute one HTTP request against the configured webserver.
7    ExecuteApi(ApiRequest),
8    /// Open or replace the active WebSocket stream for one device.
9    ConnectWebSocket { device_id: String, url: String },
10    /// Stop the active WebSocket stream for one device, if any.
11    DisconnectWebSocket { device_id: String },
12    /// Stop the core worker gracefully (closes all WebSockets).
13    Shutdown,
14}
15
16/// A normalized HTTP request model consumed by the core worker.
17#[derive(Debug, Clone)]
18pub struct ApiRequest {
19    /// Logical operation label used for UI messages and event correlation.
20    pub label: String,
21    /// Device this request is addressed to, if any. `None` for fleet-wide
22    /// calls such as `GET /api/devices`.
23    pub device_id: Option<String>,
24    /// HTTP verb to send.
25    pub method: HttpMethod,
26    /// Base URL (for example, `http://127.0.0.1:3000`).
27    pub base_url: String,
28    /// Request path (for example, `/api/devices/ttyUSB0/config/wifi`).
29    pub path: String,
30    /// Optional JSON body.
31    pub body: Option<Value>,
32}
33
34/// Event payload returned to the app after an HTTP request.
35#[derive(Debug, Clone)]
36pub struct ApiResponseEvent {
37    /// Logical request label echoed from [`ApiRequest::label`].
38    pub label: String,
39    /// Device id echoed from [`ApiRequest::device_id`] for routing.
40    pub device_id: Option<String>,
41    /// True when status code is in the 2xx range.
42    pub success: bool,
43    /// HTTP status code. `0` is used for transport-level failures.
44    pub status: u16,
45    /// Human-readable message for status/error UI.
46    pub message: String,
47    /// Parsed response payload, if available.
48    pub data: Option<Value>,
49}
50
51/// Events emitted by the core worker and consumed by the app layer.
52#[derive(Debug, Clone)]
53pub enum CoreEvent {
54    /// HTTP request completed.
55    ApiResponse(ApiResponseEvent),
56    /// WebSocket connection successfully established for a device.
57    WebSocketConnected { device_id: String },
58    /// WebSocket connection ended for a device.
59    WebSocketDisconnected { device_id: String, reason: String },
60    /// One WebSocket payload received from a device's stream.
61    WebSocketFrame { device_id: String, bytes: Vec<u8> },
62    /// Internal diagnostic log line from core worker/runtime.
63    Log(String),
64}
65
66/// Supported HTTP methods in this client.
67#[derive(Debug, Clone)]
68pub enum HttpMethod {
69    /// `GET`.
70    Get,
71    /// `POST`.
72    Post,
73}