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
@@ -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,
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user