diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/badge_exception.dart | 18 | ||||
-rw-r--r-- | lib/main.dart | 2 | ||||
-rw-r--r-- | lib/model/motive_selection/universal_blue_motive_selection.dart | 64 | ||||
-rw-r--r-- | lib/string_ext.dart | 24 |
4 files changed, 97 insertions, 11 deletions
diff --git a/lib/badge_exception.dart b/lib/badge_exception.dart new file mode 100644 index 0000000..4dae0f8 --- /dev/null +++ b/lib/badge_exception.dart @@ -0,0 +1,18 @@ +// Copyright (C) 2025, uvok cheetah +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +class BadgeException implements Exception { + BadgeException(String cause); +} diff --git a/lib/main.dart b/lib/main.dart index e160a76..7cbafbf 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -26,7 +26,7 @@ var logger = Logger(); void main() { final ScannerController scanner; - final int arg = 0; + final int arg = 1; switch (arg) { case 1: 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."); + } + } } diff --git a/lib/string_ext.dart b/lib/string_ext.dart new file mode 100644 index 0000000..6119c5b --- /dev/null +++ b/lib/string_ext.dart @@ -0,0 +1,24 @@ +// Copyright (C) 2025, uvok cheetah +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. + +extension StringExt on String { + /// split string on first occurrence. + /// if s is not inside string, the original value is returned. + List<String> splitFirst(String s) { + int idx = indexOf(s); + if (idx == -1) return [this]; + return [substring(0, idx).trim(), substring(idx + s.length).trim()]; + } +} |