From 4911472f43d4c6689dbc40a643c32cc08de2e21a Mon Sep 17 00:00:00 2001 From: TheLastGimbus Date: Sat, 10 Aug 2024 22:48:20 +0200 Subject: [PATCH] more cleanup --- lib/headphones/huawei/freebuds4i_impl.dart | 25 +++++++++++----------- lib/headphones/huawei/mbb.dart | 15 +++++-------- lib/logger.dart | 5 +++++ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/headphones/huawei/freebuds4i_impl.dart b/lib/headphones/huawei/freebuds4i_impl.dart index 892a3f4..60ea842 100644 --- a/lib/headphones/huawei/freebuds4i_impl.dart +++ b/lib/headphones/huawei/freebuds4i_impl.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:math'; import 'package:collection/collection.dart'; import 'package:rxdart/rxdart.dart'; @@ -39,8 +38,17 @@ final class HuaweiFreeBuds4iImpl extends HuaweiFreeBuds4i { _bluetoothAliasCtrl.onCancel = () => aliasStreamSub.cancel(); _mbb.stream.listen( - _evalMbbCommand, + (e) { + try { + _evalMbbCommand(e); + } catch (e, s) { + logg.e(e, stackTrace: s); + } + }, + onError: logg.onError, onDone: () { + _watchdogStreamSub.cancel(); + // close all streams _batteryLevelCtrl.close(); _bluetoothAliasCtrl.close(); @@ -48,8 +56,6 @@ final class HuaweiFreeBuds4iImpl extends HuaweiFreeBuds4i { _lrcBatteryCtrl.close(); _ancModeCtrl.close(); _settingsCtrl.close(); - - _watchdogStreamSub.cancel(); }, ); _initRequestInfo(); @@ -60,14 +66,13 @@ final class HuaweiFreeBuds4iImpl extends HuaweiFreeBuds4i { // no alias because it's okay to be null 👍 lrcBattery.valueOrNull, ancMode.valueOrNull, + settings.valueOrNull, ].any((e) => e == null)) { _initRequestInfo(); } }); } - // TODO: Do something smart about this for @starw1nd_ :) - // TODO: Decide how we treat exceptions here void _evalMbbCommand(MbbCommand cmd) { final lastSettings = _settingsCtrl.valueOrNull ?? const HuaweiFreeBuds4iSettings(); @@ -137,13 +142,8 @@ final class HuaweiFreeBuds4iImpl extends HuaweiFreeBuds4i { _mbb.sink.add(_Cmd.getGestureHoldToggledAncModes); } - // TODO: Get this from basic bluetooth object (when we actually have those) - // but this is fairly good for now @override - ValueStream get batteryLevel => _lrcBatteryCtrl.stream - .map((l) => max(l.levelLeft ?? -1, l.levelRight ?? -1)) - .where((b) => b >= 0) - .shareValue(); + ValueStream get batteryLevel => _bluetoothDevice.battery; // i could pass btDevice.alias directly here, but Headphones take care // of closing everything @@ -255,6 +255,7 @@ abstract class _Cmd { static MbbCommand gestureHoldToggledAncModes(Set toggledModes) { int? mbbValue; const se = SetEquality(); + // can't really do that with pattern matching because it's a Set if (se.equals(toggledModes, {AncMode.off, AncMode.noiseCancelling})) { mbbValue = 1; } diff --git a/lib/headphones/huawei/mbb.dart b/lib/headphones/huawei/mbb.dart index 76444b0..4b047a4 100644 --- a/lib/headphones/huawei/mbb.dart +++ b/lib/headphones/huawei/mbb.dart @@ -6,7 +6,6 @@ import 'package:collection/collection.dart'; import 'package:crclib/catalog.dart'; import 'package:stream_channel/stream_channel.dart'; -import '../../logger.dart'; import '../_old/headphones_data_objects.dart'; /// Helper class for Mbb protocol used to communicate with headphones @@ -128,7 +127,7 @@ class MbbCommand { } if (divided.isEmpty) { if (verify) { - throw Exception("No commands found in payload"); + throw Exception("No commands found in $payload"); } else { return []; } @@ -161,14 +160,10 @@ StreamChannel mbbChannel(StreamChannel rfcomm) => StreamChannelTransformer( StreamTransformer.fromHandlers( handleData: (data, stream) { - try { - for (final cmd in MbbCommand.fromPayload(data)) { - // FILTER THE SHIT OUT - if (cmd.serviceId == 10 && cmd.commandId == 13) continue; - stream.add(cmd); - } - } catch (e, s) { - logg.e("mbb parsing error", error: e, stackTrace: s); + for (final cmd in MbbCommand.fromPayload(data)) { + // FILTER THE SHIT OUT + if (cmd.serviceId == 10 && cmd.commandId == 13) continue; + stream.add(cmd); } }, ), diff --git a/lib/logger.dart b/lib/logger.dart index 8458841..d95e60e 100644 --- a/lib/logger.dart +++ b/lib/logger.dart @@ -7,3 +7,8 @@ final _prettyLogger = Logger( ); Logger get logg => _prettyLogger; + +extension Errors on Logger { + /// Quick drop-in for Stream's onError + void onError(Object m, StackTrace s) => e(m, stackTrace: s); +} \ No newline at end of file