mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-02 10:09:09 +01:00
92 lines
3.9 KiB
Dart
92 lines
3.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer' as developer;
|
|
|
|
import 'package:bike_control/bluetooth/devices/openbikecontrol/obc_gear.dart';
|
|
import 'package:bike_control/bluetooth/devices/proxy/proxy_device.dart';
|
|
import 'package:bike_control/pages/proxy_device_details/gear_ratios_editor_page.dart';
|
|
import 'package:bike_control/utils/core.dart';
|
|
import 'package:bike_control/utils/keymap/buttons.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
/// Debug-build service extensions for driving and inspecting the app from
|
|
/// outside, through the Dart VM service (`ext.bikecontrol.*`). Not registered
|
|
/// in release or profile builds.
|
|
///
|
|
/// - `ext.bikecontrol.state`: the selected trainer app, connected OBC apps and
|
|
/// every bridged trainer's gear, ratio, mode and gear settings.
|
|
/// - `ext.bikecontrol.press?action=<InGameAction name>`: one press, the way a
|
|
/// controller's goes (e.g. `shiftUp`, `shiftDown`, `frontShift`).
|
|
/// - `ext.bikecontrol.set?key=followAppGears|frontShiftEnabled&value=true|false`: changes the
|
|
/// active shifting config of the first connected bridged trainer.
|
|
/// - `ext.bikecontrol.pedalling?on=true|false`: the simulated rider.
|
|
void registerDebugHooks() {
|
|
if (!kDebugMode) return;
|
|
_register('state', (_) async => _state());
|
|
_register('press', (params) async {
|
|
final name = params['action'];
|
|
final action = InGameAction.values.where((a) => a.name == name).firstOrNull;
|
|
if (action == null) throw ArgumentError('unknown action $name');
|
|
final result = await core.actionHandler.performInGameAction(action, source: 'debugHook');
|
|
return {'result': result.runtimeType.toString(), 'message': result.message, ..._state()};
|
|
});
|
|
_register('set', (params) async {
|
|
final proxy = _bridged().firstOrNull;
|
|
if (proxy == null) throw StateError('no connected bridged trainer');
|
|
final value = params['value'] == 'true';
|
|
switch (params['key']) {
|
|
case 'followAppGears':
|
|
await updateActiveShiftingConfig(proxy, (c) => c.copyWith(followAppGears: value));
|
|
case 'frontShiftEnabled':
|
|
await updateActiveShiftingConfig(proxy, (c) => c.copyWith(frontShiftEnabled: value));
|
|
proxy.fitnessBike?.setFrontShiftEnabled(value);
|
|
default:
|
|
throw ArgumentError('unknown key ${params['key']}');
|
|
}
|
|
return _state();
|
|
});
|
|
_register('pedalling', (params) async {
|
|
final sim = core.pedallingSimulation;
|
|
params['on'] == 'true' ? sim.start() : sim.stop();
|
|
return {'pedalling': sim.isRunning.value};
|
|
});
|
|
}
|
|
|
|
Iterable<ProxyDevice> _bridged() => core.connection.proxyDevices.where((p) => p.isConnected);
|
|
|
|
Map<String, Object?> _state() => {
|
|
'trainerApp': core.settings.getTrainerApp()?.name,
|
|
'obcApps': [
|
|
for (final sink in [core.obpMdnsEmulator, core.obpBluetoothEmulator])
|
|
if ((sink as GearStateSink).connectedApp.value case final app?) app.appId,
|
|
],
|
|
'pedalling': core.pedallingSimulation.isRunning.value,
|
|
'trainers': [
|
|
for (final proxy in core.connection.proxyDevices)
|
|
{
|
|
'name': proxy.trainerKey,
|
|
'connected': proxy.isConnected,
|
|
'followsAppGears': proxy.followsAppGears,
|
|
'reportsGearToApp': proxy.reportsGearToApp,
|
|
if (proxy.fitnessBike case final def?) ...{
|
|
'mode': def.trainerMode.value.name,
|
|
'gear': def.currentGear.value,
|
|
'maxGear': def.maxGear,
|
|
'ratio': def.gearRatio.value,
|
|
'frontShift': def.frontShiftEnabled,
|
|
'frontRing': def.frontRing.value.name,
|
|
'appSendsNoWind': def.appSendsNoWind.value,
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
void _register(String name, Future<Map<String, Object?>> Function(Map<String, String> params) handler) {
|
|
developer.registerExtension('ext.bikecontrol.$name', (method, params) async {
|
|
try {
|
|
return developer.ServiceExtensionResponse.result(jsonEncode(await handler(params)));
|
|
} catch (e) {
|
|
return developer.ServiceExtensionResponse.error(developer.ServiceExtensionResponse.extensionError, '$e');
|
|
}
|
|
});
|
|
}
|