From 18cbbf877b171fc23950e64dac5fa6abda6fb02b Mon Sep 17 00:00:00 2001 From: xengineering Date: Wed, 8 Apr 2026 14:16:28 +0200 Subject: Add plug control This allows the user to turn Wi-Fi plugs on and off. --- lib/data.dart | 15 +++++++++++++++ lib/ui.dart | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) (limited to 'lib') diff --git a/lib/data.dart b/lib/data.dart index 14e6811..2016f6f 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -52,6 +52,7 @@ class AppState with ChangeNotifier { Map contacts = {}; Set covers = {}; + Set plugs = {}; late MqttServerClient _client; late Directory supportDir; @@ -143,6 +144,7 @@ class AppState with ChangeNotifier { void _onConnected() { _client.subscribe('$topicPrefix/contact/+/state', MqttQos.exactlyOnce); _client.subscribe('$topicPrefix/cover/+', MqttQos.exactlyOnce); + _client.subscribe('$topicPrefix/plug/+', MqttQos.exactlyOnce); _client.subscribe('$topicPrefix/server/health', MqttQos.exactlyOnce); process(MachineEvent.connected); @@ -151,6 +153,7 @@ class AppState with ChangeNotifier { void _onDisconnected() { contacts = {}; covers = {}; + plugs = {}; process(MachineEvent.disconnected); } @@ -184,6 +187,12 @@ class AppState with ChangeNotifier { _onCoverMessage(payload, id); return; } + + if (parts.length == 3 && parts[1] == 'plug') { + String id = parts[2]; + _onPlugMessage(payload, id); + return; + } } } @@ -211,6 +220,12 @@ class AppState with ChangeNotifier { covers.add(id); } + void _onPlugMessage(String payload, String id) { + if (payload != 'exists') return; + + plugs.add(id); + } + bool? _parseBool(String payload) { switch (payload.toLowerCase()) { case 'open': diff --git a/lib/ui.dart b/lib/ui.dart index 67a830f..2c964ed 100644 --- a/lib/ui.dart +++ b/lib/ui.dart @@ -107,6 +107,11 @@ class DevicesPage extends StatelessWidget { child: Text("Covers", style: TextStyle(fontSize: 22)), ), CoverList(), + Padding( + padding: EdgeInsets.all(15.0), + child: Text("Plugs", style: TextStyle(fontSize: 22)), + ), + PlugList(), ], ), ), @@ -211,6 +216,61 @@ class CoverList extends StatelessWidget { } } +class PlugList extends StatelessWidget { + const PlugList({super.key}); + + @override + Widget build(BuildContext context) { + return Consumer( + builder: (BuildContext context, AppState state, Widget? child) { + return ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + itemCount: state.plugs.length, + itemBuilder: (BuildContext context, int index) { + String plug = state.plugs.elementAt(index); + return Column( + children: [ + ListTile( + leading: const Icon(Icons.outlet), + title: Text(plug), + trailing: Row( + mainAxisSize: MainAxisSize.min, + children: [ + IconButton( + icon: const Icon(Icons.flash_on), + constraints: const BoxConstraints(), + onPressed: () { + state.publish( + 'plug/$plug/action', + 'on', + MqttQos.exactlyOnce + ); + }, + ), + IconButton( + icon: const Icon(Icons.flash_off), + constraints: const BoxConstraints(), + onPressed: () { + state.publish( + 'plug/$plug/action', + 'off', + MqttQos.exactlyOnce + ); + }, + ), + ], + ), + ), + ], + ); + }, + ); + }, + ); + } +} + class ConnectionStatus extends StatelessWidget { const ConnectionStatus({super.key}); -- cgit v1.3