summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2026-01-17 20:53:07 +0100
committerxengineering <me@xengineering.eu>2026-01-17 20:56:55 +0100
commitf083de5a7d4bbc296e5fa07707a4a179f780f85b (patch)
treeeca563ff877e3333f9fc4c283ce0ae0e896643a1 /lib
parent1dc47d1991910d95b4472ed3e415adb0d46a6617 (diff)
downloadsia-app-f083de5a7d4bbc296e5fa07707a4a179f780f85b.tar
sia-app-f083de5a7d4bbc296e5fa07707a4a179f780f85b.tar.zst
sia-app-f083de5a7d4bbc296e5fa07707a4a179f780f85b.zip
Add automatic reconnecting
This allows to use the app after a temporary disconnect between app and MQTT broker.
Diffstat (limited to 'lib')
-rw-r--r--lib/data.dart13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/data.dart b/lib/data.dart
index f5a24e8..38fd564 100644
--- a/lib/data.dart
+++ b/lib/data.dart
@@ -32,11 +32,14 @@ class AppState with ChangeNotifier {
_client.port = brokerPort;
_client.keepAlivePeriod = 2;
+ _client.autoReconnect = true;
_client.disconnectOnNoResponsePeriod = 1;
_client.logging(on: false);
_client.onConnected = _onConnected;
_client.onDisconnected = _onDisconnected;
+ _client.onAutoReconnect = _onAutoReconnect;
+ _client.onAutoReconnected = _onAutoReconnected;
try {
await _client.connect();
@@ -59,6 +62,16 @@ class AppState with ChangeNotifier {
notifyListeners();
}
+ void _onAutoReconnect() {
+ _brokerConnected = false;
+ notifyListeners();
+ }
+
+ void _onAutoReconnected() {
+ _brokerConnected = true;
+ notifyListeners();
+ }
+
void _onMessage(List<MqttReceivedMessage<MqttMessage>> messages) {
for (final MqttReceivedMessage<MqttMessage> message in messages) {
final String topic = message.topic;