mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-28 16:58:41 +01:00
feat(trainer): cycle the bridge connection when the control protocol changes
Trainers latch their control session to the protocol live at connect time, so a switch only takes real effect on a fresh connection (Jonas verified on hardware via the manual Bridge disconnect/reconnect). Both switch sites — the VS-options selector and the self-test verdict CTA — now run ProxyDevice.reconnectUpstream(), which mirrors the ConnectionCard cycle and waits for the reseeded definition. Inert picks (same effective delivery) skip the cycle. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -855,6 +855,25 @@ class ProxyDevice extends BluetoothDevice {
|
||||
@override
|
||||
Future<void> disconnectUpstream() => transport.disconnect();
|
||||
|
||||
/// Tears the upstream trainer connection down and brings it back up — the
|
||||
/// programmatic twin of picking "No connection" and then the bridge mode
|
||||
/// again in the ConnectionCard. A control-protocol change only takes real
|
||||
/// effect on a fresh connection: trainers latch their control session to
|
||||
/// the protocol that was live at connect time, so the cycle re-runs
|
||||
/// discovery, reseeds the definition (re-applying the persisted override)
|
||||
/// and performs the new protocol's handshakes from scratch.
|
||||
Future<void> reconnectUpstream() async {
|
||||
if (!isConnected && !isStartedListenable.value) return;
|
||||
await core.connection.disconnect(this, forget: false, persistForget: false, keepInList: true);
|
||||
await core.connection.connectDevice(this);
|
||||
// connectDevice resolves before discovery finishes on some transports —
|
||||
// wait for the rebuilt definition so callers can start using it.
|
||||
final deadline = DateTime.now().add(const Duration(seconds: 15));
|
||||
while ((!isConnected || fitnessBike == null) && DateTime.now().isBefore(deadline)) {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 200));
|
||||
}
|
||||
}
|
||||
|
||||
void _onWifiDisconnected() {
|
||||
if (!isConnected) return;
|
||||
isConnected = false;
|
||||
|
||||
@@ -34,7 +34,11 @@ class SelfTestCard extends StatefulWidget {
|
||||
/// Wired by the page to its overlay-section reveal; used by the PASS CTA.
|
||||
final VoidCallback? onShowOverlaySettings;
|
||||
|
||||
const SelfTestCard({super.key, required this.device, this.engineFactory, this.onShowOverlaySettings});
|
||||
/// Test seam: the connection cycle a protocol switch triggers; defaults to
|
||||
/// [ProxyDevice.reconnectUpstream].
|
||||
final Future<void> Function()? reconnectDevice;
|
||||
|
||||
const SelfTestCard({super.key, required this.device, this.engineFactory, this.onShowOverlaySettings, this.reconnectDevice});
|
||||
|
||||
@override
|
||||
State<SelfTestCard> createState() => _SelfTestCardState();
|
||||
@@ -411,6 +415,16 @@ class _SelfTestCardState extends State<SelfTestCard> {
|
||||
/// [ShiftingConfig].
|
||||
Future<void> _switchProtocolAndRerun(String protocol) async {
|
||||
_engine?.harness.setProtocol(protocol);
|
||||
// The trainer only honors the new delivery on a fresh connection (it
|
||||
// latches its control session at connect time) — cycle the bridge the way
|
||||
// the ConnectionCard's manual disconnect/reconnect does, then rerun on
|
||||
// the rebuilt definition. [ProxyDevice.reconnectUpstream] itself waits
|
||||
// for the definition to come back.
|
||||
try {
|
||||
await (widget.reconnectDevice ?? widget.device.reconnectUpstream)();
|
||||
} catch (e, s) {
|
||||
recordError(e, s, context: 'self-test protocol switch reconnect');
|
||||
}
|
||||
await _start();
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,12 @@ import 'package:shadcn_flutter/shadcn_flutter.dart';
|
||||
class TrainerSettingsSection extends StatefulWidget {
|
||||
final FitnessBikeDefinition definition;
|
||||
final ProxyDevice device;
|
||||
const TrainerSettingsSection({super.key, required this.definition, required this.device});
|
||||
|
||||
/// Test seam: the connection cycle a protocol change triggers; defaults to
|
||||
/// [ProxyDevice.reconnectUpstream].
|
||||
final Future<void> Function()? reconnectDevice;
|
||||
|
||||
const TrainerSettingsSection({super.key, required this.definition, required this.device, this.reconnectDevice});
|
||||
|
||||
@override
|
||||
State<TrainerSettingsSection> createState() => _TrainerSettingsSectionState();
|
||||
@@ -110,11 +115,21 @@ class _TrainerSettingsSectionState extends State<TrainerSettingsSection> {
|
||||
Text(protocol == null ? context.i18n.controlProtocolAuto : _protocolLabel(protocol)),
|
||||
placeholder: Text(context.i18n.controlProtocolAuto),
|
||||
onChanged: (protocol) async {
|
||||
final before = def.controlProtocol;
|
||||
def.setControlProtocolOverride(protocol);
|
||||
await core.settings.setControlProtocolOverride(widget.device.trainerKey, protocol?.name);
|
||||
// The override is plain state on the definition, not a listenable —
|
||||
// nothing else would repaint the select with the new value.
|
||||
if (mounted) setState(() {});
|
||||
// The trainer latches its control session to the protocol that was
|
||||
// live at connect time, so an effective change only takes hold on a
|
||||
// fresh connection — cycle the bridge like the ConnectionCard's
|
||||
// manual disconnect/reconnect would. Inert picks (same effective
|
||||
// delivery, e.g. forcing zwiftHub on an auto-zwiftHub trainer)
|
||||
// skip the cycle.
|
||||
if (def.controlProtocol != before) {
|
||||
await (widget.reconnectDevice ?? widget.device.reconnectUpstream)();
|
||||
}
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@@ -116,7 +116,11 @@ Future<void> main() async {
|
||||
return device;
|
||||
}
|
||||
|
||||
Future<void> pumpSection(WidgetTester tester, ProxyDevice device) async {
|
||||
Future<void> pumpSection(
|
||||
WidgetTester tester,
|
||||
ProxyDevice device, {
|
||||
Future<void> Function()? reconnect,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
ShadcnApp(
|
||||
localizationsDelegates: const [AppLocalizations.delegate],
|
||||
@@ -125,7 +129,12 @@ Future<void> main() async {
|
||||
child: SingleChildScrollView(
|
||||
child: SizedBox(
|
||||
width: 380,
|
||||
child: TrainerSettingsSection(definition: device.fitnessBike!, device: device),
|
||||
child: TrainerSettingsSection(
|
||||
definition: device.fitnessBike!,
|
||||
device: device,
|
||||
// Keep widget tests off the real connection manager.
|
||||
reconnectDevice: reconnect ?? () async {},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -166,6 +175,38 @@ Future<void> main() async {
|
||||
expect(core.settings.getControlProtocolOverride(device.trainerKey), 'zwiftHub');
|
||||
});
|
||||
|
||||
testWidgets('switching to a different effective protocol cycles the connection', (tester) async {
|
||||
final device = trainer(dualProtocolServices());
|
||||
var reconnects = 0;
|
||||
await pumpSection(tester, device, reconnect: () async => reconnects++);
|
||||
|
||||
await tester.tap(find.byType(Select<TrainerControlProtocol?>));
|
||||
await tester.pumpAndSettle();
|
||||
// Auto on this fixture is zwiftHub, so forcing FTMS changes the delivery.
|
||||
await tester.tap(find.text(AppLocalizations.current.controlProtocolFtms).last);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(device.fitnessBike!.controlProtocol, TrainerControlProtocol.ftms);
|
||||
expect(reconnects, 1);
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
});
|
||||
|
||||
testWidgets('an inert selection (same effective protocol) does not cycle the connection', (tester) async {
|
||||
final device = trainer(dualProtocolServices());
|
||||
var reconnects = 0;
|
||||
await pumpSection(tester, device, reconnect: () async => reconnects++);
|
||||
|
||||
await tester.tap(find.byType(Select<TrainerControlProtocol?>));
|
||||
await tester.pumpAndSettle();
|
||||
// Explicitly picking "Zwift protocol" on an auto-zwiftHub trainer is the
|
||||
// documented inert choice — no delivery change, so no reconnect.
|
||||
await tester.tap(find.text(AppLocalizations.current.controlProtocolZwift).last);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(device.fitnessBike!.controlProtocol, TrainerControlProtocol.zwiftHub);
|
||||
expect(reconnects, 0);
|
||||
});
|
||||
|
||||
testWidgets('picking Auto again clears the stored override', (tester) async {
|
||||
final device = trainer(dualProtocolServices());
|
||||
final def = device.fitnessBike!;
|
||||
|
||||
@@ -71,6 +71,7 @@ Future<void> main() async {
|
||||
ProxyDevice device,
|
||||
FakeSelfTestHarness harness, {
|
||||
VoidCallback? onShowOverlaySettings,
|
||||
Future<void> Function()? reconnect,
|
||||
}) async {
|
||||
await tester.pumpWidget(
|
||||
ShadcnApp(
|
||||
@@ -81,6 +82,8 @@ Future<void> main() async {
|
||||
device: device,
|
||||
engineFactory: () => engineOver(harness),
|
||||
onShowOverlaySettings: onShowOverlaySettings,
|
||||
// Keep widget tests off the real connection manager.
|
||||
reconnectDevice: reconnect ?? () async {},
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -276,7 +279,8 @@ Future<void> main() async {
|
||||
..obeysShift = false
|
||||
..supportedProtocolNames = ['ftms', 'zwiftHub'];
|
||||
|
||||
await pumpCard(tester, connectedTrainer(), harness);
|
||||
var reconnects = 0;
|
||||
await pumpCard(tester, connectedTrainer(), harness, reconnect: () async => reconnects++);
|
||||
await tester.tap(find.text('Test resistance control'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
@@ -288,8 +292,10 @@ Future<void> main() async {
|
||||
await tester.tap(find.text('Try via Zwift protocol & run again'));
|
||||
await tester.pump();
|
||||
|
||||
// The tap flips the fake's recorded protocol before the fresh run starts.
|
||||
// The tap flips the fake's recorded protocol and cycles the trainer
|
||||
// connection before the fresh run starts.
|
||||
expect(harness.protocolName, 'zwiftHub');
|
||||
expect(reconnects, 1);
|
||||
expect(find.text('Stop test'), findsOneWidget);
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
Reference in New Issue
Block a user