connection settings cleanup and mock test added. removed bluetooth implementation

This commit is contained in:
Constantin Leue
2026-08-05 20:01:25 +02:00
parent 8e4df2d0d3
commit f65dceb338
15 changed files with 113 additions and 895 deletions
+15 -13
View File
@@ -233,19 +233,21 @@ class _FlyScreenState extends ConsumerState<FlyScreen> {
);
// Drohnen-Status fuer die Drohnen-Pille (Doku: "im fly modus zeigt die
// drone pille den zustand der drohne") - null solange keine Telemetrie
// vorliegt, dann zeigt BottomStatsBar weiterhin den Drohnennamen-Chip.
final droneStatus = telemetry != null
? computeDroneStatus(
armed: telemetry.armed,
hasFix: telemetry.hasFix,
numSat: telemetry.numSat,
hdop: telemetry.hdop,
linkQuality: telemetry.linkQuality,
snrDb: telemetry.snrDb,
navMode: telemetry.navMode,
)
: null;
// drone pille den zustand der drohne") - das Aussehen des Fly-Modus darf
// nicht am Vorhandensein eines Telemetrie-Frames haengen (sonst faellt
// BottomStatsBar auf den Drohnennamen-Chip zurueck, den Plan-Modus-Look).
// Ohne Frame (Verbindungsaufbau laeuft noch) liefern die Platzhalterwerte
// ueber computeDroneStatus zuverlaessig idle/rot/rot - eine ehrliche
// "keine Daten"-Anzeige statt eines unterschiedlichen Widget-Typs.
final droneStatus = computeDroneStatus(
armed: telemetry?.armed ?? false,
hasFix: telemetry?.hasFix ?? false,
numSat: telemetry?.numSat ?? 0,
hdop: telemetry?.hdop ?? 99.0,
linkQuality: telemetry?.linkQuality ?? 0,
snrDb: telemetry?.snrDb ?? -99,
navMode: telemetry?.navMode ?? 0,
);
return Stack(
children: [
+24 -236
View File
@@ -1,13 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../transport/bluetooth/bluetooth_device_info.dart';
import '../../../transport/bluetooth/bluetooth_system_settings.dart';
import '../../../transport/connection_type.dart';
import '../../../transport/flight_controller_link.dart';
import '../../../transport/link_transport.dart';
import '../../dmc_colors.dart';
import '../../providers/bluetooth_connection_provider.dart';
import '../../providers/connection_settings_provider.dart';
import '../../providers/database_provider.dart';
import '../../providers/telemetry_provider.dart';
@@ -56,8 +53,8 @@ class _ConnectionSection extends ConsumerWidget {
children: [
// Fuer WLAN rendert _WifiPanel das Typ-Dropdown selbst als Teil
// seiner kompakten ersten Zeile (zusammen mit Connect/Disconnect
// und den Status-Pillen) - fuer die anderen Verbindungsarten
// bleibt die bisherige, einfachere Kopfzeile bestehen.
// und den Status-Pillen) - fuer Mock bleibt die bisherige,
// einfachere Kopfzeile bestehen.
if (selectedType != ConnectionType.wifi) ...[
Text('Connection', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 12),
@@ -79,11 +76,7 @@ class _ConnectionSection extends ConsumerWidget {
],
switch (selectedType) {
ConnectionType.wifi => const _WifiPanel(),
ConnectionType.bluetooth => const _BluetoothPanel(),
ConnectionType.cellular5g => const _NotAvailablePanel(
label: '5G',
),
ConnectionType.usb => const _NotAvailablePanel(label: 'USB'),
ConnectionType.mock => const _MockPanel(),
},
],
),
@@ -98,21 +91,31 @@ class _ConnectionSection extends ConsumerWidget {
/// gleich lauten.
const _connectionTypeItems = [
DropdownMenuItem(value: ConnectionType.wifi, child: Text('WiFi mLRS UDP')),
DropdownMenuItem(value: ConnectionType.cellular5g, child: Text('5G (soon)')),
DropdownMenuItem(value: ConnectionType.bluetooth, child: Text('Bluetooth')),
DropdownMenuItem(value: ConnectionType.usb, child: Text('USB (soon)')),
DropdownMenuItem(value: ConnectionType.mock, child: Text('Mock (Test)')),
];
class _NotAvailablePanel extends StatelessWidget {
const _NotAvailablePanel({required this.label});
final String label;
/// Zeigt die simulierte Telemetrie von [MockFlightControllerLink] (Doku:
/// "Mock-Daten fuer die Live-Details-Ansicht nutzbar machen") - der Mock
/// verbindet sich selbst ohne "Connect"-Knopf (siehe telemetryProvider), es
/// gibt hier also nichts zu bedienen, nur zur Kontrolle zu beobachten.
class _MockPanel extends ConsumerWidget {
const _MockPanel();
@override
Widget build(BuildContext context) {
return Text(
'$label support is not available yet.',
style: const TextStyle(fontSize: 12, color: Colors.grey),
Widget build(BuildContext context, WidgetRef ref) {
final telemetry = ref.watch(telemetryProvider).value;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Simulated telemetry for UI testing without hardware.',
style: TextStyle(fontSize: 12, color: Colors.grey),
),
const SizedBox(height: 12),
Text('Telemetry', style: Theme.of(context).textTheme.titleSmall),
const SizedBox(height: 4),
_TelemetryFields(telemetry: telemetry),
],
);
}
}
@@ -352,161 +355,6 @@ class _WifiErrorHint extends StatelessWidget {
}
}
class _BluetoothPanel extends ConsumerWidget {
const _BluetoothPanel();
@override
Widget build(BuildContext context, WidgetRef ref) {
final linkState = ref.watch(linkStateProvider).value;
final errorReason = ref.watch(linkErrorReasonProvider);
final bondedDevices = ref.watch(bondedDevicesProvider);
final connectionSettings = ref.watch(connectionSettingsProvider).value;
final log = ref.watch(connectionLogProvider);
final packetCount = ref.watch(connectionLogProvider.notifier).packetCount;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text('Bluetooth', style: Theme.of(context).textTheme.titleSmall),
const Spacer(),
_StatePill(state: linkState),
],
),
if (errorReason != null) ...[
const SizedBox(height: 12),
_ErrorHint(reason: errorReason),
],
const SizedBox(height: 16),
Row(
children: [
ElevatedButton(
onPressed: () => _connect(ref),
child: const Text('Connect'),
),
const SizedBox(width: 8),
OutlinedButton(
onPressed: () => ref.read(bluetoothTransportProvider).disconnect(),
child: const Text('Disconnect'),
),
const SizedBox(width: 8),
TextButton(
onPressed: BluetoothSystemSettings.openBluetoothSettings,
child: const Text('Pair device'),
),
],
),
const SizedBox(height: 16),
SwitchListTile(
contentPadding: EdgeInsets.zero,
title: const Text('Auto-connect when entering Fly mode'),
value: connectionSettings?.connectionAutoConnect ?? true,
onChanged: (value) async {
await ref
.read(appSettingsRepositoryProvider)
.setConnectionAutoConnect(value);
ref.invalidate(connectionSettingsProvider);
},
),
const SizedBox(height: 8),
Text('Paired devices', style: Theme.of(context).textTheme.titleSmall),
const SizedBox(height: 4),
bondedDevices.when(
data: (devices) => devices.isEmpty
? const Text(
'No paired devices yet. Pair one once via the system '
'dialog using "Pair device".',
style: TextStyle(fontSize: 12, color: Colors.grey),
)
: RadioGroup<String>(
groupValue: connectionSettings?.connectionDeviceAddress,
onChanged: (address) => _selectDevice(ref, address, devices),
child: Column(
children: [
for (final device in devices)
RadioListTile<String>(
contentPadding: EdgeInsets.zero,
dense: true,
value: device.address,
title: Text(device.displayName),
subtitle: Text(
device.address,
style: const TextStyle(fontSize: 11),
),
),
],
),
),
loading: () => const Padding(
padding: EdgeInsets.symmetric(vertical: 8),
child: LinearProgressIndicator(),
),
error: (e, _) => Text(
'Could not read paired devices: $e',
style: const TextStyle(fontSize: 12, color: Colors.grey),
),
),
const SizedBox(height: 16),
Text(
'Connection log ($packetCount packets received)',
style: Theme.of(context).textTheme.titleSmall,
),
const SizedBox(height: 4),
SizedBox(
height: 120,
child: log.isEmpty
? const Text(
'No events yet.',
style: TextStyle(fontSize: 12, color: Colors.grey),
)
: ListView.builder(
reverse: true,
itemCount: log.length,
itemBuilder: (context, index) {
final entry = log[log.length - 1 - index];
return Text(
'${_formatTime(entry.timestamp)} ${entry.message}',
style: const TextStyle(fontSize: 12, fontFamily: 'monospace'),
);
},
),
),
],
);
}
Future<void> _connect(WidgetRef ref) async {
try {
await ref.read(bluetoothTransportProvider).connect();
} on LinkConnectException {
// Grund/Nachricht sind bereits ueber lastErrorReason/state gemeldet
// (siehe LinkConnectException-Doku) - die Oberflaeche reagiert darauf
// ueber linkErrorReasonProvider/_ErrorHint, hier ist nichts weiter zu tun.
}
}
String _formatTime(DateTime t) =>
'${t.hour.toString().padLeft(2, '0')}:${t.minute.toString().padLeft(2, '0')}:${t.second.toString().padLeft(2, '0')}';
Future<void> _selectDevice(
WidgetRef ref,
String? address,
List<BluetoothDeviceInfo> devices,
) async {
if (address == null) return;
await ref
.read(appSettingsRepositoryProvider)
.setConnectionDeviceAddress(address);
ref.invalidate(connectionSettingsProvider);
try {
await ref.read(bluetoothTransportProvider).connect();
} on LinkConnectException {
// siehe _connect() oben
}
}
}
/// Zeigt neben [_StatePill], ob gerade UDP-Pakete ankommen ("receiving") -
/// unabhaengig vom Verbindungszustand, der Socket kann offen sein, ohne
/// dass die Gegenstelle sendet (siehe wifiStreamActivityProvider).
@@ -569,63 +417,3 @@ class _StatePill extends StatelessWidget {
}
}
class _ErrorHint extends StatelessWidget {
const _ErrorHint({required this.reason});
final LinkErrorReason reason;
@override
Widget build(BuildContext context) {
final (text, action) = switch (reason) {
LinkErrorReason.bluetoothOff => (
'Bluetooth is off. Please enable it in system settings.',
_HintAction('Settings', BluetoothSystemSettings.openBluetoothSettings),
),
LinkErrorReason.permissionDenied => (
'The "Nearby devices" permission is required. Please tap '
'"Connect" again.',
null,
),
LinkErrorReason.permissionDeniedPermanently => (
'The "Nearby devices" permission was permanently denied. Please '
'grant it manually in the app settings.',
_HintAction('App settings', BluetoothSystemSettings.openAppSettings),
),
LinkErrorReason.deviceSelectionRequired => (
'No paired mLRS device found. Please select a device below.',
null,
),
LinkErrorReason.connectionFailed => (
'Connection to the device failed.',
null,
),
// networkUnavailable/networkLost betreffen nur den WLAN-Transport
// (siehe _WifiErrorHint) - hier nur der Vollstaendigkeit halber, damit
// der gemeinsame LinkErrorReason-Enum exhaustiv bleibt.
LinkErrorReason.networkUnavailable ||
LinkErrorReason.networkLost ||
LinkErrorReason.unknown =>
('Unknown connection error.', null),
};
return Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: DmcColors.warnRed.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
Expanded(child: Text(text, style: const TextStyle(fontSize: 12))),
if (action != null)
TextButton(onPressed: action.onTap, child: Text(action.label)),
],
),
);
}
}
class _HintAction {
const _HintAction(this.label, this.onTap);
final String label;
final Future<void> Function() onTap;
}