feat: wire Di2Emulator into Core connection-method registries

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jonas Bark
2026-04-28 19:08:00 +02:00
parent 5fa96ddb8c
commit 3003bf63c8

View File

@@ -5,6 +5,7 @@ import 'package:bike_control/bluetooth/devices/openbikecontrol/obc_mdns_emulator
import 'package:bike_control/bluetooth/devices/openbikecontrol/protocol_parser.dart';
import 'package:bike_control/bluetooth/devices/trainer_connection.dart';
import 'package:bike_control/bluetooth/devices/zwift/ftms_mdns_emulator.dart';
import 'package:bike_control/bluetooth/devices/shimano/di2_emulator.dart';
import 'package:bike_control/bluetooth/devices/zwift/zwift_emulator.dart';
import 'package:bike_control/bluetooth/messages/notification.dart';
import 'package:bike_control/bluetooth/remote_keyboard_pairing.dart';
@@ -69,6 +70,7 @@ class Core {
late final obpBluetoothEmulator = OpenBikeControlBluetoothEmulator();
late final remotePairing = RemotePairing();
late final remoteKeyboardPairing = RemoteKeyboardPairing();
late final di2Emulator = Di2Emulator();
late final mediaKeyHandler = MediaKeyHandler();
late final logic = CoreLogic();
@@ -102,6 +104,10 @@ class Core {
settings.setRemoteKeyboardControlEnabled(false);
await remoteKeyboardPairing.stopAdvertising();
}
if (settings.getDi2BleEnabled()) {
await settings.setDi2BleEnabled(false);
await di2Emulator.stopAdvertising();
}
}
}
@@ -201,6 +207,10 @@ class CoreLogic {
return core.settings.getZwiftMdnsEmulatorEnabled() && showZwiftMsdnEmulator;
}
bool get isDi2BleEnabled {
return core.settings.getDi2BleEnabled() && showDi2Ble;
}
bool get isObpBleEnabled {
return core.settings.getObpBleEnabled() && showObpBluetoothEmulator;
}
@@ -220,6 +230,13 @@ class CoreLogic {
core.settings.getLastTarget() != Target.thisDevice;
}
bool get showDi2Ble {
final app = core.settings.getTrainerApp();
return app != null &&
app.supports(AppConnectionMethod.di2Ble) &&
core.settings.getLastTarget() != Target.thisDevice;
}
bool get showZwiftMsdnEmulator {
final app = core.settings.getTrainerApp();
return app != null && app.supports(AppConnectionMethod.zwiftMdns);
@@ -264,7 +281,8 @@ class CoreLogic {
(core.settings.getZwiftBleEmulatorEnabled() && showZwiftBleEmulator) ||
(core.settings.getZwiftMdnsEmulatorEnabled() && showZwiftMsdnEmulator) ||
(core.settings.getObpBleEnabled() && showObpBluetoothEmulator) ||
(core.settings.getObpMdnsEnabled() && showObpMdnsEmulator);
(core.settings.getObpMdnsEnabled() && showObpMdnsEmulator) ||
(core.settings.getDi2BleEnabled() && showDi2Ble);
bool get showObpActions =>
(core.settings.getObpBleEnabled() && showObpBluetoothEmulator) ||
@@ -282,7 +300,8 @@ class CoreLogic {
!isZwiftMdnsEnabled &&
!showObpActions &&
!(core.settings.getMyWhooshLinkEnabled() && showMyWhooshLink) &&
!showLocalRemoteOptions;
!showLocalRemoteOptions &&
!isDi2BleEnabled;
bool get hasRecommendedConnectionMethods =>
showObpBluetoothEmulator ||
@@ -302,6 +321,7 @@ class CoreLogic {
if (isMyWhooshLinkEnabled) core.whooshLink,
if (isZwiftBleEnabled) core.zwiftEmulator,
if (isZwiftMdnsEnabled) core.zwiftMdnsEmulator,
if (isDi2BleEnabled) core.di2Emulator,
if (isRemoteControlEnabled) core.remotePairing,
if (isRemoteKeyboardControlEnabled) core.remoteKeyboardPairing,
].filter((e) => e.isConnected.value).toList();
@@ -312,6 +332,7 @@ class CoreLogic {
if (isMyWhooshLinkEnabled) core.whooshLink,
if (isZwiftBleEnabled) core.zwiftEmulator,
if (isZwiftMdnsEnabled) core.zwiftMdnsEmulator,
if (isDi2BleEnabled) core.di2Emulator,
if (isRemoteControlEnabled) core.remotePairing,
if (isRemoteKeyboardControlEnabled) core.remoteKeyboardPairing,
].filter((e) => e.isConnected.value).toList();
@@ -323,6 +344,7 @@ class CoreLogic {
if (isMyWhooshLinkEnabled) core.whooshLink,
if (isZwiftBleEnabled) core.zwiftEmulator,
if (isZwiftMdnsEnabled) core.zwiftMdnsEmulator,
if (isDi2BleEnabled) core.di2Emulator,
if (isRemoteControlEnabled) core.remotePairing,
if (isRemoteKeyboardControlEnabled) core.remoteKeyboardPairing,
].sortedBy((e) => e.isConnected.value ? 0 : 1);
@@ -353,6 +375,7 @@ class CoreLogic {
if (isMyWhooshLinkEnabled) core.whooshLink,
if (isZwiftBleEnabled) core.zwiftEmulator,
if (isZwiftMdnsEnabled) core.zwiftMdnsEmulator,
if (isDi2BleEnabled) core.di2Emulator,
if (isRemoteControlEnabled) core.remotePairing,
if (isRemoteKeyboardControlEnabled) core.remoteKeyboardPairing,
];
@@ -363,6 +386,7 @@ class CoreLogic {
if (showMyWhooshLink) core.whooshLink,
if (showZwiftBleEmulator) core.zwiftEmulator,
if (showZwiftMsdnEmulator) core.zwiftMdnsEmulator,
if (showDi2Ble) core.di2Emulator,
if (showRemote) core.remotePairing,
if (showRemote) core.remoteKeyboardPairing,
];
@@ -434,6 +458,18 @@ class CoreLogic {
});
}
if (isDi2BleEnabled &&
await core.permissions.getRemoteControlRequirements().allGranted &&
!core.di2Emulator.isStarted.value) {
core.di2Emulator.startAdvertising().catchError((e, s) {
recordError(e, s, context: 'Di2 Emulator');
core.settings.setDi2BleEnabled(false);
core.connection.signalNotification(
AlertNotification(LogLevel.LOGLEVEL_WARNING, 'Failed to start Di2 Emulator: $e'),
);
});
}
if (isMyWhooshLinkEnabled && !core.whooshLink.isStarted.value) {
core.connection.startMyWhooshServer();
}