You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 lines
13 KiB

import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
// 移除了dart:async导入,因为不再使用Timer
import '../../../data/services/novice_guide_service.dart';
import '../../../routes/app_routes.dart';
import '../../../core/utils/logger.dart';
import 'package:flutter/material.dart';
import '../../../data/services/bluetooth_manager.dart';
import '../../../data/services/ble_manager.dart';
import '../../../data/services/ota_check_service.dart';
import '../../../data/models/appconfig.dart';
import '../../../data/models/appconfig_model.dart';
/// 连接状态枚举
enum ConnectionStatus {
disconnected, // 未连接
connecting, // 连接中
connected, // 已连接
failed // 连接失败
}
/// 主页控制器,管理设备连接状态和信息展示
class HomeController extends GetxController {
static const String _tag = 'HomeCtrl';
static HomeController get to => Get.find();
final GetStorage _storage = GetStorage();
final _isFullyConnected = false.obs;
String? _lastConnectedDeviceName;
// 移除了电池信息定时刷新相关变量
// OTA检查服务
late final OtaCheckService _otaCheckService;
bool get isFullyConnected => _isFullyConnected.value;
RxBool get isFullyConnectedRx => _isFullyConnected;
BluetoothDevice? get currentDevice => _bluetoothManager.currentDevice;
Rx<DeviceInfo?> get deviceInfo => _bleManager.deviceInfo;
static final leftEarBattery = 0.obs;
static final rightEarBattery = 0.obs;
final caseBattery = 0.obs;
final leftEarCharging = false.obs;
final rightEarCharging = false.obs;
final caseCharging = false.obs;
final firmwareVersion = ''.obs;
void updateDeviceInfo(DeviceInfo? newDeviceInfo) {
Logger.d(_tag, 'lxm--updateDeviceInfo被调用');
Logger.d(_tag, 'lxm--接收到的DeviceInfo: $newDeviceInfo');
if (newDeviceInfo != null) {
// Logger.d(_tag, 'lxm--原始数据 - 左耳电量: ${newDeviceInfo.leftEarBattery}');
// Logger.d(_tag, 'lxm--原始数据 - 右耳电量: ${newDeviceInfo.rightEarBattery}');
// Logger.d(_tag, 'lxm--原始数据 - 充电仓电量: ${newDeviceInfo.caseBattery}');
// Logger.d(_tag, 'lxm--原始数据 - 左耳充电: ${newDeviceInfo.leftEarCharging}');
// Logger.d(_tag, 'lxm--原始数据 - 右耳充电: ${newDeviceInfo.rightEarCharging}');
// Logger.d(_tag, 'lxm--原始数据 - 充电仓充电: ${newDeviceInfo.caseCharging}');
// Logger.d(_tag, 'lxm--原始数据 - 固件版本: ${newDeviceInfo.firmwareVersion}');
} else {
Logger.d(_tag, 'lxm--接收到的DeviceInfo为null');
}
// 只有当新数据不为0时才更新电池值,否则保持原值
if ((newDeviceInfo?.leftEarBattery ?? 0) != 0) {
Logger.d(_tag, 'lxm--更新左耳电量: ${newDeviceInfo!.leftEarBattery}');
leftEarBattery.value = newDeviceInfo.leftEarBattery;
}
if ((newDeviceInfo?.rightEarBattery ?? 0) != 0) {
Logger.d(_tag, 'lxm--更新右耳电量: ${newDeviceInfo!.rightEarBattery}');
rightEarBattery.value = newDeviceInfo.rightEarBattery;
}
caseBattery.value = newDeviceInfo?.caseBattery ?? 0;
leftEarCharging.value = newDeviceInfo?.leftEarCharging ?? false;
rightEarCharging.value = newDeviceInfo?.rightEarCharging ?? false;
caseCharging.value = newDeviceInfo?.caseCharging ?? false;
firmwareVersion.value = newDeviceInfo?.firmwareVersion ?? '';
// Logger.d(_tag, 'lxm--更新后的值 - 左耳电量: ${leftEarBattery.value}');
// Logger.d(_tag, 'lxm--更新后的值 - 右耳电量: ${rightEarBattery.value}');
// Logger.d(_tag, 'lxm--更新后的值 - 充电仓电量: ${caseBattery.value}');
// Logger.d(_tag, 'lxm--更新后的值 - 左耳充电: ${leftEarCharging.value}');
// Logger.d(_tag, 'lxm--更新后的值 - 右耳充电: ${rightEarCharging.value}');
// Logger.d(_tag, 'lxm--更新后的值 - 充电仓充电: ${caseCharging.value}');
// Logger.d(_tag, 'lxm--更新后的值 - 固件版本: ${firmwareVersion.value}');
}
Worker? _deviceInfoWorker;
Worker? _classicBluetoothWorker;
Worker? _bleConnectionWorker;
final _bluetoothManager = Get.find<BluetoothManager>();
final _bleManager = Get.find<BleManager>();
final noviceGuideService = Get.find<NoviceGuideService>();
@override
void onInit() {
super.onInit();
_otaCheckService = Get.find<OtaCheckService>();
_initWithCachedInfo();
// 移除了电池信息定时刷新的启动
}
@override
void onClose() {
_deviceInfoWorker?.dispose();
_classicBluetoothWorker?.dispose();
_bleConnectionWorker?.dispose();
// 移除了定时器停止逻辑
super.onClose();
}
void _initWithCachedInfo() {
_setupStateListeners();
_checkConnectionStatus();
}
void _setupStateListeners() {
_deviceInfoWorker = ever(_bleManager.deviceInfo, (DeviceInfo? info) {
Logger.d(_tag, 'lxm--_deviceInfoWorker监听器被触发');
Logger.d(_tag, 'lxm--从BleManager接收到的DeviceInfo: $info');
if (info != null) {
Logger.d(_tag, 'lxm--DeviceInfo不为null,调用updateDeviceInfo');
updateDeviceInfo(info);
} else {
Logger.d(_tag, 'lxm--DeviceInfo为null,跳过更新');
}
update();
});
_classicBluetoothWorker =
ever(_bluetoothManager.currentDeviceRx, (BluetoothDevice? device) {
if (device != null) {
_lastConnectedDeviceName = device.name;
}
_checkConnectionStatus();
});
_bleConnectionWorker = ever(_bleManager.connectionStateRx, (int state) {
_checkConnectionStatus();
});
}
Future<void> refreshDeviceConnection() async {
await _checkConnectionStatus();
// 如果设备已连接,主动刷新设备信息
if (_isFullyConnected.value) {
try {
Logger.d(_tag, '刷新设备信息');
await _bleManager.fetchDeviceInfo();
} catch (e) {
Logger.e(_tag, '刷新设备信息失败: $e');
}
}
}
void rescanDevices() {
Get.toNamed(Routes.pairing);
}
void openTranslation() {
Get.toNamed(Routes.translation);
}
void exploreAIFeatures() {
Get.toNamed(Routes.agent);
}
Future<void> _checkConnectionStatus() async {
try {
final currentBleState = await _bleManager.getConnectionState();
print('fdp--_checkConnectionStatus--currentBleState: $currentBleState');
bool hasClassicBluetooth = currentDevice != null;
bool hasBleConnection = currentBleState == 2;
bool wasConnected = _isFullyConnected.value;
bool isNowConnected = hasClassicBluetooth && hasBleConnection;
bool isGuideCompleted = _storage.read('home_guide') ?? false;
if (isNowConnected && currentDevice == null) {
isNowConnected = false;
}
if (_isFullyConnected.value != isNowConnected) {
_isFullyConnected.value = isNowConnected;
_isFullyConnected.refresh();
update();
if (!wasConnected && isNowConnected) {
// 连接成功后等待1秒再获取设备信息
Future.delayed(const Duration(seconds: 1), () async {
await _bleManager.fetchDeviceInfo();
});
// 移除了连接成功后的电池信息刷新
// 连接成功后检查OTA升级
if (isGuideCompleted) {
Future.delayed(const Duration(seconds: 2), () {
_checkOtaUpgrade();
});
}
}
// 移除了根据连接状态控制定时器的逻辑
}
update();
} catch (e) {
Logger.e(_tag, '${'checkConnectionStatusFailed'.tr}: $e');
if (_isFullyConnected.value) {
_isFullyConnected.value = false;
_isFullyConnected.refresh();
// 移除了发生错误时停止定时器的逻辑
// 注意:不再重置OTA检查状态,因为现在由全局服务管理
update();
}
}
}
Color getConnectionStatusColor() {
return _isFullyConnected.value ? Colors.green : Colors.grey;
}
String getConnectionStatusText() {
return _isFullyConnected.value ? 'connected'.tr : 'unconnected'.tr;
}
bool get shouldShowDeviceInfo => currentDevice != null;
String getDeviceName() {
if (_isFullyConnected.value && currentDevice == null) {
_checkConnectionStatus();
}
final deviceName = currentDevice?.name;
if (deviceName != null && deviceName.isNotEmpty) {
return deviceName;
}
final bleDeviceName = deviceInfo.value?.name;
if (bleDeviceName != null && bleDeviceName.isNotEmpty) {
return bleDeviceName;
}
if (!_isFullyConnected.value &&
_lastConnectedDeviceName != null &&
_lastConnectedDeviceName!.isNotEmpty) {
return _lastConnectedDeviceName!;
}
return 'unknownEquipment'.tr;
}
String getSignalStrength() {
if (!_isFullyConnected.value) {
return 'zeroSignal'.tr;
}
final random = DateTime.now().millisecondsSinceEpoch % 3;
switch (random) {
case 0:
return 'good'.tr;
case 1:
return 'fine'.tr;
default:
return 'excellent'.tr;
}
}
String getDeviceId() {
return deviceInfo.value?.address ?? currentDevice?.address ?? 'unknown'.tr;
}
String getBatteryText() {
return deviceInfo.value?.batteryLevel != null &&
deviceInfo.value!.batteryLevel > 0
? '${deviceInfo.value?.batteryLevel}%'
: 'unknown'.tr;
}
String getEarbudsText() {
if (deviceInfo.value == null ||
(deviceInfo.value!.leftEarBattery <= 0 &&
deviceInfo.value!.rightEarBattery <= 0 ||
deviceInfo.value!.caseBattery > 100)) {
return 'unknown'.tr;
}
String leftStatus = deviceInfo.value!.leftEarBattery > 0
? '${deviceInfo.value!.leftEarBattery}%'
: 'N/A';
String rightStatus = deviceInfo.value!.rightEarBattery > 0
? '${deviceInfo.value!.rightEarBattery}%'
: 'N/A';
leftStatus =
deviceInfo.value!.leftEarCharging ? '$leftStatus ⚡' : leftStatus;
rightStatus =
deviceInfo.value!.rightEarCharging ? '$rightStatus ⚡' : rightStatus;
return '${'leftEar'.tr}: $leftStatus / ${'rightEar'.tr}: $rightStatus'; // 左耳: xxx / 右耳: xxx
}
String getCaseText() {
if (deviceInfo.value == null ||
deviceInfo.value!.caseBattery <= 0 ||
deviceInfo.value!.caseBattery > 100) {
return 'unknown'.tr;
}
String caseStatus = '${deviceInfo.value!.caseBattery}%';
if (deviceInfo.value!.caseCharging) {
caseStatus = '$caseStatus ⚡';
}
return '${'chargingCase'.tr}: $caseStatus'; // 充电仓: xxx
}
String getChargingText() {
if (deviceInfo.value == null) {
return 'unknown'.tr;
}
bool isCharging = deviceInfo.value!.leftEarCharging ||
deviceInfo.value!.rightEarCharging ||
deviceInfo.value!.caseCharging;
return isCharging ? 'charging'.tr : 'notCharging'.tr;
}
/// 获取当前设备类型
/// 返回设备类型,如果无法获取则返回未知类型
DeviceType getCurrentDeviceType() {
try {
final deviceName = getDeviceName();
if (deviceName == 'unknownEquipment'.tr) {
return DeviceType.unknown;
}
// 通过AppConfig获取产品信息
final product = AppConfig.getproduct(deviceName);
if (product != null) {
// 根据产品的devicetype字段返回对应的DeviceType
switch (product.devicetype) {
case 1:
return DeviceType.classicBluetoothHeadset;
case 2:
return DeviceType.bleBluetoothDoubleHeadset;
case 3:
return DeviceType.bleBluetoothSingleHeadset;
case 4:
return DeviceType.smartVoiceRecorder;
case 5:
return DeviceType.smartEye;
default:
return DeviceType.unknown;
}
}
return DeviceType.unknown;
} catch (e) {
Logger.e(_tag, '获取设备类型失败: $e');
return DeviceType.unknown;
}
}
/// 判断当前设备是否为单端耳机
bool isSingleHeadset() {
return getCurrentDeviceType() == DeviceType.bleBluetoothSingleHeadset;
}
String getVersionText() {
return deviceInfo.value?.firmwareVersion.isNotEmpty == true
? deviceInfo.value!.firmwareVersion
: 'unknown'.tr;
}
// 移除了电池信息定时刷新相关方法
/// 检查OTA固件升级
/// 在BLE连接成功后调用,检查是否需要升级
Future<void> _checkOtaUpgrade() async {
Logger.d(_tag, '触发OTA升级检查...');
// 打印当前OTA检查状态(用于调试)
final status = _otaCheckService.getCheckStatus();
Logger.d(_tag, 'OTA检查状态: $status');
// 使用全局OTA检查服务执行检查
final success =
await _otaCheckService.performOtaCheck(_isFullyConnected.value);
if (success) {
Logger.d(_tag, 'OTA检查已成功执行');
} else {
Logger.d(_tag, 'OTA检查被跳过或执行失败');
}
}
/// 调试方法:获取OTA检查状态
/// 可以在开发阶段用于调试
Map<String, dynamic> getOtaCheckStatus() {
return _otaCheckService.getCheckStatus();
}
}