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
@@ -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