|
|
@ -22,8 +22,12 @@ enum ConnectionStatus { |
|
|
/// 主页控制器,管理设备连接状态和信息展示 |
|
|
/// 主页控制器,管理设备连接状态和信息展示 |
|
|
class HomeController extends GetxController { |
|
|
class HomeController extends GetxController { |
|
|
static const String _tag = 'HomeCtrl'; |
|
|
static const String _tag = 'HomeCtrl'; |
|
|
|
|
|
static const String _lastDeviceNameKey = 'last_connected_device_name'; // 存储键 |
|
|
|
|
|
static const String _lastDeviceImageKey = |
|
|
|
|
|
'last_connected_device_image'; // 存储上一次设备图片的键 |
|
|
static HomeController get to => Get.find(); |
|
|
static HomeController get to => Get.find(); |
|
|
final GetStorage _storage = GetStorage(); |
|
|
final GetStorage _storage = GetStorage(); |
|
|
|
|
|
String? _lastConnectedDeviceImage; // 新增:存储上一次连接设备的图片链接 |
|
|
|
|
|
|
|
|
final _isFullyConnected = false.obs; |
|
|
final _isFullyConnected = false.obs; |
|
|
String? _lastConnectedDeviceName; |
|
|
String? _lastConnectedDeviceName; |
|
|
@ -98,6 +102,7 @@ class HomeController extends GetxController { |
|
|
void onInit() { |
|
|
void onInit() { |
|
|
super.onInit(); |
|
|
super.onInit(); |
|
|
_otaCheckService = Get.find<OtaCheckService>(); |
|
|
_otaCheckService = Get.find<OtaCheckService>(); |
|
|
|
|
|
_loadLastConnectedDeviceName(); // 加载持久化的设备名称 |
|
|
_initWithCachedInfo(); |
|
|
_initWithCachedInfo(); |
|
|
// 移除了电池信息定时刷新的启动 |
|
|
// 移除了电池信息定时刷新的启动 |
|
|
} |
|
|
} |
|
|
@ -132,7 +137,7 @@ class HomeController extends GetxController { |
|
|
_classicBluetoothWorker = |
|
|
_classicBluetoothWorker = |
|
|
ever(_bluetoothManager.currentDeviceRx, (BluetoothDevice? device) { |
|
|
ever(_bluetoothManager.currentDeviceRx, (BluetoothDevice? device) { |
|
|
if (device != null) { |
|
|
if (device != null) { |
|
|
_lastConnectedDeviceName = device.name; |
|
|
_saveLastConnectedDeviceName(device.name); // 使用新的保存方法 |
|
|
} |
|
|
} |
|
|
_checkConnectionStatus(); |
|
|
_checkConnectionStatus(); |
|
|
}); |
|
|
}); |
|
|
@ -142,6 +147,28 @@ class HomeController extends GetxController { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 从本地存储加载最后连接的设备名称 |
|
|
|
|
|
void _loadLastConnectedDeviceName() { |
|
|
|
|
|
_lastConnectedDeviceName = _storage.read(_lastDeviceNameKey); |
|
|
|
|
|
Logger.d(_tag, '加载最后连接的设备名称: $_lastConnectedDeviceName'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 保存最后连接的设备名称到本地存储 |
|
|
|
|
|
void _saveLastConnectedDeviceName(String deviceName) { |
|
|
|
|
|
_lastConnectedDeviceName = deviceName; |
|
|
|
|
|
_storage.write(_lastDeviceNameKey, deviceName); |
|
|
|
|
|
Logger.d(_tag, '保存最后连接的设备名称: $deviceName'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 保存上一次连接设备的图片链接到本地存储 |
|
|
|
|
|
void _saveLastConnectedDeviceImage(String? imageUrl) { |
|
|
|
|
|
if (imageUrl != null && imageUrl.isNotEmpty) { |
|
|
|
|
|
_lastConnectedDeviceImage = imageUrl; |
|
|
|
|
|
_storage.write(_lastDeviceImageKey, imageUrl); |
|
|
|
|
|
Logger.d(_tag, '保存上一次连接设备的图片: $imageUrl'); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Future<void> refreshDeviceConnection() async { |
|
|
Future<void> refreshDeviceConnection() async { |
|
|
await _checkConnectionStatus(); |
|
|
await _checkConnectionStatus(); |
|
|
|
|
|
|
|
|
@ -235,6 +262,7 @@ class HomeController extends GetxController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
final deviceName = currentDevice?.name; |
|
|
final deviceName = currentDevice?.name; |
|
|
|
|
|
//final deviceName = '102'; |
|
|
if (deviceName != null && deviceName.isNotEmpty) { |
|
|
if (deviceName != null && deviceName.isNotEmpty) { |
|
|
return deviceName; |
|
|
return deviceName; |
|
|
} |
|
|
} |
|
|
@ -404,4 +432,69 @@ class HomeController extends GetxController { |
|
|
Map<String, dynamic> getOtaCheckStatus() { |
|
|
Map<String, dynamic> getOtaCheckStatus() { |
|
|
return _otaCheckService.getCheckStatus(); |
|
|
return _otaCheckService.getCheckStatus(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 获取当前设备的产品图片路径 |
|
|
|
|
|
/// 如果没有配置产品图片或获取失败,返回null使用默认图片 |
|
|
|
|
|
String? getCurrentDeviceImage() { |
|
|
|
|
|
try { |
|
|
|
|
|
final deviceName = getDeviceName(); |
|
|
|
|
|
if (deviceName == 'unknownEquipment'.tr) { |
|
|
|
|
|
Logger.d(_tag, '设备未知,使用默认图片'); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 通过AppConfig获取产品信息 |
|
|
|
|
|
if (!AppConfig.isInitialized()) { |
|
|
|
|
|
Logger.w(_tag, 'AppConfig未初始化,使用默认图片'); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final product = AppConfig.getproduct(deviceName); |
|
|
|
|
|
if (product != null && |
|
|
|
|
|
product.productimage != null && |
|
|
|
|
|
product.productimage!.isNotEmpty) { |
|
|
|
|
|
Logger.d(_tag, '获取到设备产品图片: ${product.productimage}'); |
|
|
|
|
|
_saveLastConnectedDeviceImage(product.productimage); |
|
|
|
|
|
return product.productimage; |
|
|
|
|
|
} else { |
|
|
|
|
|
Logger.d(_tag, '设备 "$deviceName" 未配置产品图片,使用默认图片'); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
Logger.e(_tag, '获取设备产品图片失败: $e'); |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 获取设备图片的完整资源路径 |
|
|
|
|
|
/// 处理网络图片URL和本地资源路径 |
|
|
|
|
|
String getDeviceImagePath(bool isDarkMode) { |
|
|
|
|
|
final productImage = getCurrentDeviceImage(); |
|
|
|
|
|
|
|
|
|
|
|
if (productImage != null) { |
|
|
|
|
|
// 如果是网络图片URL |
|
|
|
|
|
if (productImage.startsWith('http://') || |
|
|
|
|
|
productImage.startsWith('https://')) { |
|
|
|
|
|
return productImage; |
|
|
|
|
|
} |
|
|
|
|
|
// 如果是本地资源路径,确保以assets/开头 |
|
|
|
|
|
else if (productImage.startsWith('assets/')) { |
|
|
|
|
|
return productImage; |
|
|
|
|
|
} |
|
|
|
|
|
// 如果只是文件名,添加assets/images/前缀 |
|
|
|
|
|
else { |
|
|
|
|
|
return 'assets/images/$productImage'; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 使用默认耳机图片 |
|
|
|
|
|
return isDarkMode |
|
|
|
|
|
? 'assets/images/headphone_new3.png' |
|
|
|
|
|
: 'assets/images/headphone_new4.png'; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// 判断是否为网络图片 |
|
|
|
|
|
bool isNetworkImage(String imagePath) { |
|
|
|
|
|
return imagePath.startsWith('http://') || imagePath.startsWith('https://'); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|