mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-04 20:52:23 +01:00
feat: add mDNS discovery scan for diagnostics (openbikecontrol + wahoo)
This commit is contained in:
116
lib/services/mdns_discovery_scan.dart
Normal file
116
lib/services/mdns_discovery_scan.dart
Normal file
@@ -0,0 +1,116 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:bike_control/bluetooth/wifi_trainer_scanner.dart';
|
||||
import 'package:bike_control/main.dart' show recordError;
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:nsd/nsd.dart' as nsd;
|
||||
import 'package:prop/utils/advertised_service_registry.dart';
|
||||
|
||||
/// One mDNS service seen on the LAN during a diagnostics scan.
|
||||
class DiscoveredMdnsService {
|
||||
final String type;
|
||||
final String name;
|
||||
final String host;
|
||||
final int port;
|
||||
final Map<String, String> txt;
|
||||
|
||||
/// True when this is one of BikeControl's own advertisements echoed back.
|
||||
final bool isSelf;
|
||||
|
||||
const DiscoveredMdnsService({
|
||||
required this.type,
|
||||
required this.name,
|
||||
required this.host,
|
||||
required this.port,
|
||||
required this.txt,
|
||||
required this.isSelf,
|
||||
});
|
||||
|
||||
factory DiscoveredMdnsService.fromService(
|
||||
nsd.Service service, {
|
||||
required String host,
|
||||
required int port,
|
||||
required bool isSelf,
|
||||
}) {
|
||||
final txt = <String, String>{};
|
||||
service.txt?.forEach((k, v) {
|
||||
txt[k] = v == null ? '' : decodeMdnsTxt(v);
|
||||
});
|
||||
return DiscoveredMdnsService(
|
||||
type: service.type ?? '',
|
||||
name: service.name ?? '',
|
||||
host: host,
|
||||
port: port,
|
||||
txt: txt,
|
||||
isSelf: isSelf,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Time-boxed browse of the OpenBikeControl and Wahoo DirCon service types.
|
||||
/// Includes our own advertisement (flagged [DiscoveredMdnsService.isSelf]) so
|
||||
/// the user can confirm the responder is reachable over the wire.
|
||||
class MdnsDiscoveryScan {
|
||||
static const types = ['_openbikecontrol._tcp', '_wahoo-fitness-tnp._tcp'];
|
||||
|
||||
Future<List<DiscoveredMdnsService>> run({
|
||||
Duration timeout = const Duration(seconds: 4),
|
||||
}) async {
|
||||
if (kIsWeb) return [];
|
||||
final localAddresses = await _listLocalAddresses();
|
||||
nsd.disableServiceTypeValidation(true);
|
||||
|
||||
final found = <String, DiscoveredMdnsService>{};
|
||||
void listener(nsd.Service service, nsd.ServiceStatus status) {
|
||||
if (status == nsd.ServiceStatus.lost) return;
|
||||
final name = service.name;
|
||||
final port = service.port;
|
||||
final host = service.addresses
|
||||
?.firstOrNullWhere((a) => a.type == InternetAddressType.IPv4)
|
||||
?.address ??
|
||||
service.host;
|
||||
if (name == null || port == null || host == null) return;
|
||||
final isSelf =
|
||||
WifiTrainerScanner.isSelfAdvertisement(service, localAddresses: localAddresses);
|
||||
found['${service.type}/$name'] = DiscoveredMdnsService.fromService(
|
||||
service,
|
||||
host: host,
|
||||
port: port,
|
||||
isSelf: isSelf,
|
||||
);
|
||||
}
|
||||
|
||||
final discoveries = <nsd.Discovery>[];
|
||||
try {
|
||||
for (final type in types) {
|
||||
final discovery = await nsd.startDiscovery(type, autoResolve: true);
|
||||
discovery.addServiceListener(listener);
|
||||
discoveries.add(discovery);
|
||||
}
|
||||
await Future<void>.delayed(timeout);
|
||||
} finally {
|
||||
for (final discovery in discoveries) {
|
||||
discovery.removeServiceListener(listener);
|
||||
try {
|
||||
await nsd.stopDiscovery(discovery);
|
||||
} catch (e, s) {
|
||||
recordError(e, s, context: 'MdnsDiscoveryScan.stop');
|
||||
}
|
||||
}
|
||||
}
|
||||
return found.values.toList();
|
||||
}
|
||||
|
||||
Future<Set<String>> _listLocalAddresses() async {
|
||||
try {
|
||||
final interfaces =
|
||||
await NetworkInterface.list(includeLinkLocal: true, type: InternetAddressType.any);
|
||||
return interfaces.expand((i) => i.addresses).map((a) => a.address).toSet();
|
||||
} catch (e, s) {
|
||||
recordError(e, s, context: 'MdnsDiscoveryScan.localAddresses');
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
34
test/services/mdns_discovery_scan_test.dart
Normal file
34
test/services/mdns_discovery_scan_test.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:bike_control/services/mdns_discovery_scan.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:nsd/nsd.dart' as nsd;
|
||||
|
||||
void main() {
|
||||
test('fromService decodes txt, carries host/port and self flag', () {
|
||||
final service = nsd.Service(
|
||||
name: 'KICKR ABCD',
|
||||
type: '_wahoo-fitness-tnp._tcp',
|
||||
port: 36866,
|
||||
txt: {
|
||||
'serial-number': Uint8List.fromList('1234'.codeUnits),
|
||||
'version': Uint8List.fromList([0x01]),
|
||||
},
|
||||
);
|
||||
|
||||
final d = DiscoveredMdnsService.fromService(
|
||||
service,
|
||||
host: '192.168.1.50',
|
||||
port: 36866,
|
||||
isSelf: false,
|
||||
);
|
||||
|
||||
expect(d.name, 'KICKR ABCD');
|
||||
expect(d.type, '_wahoo-fitness-tnp._tcp');
|
||||
expect(d.host, '192.168.1.50');
|
||||
expect(d.port, 36866);
|
||||
expect(d.isSelf, isFalse);
|
||||
expect(d.txt['serial-number'], '1234');
|
||||
expect(d.txt['version'], '0x01');
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user