docs: add per-subsystem README files
Add inline documentation for all src/ subsystems covering purpose, responsibilities, public API, and usage notes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
# HTTPTransport
|
||||
|
||||
## Summary
|
||||
|
||||
`HTTPTransport` exposes a minimal REST API over HTTP that routes commands through the `CommandBus`. It registers three endpoints on the `AsyncWebServer`: `POST /api/command` accepts any v2 command in JSON, `GET /api/ping` is a lightweight health check that bypasses the bus entirely, and `GET /api/status` is a convenience shortcut that dispatches `system.status` and returns the result. HTTP is stateless — there are no persistent connections — so `broadcast()` is a no-op. Each request's reply lambda captures the `AsyncWebServerRequest*` pointer and sends the HTTP response when the handler returns.
|
||||
|
||||
---
|
||||
|
||||
## Public API
|
||||
|
||||
| Method | Description |
|
||||
|--------|-------------|
|
||||
| `HTTPTransport(AsyncWebServer& server, CommandBus& bus, ConfigManager& configManager)` | Constructor. Does not register routes yet. |
|
||||
| `void begin()` | Registers all HTTP routes and sets global CORS headers (`Access-Control-Allow-Origin: *`). Sets `_initialised = true`. |
|
||||
| `void loop()` | No-op. AsyncWebServer is event-driven. |
|
||||
| `bool isHealthy() const` | Returns `_initialised`. True once `begin()` has been called. |
|
||||
| `String name() const` | Returns `"HTTP"`. |
|
||||
| `void broadcast(const String&)` | No-op. |
|
||||
|
||||
---
|
||||
|
||||
## Endpoints
|
||||
|
||||
| Method | Path | Description |
|
||||
|--------|------|-------------|
|
||||
| `POST` | `/api/command` | Execute any command. Body must be JSON with at least a `"cmd"` field. |
|
||||
| `GET` | `/api/ping` | Returns `{ "status": "SUCCESS", "type": "pong", "uptime": <ms> }`. No bus involvement. |
|
||||
| `GET` | `/api/status` | Dispatches `system.status` and returns the result. |
|
||||
|
||||
### POST /api/command
|
||||
|
||||
Request body:
|
||||
```json
|
||||
{ "v": 2, "cmd": "relay.get_config", "contents": {} }
|
||||
```
|
||||
|
||||
Response (success):
|
||||
```json
|
||||
{ "status": "SUCCESS", "type": "relay.get_config", "data": { ... } }
|
||||
```
|
||||
|
||||
Response (error):
|
||||
```json
|
||||
{ "status": "ERROR", "type": "relay.set_durations", "message": "Missing or invalid 'bells' object" }
|
||||
```
|
||||
|
||||
HTTP status code is always `200` for dispatched commands (even errors — the error is in the JSON body). HTTP `400` is returned only for malformed requests (bad JSON, missing `"cmd"`).
|
||||
|
||||
---
|
||||
|
||||
## Initialization
|
||||
|
||||
`CommunicationRouter::begin()` calls `_httpTransport.begin()`, then adds the transport to the bus with `_bus.addTransport(&_httpTransport)`. The underlying `AsyncWebServer` is started separately by `main.cpp` in the network-connected callback.
|
||||
|
||||
---
|
||||
|
||||
## Dependencies
|
||||
|
||||
- `AsyncWebServer` (ESPAsyncWebServer) — route registration and request handling
|
||||
- `CommandBus` — dispatch
|
||||
- `ConfigManager` — held as a reference (available for future auth/rate-limiting)
|
||||
- `ArduinoJson`, `Logging`
|
||||
|
||||
---
|
||||
|
||||
## Gotchas / Notes
|
||||
|
||||
- **CORS is global.** `DefaultHeaders::Instance().addHeader(...)` applies to all responses served by the `AsyncWebServer`, not just HTTP transport routes. Any other handler sharing the same server will also emit these headers.
|
||||
- **Body must arrive complete.** The body callback only processes the message when `index + len == total`, i.e. when the full body has been received. Partial delivery (chunked transfer, slow clients) will be ignored until the final chunk.
|
||||
- **HTTP status is always 200 for dispatched commands.** Error results from handlers still return HTTP 200. Callers must inspect `"status"` in the JSON body. Only truly malformed requests (no body, bad JSON, missing `"cmd"`) return 4xx.
|
||||
- **No authentication.** Any client that can reach the device on port 80 can issue any command. This is intentional for the local-network deployment context.
|
||||
- **`broadcast()` is a no-op.** HTTP has no persistent connection. The transport is registered with the bus so that `broadcast()` calls don't crash, but no data is sent.
|
||||
Reference in New Issue
Block a user