diff options
| author | xengineering <me@xengineering.eu> | 2026-03-07 16:35:13 +0100 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2026-03-07 16:35:59 +0100 |
| commit | 796a2b21e8b5989187bfc7d4ddb7ba648038cbca (patch) | |
| tree | d1b23fa6ebe0f2601bef0158475452e46962e790 /lib/data.dart | |
| parent | 2e15dd323b7c3198e2af10f35f1186d91cc6cfaf (diff) | |
| download | sia-app-796a2b21e8b5989187bfc7d4ddb7ba648038cbca.tar sia-app-796a2b21e8b5989187bfc7d4ddb7ba648038cbca.tar.zst sia-app-796a2b21e8b5989187bfc7d4ddb7ba648038cbca.zip | |
Remove automatic initial connection
The user should provide the fully qualified domain name (FQDN) of the
MQTT broker manually and then press connect to actually connect
including automated re-connects.
As a first step the initial connect is bound to the manual button in the
bottom bar. To reduce the scope the disconnect button is labeled
"(disabled)" to make clear this is not expected to work.
Diffstat (limited to 'lib/data.dart')
| -rw-r--r-- | lib/data.dart | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/lib/data.dart b/lib/data.dart index 53c3d01..89d8760 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:flutter/foundation.dart'; import 'package:mqtt_client/mqtt_client.dart'; import 'package:mqtt_client/mqtt_server_client.dart'; @@ -23,9 +21,6 @@ class AppState with ChangeNotifier { MachineState.init: <MachineEvent, MachineState> { MachineEvent.connect: MachineState.disconnected, }, - MachineState.disconnected: <MachineEvent, MachineState> { - MachineEvent.disconnect: MachineState.init, - }, }; MachineState state = MachineState.init; @@ -36,22 +31,30 @@ class AppState with ChangeNotifier { bool _serverConnected = false; bool get serverConnected => _serverConnected; - AppState() { - _initMqtt(); - } + AppState(); void process(MachineEvent event) { - Map<MachineEvent, MachineState>? ruleset = machine[state]; - if (ruleset == null) { + MachineState lastState = state; + + Map<MachineEvent, MachineState>? transitions = machine[lastState]; + if (transitions == null) { return; } - MachineState? nextMachineState = ruleset[event]; - if (nextMachineState == null) { + MachineState? nextState = transitions[event]; + if (nextState == null) { return; } - state = nextMachineState; + if (nextState == lastState) { + return; + } + + if (lastState == MachineState.init) { + _initMqtt(); + } + + state = nextState; notifyListeners(); } @@ -61,7 +64,7 @@ class AppState with ChangeNotifier { super.dispose(); } - Future<void> _initMqtt() async { + void _initMqtt() { _client = MqttServerClient( brokerHostname, 'sia_app_${DateTime.now().millisecondsSinceEpoch}', @@ -79,7 +82,7 @@ class AppState with ChangeNotifier { _client.onAutoReconnected = _onAutoReconnected; try { - await _client.connect(); + _client.connect(); } catch (e) { _client.disconnect(); return; |
