feat: 配对信息二维码与主控端导入
- 被控端新增 zxing 二维码,展示 lancast://pair 链接(含设备 id、指纹、TLS 指纹位) - 配对 URI 增加 id/name/tls 字段,端口与指纹仍由手机端生成 - 主控端设备列表新增「导入配对信息」,粘贴链接即可添加设备并写入 pin - 导入设备落盘 ttstd_paired_devices.json,与发现结果按 id/ip+端口去重
This commit is contained in:
1 parent
1548139051
commit
9a82766fa2
8 files changed
+338
-6
No files matched your search
@@ -51,6 +51,8 @@ class DeviceInfo {
|
||||
final String model;
|
||||
final String resolution;
|
||||
final String? signalingUrl;
|
||||
/// 被控端 TLS 自签证书的 SHA-256(为空表示对端不支持 TLS)。
|
||||
final String tlsFingerprint;
|
||||
|
||||
const DeviceInfo({
|
||||
required this.id,
|
||||
@@ -63,6 +65,7 @@ class DeviceInfo {
|
||||
this.model = '',
|
||||
this.resolution = '1920x1080',
|
||||
this.signalingUrl,
|
||||
this.tlsFingerprint = '',
|
||||
});
|
||||
|
||||
factory DeviceInfo.fromJson(Map<String, dynamic> json) {
|
||||
@@ -79,6 +82,7 @@ class DeviceInfo {
|
||||
model: (json['model'] ?? '').toString(),
|
||||
resolution: (json['res'] ?? '1920x1080').toString(),
|
||||
signalingUrl: json['sig']?.toString(),
|
||||
tlsFingerprint: (json['tls'] ?? json['tlsFp'] ?? '').toString(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -93,6 +97,7 @@ class DeviceInfo {
|
||||
'model': model,
|
||||
'res': resolution,
|
||||
if (signalingUrl != null) 'sig': signalingUrl,
|
||||
if (tlsFingerprint.isNotEmpty) 'tls': tlsFingerprint,
|
||||
};
|
||||
|
||||
bool supports(String capability) => capabilities.contains(capability);
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'models.dart';
|
||||
|
||||
/// `lancast://pair?ip=&port=&fp=&id=&name=&tls=` 配对信息。
|
||||
///
|
||||
/// 被控端把这条 URI 渲染成二维码展示;主控端扫码(或直接粘贴文本)即可导入设备,
|
||||
/// 省掉手输 IP 和核对指纹。`fp` 是应用签名证书指纹,`tls` 是 TLS 自签证书指纹(可选)。
|
||||
class PairingUri {
|
||||
const PairingUri({
|
||||
required this.ip,
|
||||
required this.port,
|
||||
this.fingerprint = '',
|
||||
this.deviceId = '',
|
||||
this.name = '',
|
||||
this.tlsFingerprint = '',
|
||||
});
|
||||
|
||||
final String ip;
|
||||
final int port;
|
||||
final String fingerprint;
|
||||
final String deviceId;
|
||||
final String name;
|
||||
final String tlsFingerprint;
|
||||
|
||||
static PairingUri? parse(String text) {
|
||||
final trimmed = text.trim();
|
||||
if (trimmed.isEmpty) return null;
|
||||
final uri = Uri.tryParse(trimmed);
|
||||
if (uri == null) return null;
|
||||
if (uri.scheme.toLowerCase() != 'lancast') return null;
|
||||
if (uri.host.toLowerCase() != 'pair') return null;
|
||||
final ip = (uri.queryParameters['ip'] ?? '').trim();
|
||||
final port = int.tryParse(uri.queryParameters['port'] ?? '');
|
||||
if (ip.isEmpty || port == null || port <= 0 || port > 65535) return null;
|
||||
return PairingUri(
|
||||
ip: ip,
|
||||
port: port,
|
||||
fingerprint: (uri.queryParameters['fp'] ?? '').trim(),
|
||||
deviceId: (uri.queryParameters['id'] ?? '').trim(),
|
||||
name: (uri.queryParameters['name'] ?? '').trim(),
|
||||
tlsFingerprint: (uri.queryParameters['tls'] ?? '').trim(),
|
||||
);
|
||||
}
|
||||
|
||||
DeviceInfo toDevice() {
|
||||
final resolvedName = name.isNotEmpty ? name : ip;
|
||||
return DeviceInfo(
|
||||
id: deviceId.isNotEmpty ? deviceId : '$ip:$port',
|
||||
name: resolvedName,
|
||||
ip: ip,
|
||||
mediaPort: 41000,
|
||||
controlPort: port,
|
||||
fingerprint: fingerprint,
|
||||
tlsFingerprint: tlsFingerprint,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 已导入的配对设备,落盘 `ttstd_paired_devices.json`。
|
||||
///
|
||||
/// 与发现结果按「设备 id,缺失时 ip+控制端口」去重;发现结果优先(信息更新),
|
||||
/// 但导入记录能保证设备不在线时也留一个可连接的条目。
|
||||
class PairedDeviceStore {
|
||||
PairedDeviceStore({this.fileName = 'ttstd_paired_devices.json'});
|
||||
|
||||
final String fileName;
|
||||
final List<DeviceInfo> _devices = <DeviceInfo>[];
|
||||
File? _file;
|
||||
|
||||
bool get isReady => _file != null;
|
||||
|
||||
String get path => _file?.path ?? '';
|
||||
|
||||
List<DeviceInfo> get devices => List<DeviceInfo>.unmodifiable(_devices);
|
||||
|
||||
void load() {
|
||||
for (final directory in _directories()) {
|
||||
try {
|
||||
final candidate = File('${directory.path}${Platform.pathSeparator}$fileName');
|
||||
if (!candidate.existsSync()) {
|
||||
candidate.writeAsStringSync('[]', flush: true);
|
||||
}
|
||||
final decoded = jsonDecode(candidate.readAsStringSync());
|
||||
if (decoded is List) {
|
||||
_devices
|
||||
..clear()
|
||||
..addAll(
|
||||
decoded.whereType<Map<dynamic, dynamic>>().map(
|
||||
(item) => DeviceInfo.fromJson(Map<String, dynamic>.from(item)),
|
||||
),
|
||||
);
|
||||
}
|
||||
_file = candidate;
|
||||
return;
|
||||
} on Object {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 保存一台设备;同 id(或同 ip:端口)的旧记录会被覆盖。
|
||||
void save(DeviceInfo device) {
|
||||
_devices.removeWhere((item) => sameDevice(item, device));
|
||||
_devices.add(device);
|
||||
_flush();
|
||||
}
|
||||
|
||||
void remove(DeviceInfo device) {
|
||||
final before = _devices.length;
|
||||
_devices.removeWhere((item) => sameDevice(item, device));
|
||||
if (_devices.length != before) _flush();
|
||||
}
|
||||
|
||||
static bool sameDevice(DeviceInfo left, DeviceInfo right) {
|
||||
final leftId = left.id.trim();
|
||||
final rightId = right.id.trim();
|
||||
if (leftId.isNotEmpty && rightId.isNotEmpty) return leftId == rightId;
|
||||
return left.ip == right.ip && left.controlPort == right.controlPort;
|
||||
}
|
||||
|
||||
void _flush() {
|
||||
final file = _file;
|
||||
if (file == null) return;
|
||||
try {
|
||||
file.writeAsStringSync(
|
||||
jsonEncode(_devices.map((device) => device.toJson()).toList()),
|
||||
flush: true,
|
||||
);
|
||||
} on Object {
|
||||
// 落盘失败不影响本次运行。
|
||||
}
|
||||
}
|
||||
|
||||
static List<Directory> _directories() => <Directory>[
|
||||
File(Platform.resolvedExecutable).parent,
|
||||
Directory.current,
|
||||
Directory.systemTemp,
|
||||
];
|
||||
}
|
||||
@@ -9,6 +9,7 @@ class DeviceList extends StatelessWidget {
|
||||
required this.selectedId,
|
||||
required this.onSelected,
|
||||
required this.onRefresh,
|
||||
this.onPair,
|
||||
this.refreshing = false,
|
||||
this.shrinkWrap = false,
|
||||
});
|
||||
@@ -17,6 +18,9 @@ class DeviceList extends StatelessWidget {
|
||||
final String? selectedId;
|
||||
final ValueChanged<DeviceInfo> onSelected;
|
||||
final VoidCallback onRefresh;
|
||||
|
||||
/// 导入配对信息(扫码/粘贴 `lancast://pair?...`)。
|
||||
final VoidCallback? onPair;
|
||||
final bool refreshing;
|
||||
final bool shrinkWrap;
|
||||
|
||||
@@ -35,6 +39,12 @@ class DeviceList extends StatelessWidget {
|
||||
Text('可连接设备', style: theme.textTheme.titleMedium),
|
||||
const Spacer(),
|
||||
Text('${devices.length} 台', style: theme.textTheme.bodySmall),
|
||||
if (onPair != null)
|
||||
IconButton(
|
||||
tooltip: '导入配对信息',
|
||||
onPressed: onPair,
|
||||
icon: const Icon(Icons.qr_code_scanner),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: '重新发现',
|
||||
onPressed: onRefresh,
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'core/diagnostic_log.dart';
|
||||
import 'core/control_file_client.dart';
|
||||
import 'core/native_core.dart';
|
||||
import 'core/models.dart';
|
||||
import 'core/pairing.dart';
|
||||
import 'core/pin_store.dart';
|
||||
import 'core/watch_client.dart';
|
||||
import 'features/control/control_toolbar.dart';
|
||||
@@ -50,6 +51,7 @@ class _HomePageState extends State<HomePage> {
|
||||
late final SenderBridge _bridge;
|
||||
final DiagnosticLog _diagnosticLog = DiagnosticLog();
|
||||
final PinStore _pins = PinStore();
|
||||
final PairedDeviceStore _paired = PairedDeviceStore();
|
||||
Timer? _demoStatusTimer;
|
||||
Timer? _diagnosticTimer;
|
||||
Timer? _connectWatchdog;
|
||||
@@ -111,6 +113,11 @@ class _HomePageState extends State<HomePage> {
|
||||
_diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}');
|
||||
_pins.load();
|
||||
_diagnosticLog.write('pinned devices file: ${_pins.path}');
|
||||
_paired.load();
|
||||
_diagnosticLog.write('paired devices file: ${_paired.path}');
|
||||
if (_paired.devices.isNotEmpty) {
|
||||
_devices = _mergePaired(const <DeviceInfo>[]);
|
||||
}
|
||||
_bridge.watchPhase().listen((phase) {
|
||||
_diagnosticLog.write('phase event -> ${phase.wireName} (${phase.label})');
|
||||
if (mounted) setState(() => _phase = phase);
|
||||
@@ -669,22 +676,105 @@ class _HomePageState extends State<HomePage> {
|
||||
return;
|
||||
}
|
||||
setState(() => _refreshing = true);
|
||||
final devices = await _bridge.discover(signalingUrl: _effectiveSignalingUrl);
|
||||
final discovered = await _bridge.discover(signalingUrl: _effectiveSignalingUrl);
|
||||
if (!mounted) return;
|
||||
final devices = _mergePaired(discovered);
|
||||
setState(() {
|
||||
_devices = devices;
|
||||
if (_selected == null || !devices.any((device) => device.id == _selected!.id)) {
|
||||
if (_selected == null ||
|
||||
!devices.any((device) => PairedDeviceStore.sameDevice(device, _selected!))) {
|
||||
_selected = devices.isEmpty ? null : devices.first;
|
||||
}
|
||||
_refreshing = false;
|
||||
});
|
||||
_diagnosticLog.write('discover result: ${devices.length} devices');
|
||||
_diagnosticLog.write(
|
||||
'discover result: ${discovered.length} found, ${devices.length} shown '
|
||||
'(${_paired.devices.length} paired)',
|
||||
);
|
||||
if (_refreshQueued) {
|
||||
_refreshQueued = false;
|
||||
unawaited(_refreshDevices());
|
||||
}
|
||||
}
|
||||
|
||||
/// 发现结果与已导入的配对设备合并:发现结果优先,导入记录补位。
|
||||
List<DeviceInfo> _mergePaired(List<DeviceInfo> discovered) {
|
||||
final merged = <DeviceInfo>[...discovered];
|
||||
for (final paired in _paired.devices) {
|
||||
final index = merged.indexWhere((device) => PairedDeviceStore.sameDevice(device, paired));
|
||||
if (index < 0) merged.add(paired);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
/// 导入 `lancast://pair?...`(扫码内容或手工粘贴)。
|
||||
Future<void> _showPairingDialog() async {
|
||||
final controller = TextEditingController();
|
||||
final text = await showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('导入配对信息'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(
|
||||
'用手机扫描被控端界面上的二维码,把得到的链接粘贴到这里;'
|
||||
'也可以让被控端把链接文本发给你。',
|
||||
style: TextStyle(fontSize: 13),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
minLines: 1,
|
||||
maxLines: 3,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'lancast://pair?...',
|
||||
hintText: 'lancast://pair?ip=192.168.1.10&port=41001&fp=...',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('取消'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(context).pop(controller.text),
|
||||
child: const Text('导入'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
controller.dispose();
|
||||
if (text == null || text.trim().isEmpty) return;
|
||||
final pairing = PairingUri.parse(text);
|
||||
if (pairing == null) {
|
||||
_snack('配对信息无法识别,请确认粘贴的是 lancast://pair?... 链接');
|
||||
return;
|
||||
}
|
||||
final device = pairing.toDevice();
|
||||
// 配对链接本身就是带外渠道,导入即可信:直接写 pin,后续连接走一致性校验。
|
||||
if (device.fingerprint.isNotEmpty) {
|
||||
_pins.pin(_pinKeyOf(device), device.fingerprint);
|
||||
}
|
||||
_paired.save(device);
|
||||
_diagnosticLog.write(
|
||||
'paired device imported: ${device.id} ${device.ip}:${device.controlPort} '
|
||||
'tls=${device.tlsFingerprint.isEmpty ? '-' : device.tlsFingerprint}',
|
||||
);
|
||||
setState(() {
|
||||
_devices = _mergePaired(_devices);
|
||||
_selected = device;
|
||||
});
|
||||
_snack('已导入 ${device.name}');
|
||||
}
|
||||
|
||||
static String _pinKeyOf(DeviceInfo device) =>
|
||||
device.id.trim().isNotEmpty ? device.id.trim() : '${device.ip}:${device.controlPort}';
|
||||
|
||||
Future<void> _connect() async {
|
||||
final device = _selected;
|
||||
if (device == null) return;
|
||||
@@ -942,6 +1032,7 @@ class _HomePageState extends State<HomePage> {
|
||||
selectedId: _selected?.id,
|
||||
onSelected: (device) => setState(() => _selected = device),
|
||||
onRefresh: _refreshDevices,
|
||||
onPair: _showPairingDialog,
|
||||
refreshing: _refreshing,
|
||||
),
|
||||
),
|
||||
@@ -965,6 +1056,7 @@ class _HomePageState extends State<HomePage> {
|
||||
selectedId: _selected?.id,
|
||||
onSelected: (device) => setState(() => _selected = device),
|
||||
onRefresh: _refreshDevices,
|
||||
onPair: _showPairingDialog,
|
||||
refreshing: _refreshing,
|
||||
),
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user