From 8d1f94e02e927fcb80d170fc85d13a091e5dc304 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Sun, 05 Apr 2026 16:30:42 +0200
Subject: [PATCH] feat: add message trace log for end-to-end delivery diagnostics

---
 lib/services/mqtt_service.dart |   22 ++++++++++++++++++++--
 1 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/lib/services/mqtt_service.dart b/lib/services/mqtt_service.dart
index 7edcbff..34ffc93 100644
--- a/lib/services/mqtt_service.dart
+++ b/lib/services/mqtt_service.dart
@@ -15,6 +15,7 @@
 import 'package:uuid/uuid.dart';
 
 import '../models/server_config.dart';
+import 'trace_service.dart';
 import 'wol_service.dart';
 
 /// Connection status for the MQTT client.
@@ -25,9 +26,11 @@
   reconnecting,
 }
 
-// Debug log — writes to file only in debug builds, always prints via debugPrint
+// Debug log — writes to file only in debug builds, always prints via debugPrint.
+// Also adds entries to TraceService so they appear in the trace log viewer.
 Future<void> _mqttLog(String msg) async {
   debugPrint('[MQTT] $msg');
+  TraceService.instance.addTrace('MQTT', msg);
   if (!kDebugMode) return;
   try {
     final dir = await pp.getApplicationDocumentsDirectory();
@@ -490,12 +493,27 @@
       // Dedup by msgId
       final msgId = json['msgId'] as String?;
       if (msgId != null) {
-        if (_seenMsgIds.contains(msgId)) continue;
+        if (_seenMsgIds.contains(msgId)) {
+          final seq = json['seq'];
+          final type = json['type'] as String? ?? '?';
+          TraceService.instance.addTrace(
+            'MQTT deduped',
+            'msgId=${msgId.substring(0, 8)} type=$type seq=$seq topic=${msg.topic}',
+          );
+          continue;
+        }
         _seenMsgIds.add(msgId);
         _seenMsgIdOrder.add(msgId);
         _evictOldIds();
       }
 
+      final seq = json['seq'];
+      final type = json['type'] as String? ?? '?';
+      TraceService.instance.addTrace(
+        'MQTT received',
+        'seq=$seq type=$type on ${msg.topic}',
+      );
+
       // Dispatch: parse topic to enrich the message with routing info
       _dispatchMessage(msg.topic, json);
     }

--
Gitblit v1.3.1