diff options
| author | xengineering <me@xengineering.eu> | 2026-04-08 11:29:27 +0200 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2026-04-08 11:29:27 +0200 |
| commit | 9048de5d0ad7611fb92a51b59be332921a93b943 (patch) | |
| tree | 9789c4af85f680d877be9af9e5fff1614b4e8c05 /lib/data.dart | |
| parent | c2110a2098d475c5fa06d265833e229fad938e8d (diff) | |
| parent | e4c61d1b669a43d292be1f40d5b36dbcb93500a4 (diff) | |
| download | sia-app-9048de5d0ad7611fb92a51b59be332921a93b943.tar sia-app-9048de5d0ad7611fb92a51b59be332921a93b943.tar.zst sia-app-9048de5d0ad7611fb92a51b59be332921a93b943.zip | |
Merge cover control
This allows the user to move covers controlled by the Sia server to move
up, down and to stop them.
Diffstat (limited to 'lib/data.dart')
| -rw-r--r-- | lib/data.dart | 75 |
1 files changed, 52 insertions, 23 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, + ); + } } |
