Matthias Nott
2026-03-22 16893f9606d43171992199cf2529f3774a226b84
fix: MQTT port matches config, keyboard dismiss on drawer, debug logging
2 files modified
changed files
lib/screens/chat_screen.dart patch | view | blame | history
lib/services/mqtt_service.dart patch | view | blame | history
lib/screens/chat_screen.dart
....@@ -879,7 +879,10 @@
879879 appBar: AppBar(
880880 leading: IconButton(
881881 icon: const Icon(Icons.menu),
882
- onPressed: () => _scaffoldKey.currentState?.openDrawer(),
882
+ onPressed: () {
883
+ FocusScope.of(context).unfocus();
884
+ _scaffoldKey.currentState?.openDrawer();
885
+ },
883886 ),
884887 title: Text(
885888 activeSession?.name ?? 'PAILot',
lib/services/mqtt_service.dart
....@@ -1,7 +1,9 @@
11 import 'dart:async';
22 import 'dart:convert';
3
+import 'dart:io';
34
45 import 'package:flutter/widgets.dart';
6
+import 'package:path_provider/path_provider.dart' as pp;
57 import 'package:mqtt_client/mqtt_client.dart';
68 import 'package:mqtt_client/mqtt_server_client.dart';
79 import 'package:shared_preferences/shared_preferences.dart';
....@@ -11,8 +13,15 @@
1113 import 'websocket_service.dart' show ConnectionStatus;
1214 import 'wol_service.dart';
1315
14
-/// MQTT port — standard unencrypted MQTT.
15
-const int mqttPort = 1883;
16
+// Debug log to file (survives release builds)
17
+Future<void> _mqttLog(String msg) async {
18
+ try {
19
+ final dir = await pp.getApplicationDocumentsDirectory();
20
+ final file = File('${dir.path}/mqtt_debug.log');
21
+ final ts = DateTime.now().toIso8601String().substring(11, 19);
22
+ await file.writeAsString('[$ts] $msg\n', mode: FileMode.append);
23
+ } catch (_) {}
24
+}
1625
1726 /// MQTT client for PAILot, replacing WebSocketService.
1827 ///
....@@ -56,8 +65,10 @@
5665 if (_clientId != null) return _clientId!;
5766 final prefs = await SharedPreferences.getInstance();
5867 var id = prefs.getString('mqtt_client_id');
59
- if (id == null) {
60
- id = 'pailot-${const Uuid().v4()}';
68
+ // Regenerate if old format (too long for MQTT 3.1.1)
69
+ if (id == null || id.length > 23) {
70
+ // MQTT 3.1.1 client IDs: max 23 chars, alphanumeric
71
+ id = 'pailot${const Uuid().v4().replaceAll('-', '').substring(0, 16)}';
6172 await prefs.setString('mqtt_client_id', id);
6273 }
6374 _clientId = id;
....@@ -88,19 +99,23 @@
8899 for (final host in hosts) {
89100 if (_intentionalClose) return;
90101
102
+ _mqttLog('MQTT: trying $host:${config.port}');
91103 try {
92104 final connected = await _tryConnect(
93105 host,
94106 clientId,
95107 timeout: host == hosts.first && hosts.length > 1 ? 2500 : 5000,
96108 );
109
+ _mqttLog('MQTT: $host result=$connected');
97110 if (connected) return;
98
- } catch (_) {
111
+ } catch (e) {
112
+ _mqttLog('MQTT: $host error=$e');
99113 continue;
100114 }
101115 }
102116
103117 // All hosts failed
118
+ debugPrint('MQTT: all hosts failed');
104119 _setStatus(ConnectionStatus.disconnected);
105120 onError?.call('Failed to connect to MQTT broker');
106121 }
....@@ -117,11 +132,11 @@
117132
118133 Future<bool> _tryConnect(String host, String clientId, {int timeout = 5000}) async {
119134 try {
120
- final client = MqttServerClient.withPort(host, clientId, mqttPort);
135
+ final client = MqttServerClient.withPort(host, clientId, config.port);
121136 client.keepAlivePeriod = 30;
122137 client.autoReconnect = true;
123138 client.connectTimeoutPeriod = timeout;
124
- client.logging(on: false);
139
+ client.logging(on: true);
125140
126141 client.onConnected = _onConnected;
127142 client.onDisconnected = _onDisconnected;
....@@ -140,7 +155,9 @@
140155
141156 client.connectionMessage = connMessage;
142157
158
+ _mqttLog('MQTT: connecting to $host:${config.port} as $clientId');
143159 final result = await client.connect();
160
+ _mqttLog('MQTT: connect result=${result?.state}');
144161 if (result?.state == MqttConnectionState.connected) {
145162 _client = client;
146163 return true;
....@@ -148,6 +165,7 @@
148165 client.disconnect();
149166 return false;
150167 } catch (e) {
168
+ _mqttLog('MQTT: connect exception=$e');
151169 return false;
152170 }
153171 }