feat(comms): Stage 1 — CommandBus scaffolding (headers + skeleton)
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>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CommandResult
|
||||
//
|
||||
// Standardised return type from every ICommandHandler::execute() call.
|
||||
// Replaces ad-hoc string building scattered across CommandHandler.cpp.
|
||||
//
|
||||
// Use the static factory methods — they read clearly at the call site:
|
||||
//
|
||||
// return CommandResult::ok("playback", "Command executed");
|
||||
// return CommandResult::ok("relay.get_config", doc);
|
||||
// return CommandResult::err("relay.test_output", "Player must be stopped first");
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
struct CommandResult {
|
||||
enum class Status { SUCCESS, ERROR, INFO };
|
||||
|
||||
Status status = Status::ERROR;
|
||||
String type; // echoes the command name back to the caller
|
||||
String payload; // human-readable message (empty is fine for data responses)
|
||||
JsonDocument data; // structured data payload (optional)
|
||||
bool hasData = false;
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Factory methods
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
// Simple success with an optional message
|
||||
static CommandResult ok(const String& type, const String& msg = "") {
|
||||
CommandResult r;
|
||||
r.status = Status::SUCCESS;
|
||||
r.type = type;
|
||||
r.payload = msg;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Success with a structured JSON data payload
|
||||
static CommandResult ok(const String& type, const JsonDocument& d) {
|
||||
CommandResult r;
|
||||
r.status = Status::SUCCESS;
|
||||
r.type = type;
|
||||
r.data.set(d);
|
||||
r.hasData = true;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Error with a required message
|
||||
static CommandResult err(const String& type, const String& msg) {
|
||||
CommandResult r;
|
||||
r.status = Status::ERROR;
|
||||
r.type = type;
|
||||
r.payload = msg;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Info / event (used for unsolicited broadcasts)
|
||||
static CommandResult info(const String& type, const String& msg = "") {
|
||||
CommandResult r;
|
||||
r.status = Status::INFO;
|
||||
r.type = type;
|
||||
r.payload = msg;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Convenience
|
||||
bool isOk() const { return status == Status::SUCCESS; }
|
||||
bool isErr() const { return status == Status::ERROR; }
|
||||
};
|
||||
Reference in New Issue
Block a user