From 3003bf63c8f5b347c5445becf2e58120fc53ea6b Mon Sep 17 00:00:00 2001 From: Jonas Bark Date: Tue, 28 Apr 2026 19:08:00 +0200 Subject: [PATCH] feat: wire Di2Emulator into Core connection-method registries Co-Authored-By: Claude Sonnet 4.6 --- lib/utils/core.dart | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/utils/core.dart b/lib/utils/core.dart index 36ca2d44..1289dba8 100644 --- a/lib/utils/core.dart +++ b/lib/utils/core.dart @@ -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(); }