diff options
author | uvok | 2025-08-01 18:16:48 +0200 |
---|---|---|
committer | uvok | 2025-08-01 18:16:48 +0200 |
commit | 28e4d74ef0a6e46d37c9cc5d98ca62334e69523c (patch) | |
tree | 7aaad4cb9e22ad90e0e78a8c8dcaee6ad0c959b1 /lib/model | |
parent | b1526aec0849fdc710f98540d2928d4fe6ee3959 (diff) |
UB: Set motive
Diffstat (limited to 'lib/model')
-rw-r--r-- | lib/model/motive_selection/universal_blue_motive_selection.dart | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/lib/model/motive_selection/universal_blue_motive_selection.dart b/lib/model/motive_selection/universal_blue_motive_selection.dart index 86429b6..43b717e 100644 --- a/lib/model/motive_selection/universal_blue_motive_selection.dart +++ b/lib/model/motive_selection/universal_blue_motive_selection.dart @@ -14,8 +14,11 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. import 'dart:convert'; +import 'dart:io'; import 'package:universal_ble/universal_ble.dart'; +import 'package:uvok_epaper_badge/string_ext.dart'; +import 'package:uvok_epaper_badge/badge_exception.dart'; import 'package:uvok_epaper_badge/model/badge_motive.dart'; import 'package:uvok_epaper_badge/model/device/universal_ble_device.dart'; import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; @@ -35,16 +38,41 @@ class UniversalBlueMotiveSelection implements BadgeMotiveSelection { @override Future<BadgeMotive> getCurrentMotive() async { + await _ensureConnected(); + + if (_cachedMotives.isEmpty) { + await getMotives(); + } if (_cachedMotives.isEmpty) { - return BadgeMotive(-1, "No motives"); + throw BadgeException( + "No motives available, so there's no current motive", + ); + } + + try { + var c = await _device.device.getCharacteristic( + _currentMotiveCharacteristic, + service: _badgeService, + ); + var val = await c.read(); + int? currentMotive = int.tryParse(ascii.decode(val)); + if (currentMotive == null) { + throw BadgeException("Error reading current motive."); + } + return _cachedMotives.singleWhere( + (bm) => bm.id == currentMotive, + orElse: () => throw BadgeException("Selected motive not in templates"), + ); + } on NotFoundException { + throw BadgeException("Characeristic/Service not found."); } - return BadgeMotive(-1, "Unspecified motive"); } @override Future<List<BadgeMotive>> getMotives() async { - bool gotMotives = false; - if (await _device.device.connectionState == BleConnectionState.connected) { + await _ensureConnected(); + + try { var c = await _device.device.getCharacteristic( _availableMotivesCharacteristic, service: _badgeService, @@ -55,22 +83,38 @@ class UniversalBlueMotiveSelection implements BadgeMotiveSelection { .split(";") .where((s) => s.isNotEmpty) .map((String s) { - List<String> parts = s.split("-"); + List<String> parts = s.splitFirst("-"); if (parts.length != 2) { return BadgeMotive(-1, "Invalid value"); } return BadgeMotive(int.tryParse(parts[0]) ?? -1, parts[1]); }) .toList(growable: false); - gotMotives = true; - } - if (!gotMotives) { - _cachedMotives = [BadgeMotive(-1, "Failed fetching motives")]; + } on NotFoundException { + throw BadgeException("Characeristic/Service not found."); } return _cachedMotives; } + Future<void> _ensureConnected() async { + if (await _device.device.connectionState != BleConnectionState.connected) { + throw BadgeException("Not connected"); + } + } + @override - Future<void> setCurrentMotive(BadgeMotive motive) async {} + Future<void> setCurrentMotive(BadgeMotive motive) async { + await _ensureConnected(); + try { + var c = await _device.device.getCharacteristic( + _currentMotiveCharacteristic, + service: _badgeService, + ); + + await c.write(ascii.encode(motive.id.toString()), withResponse: false); + } on NotFoundException { + throw BadgeException("Characeristic/Service not found."); + } + } } |