feat(comms): Stage 2 — all 10 domain handlers implemented
Adds the complete set of v2 command handlers. Old CommandHandler is
untouched — handlers are standalone and not yet wired to anything.
Handlers added:
PlaybackHandler — playback (1 dependency)
LoggingHandler — log.set_serial/sd/mqtt
FileHandler — files.list/list_builtin/download/delete
TelemetryHandler — telemetry.get_strikes/reset_strikes/get_loads
RelayHandler — relay.set_durations/set_outputs/get_config/test_output
FirmwareHandler — firmware.status/commit/rollback + ota.update/custom
NetworkHandler — network.info/status/set_config
MQTTHandler — mqtt.enable/disable
SystemHandler — ping/identify + system.* commands (8 total)
ClockHandler — 16 clock.* commands
Notable fixes vs. original CommandHandler:
- clock.set_timings now sets pulseDuration/pauseDuration correctly
(original called updateClockOutputs — was a bug)
- system.restart uses a deferred FreeRTOS task (no blocking delay)
- relay.test_output safety-gated: player must be fully stopped
- Timekeeper class name (lowercase k) corrected throughout
Two compile-time fixes applied during build:
- JsonVariantConst → const_cast<JsonDocument&>().as<JsonVariant>()
(ConfigManager methods take non-const JsonVariant)
- class TimeKeeper → class Timekeeper (actual class name in header)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#include "FileHandler.hpp"
|
||||
#include "../../../FileManager/FileManager.hpp"
|
||||
#include "../../../BuiltInMelodies/BuiltInMelodies.hpp"
|
||||
#include "../../../Logging/Logging.hpp"
|
||||
|
||||
#define TAG "FileHandler"
|
||||
|
||||
FileHandler::FileHandler(FileManager& fileManager) : _fileManager(fileManager) {}
|
||||
|
||||
std::vector<String> FileHandler::commands() const {
|
||||
return { "files.list", "files.list_builtin", "files.download", "files.delete" };
|
||||
}
|
||||
|
||||
CommandResult FileHandler::execute(const CommandEnvelope& envelope) {
|
||||
if (envelope.command == "files.list") return handleList(envelope);
|
||||
if (envelope.command == "files.list_builtin") return handleListBuiltin(envelope);
|
||||
if (envelope.command == "files.download") return handleDownload(envelope);
|
||||
if (envelope.command == "files.delete") return handleDelete(envelope);
|
||||
return CommandResult::err(envelope.command, "Unknown files command");
|
||||
}
|
||||
|
||||
CommandResult FileHandler::handleList(const CommandEnvelope& e) {
|
||||
String fileListJson = _fileManager.listFilesAsJson("/melodies");
|
||||
|
||||
JsonDocument doc;
|
||||
DeserializationError err = deserializeJson(doc, fileListJson);
|
||||
if (err) {
|
||||
LOG_ERROR(TAG, "Failed to parse file list JSON: %s", err.c_str());
|
||||
return CommandResult::err("files.list", "Failed to read melody list");
|
||||
}
|
||||
|
||||
JsonDocument result;
|
||||
result["files"] = doc["files"];
|
||||
return CommandResult::ok("files.list", result);
|
||||
}
|
||||
|
||||
CommandResult FileHandler::handleListBuiltin(const CommandEnvelope& e) {
|
||||
String builtinJson = BuiltInMelodies::getBuiltInMelodiesJSON();
|
||||
|
||||
JsonDocument doc;
|
||||
DeserializationError err = deserializeJson(doc, builtinJson);
|
||||
if (err) {
|
||||
LOG_ERROR(TAG, "Failed to parse built-in melodies JSON: %s", err.c_str());
|
||||
return CommandResult::err("files.list_builtin", "Failed to read built-in melody list");
|
||||
}
|
||||
|
||||
return CommandResult::ok("files.list_builtin", doc);
|
||||
}
|
||||
|
||||
CommandResult FileHandler::handleDownload(const CommandEnvelope& e) {
|
||||
bool ok = _fileManager.addMelody(const_cast<JsonDocument&>(e.contents).as<JsonVariant>());
|
||||
String uid = e.contents["melodys_uid"] | e.contents["name"] | String("unknown");
|
||||
if (ok) return CommandResult::ok("files.download", "Melody downloaded: " + uid);
|
||||
return CommandResult::err("files.download", "Failed to download melody: " + uid);
|
||||
}
|
||||
|
||||
CommandResult FileHandler::handleDelete(const CommandEnvelope& e) {
|
||||
if (!e.contents["name"].is<const char*>()) {
|
||||
return CommandResult::err("files.delete", "Missing required field: name");
|
||||
}
|
||||
String name = e.contents["name"].as<String>();
|
||||
bool ok = _fileManager.deleteFile("/melodies/" + name);
|
||||
if (ok) return CommandResult::ok("files.delete", "Melody deleted: " + name);
|
||||
return CommandResult::err("files.delete", "Failed to delete melody: " + name);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "../ICommandHandler.hpp"
|
||||
|
||||
class FileManager;
|
||||
|
||||
class FileHandler : public ICommandHandler {
|
||||
public:
|
||||
explicit FileHandler(FileManager& fileManager);
|
||||
|
||||
std::vector<String> commands() const override;
|
||||
CommandResult execute(const CommandEnvelope& envelope) override;
|
||||
|
||||
private:
|
||||
FileManager& _fileManager;
|
||||
|
||||
CommandResult handleList(const CommandEnvelope& e);
|
||||
CommandResult handleListBuiltin(const CommandEnvelope& e);
|
||||
CommandResult handleDownload(const CommandEnvelope& e);
|
||||
CommandResult handleDelete(const CommandEnvelope& e);
|
||||
};
|
||||
Reference in New Issue
Block a user