|
|
|
@ -1,5 +1,6 @@ |
|
|
|
import 'dart:io'; |
|
|
|
import 'dart:async'; |
|
|
|
import 'dart:convert'; |
|
|
|
|
|
|
|
import 'package:app_settings/app_settings.dart'; |
|
|
|
import '/data/models/appconfig_model.dart'; |
|
|
|
@ -1194,15 +1195,12 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 检测是否为移动精灵(YIDONG)设备,并写入 GetStorage |
|
|
|
/// 检测是否为移动精灵设备,并写入 GetStorage |
|
|
|
/// 判定依据:服务端下发的 DBProduct.channel == 'mobile' |
|
|
|
/// 平台认证参数从 DBProduct.channelextra (JSON) 中解析: |
|
|
|
/// { "platform_id": "...", "platform_key": "...", "tui": "..." } |
|
|
|
void _detectAndSaveYidongDevice() { |
|
|
|
final yidongPid = AppConfig.env('YIDONG_PID') ?? ''; |
|
|
|
if (yidongPid.isEmpty) { |
|
|
|
Logger.i(_tag, '[YIDONG] 未配置 YIDONG_PID,跳过检测'); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 优先使用本次扫描解析的 pid,否则从已绑定设备列表中查找 |
|
|
|
// 1) 获取当前设备 pid(BLE 扫描 > 已绑定设备) |
|
|
|
int? pid = _verifiedDevicePid; |
|
|
|
String pidSource = 'BLE扫描'; |
|
|
|
if (pid == null) { |
|
|
|
@ -1229,42 +1227,96 @@ class PairingController extends GetxController with WidgetsBindingObserver { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
// 将 int pid 转为十六进制字符串(不补零,大写)与 YIDONG_PID 比对 |
|
|
|
final pidHex = pid.toRadixString(16).toUpperCase(); |
|
|
|
final isYidong = pidHex == yidongPid.toUpperCase(); |
|
|
|
// 2) 从服务端下发的产品列表中查找对应产品 |
|
|
|
DBProduct? product; |
|
|
|
try { |
|
|
|
product = AppConfig.config.products.firstWhere((p) => p.id == pid); |
|
|
|
} catch (_) { |
|
|
|
product = null; |
|
|
|
} |
|
|
|
if (product == null) { |
|
|
|
Logger.i(_tag, '[YIDONG] 未在 AppConfig.products 中找到 pid=$pid 对应产品,清理旧凭证'); |
|
|
|
_clearYidongStorage(); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
final channel = (product.channel ?? '').trim().toLowerCase(); |
|
|
|
final isYidong = channel == 'mobile'; |
|
|
|
Logger.i(_tag, |
|
|
|
'[YIDONG] 设备检测: pid=$pid channel=${product.channel} source=$pidSource matched=$isYidong'); |
|
|
|
|
|
|
|
if (!isYidong) { |
|
|
|
_clearYidongStorage(); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
Logger.i(_tag, '[YIDONG] 设备检测: pidHex=$pidHex yidongPid=${yidongPid.toUpperCase()} source=$pidSource matched=$isYidong'); |
|
|
|
// 3) 解析 channelextra,取 platform_id / platform_key / tui |
|
|
|
String platformId = ''; |
|
|
|
String platformKey = ''; |
|
|
|
String tui = ''; |
|
|
|
final extra = product.channelextra; |
|
|
|
if (extra != null && extra.isNotEmpty) { |
|
|
|
try { |
|
|
|
final dynamic decoded = jsonDecode(extra); |
|
|
|
if (decoded is Map) { |
|
|
|
platformId = (decoded['platform_id'] ?? '').toString(); |
|
|
|
platformKey = (decoded['platform_key'] ?? '').toString(); |
|
|
|
tui = (decoded['tui'] ?? '').toString(); |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
Logger.e(_tag, '[YIDONG] 解析 channelextra 失败: $e raw=$extra'); |
|
|
|
} |
|
|
|
} |
|
|
|
if (platformId.isEmpty || platformKey.isEmpty) { |
|
|
|
Logger.w(_tag, |
|
|
|
'[YIDONG] channelextra 缺失 platform_id/platform_key,无法配置 STS,清理旧凭证'); |
|
|
|
_clearYidongStorage(); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (isYidong) { |
|
|
|
// 4) 写入存储:设备标识 + STS 平台认证参数 |
|
|
|
_storage.write('device_agent_type', 'yidong'); |
|
|
|
// deviceCode 来源: |
|
|
|
// 新设备 → BLE 扫描从广播包提取 |
|
|
|
// 已绑定设备 → authbindingDevice 从 existingDevice.license 赋值 |
|
|
|
_storage.write('sts_product_id', platformId); |
|
|
|
_storage.write('sts_product_key', platformKey); |
|
|
|
if (tui.isNotEmpty) { |
|
|
|
_storage.write('sts_tui', tui); |
|
|
|
} else { |
|
|
|
_storage.remove('sts_tui'); |
|
|
|
} |
|
|
|
Logger.i(_tag, |
|
|
|
'[YIDONG] 识别成功,已写入 sts_product_id=$platformId sts_product_key=*** tui=$tui'); |
|
|
|
|
|
|
|
// deviceCode 来源:新设备=BLE扫描提取;已绑定设备=existingDevice.license |
|
|
|
if (deviceCode.value.isNotEmpty) { |
|
|
|
final prevCode = _storage.read<String>('device_sts_code') ?? ''; |
|
|
|
// SN 变了说明换了设备,清除旧凭证,强制重新向移动平台认证 |
|
|
|
if (prevCode != deviceCode.value) { |
|
|
|
_storage.remove('sts_device_id'); |
|
|
|
_storage.remove('sts_device_secret'); |
|
|
|
Logger.i(_tag, '[YIDONG] SN 变更($prevCode → ${deviceCode.value}),已清除旧凭证,强制重新认证'); |
|
|
|
Logger.i(_tag, |
|
|
|
'[YIDONG] SN 变更($prevCode → ${deviceCode.value}),已清除旧凭证,强制重新认证'); |
|
|
|
} |
|
|
|
_storage.write('device_sts_code', deviceCode.value); |
|
|
|
Logger.i(_tag, '[YIDONG] 识别成功,写入 device_sts_code=${deviceCode.value}'); |
|
|
|
Logger.i(_tag, '[YIDONG] 写入 device_sts_code=${deviceCode.value}'); |
|
|
|
} else { |
|
|
|
// 既无扫描结果也无 license,清除所有凭证,认证失败会提示用户 |
|
|
|
_storage.remove('device_sts_code'); |
|
|
|
_storage.remove('sts_device_id'); |
|
|
|
_storage.remove('sts_device_secret'); |
|
|
|
Logger.i(_tag, '[YIDONG] deviceCode 为空,已清除所有凭证'); |
|
|
|
Logger.i(_tag, '[YIDONG] deviceCode 为空,已清除设备凭证'); |
|
|
|
} |
|
|
|
} else { |
|
|
|
} |
|
|
|
|
|
|
|
/// 清理移动精灵相关本地存储 |
|
|
|
void _clearYidongStorage() { |
|
|
|
_storage.remove('device_agent_type'); |
|
|
|
_storage.remove('device_sts_code'); |
|
|
|
_storage.remove('sts_device_id'); |
|
|
|
_storage.remove('sts_device_secret'); |
|
|
|
_storage.remove('sts_product_id'); |
|
|
|
_storage.remove('sts_product_key'); |
|
|
|
_storage.remove('sts_tui'); |
|
|
|
Logger.i(_tag, '[YIDONG] 非移动精灵设备,已清除相关存储'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// 导航到主页 |
|
|
|
void _navigateToHome() { |
|
|
|
|