Added Telemetry data report to the App

This commit is contained in:
2025-10-31 21:10:38 +02:00
parent f286abb023
commit a7f1bd1667
13 changed files with 194 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
#include "Telemetry.hpp"
#include <ArduinoJson.h>
void Telemetry::begin() {
// Initialize arrays
@@ -10,6 +11,9 @@ void Telemetry::begin() {
coolingActive = false;
// Load strike counters from SD if available
loadStrikeCounters();
// Create the telemetry task
xTaskCreatePinnedToCore(telemetryTask, "TelemetryTask", 4096, this, 2, &telemetryTaskHandle, 1);
@@ -21,6 +25,11 @@ void Telemetry::setPlayerReference(bool* isPlayingPtr) {
LOG_DEBUG("Player reference set");
}
void Telemetry::setFileManager(FileManager* fm) {
fileManager = fm;
LOG_DEBUG("FileManager reference set");
}
void Telemetry::setForceStopCallback(void (*callback)()) {
forceStopCallback = callback;
LOG_DEBUG("Force stop callback set");
@@ -175,6 +184,62 @@ void Telemetry::telemetryTask(void* parameter) {
}
}
// ════════════════════════════════════════════════════════════════════════════
// STRIKE COUNTER PERSISTENCE
// ════════════════════════════════════════════════════════════════════════════
void Telemetry::saveStrikeCounters() {
if (!fileManager) {
LOG_WARNING("Cannot save strike counters: FileManager not set");
return;
}
StaticJsonDocument<512> doc;
JsonArray counters = doc.createNestedArray("strikeCounters");
// Thread-safe read of strike counters
portENTER_CRITICAL(&telemetrySpinlock);
for (uint8_t i = 0; i < 16; i++) {
counters.add(strikeCounters[i]);
}
portEXIT_CRITICAL(&telemetrySpinlock);
if (fileManager->writeJsonFile("/telemetry_data.json", doc)) {
LOG_INFO("Strike counters saved to SD card");
} else {
LOG_ERROR("Failed to save strike counters to SD card");
}
}
void Telemetry::loadStrikeCounters() {
if (!fileManager) {
LOG_WARNING("Cannot load strike counters: FileManager not set");
return;
}
StaticJsonDocument<512> doc;
if (!fileManager->readJsonFile("/telemetry_data.json", doc)) {
LOG_INFO("No previous strike counter data found, starting fresh");
return;
}
JsonArray counters = doc["strikeCounters"];
if (counters.isNull()) {
LOG_WARNING("Invalid telemetry data format");
return;
}
// Thread-safe write of strike counters
portENTER_CRITICAL(&telemetrySpinlock);
for (uint8_t i = 0; i < 16 && i < counters.size(); i++) {
strikeCounters[i] = counters[i].as<uint32_t>();
}
portEXIT_CRITICAL(&telemetrySpinlock);
LOG_INFO("Strike counters loaded from SD card");
}
// ════════════════════════════════════════════════════════════════════════════
// HEALTH CHECK IMPLEMENTATION
// ════════════════════════════════════════════════════════════════════════════

View File

@@ -57,6 +57,7 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "../Logging/Logging.hpp"
#include "../FileManager/FileManager.hpp"
class Telemetry {
private:
@@ -71,6 +72,7 @@ private:
// External references (to be set via setters)
bool* playerIsPlayingPtr = nullptr;
FileManager* fileManager = nullptr;
// Spinlock for critical sections
portMUX_TYPE telemetrySpinlock = portMUX_INITIALIZER_UNLOCKED;
@@ -81,6 +83,7 @@ public:
// Set external references
void setPlayerReference(bool* isPlayingPtr);
void setFileManager(FileManager* fm);
// Bell strike handling (call this on every hammer strike)
void recordBellStrike(uint8_t bellIndex);
@@ -89,6 +92,10 @@ public:
uint32_t getStrikeCount(uint8_t bellIndex);
void resetStrikeCounters(); // User-requested reset
// Persistence methods
void saveStrikeCounters();
void loadStrikeCounters();
// Bell load management
uint16_t getBellLoad(uint8_t bellIndex);
void setBellMaxLoad(uint8_t bellIndex, uint16_t maxLoad);