53 lines
1.7 KiB
Dart
53 lines
1.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'package:abawo_bt_app/controller/bluetooth.dart';
|
|
import 'package:abawo_bt_app/model/shifter_types.dart';
|
|
import 'package:abawo_bt_app/model/simulation.dart';
|
|
import 'package:anyhow/anyhow.dart';
|
|
|
|
class SettingsBluetooth implements BluetoothController {
|
|
final values = <String, List<int>>{
|
|
trainerProtocolCharacteristicUuid: utf8.encode('auto'),
|
|
simulationConfigCharacteristicUuid: const SimulationConfig().encode(),
|
|
simulationModelsCharacteristicUuid: utf8.encode('1:fe-c;2:track-only'),
|
|
simulationStatusCharacteristicUuid: [1, 2, 1, 0, 145, 0],
|
|
};
|
|
final steps = <String>[];
|
|
final scan = StreamController<List<int>>.broadcast();
|
|
final simulation = StreamController<List<int>>.broadcast();
|
|
bool rejectWrites = false;
|
|
|
|
@override
|
|
Future<Result<List<int>>> readCharacteristic(
|
|
String deviceId, String serviceUuid, String uuid) async {
|
|
steps.add('read:$uuid');
|
|
return values.containsKey(uuid)
|
|
? Ok(values[uuid]!)
|
|
: bail('Characteristic not found');
|
|
}
|
|
|
|
@override
|
|
Future<Result<void>> writeCharacteristic(
|
|
String deviceId, String serviceUuid, String uuid, List<int> value,
|
|
{bool withResponse = true}) async {
|
|
steps.add('write:$uuid');
|
|
if (!rejectWrites) values[uuid] = List.of(value);
|
|
return const Ok(null);
|
|
}
|
|
|
|
@override
|
|
Stream<List<int>> subscribeToCharacteristic(
|
|
String deviceId, String serviceUuid, String uuid) =>
|
|
uuid == universalShifterScanResultCharacteristicUuid
|
|
? scan.stream
|
|
: simulation.stream;
|
|
|
|
@override
|
|
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
|
|
|
Future<void> close() async {
|
|
await scan.close();
|
|
await simulation.close();
|
|
}
|
|
}
|