Skip to content

refactor: structural Clean Code — setup lifecycle, dispatch table, OTA split, MqttUtils - #38

Merged
stritti merged 2 commits into
mainfrom
refactor/structural-clean-code
Jul 6, 2026
Merged

stritti merged 2 commits into
mainfrom
refactor/structural-clean-code

Conversation

@stritti

@stritti stritti commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Structural Clean Code Refactoring

Dieser PR setzt die umfangreicheren strukturellen Verbesserungen aus dem Clean-Code-Review um. Fokus: Funktionen klein halten, if-else-Ketten eliminieren, Verantwortlichkeiten trennen.

1. PoolMonitorContext::setup() aufgeteilt (~133 Zeilen → 6 Phasen)

Problem: setup() war eine monolithische 133-Zeilen-Funktion, die Boot-Banner, System-Init, Uptime-Tracking, Netzwerk-Entscheidung, Boot-Loop-Handling, NTP-Sync und Display-Update in einer einzigen Methode vermischte.

Lösung: Aufgeteilt in sechs fokussierte Methoden mit klaren Namen:

  • printBootBanner() — FW-Logo und Version
  • initSystem() — SystemMonitor, Preferences, OTA
  • updateBootAndUptimeStats() — boot_count, total_uptime, Netzwerk-Entscheidung
  • handleBootLoop(totalUptime) — Safe Mode bei Boot-Loop (Deep Sleep)
  • runNetworkCycle(totalUptime) — Display + WiFi + MQTT + NTP + Display-Update
  • runOfflineCycle(totalUptime) — Zeit-Rekonstruktion, kein Netzwerk

Die setup()-Methode selbst ist jetzt nur noch 10 Zeilen und zeigt den Ablauf auf einen Blick.

2. if-else-Kette in handleMqttMessage → Dispatch Table

Problem: 5-fach verschachteltes if/else if für MQTT-Topic-Dispatch.

Lösung: Datengetriebene Dispatch-Tabelle mit TopicHandler-Array. Jeder Eintrag enthält Topic, Label und Handler-Funktion. Die Schleife matcht und dispatched in 3 Zeilen.

Erweiterbarkeit: Neues Topic = neuer Eintrag im Array, kein neuer else-Zweig.

3. OTA downloadAndApply aufgeteilt (100 Zeilen → 2 Helfer)

Problem: downloadAndApply() war 100 Zeilen lang und vermischte HTTP-Download, Streaming und Update-Finalisierung.

Lösung: Drei klar getrennte Phasen:

  • openFirmwareDownload() — HTTP-Request, Validierung, Stream-Öffnung
  • streamToUpdate(stream, size, &progress) — Streaming in Update-Objekt mit Stall-Erkennung
  • downloadAndApply() — Orchestrierungs-Funktion (16 Zeilen)

4. Utility-Funktionen extrahiert (MqttUtils)

Problem: equalsIgnoreCaseAscii() und parseHomeAssistantBoolState() waren als statische Funktionen in PoolMonitorContext.cpp vergraben.

Lösung: Neue Datei MqttUtils.hpp/.cpp — wiederverwendbar, testbar, getrennte Verantwortung.

5. Preferences-Fehler: Hard Lock → Clean Restart

Problem: Bei fehlschlagendem preferences_->begin() trat ein while(1) { delay(1000); } auf — hartes Lockup ohne Diagnose.

Lösung: Cleanup (end + delete) + ESP.restart() — das Gerät startet neu statt für immer zu hängen.

Checkliste

  • Build erfolgreich (pio run)
  • Static Analysis bestanden (platformio check)
  • Keine Verhaltensänderung (Refactoring only)
  • Setup-Funktion von 133 → 10 Zeilen reduziert

…ispatch table, OTA split, utility extraction

- Decompose PoolMonitorContext::setup() (133 lines) into focused phases:
  printBootBanner, initSystem, updateBootAndUptimeStats,
  handleBootLoop, runNetworkCycle, runOfflineCycle
- Replace handleMqttMessage if-else chain with data-driven
  dispatch table (TopicHandler array + range-for loop)
- Split OtaUpdater::downloadAndApply() (100 lines) into
  openFirmwareDownload() + streamToUpdate() helper functions
- Extract equalsIgnoreCaseAscii and parseHomeAssistantBoolState
  into dedicated MqttUtils.hpp/.cpp (separation of concerns)
- Replace while(1) hard-lock in initSystem() with proper
  NVS cleanup + ESP.restart()
- Make kOtaBufferSize public for external access
- Zero functional changes — verified by build + static analysis
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Super-linter summary

Language Validation result
CPP Pass ✅
EDITORCONFIG Pass ✅
GITLEAKS Pass ✅

All files and directories linted successfully

For more information, see the GitHub Actions workflow run

Powered by Super-linter

…rn type

- Move WiFiClientSecure client to downloadAndApply() scope so it
  outlives HTTPClient usage (fix dangling reference bug)
- Replace printf() with Serial.printf() in MqttUtils (Arduino context)
- Normalize isNtpSyncNeeded() to trailing return type for consistency
@stritti
stritti merged commit 066ae31 into main Jul 6, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant