feat(comms): bidirectional device control — bells master switch, guard toggle, runtime tracking, system aggregate commands

Adds full bidirectional remote control across all major subsystems,
establishing the device as the single source of truth for all settings.

New firmware features (F-029 to F-037):
- bells.enable / bells.disable: master ON/OFF for all playback and clock
  alerts (relay.test_output intentionally unaffected)
- telemetry.set_guard_enabled: enable/disable thermal protection at runtime
  without touching thresholds
- telemetry.set_max_loads: set bellMaxLoad per-bell or globally via command
- Device runtime tracking: lifetime seconds flushed to SD every 10 min,
  boot log (up to 200 entries, rotating) written after first NTP sync
- Total playback counter persisted to SD, increments on every play()
- telemetry.get_metrics / telemetry.reset_metrics: new aggregate read + reset
- system.get_config (replaces system.get_settings, old name still accepted),
  system.get_all (everything in one call), system.get_telemetry
- log.get_config, mqtt.get_config, bells.get_config: new getters for
  previously write-only subsystems
- network.status now includes ssid, mac, hostname
- BellsHandler: new handler registered on CommandBus
- LegacyAdapter updated with all new command mappings

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-10 18:46:18 +03:00
co-authored by Claude Sonnet 4.6
parent 825ffce5f5
commit e90771a539
22 changed files with 634 additions and 71 deletions
+128 -11
View File
@@ -4,21 +4,20 @@
#include <ArduinoJson.h>
void Telemetry::begin() {
// Initialize arrays
for (uint8_t i = 0; i < 16; i++) {
strikeCounters[i] = 0;
bellLoad[i] = 0;
bellMaxLoad[i] = 500; // Default max load
bellMaxLoad[i] = 500;
}
coolingActive = false;
guardEnabled = true;
sessionStartMs = millis();
lastRuntimeFlushMs = millis();
// Load strike counters from SD if available
loadStrikeCounters();
loadMetrics();
// Create the telemetry task
xTaskCreatePinnedToCore(telemetryTask, "TelemetryTask", 4096, this, 2, &telemetryTaskHandle, 1);
LOG_INFO(TAG, "Telemetry initialized");
}
@@ -66,7 +65,7 @@ void Telemetry::resetStrikeCounters() {
strikeCounters[i] = 0;
}
portEXIT_CRITICAL(&telemetrySpinlock);
saveStrikeCounters();
LOG_WARNING(TAG, "Strike counters reset by user");
}
@@ -78,6 +77,11 @@ uint16_t Telemetry::getBellLoad(uint8_t bellIndex) {
return bellLoad[bellIndex];
}
uint16_t Telemetry::getBellMaxLoad(uint8_t bellIndex) {
if (bellIndex >= 16) return 0;
return bellMaxLoad[bellIndex];
}
void Telemetry::setBellMaxLoad(uint8_t bellIndex, uint16_t maxLoad) {
if (bellIndex >= 16) {
LOG_ERROR(TAG, "Invalid bell index: %d", bellIndex);
@@ -111,6 +115,15 @@ void Telemetry::logVibration(float vibration) {
}
void Telemetry::checkBellLoads() {
if (!guardEnabled) {
// Guard disabled — still decay loads for accurate reporting, but never force-stop
for (uint8_t i = 0; i < 16; i++) {
if (bellLoad[i] > 0) bellLoad[i]--;
}
coolingActive = false;
return;
}
coolingActive = false; // Reset cooling flag
// Collect overloaded bells for batch notification
@@ -170,25 +183,129 @@ void Telemetry::checkBellLoads() {
void Telemetry::telemetryTask(void* parameter) {
Telemetry* telemetry = static_cast<Telemetry*>(parameter);
LOG_INFO(TAG, "Telemetry task started");
while(1) {
// Skip processing if paused (OTA freeze mode)
if (!telemetry->isPaused) {
// Only run if player is playing OR we're still cooling
bool isPlaying = (telemetry->playerIsPlayingPtr != nullptr) ?
*(telemetry->playerIsPlayingPtr) : false;
if (isPlaying || telemetry->coolingActive) {
telemetry->checkBellLoads();
}
// Flush lifetime runtime to SD every 10 minutes
uint32_t now = millis();
if (now - telemetry->lastRuntimeFlushMs >= RUNTIME_FLUSH_INTERVAL_MS) {
telemetry->flushRuntimeToSD();
telemetry->lastRuntimeFlushMs = now;
}
}
vTaskDelay(pdMS_TO_TICKS(1000)); // Run every 1s
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
// ════════════════════════════════════════════════════════════════════════════
// PLAYBACK COUNTER
// ════════════════════════════════════════════════════════════════════════════
void Telemetry::recordPlayback() {
portENTER_CRITICAL(&telemetrySpinlock);
totalPlaybackCount++;
portEXIT_CRITICAL(&telemetrySpinlock);
saveMetrics();
}
void Telemetry::resetMetrics() {
portENTER_CRITICAL(&telemetrySpinlock);
totalPlaybackCount = 0;
lifetimeRuntimeSeconds = 0;
portEXIT_CRITICAL(&telemetrySpinlock);
saveMetrics();
LOG_WARNING(TAG, "Telemetry metrics reset by user");
}
// ════════════════════════════════════════════════════════════════════════════
// RUNTIME PERSISTENCE
// ════════════════════════════════════════════════════════════════════════════
void Telemetry::flushRuntimeToSD() {
if (!fileManager) return;
uint32_t sessionSecs = (millis() - sessionStartMs) / 1000;
uint32_t total = lifetimeRuntimeSeconds + sessionSecs;
StaticJsonDocument<256> doc;
doc["lifetime_seconds"] = total;
doc["total_playbacks"] = totalPlaybackCount;
if (fileManager->writeJsonFile("/telemetry_runtime.json", doc)) {
LOG_DEBUG(TAG, "Runtime flushed: %lu lifetime seconds, %lu playbacks", total, totalPlaybackCount);
} else {
LOG_WARNING(TAG, "Failed to flush runtime to SD");
}
}
void Telemetry::logBootEvent(uint32_t bootTimestamp) {
if (!fileManager) return;
// Read existing boot log (or create fresh)
StaticJsonDocument<8192> doc;
fileManager->readJsonFile("/telemetry_boot_log.json", doc);
JsonArray entries = doc["boots"].is<JsonArray>()
? doc["boots"].as<JsonArray>()
: doc["boots"].to<JsonArray>();
// Rotate: keep only the last BOOT_LOG_MAX_ENTRIES - 1 entries
while (entries.size() >= BOOT_LOG_MAX_ENTRIES) {
// Remove oldest (index 0) by rebuilding — ArduinoJson doesn't support remove(0) on array
JsonDocument tmp;
JsonArray fresh = tmp["boots"].to<JsonArray>();
for (size_t i = 1; i < entries.size(); i++) fresh.add(entries[i]);
doc["boots"] = fresh;
entries = doc["boots"].as<JsonArray>();
}
JsonObject entry = entries.add<JsonObject>();
entry["ts"] = bootTimestamp;
entry["boot"] = (uint32_t)(doc["total_boots"] | 0) + 1;
doc["total_boots"] = (uint32_t)(doc["total_boots"] | 0) + 1;
if (!fileManager->writeJsonFile("/telemetry_boot_log.json", doc)) {
LOG_WARNING(TAG, "Failed to write boot log");
} else {
LOG_INFO(TAG, "Boot #%u logged at timestamp %u", (uint32_t)doc["total_boots"], bootTimestamp);
}
}
void Telemetry::loadMetrics() {
if (!fileManager) return;
StaticJsonDocument<256> doc;
if (!fileManager->readJsonFile("/telemetry_runtime.json", doc)) {
LOG_INFO(TAG, "No runtime metrics found, starting fresh");
return;
}
lifetimeRuntimeSeconds = doc["lifetime_seconds"] | 0;
totalPlaybackCount = doc["total_playbacks"] | 0;
LOG_INFO(TAG, "Metrics loaded: %lu lifetime seconds, %lu playbacks",
lifetimeRuntimeSeconds, totalPlaybackCount);
}
void Telemetry::saveMetrics() {
if (!fileManager) return;
uint32_t sessionSecs = (millis() - sessionStartMs) / 1000;
StaticJsonDocument<256> doc;
doc["lifetime_seconds"] = lifetimeRuntimeSeconds + sessionSecs;
doc["total_playbacks"] = totalPlaybackCount;
fileManager->writeJsonFile("/telemetry_runtime.json", doc);
}
// ════════════════════════════════════════════════════════════════════════════
// STRIKE COUNTER PERSISTENCE
// ════════════════════════════════════════════════════════════════════════════