Adds the core data structures and interfaces for the comms refactor. No behaviour change — old CommandHandler is completely untouched. New files: CommandEnvelope.hpp — universal message wrapper (transport + command + reply lambda) CommandResult.hpp — standardised return type with ok/err/info factories ICommandHandler.hpp — interface every domain handler will implement ITransport.hpp — interface every transport layer will implement CommandBus.hpp/.cpp — registry + dispatcher (std::map, O(log n) lookup) Two minor corrections vs. plan: std::unordered_map → std::map (Arduino String has no std::hash) LOG_WARN → LOG_WARNING (codebase macro name) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
46 lines
1.4 KiB
C++
46 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
#include <ArduinoJson.h>
|
|
#include <functional>
|
|
|
|
// Forward declare so CommandEnvelope.hpp doesn't pull in CommandResult.hpp circularly
|
|
struct CommandResult;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// CommandEnvelope
|
|
//
|
|
// Universal message wrapper. Every transport (MQTT, WebSocket, HTTP, UART)
|
|
// parses its raw input and produces one of these before calling CommandBus::dispatch().
|
|
//
|
|
// The reply lambda is injected by the transport so the bus and handlers never
|
|
// need to know which wire the response goes back on.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
struct CommandEnvelope {
|
|
enum class Transport {
|
|
MQTT,
|
|
WEBSOCKET,
|
|
HTTP,
|
|
UART
|
|
};
|
|
|
|
// Protocol version: 2 = v2 namespaced commands, 1 = legacy v1 (default 2)
|
|
uint8_t version = 2;
|
|
|
|
Transport transport = Transport::WEBSOCKET;
|
|
|
|
// WebSocket client ID — 0 for transports that don't have per-client identity
|
|
uint32_t clientId = 0;
|
|
|
|
// Flat command name — e.g. "playback", "relay.set_durations", "system.restart"
|
|
String command;
|
|
|
|
// The "contents" object from the raw JSON message
|
|
JsonDocument contents;
|
|
|
|
// Reply callback injected by the transport layer.
|
|
// The bus calls this exactly once after dispatch completes.
|
|
std::function<void(const CommandResult&)> reply;
|
|
};
|