diff options
| author | xengineering <me@xengineering.eu> | 2026-01-17 21:12:25 +0100 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2026-01-17 21:12:25 +0100 |
| commit | b38f887c0ed04e32ae62f097288fd318b4d3c8f2 (patch) | |
| tree | 814e5c8c4056160d247bd30414b711d521fb233f | |
| parent | f083de5a7d4bbc296e5fa07707a4a179f780f85b (diff) | |
| download | sia-app-main.tar sia-app-main.tar.zst sia-app-main.zip | |
There are now three instead of two status states:
- "Connected" / MQTT connected, Sia server connected
- "Connection issue" / MQTT connected, Sia server disconnected
- "Disconnected" / all other cases (Sia server connection unknown)
| -rw-r--r-- | lib/data.dart | 25 | ||||
| -rw-r--r-- | lib/ui.dart | 6 |
2 files changed, 25 insertions, 6 deletions
diff --git a/lib/data.dart b/lib/data.dart index 38fd564..2583d1a 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -13,6 +13,8 @@ class AppState with ChangeNotifier { late final MqttServerClient _client; bool _brokerConnected = false; bool get brokerConnected => _brokerConnected; + bool _serverConnected = false; + bool get serverConnected => _serverConnected; AppState() { _initMqtt(); @@ -53,6 +55,7 @@ class AppState with ChangeNotifier { void _onConnected() { _client.subscribe('$topicPrefix/contact/+/state', MqttQos.exactlyOnce); + _client.subscribe('$topicPrefix/server/health', MqttQos.exactlyOnce); _brokerConnected = true; notifyListeners(); } @@ -76,6 +79,23 @@ class AppState with ChangeNotifier { for (final MqttReceivedMessage<MqttMessage> message in messages) { final String topic = message.topic; + final MqttPublishMessage payloadMessage = + message.payload as MqttPublishMessage; + final String payload = + MqttPublishPayload.bytesToStringAsString(payloadMessage.payload.message); + + if (topic == '$topicPrefix/server/health') { + if (payload == 'good') { + _serverConnected = true; + notifyListeners(); + } + + if (payload == 'bad') { + _serverConnected = false; + notifyListeners(); + } + } + // format <prefix>/contacts/<address>/state final List<String> parts = topic.split('/'); if (parts.length != 4 || parts[1] != 'contact' || parts[3] != 'state') { @@ -83,11 +103,6 @@ class AppState with ChangeNotifier { } final String address = parts[2]; - final MqttPublishMessage payloadMessage = - message.payload as MqttPublishMessage; - final String payload = - MqttPublishPayload.bytesToStringAsString(payloadMessage.payload.message); - final bool? parsedState = _parseBool(payload); if (parsedState != null) { diff --git a/lib/ui.dart b/lib/ui.dart index 38ace9a..e6cb6f4 100644 --- a/lib/ui.dart +++ b/lib/ui.dart @@ -58,9 +58,13 @@ class ConnectionStatus extends StatelessWidget { builder: (BuildContext context, AppState state, Widget? child) { Icon icon; Text text; - if (state.brokerConnected) { + + if (state.brokerConnected && state.serverConnected) { icon = const Icon(Icons.cloud, color: Colors.green); text = const Text('Connected'); + } else if (state.brokerConnected && !state.serverConnected) { + icon = const Icon(Icons.cloud_off, color: Colors.orange); + text = const Text('Connection issue'); } else { icon = const Icon(Icons.cloud_off, color: Colors.red); text = const Text('Disconnected'); |
