Updates to BellEngine, ClientManager, ConfigManager, and Telemetry Logging

This commit is contained in:
2025-10-30 22:42:01 +02:00
parent c9f1e8e4ae
commit f286abb023
5 changed files with 143 additions and 158 deletions

View File

@@ -131,7 +131,7 @@ void BellEngine::stop() {
}
void BellEngine::emergencyStop() {
LOG_INFO("BellEngine 🛑 EMERGENCY STOP ACTIVATED");
LOG_INFO("BellEngine - 🛑 Forcing Stop Immediately");
_emergencyStop.store(true);
_engineRunning.store(false);
emergencyShutdown();
@@ -142,7 +142,7 @@ void BellEngine::setMelodyData(const std::vector<uint16_t>& melodySteps) {
_melodySteps = melodySteps;
_melodyDataReady.store(true);
portEXIT_CRITICAL(&_melodyMutex);
LOG_DEBUG("BellEngine loaded melody: %d steps", melodySteps.size());
LOG_DEBUG("BellEngine - Loaded melody: %d steps", melodySteps.size());
}
void BellEngine::clearMelodyData() {
@@ -150,7 +150,7 @@ void BellEngine::clearMelodyData() {
_melodySteps.clear();
_melodyDataReady.store(false);
portEXIT_CRITICAL(&_melodyMutex);
LOG_DEBUG("BellEngine melody data cleared");
LOG_DEBUG("BellEngine - Melody data cleared");
}
// ================== CRITICAL TIMING SECTION ==================
@@ -158,7 +158,7 @@ void BellEngine::clearMelodyData() {
void BellEngine::engineTask(void* parameter) {
BellEngine* engine = static_cast<BellEngine*>(parameter);
LOG_DEBUG("🔥 BellEngine task started on Core %d with MAXIMUM priority", xPortGetCoreID());
LOG_DEBUG("BellEngine - 🔥 Engine task started on Core %d with MAXIMUM priority", xPortGetCoreID());
while (true) {
if (engine->_engineRunning.load() && !engine->_emergencyStop.load()) {
@@ -186,7 +186,7 @@ void BellEngine::engineLoop() {
// Pause handling AFTER complete loop - never interrupt mid-melody!
while (_player.isPaused && _player.isPlaying && !_player.hardStop) {
LOG_DEBUG("⏸️ Pausing between melody loops");
LOG_VERBOSE("BellEngine - ⏸️ Pausing between melody loops");
vTaskDelay(pdMS_TO_TICKS(10)); // Wait during pause
}
@@ -207,17 +207,17 @@ void BellEngine::playbackLoop() {
portEXIT_CRITICAL(&_melodyMutex);
if (melodySteps.empty()) {
LOG_ERROR("Empty melody in playback loop!");
LOG_ERROR("BellEngine - ❌ Empty melody in playback loop!");
return;
}
LOG_DEBUG("🎵 Starting melody loop (%d steps)", melodySteps.size());
LOG_DEBUG("BellEngine - 🎵 Starting melody loop (%d steps)", melodySteps.size());
// CRITICAL TIMING LOOP - Complete the entire melody without interruption
for (uint16_t note : melodySteps) {
// Emergency exit check (only emergency stops can interrupt mid-loop)
if (_emergencyStop.load() || _player.hardStop) {
LOG_DEBUG("Emergency exit from playback loop");
LOG_DEBUG("BellEngine - Emergency exit from playback loop");
return;
}
@@ -225,8 +225,9 @@ void BellEngine::playbackLoop() {
activateNote(note);
// Precise timing delay - validate speed to prevent division by zero
// I THINK this should be moved outside the Bell Engine
if (_player.speed == 0) {
LOG_ERROR("❌ Invalid speed=0 detected, stopping playback");
LOG_ERROR("BellEngine - ❌ Invalid Speed (0) detected, stopping playback");
_player.hardStop = true;
_engineRunning.store(false);
return;
@@ -241,9 +242,9 @@ void BellEngine::playbackLoop() {
_player.onMelodyLoopCompleted(); // 🔥 Notify Player that melody actually finished!
if ((_player.continuous_loop && _player.segment_duration == 0) || _player.total_duration == 0) {
vTaskDelay(pdMS_TO_TICKS(500)); //Give Player time to pause/stop
LOG_VERBOSE("Melody loop completed for SINGLE Mode - waiting for Player to handle pause/stop");
LOG_VERBOSE("BellEngine - Loop completed in SINGLE Mode - waiting for Player to handle pause/stop");
}
LOG_DEBUG("🎵 Melody loop completed with PRECISION");
LOG_DEBUG("BellEngine - 🎵 Melody loop completed with PRECISION");
}
@@ -267,26 +268,26 @@ void BellEngine::activateNote(uint16_t note) {
// Additional safety check to prevent underflow crashes
if (bellIndex >= 255) {
LOG_ERROR("🚨 UNDERFLOW ERROR: bellIndex underflow for noteIndex %d", noteIndex);
LOG_ERROR("BellEngine - 🚨 UNDERFLOW ERROR: bellIndex underflow for noteIndex %d", noteIndex);
continue;
}
// Bounds check (CRITICAL SAFETY)
if (bellIndex >= 16) {
LOG_ERROR("🚨 BOUNDS ERROR: bellIndex %d >= 16", bellIndex);
LOG_ERROR("BellEngine - 🚨 BOUNDS ERROR: bellIndex %d >= 16", bellIndex);
continue;
}
// Check for duplicate bell firing in this note
if (bellFired[bellIndex]) {
LOG_DEBUG("⚠️ DUPLICATE BELL: Skipping duplicate firing of bell %d for note %d", bellIndex, noteIndex);
LOG_DEBUG("BellEngine - ⚠️ DUPLICATE BELL: Skipping duplicate firing of bell %d for note %d", bellIndex, noteIndex);
continue;
}
// Check if bell is configured (OutputManager will validate this)
uint8_t physicalOutput = _outputManager.getPhysicalOutput(bellIndex);
if (physicalOutput == 255) {
LOG_DEBUG("⚠️ UNCONFIGURED: Bell %d not configured, skipping", bellIndex);
LOG_DEBUG("BellEngine - ⚠️ UNCONFIGURED: Bell %d not configured, skipping", bellIndex);
continue;
}
@@ -305,14 +306,14 @@ void BellEngine::activateNote(uint16_t note) {
// Record telemetry
_telemetry.recordBellStrike(bellIndex);
LOG_VERBOSE("🔨 STRIKE! Note:%d → Bell:%d for %dms", noteIndex, bellIndex, durationMs);
LOG_VERBOSE("BellEngine - 🔨 STRIKE! Note:%d → Bell:%d for %dms", noteIndex, bellIndex, durationMs);
}
}
// 🚀 FIRE ALL BELLS SIMULTANEOUSLY!
if (!bellDurations.empty()) {
_outputManager.fireOutputsBatchForDuration(bellDurations);
LOG_VERBOSE("🔥🔥 BATCH FIRED %d bells SIMULTANEOUSLY!", bellDurations.size());
LOG_VERBOSE("BellEngine - 🔥 Batch Fired %d bells Simultaneously !", bellDurations.size());
// 🔔 NOTIFY WEBSOCKET CLIENTS OF BELL DINGS!
// * deactivated currently, since unstable and causes performance issues *
@@ -338,7 +339,7 @@ void BellEngine::preciseDelay(uint32_t microseconds) {
}
void BellEngine::emergencyShutdown() {
LOG_INFO("🚨 EMERGENCY SHUTDOWN - Using OutputManager");
LOG_INFO("BellEngine - 🚨 Emergency Shutdown - Notifying OutputManager");
_outputManager.emergencyShutdown();
}
@@ -363,10 +364,10 @@ void BellEngine::notifyBellsFired(const std::vector<uint8_t>& bellIndices) {
// Send notification to WebSocket clients only (not MQTT)
_communicationManager->broadcastToAllWebSocketClients(dingMsg);
LOG_DEBUG("🔔 DING notification sent for %d bells", bellIndices.size());
LOG_DEBUG("BellEngine - 🔔 DING notification sent for %d bells", bellIndices.size());
} catch (...) {
LOG_ERROR("Failed to send ding notification");
LOG_WARNING("BellEngine - ❌ Failed to send ding notification");
}
}