diff options
| -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'); |
