Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastGimbus committed Aug 10, 2024
1 parent 96ddda4 commit 4911472
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
25 changes: 13 additions & 12 deletions lib/headphones/huawei/freebuds4i_impl.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:rxdart/rxdart.dart';
Expand Down Expand Up @@ -39,17 +38,24 @@ 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();
_bluetoothNameCtrl.close();
_lrcBatteryCtrl.close();
_ancModeCtrl.close();
_settingsCtrl.close();

_watchdogStreamSub.cancel();
},
);
_initRequestInfo();
Expand All @@ -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();
Expand Down Expand Up @@ -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<int> get batteryLevel => _lrcBatteryCtrl.stream
.map((l) => max(l.levelLeft ?? -1, l.levelRight ?? -1))
.where((b) => b >= 0)
.shareValue();
ValueStream<int> get batteryLevel => _bluetoothDevice.battery;

// i could pass btDevice.alias directly here, but Headphones take care
// of closing everything
Expand Down Expand Up @@ -255,6 +255,7 @@ abstract class _Cmd {
static MbbCommand gestureHoldToggledAncModes(Set<AncMode> 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;
}
Expand Down
15 changes: 5 additions & 10 deletions lib/headphones/huawei/mbb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 [];
}
Expand Down Expand Up @@ -161,14 +160,10 @@ StreamChannel<MbbCommand> mbbChannel(StreamChannel<Uint8List> 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);
}
},
),
Expand Down
5 changes: 5 additions & 0 deletions lib/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 4911472

Please sign in to comment.