summaryrefslogtreecommitdiff
path: root/lib/model/motive_selection/universal_blue_motive_selection.dart
diff options
context:
space:
mode:
Diffstat (limited to 'lib/model/motive_selection/universal_blue_motive_selection.dart')
-rw-r--r--lib/model/motive_selection/universal_blue_motive_selection.dart64
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.");
+ }
+ }
}