|
| 1 | +# MoBI-View Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +MoBI-View follows a **layered architecture** with clear separation of concerns for real-time LSL stream visualization. |
| 6 | + |
| 7 | +## Architecture Layers |
| 8 | + |
| 9 | +``` |
| 10 | +┌──────────────────────────────────────────────────────────────┐ |
| 11 | +│ Application Layer (main.py) │ |
| 12 | +│ - Discovers LSL streams │ |
| 13 | +│ - Creates DataInlet instances │ |
| 14 | +│ - Initializes Presenter with DataInlets │ |
| 15 | +│ - Starts WebSocket server │ |
| 16 | +└───────────────────────────┬──────────────────────────────────┘ |
| 17 | + │ |
| 18 | + ▼ |
| 19 | + ┌─────────────────────┐ |
| 20 | + │ Model Layer │ |
| 21 | + │ (DataInlet) │ |
| 22 | + │ │ |
| 23 | + │ - Connects to ONE │ |
| 24 | + │ LSL stream │ |
| 25 | + │ - Buffers samples │ |
| 26 | + │ - pull_sample() │ |
| 27 | + └──────────┬──────────┘ |
| 28 | + │ |
| 29 | + ▼ |
| 30 | + ┌─────────────────────┐ |
| 31 | + │ Presenter Layer │ |
| 32 | + │ (MainAppPresenter) │ |
| 33 | + │ │ |
| 34 | + │ - Manages list of │ |
| 35 | + │ DataInlets │ |
| 36 | + │ - Polls all inlets │ |
| 37 | + │ - Formats data │ |
| 38 | + │ - poll_data() │ |
| 39 | + └──────────┬──────────┘ |
| 40 | + │ |
| 41 | + ▼ |
| 42 | + ┌─────────────────────┐ |
| 43 | + │ View Layer │ |
| 44 | + │ (WebServer) │ |
| 45 | + │ │ |
| 46 | + │ - Reads from │ |
| 47 | + │ presenter │ |
| 48 | + │ - Broadcasts to │ |
| 49 | + │ WebSocket clients │ |
| 50 | + │ - Handles UI state │ |
| 51 | + │ (visibility, etc) │ |
| 52 | + └─────────────────────┘ |
| 53 | +``` |
| 54 | + |
| 55 | +**Data Flow**: Application → Model → Presenter → View |
| 56 | + |
| 57 | +- **Application** discovers streams and wires components together |
| 58 | +- **Model** (DataInlet) pulls data from LSL streams |
| 59 | +- **Presenter** polls models and formats data |
| 60 | +- **View** reads from presenter and renders to users |
| 61 | + |
| 62 | +## Component Responsibilities |
| 63 | + |
| 64 | +### 1. Application Layer (`main.py`) |
| 65 | + |
| 66 | +**Purpose**: Entry point that wires everything together |
| 67 | + |
| 68 | +**Responsibilities**: |
| 69 | +- Discover LSL streams using `discover_and_create_inlets()` |
| 70 | +- Create `DataInlet` instances for each discovered stream |
| 71 | +- Initialize `MainAppPresenter` with the inlets |
| 72 | +- Start the WebSocket server with `run_server()` |
| 73 | +- Schedule browser launch |
| 74 | + |
| 75 | +**Key Functions**: |
| 76 | +- `main()` - Main entry point |
| 77 | + |
| 78 | +### 2. Model Layer (`core/data_inlet.py`) |
| 79 | + |
| 80 | +**Purpose**: Represents a single LSL stream connection |
| 81 | + |
| 82 | +**Responsibilities**: |
| 83 | +- Connect to ONE LSL stream |
| 84 | +- Buffer incoming samples |
| 85 | +- Pull samples from the stream |
| 86 | +- Provide stream metadata (name, type, channels, sample rate) |
| 87 | + |
| 88 | +**Key Methods**: |
| 89 | +- `__init__(partial_info: StreamInfo)` - Create connection |
| 90 | +- `pull_sample()` - Pull one sample from the stream |
| 91 | +- `get_channel_information()` - Extract channel metadata |
| 92 | + |
| 93 | +**Attributes**: |
| 94 | +- `stream_name`, `stream_type`, `source_id` - Stream identifiers |
| 95 | +- `buffers` - Circular buffer for samples |
| 96 | +- `ptr` - Current position in buffer |
| 97 | +- `channel_info` - Channel labels, types, units |
| 98 | + |
| 99 | +### 3. Presenter Layer (`presenters/main_app_presenter.py`) |
| 100 | + |
| 101 | +**Purpose**: Business logic layer that manages multiple streams |
| 102 | + |
| 103 | +**Responsibilities**: |
| 104 | +- Manage list of `DataInlet` instances |
| 105 | +- Poll all inlets at regular intervals |
| 106 | +- Format data for consumption by views |
| 107 | +- Manage channel visibility state |
| 108 | +- Handle errors from individual streams |
| 109 | + |
| 110 | +**Key Methods**: |
| 111 | +- `poll_data()` - Poll all inlets and return formatted data |
| 112 | +- `on_data_updated()` - Format sample data for views |
| 113 | +- `update_channel_visibility()` - Toggle channel display |
| 114 | + |
| 115 | +**Key Principle**: The presenter does NOT know about LSL or discovery - it just manages DataInlets that are passed to it. |
| 116 | + |
| 117 | +### 4. View Layer (`web/server.py`) |
| 118 | + |
| 119 | +**Purpose**: Pure view - reads from presenter and serves WebSocket clients |
| 120 | + |
| 121 | +**Responsibilities**: |
| 122 | +- Serve static HTML/CSS/JS files |
| 123 | +- Maintain WebSocket connections |
| 124 | +- Read data from presenter's inlets |
| 125 | +- Broadcast formatted data to connected clients |
| 126 | +- Handle client commands (e.g., discover button) |
| 127 | + |
| 128 | +**Key Components**: |
| 129 | +- `Broadcaster` - Periodically reads from presenter and sends to WebSocket clients |
| 130 | +- `poll_presenter_continuously()` - Background task that calls `presenter.poll_data()` |
| 131 | +- `ws_handler()` - Handle WebSocket connections and messages |
| 132 | +- `run_server()` - Main server entry point |
| 133 | + |
| 134 | +**Key Principle**: The server does NOT discover streams or create inlets - it only reads from the presenter. |
| 135 | + |
| 136 | +### 5. Discovery Utilities (`core/discovery.py`) |
| 137 | + |
| 138 | +**Purpose**: Shared utility for discovering LSL streams |
| 139 | + |
| 140 | +**Responsibilities**: |
| 141 | +- Call `pylsl.resolve_streams()` to find available streams |
| 142 | +- Create `DataInlet` instances for discovered streams |
| 143 | +- Deduplicate streams based on (source_id, name, type) |
| 144 | +- Handle errors during inlet creation |
| 145 | + |
| 146 | +**Key Functions**: |
| 147 | +- `discover_and_create_inlets()` - Returns list of new DataInlet instances |
| 148 | + |
| 149 | +**Usage**: Called by application layer (main.py) and by WebSocket handler when user clicks "Discover Streams" button |
| 150 | + |
| 151 | +## Data Flow |
| 152 | + |
| 153 | +### Startup Flow |
| 154 | + |
| 155 | +``` |
| 156 | +main.py |
| 157 | + ├─> discover_and_create_inlets() |
| 158 | + │ └─> resolve_streams() (pylsl) |
| 159 | + │ └─> DataInlet(info) for each stream |
| 160 | + │ |
| 161 | + ├─> MainAppPresenter(data_inlets=[...]) |
| 162 | + │ └─> _initialize_channels() |
| 163 | + │ |
| 164 | + └─> run_server(presenter) |
| 165 | + ├─> start_http_static_server() |
| 166 | + ├─> poll_presenter_continuously() |
| 167 | + │ └─> presenter.poll_data() (every 2ms) |
| 168 | + │ └─> inlet.pull_sample() for each inlet |
| 169 | + │ |
| 170 | + └─> Broadcaster._run() |
| 171 | + └─> Read from presenter.data_inlets |
| 172 | + └─> ws.send(json) to all clients |
| 173 | +``` |
| 174 | + |
| 175 | +### Runtime Data Flow (Polling) |
| 176 | + |
| 177 | +``` |
| 178 | +[LSL Stream] ──samples──> [DataInlet.pull_sample()] |
| 179 | + │ |
| 180 | + ├─> Store in buffers[ptr] |
| 181 | + │ |
| 182 | + [Presenter.poll_data()] |
| 183 | + │ |
| 184 | + ├─> Read buffers[latest_index] |
| 185 | + ├─> Format as dict |
| 186 | + │ |
| 187 | + [Broadcaster._run()] |
| 188 | + │ |
| 189 | + └─> Send to WebSocket clients |
| 190 | +``` |
| 191 | + |
| 192 | +### User-Triggered Discovery Flow |
| 193 | + |
| 194 | +``` |
| 195 | +[User clicks "Discover Streams" button] |
| 196 | + │ |
| 197 | + ▼ |
| 198 | + [WebSocket message: {type: "discover_streams"}] |
| 199 | + │ |
| 200 | + ▼ |
| 201 | + [ws_handler()] |
| 202 | + │ |
| 203 | + ├─> discover_and_create_inlets() |
| 204 | + │ └─> resolve_streams() |
| 205 | + │ └─> Create new DataInlets |
| 206 | + │ |
| 207 | + ├─> Append new inlets to presenter.data_inlets |
| 208 | + ├─> Initialize channel_visibility |
| 209 | + │ |
| 210 | + └─> Send response: {type: "discover_result", count: X} |
| 211 | +``` |
| 212 | + |
| 213 | +## Key Architectural Decisions |
| 214 | + |
| 215 | +### 1. No Discovery in Presenter |
| 216 | + |
| 217 | +The presenter does NOT discover streams. Discovery happens at the application layer (main.py) or through user action (discover button). |
| 218 | + |
| 219 | +**Rationale**: Presenter is business logic layer, not responsible for I/O or system initialization. |
| 220 | + |
| 221 | +### 2. No Discovery in Server |
| 222 | + |
| 223 | +The server does NOT discover streams automatically. It only handles user-triggered discovery via WebSocket commands. |
| 224 | + |
| 225 | +**Rationale**: Server is pure view layer, should not contain business logic or know about LSL. |
| 226 | + |
| 227 | +### 3. Mutable Inlet List |
| 228 | + |
| 229 | +The `presenter.data_inlets` list is mutable and can be appended to directly. No need for `add_inlet()` method. |
| 230 | + |
| 231 | +**Rationale**: Simple and direct. The presenter initializes channel visibility in `__init__`, and the WebSocket handler can manually initialize visibility for new inlets. |
| 232 | + |
| 233 | +### 4. Separation of Concerns |
| 234 | + |
| 235 | +- **DataInlet**: ONE stream, ONE responsibility (buffer samples) |
| 236 | +- **Presenter**: Coordinate MULTIPLE inlets, format data |
| 237 | +- **Server**: Read from presenter, serve to clients |
| 238 | +- **Application**: Wire everything together |
| 239 | + |
| 240 | +### 5. Discovery Utility |
| 241 | + |
| 242 | +Shared `discover_and_create_inlets()` function used by both: |
| 243 | +- Startup (main.py) |
| 244 | +- Runtime (WebSocket handler when user clicks discover button) |
| 245 | + |
| 246 | + |
| 247 | +## Channel Color Configuration |
| 248 | + |
| 249 | +The visualization uses a **pattern-based color assignment system** located in `web/static/colors.js`: |
| 250 | + |
| 251 | +**Key Features:** |
| 252 | +- **Pattern Matching**: Channel names are matched against regex patterns (e.g., `/^c\d+$/i` for C3, C4, etc.) |
| 253 | +- **Pre-configured Sensor Types**: Includes EEG, EMG, EOG, ECG, accelerometer, gyroscope, and more |
| 254 | +- **Customizable**: Users can edit `colors.js` to add custom patterns or change colors |
| 255 | +- **Deterministic**: Same channel name always gets same color across sessions |
| 256 | +- **Fallback Hash**: If no pattern matches, color is generated from channel name hash |
| 257 | + |
| 258 | +**Example Usage:** |
| 259 | +```javascript |
| 260 | +// In colors.js - add custom pattern |
| 261 | +{ |
| 262 | + pattern: /^mydevice/i, // Matches "MyDevice_1", "mydevice_A" |
| 263 | + color: 'hsl(180, 80%, 55%)', // Teal color |
| 264 | + description: 'My custom device' // Documentation |
| 265 | +} |
| 266 | +``` |
| 267 | + |
| 268 | +**Benefits:** |
| 269 | +- **Semantic Grouping**: Related channels get similar colors (e.g., all frontal EEG in blue) |
| 270 | +- **Readable Configuration**: HSL format is intuitive (hue, saturation, lightness) |
| 271 | +- **Flexible Matching**: Supports wildcards, regex, case-insensitive matching |
| 272 | +- **Well-Documented**: See `README_COLORS.md` for complete customization guide |
0 commit comments