Matthias Nott
9 days ago 525030c3caccef27a1da44605d28e259dc1cc17c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:mqtt_client/mqtt_client.dart';
import '../services/mqtt_service.dart';
import '../services/trace_service.dart';
/// Displays the in-memory trace log for diagnosing message delivery problems.
///
/// Shows entries in reverse chronological order (newest first).
/// Accessible from the Settings screen via "Message Trace Log".
/// Works in both debug and release builds.
class TraceScreen extends StatefulWidget {
  /// Optional MQTT service reference so entries can be published to the server.
  final MqttService? mqttService;
  const TraceScreen({super.key, this.mqttService});
  @override
  State<TraceScreen> createState() => _TraceScreenState();
}
class _TraceScreenState extends State<TraceScreen> {
  bool _sending = false;
  List<TraceEntry> get _entries =>
      TraceService.instance.entries.reversed.toList();
  Future<void> _sendToServer() async {
    final service = widget.mqttService;
    if (service == null || !service.isConnected) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Not connected to server')),
        );
      }
      return;
    }
    setState(() => _sending = true);
    try {
      final entries = TraceService.instance.entries;
      final payload = jsonEncode({
        'type': 'trace_log',
        'source': 'pailot',
        'ts': DateTime.now().millisecondsSinceEpoch,
        'count': entries.length,
        'entries': entries
            .map((e) => {
                  'timestamp': e.timestamp.toIso8601String(),
                  'event': e.event,
                  'details': e.details,
                })
            .toList(),
      });
      // Build the MQTT payload
      final builder = MqttClientPayloadBuilder();
      builder.addString(payload);
      // Publish on pailot/trace — daemon can subscribe/log this
      service.send({
        'type': 'command',
        'command': 'trace_upload',
        'args': {
          'count': entries.length,
          'payload': payload,
        },
      });
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Sent ${entries.length} trace entries to server'),
          ),
        );
      }
    } catch (e) {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Send failed: $e')),
        );
      }
    } finally {
      if (mounted) setState(() => _sending = false);
    }
  }
  void _clearLog() {
    TraceService.instance.clear();
    setState(() {});
  }
  Color _colorForEvent(String event) {
    if (event.contains('error') || event.contains('fail') || event.contains('drop')) {
      return Colors.red.shade300;
    }
    if (event.contains('dedup')) return Colors.orange.shade300;
    if (event.contains('displayed') || event.contains('published')) {
      return Colors.green.shade300;
    }
    if (event.contains('MQTT')) return Colors.blue.shade300;
    if (event.contains('voice')) return Colors.purple.shade300;
    return Colors.grey.shade400;
  }
  @override
  Widget build(BuildContext context) {
    final entries = _entries;
    return Scaffold(
      appBar: AppBar(
        title: const Text('Message Trace Log'),
        actions: [
          IconButton(
            icon: const Icon(Icons.delete_outline),
            tooltip: 'Clear log',
            onPressed: _clearLog,
          ),
          IconButton(
            icon: _sending
                ? const SizedBox(
                    width: 20,
                    height: 20,
                    child: CircularProgressIndicator(strokeWidth: 2),
                  )
                : const Icon(Icons.upload_outlined),
            tooltip: 'Send to server',
            onPressed: _sending ? null : _sendToServer,
          ),
        ],
      ),
      body: entries.isEmpty
          ? const Center(
              child: Text(
                'No trace entries yet.\nSend a message to generate traces.',
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.grey),
              ),
            )
          : Column(
              children: [
                Container(
                  color: Colors.black87,
                  padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
                  child: Row(
                    children: [
                      Text(
                        '${entries.length} entries (newest first)',
                        style: const TextStyle(
                          fontSize: 11,
                          color: Colors.grey,
                          fontFamily: 'monospace',
                        ),
                      ),
                      const Spacer(),
                      const Text(
                        'Tap to copy',
                        style: TextStyle(fontSize: 11, color: Colors.grey),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: ListView.builder(
                    itemCount: entries.length,
                    itemBuilder: (context, index) {
                      final entry = entries[index];
                      final ts = entry.timestamp
                          .toIso8601String()
                          .substring(11, 23); // HH:mm:ss.mmm
                      return InkWell(
                        onTap: () {
                          // Copy full entry to clipboard on tap
                          final text = '$ts | ${entry.event} | ${entry.details}';
                          ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(
                              content: Text(
                                text,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(fontFamily: 'monospace', fontSize: 11),
                              ),
                              duration: const Duration(seconds: 2),
                            ),
                          );
                        },
                        child: Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 12,
                            vertical: 6,
                          ),
                          decoration: BoxDecoration(
                            border: Border(
                              bottom: BorderSide(
                                color: Colors.grey.shade800,
                                width: 0.5,
                              ),
                            ),
                          ),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                ts,
                                style: const TextStyle(
                                  fontFamily: 'monospace',
                                  fontSize: 10,
                                  color: Colors.grey,
                                ),
                              ),
                              const SizedBox(width: 8),
                              Expanded(
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      entry.event,
                                      style: TextStyle(
                                        fontFamily: 'monospace',
                                        fontSize: 11,
                                        fontWeight: FontWeight.bold,
                                        color: _colorForEvent(entry.event),
                                      ),
                                    ),
                                    if (entry.details.isNotEmpty)
                                      Text(
                                        entry.details,
                                        style: const TextStyle(
                                          fontFamily: 'monospace',
                                          fontSize: 10,
                                          color: Colors.grey,
                                        ),
                                        maxLines: 2,
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  ),
                ),
              ],
            ),
    );
  }
}