diff options
| -rw-r--r-- | lib/data.dart | 75 | ||||
| -rw-r--r-- | lib/ui.dart | 94 |
2 files changed, 139 insertions, 30 deletions
diff --git a/lib/data.dart b/lib/data.dart index 4962a50..14e6811 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -51,6 +51,7 @@ class AppState with ChangeNotifier { MachineState state = MachineState.init; Map<String, bool> contacts = <String, bool>{}; + Set<String> covers = <String>{}; late MqttServerClient _client; late Directory supportDir; @@ -141,6 +142,7 @@ class AppState with ChangeNotifier { void _onConnected() { _client.subscribe('$topicPrefix/contact/+/state', MqttQos.exactlyOnce); + _client.subscribe('$topicPrefix/cover/+', MqttQos.exactlyOnce); _client.subscribe('$topicPrefix/server/health', MqttQos.exactlyOnce); process(MachineEvent.connected); @@ -148,6 +150,7 @@ class AppState with ChangeNotifier { void _onDisconnected() { contacts = <String, bool>{}; + covers = <String>{}; process(MachineEvent.disconnected); } @@ -161,39 +164,53 @@ class AppState with ChangeNotifier { void _onMessage(List<MqttReceivedMessage<MqttMessage>> messages) { for (final MqttReceivedMessage<MqttMessage> message in messages) { - final String topic = message.topic; + final List<String> parts = message.topic.split('/'); + final String payload = MqttPublishPayload.bytesToStringAsString( + (message.payload as MqttPublishMessage).payload.message); - final MqttPublishMessage payloadMessage = - message.payload as MqttPublishMessage; - final String payload = - MqttPublishPayload.bytesToStringAsString(payloadMessage.payload.message); - - if (topic == '$topicPrefix/server/health') { - if (payload == 'good') { - process(MachineEvent.reachable); - } + if (message.topic == '$topicPrefix/server/health') { + _onHealthMessage(payload); + return; + } - if (payload == 'bad') { - process(MachineEvent.unreachable); - } + if (parts.length == 4 && parts[1] == 'contact' && parts[3] == 'state') { + String address = parts[2]; + _onContactMessage(payload, address); + return; } - // format <prefix>/contacts/<address>/state - final List<String> parts = topic.split('/'); - if (parts.length != 4 || parts[1] != 'contact' || parts[3] != 'state') { - continue; + if (parts.length == 3 && parts[1] == 'cover') { + String id = parts[2]; + _onCoverMessage(payload, id); + return; } - final String address = parts[2]; + } + } - final bool? parsedState = _parseBool(payload); + void _onHealthMessage(String payload) { + switch (payload) { + case 'good': + process(MachineEvent.reachable); + case 'bad': + process(MachineEvent.unreachable); + } + } - if (parsedState != null) { - contacts[address] = parsedState; - notifyListeners(); - } + void _onContactMessage(String payload, String address) { + final bool? parsedState = _parseBool(payload); + + if (parsedState != null) { + contacts[address] = parsedState; + notifyListeners(); } } + void _onCoverMessage(String payload, String id) { + if (payload != 'exists') return; + + covers.add(id); + } + bool? _parseBool(String payload) { switch (payload.toLowerCase()) { case 'open': @@ -204,4 +221,16 @@ class AppState with ChangeNotifier { return null; } } + + void publish(String topic, String payload, MqttQos qos) { + final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder(); + builder.addString(payload); + + _client.publishMessage( + '$topicPrefix/$topic', + qos, + builder.payload!, + retain: false, + ); + } } diff --git a/lib/ui.dart b/lib/ui.dart index 3a98be9..66078da 100644 --- a/lib/ui.dart +++ b/lib/ui.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:mqtt_client/mqtt_client.dart'; import 'data.dart'; @@ -90,14 +91,25 @@ class DevicesPage extends StatelessWidget { @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: const Text("Contacts")), - body: const Column( - children: <Widget>[ - Expanded(child: ContactList()), - ], + return const Scaffold( + body: SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: <Widget>[ + Padding( + padding: EdgeInsets.all(15.0), + child: Text("Contacts", style: TextStyle(fontSize: 22)), + ), + ContactList(), + Padding( + padding: EdgeInsets.all(15.0), + child: Text("Covers", style: TextStyle(fontSize: 22)), + ), + CoverList(), + ], + ), ), - bottomNavigationBar: const ConnectionStatus(), + bottomNavigationBar: ConnectionStatus(), ); } } @@ -110,6 +122,8 @@ class ContactList extends StatelessWidget { return Consumer<AppState>( builder: (BuildContext context, AppState state, Widget? child) { return ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), itemCount: state.contacts.length, itemBuilder: (BuildContext context, int index) { MapEntry<String, bool> data = state.contacts.entries.elementAt(index); @@ -129,6 +143,72 @@ class ContactList extends StatelessWidget { } } +class CoverList extends StatelessWidget { + const CoverList({super.key}); + + @override + Widget build(BuildContext context) { + return Consumer<AppState>( + builder: (BuildContext context, AppState state, Widget? child) { + return ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + itemCount: state.covers.length, + itemBuilder: (BuildContext context, int index) { + String cover = state.covers.elementAt(index); + return Column( + children: <Widget>[ + ListTile( + leading: const Icon(Icons.roller_shades), + title: Text(cover), + trailing: Row( + mainAxisSize: MainAxisSize.min, + children: <Widget>[ + IconButton( + icon: const Icon(Icons.arrow_upward), + constraints: const BoxConstraints(), + onPressed: () { + state.publish( + 'cover/$cover/movement', + 'retract', + MqttQos.exactlyOnce + ); + }, + ), + IconButton( + icon: const Icon(Icons.stop), + constraints: const BoxConstraints(), + onPressed: () { + state.publish( + 'cover/$cover/movement', + 'stop', + MqttQos.exactlyOnce + ); + }, + ), + IconButton( + icon: const Icon(Icons.arrow_downward), + constraints: const BoxConstraints(), + onPressed: () { + state.publish( + 'cover/$cover/movement', + 'extend', + MqttQos.exactlyOnce + ); + }, + ), + ], + ), + ), + ], + ); + }, + ); + }, + ); + } +} + class ConnectionStatus extends StatelessWidget { const ConnectionStatus({super.key}); |
