Browse Source

Merge branch 'new_dev' of https://github.com/deepcloud2048/deep_voice into new_dev

weicu
fdp 1 year ago
parent
commit
fbf1697ca5
  1. 12
      lib/data/services/ble_manager.dart
  2. 39
      lib/modules/devices/controllers/devices_controller.dart
  3. 84
      lib/modules/devices/views/devices_view.dart

12
lib/data/services/ble_manager.dart

@ -113,7 +113,7 @@ class BleManager extends GetxService {
// 经典蓝牙实例 // 经典蓝牙实例
// 当前连接的设备信息 // 当前连接的设备信息
String? _currentDeviceAddress; String? currentDeviceAddress;
String? _currentDeviceName; String? _currentDeviceName;
// BLE连接状态 // BLE连接状态
@ -513,7 +513,7 @@ class BleManager extends GetxService {
(firmwareVersion != null || batteryLevel != null)) { (firmwareVersion != null || batteryLevel != null)) {
Logger.info('lxm--设备信息不存在,创建新的设备信息对象'); Logger.info('lxm--设备信息不存在,创建新的设备信息对象');
// 使用保存的设备信息创建新的DeviceInfo对象 // 使用保存的设备信息创建新的DeviceInfo对象
String deviceAddress = _currentDeviceAddress ?? ""; String deviceAddress = currentDeviceAddress ?? "";
String deviceName = _currentDeviceName ?? ""; String deviceName = _currentDeviceName ?? "";
Logger.info('lxm--创建新的设备信息对象: address=$deviceAddress, name=$deviceName'); Logger.info('lxm--创建新的设备信息对象: address=$deviceAddress, name=$deviceName');
@ -561,7 +561,7 @@ class BleManager extends GetxService {
void clearDeviceInfo() { void clearDeviceInfo() {
Logger.info('清除设备信息'); Logger.info('清除设备信息');
deviceInfo.value = null; deviceInfo.value = null;
_currentDeviceAddress = null; currentDeviceAddress = null;
_currentDeviceName = null; _currentDeviceName = null;
} }
@ -573,7 +573,7 @@ class BleManager extends GetxService {
// 确保设备信息对象存在 // 确保设备信息对象存在
if (deviceInfo.value == null && _isConnected.value) { if (deviceInfo.value == null && _isConnected.value) {
// 创建初始设备信息对象 // 创建初始设备信息对象
String deviceAddress = _currentDeviceAddress ?? ""; String deviceAddress = currentDeviceAddress ?? "";
String deviceName = _currentDeviceName ?? ""; String deviceName = _currentDeviceName ?? "";
Logger.info('初始化设备信息对象: address=$deviceAddress, name=$deviceName'); Logger.info('初始化设备信息对象: address=$deviceAddress, name=$deviceName');
@ -1036,12 +1036,12 @@ class BleManager extends GetxService {
try { try {
// 保存设备地址信息 // 保存设备地址信息
if (macAddress != null) { if (macAddress != null) {
_currentDeviceAddress = macAddress; currentDeviceAddress = macAddress;
// 从蓝牙管理器获取设备名称 // 从蓝牙管理器获取设备名称
final bluetoothManager = Get.find<BluetoothManager>(); final bluetoothManager = Get.find<BluetoothManager>();
_currentDeviceName = bluetoothManager.currentDevice?.name ?? ""; _currentDeviceName = bluetoothManager.currentDevice?.name ?? "";
Logger.info( Logger.info(
'保存设备信息: address=$_currentDeviceAddress, name=$_currentDeviceName'); '保存设备信息: address=$currentDeviceAddress, name=$_currentDeviceName');
} }
return await _bleService.registerBackgroundScan(macAddress: macAddress); return await _bleService.registerBackgroundScan(macAddress: macAddress);

39
lib/modules/devices/controllers/devices_controller.dart

@ -24,29 +24,36 @@ class DevicesController extends GetxController {
@override @override
void onInit() { void onInit() {
super.onInit(); super.onInit();
print("初始化设备管理1");
_bleManager = Get.find<BleManager>(); _bleManager = Get.find<BleManager>();
print("初始化设备管理2");
// 在初始化时加载设备数据 // 在初始化时加载设备数据
_loadDevices(); _loadDevices();
} }
// 加载设备数据 // 加载设备数据
void _loadDevices() async { void _loadDevices() async {
devices.clear(); Logger.info("加载设备");
final response = await Api.getdevices(); final response = await Api.getdevices();
if (response != null) { if (response != null) {
var devices = (response['devices'] as List<dynamic>) if (response['devices'] != null) {
.map((e) => UserDevice.fromJson(e as Map<String, dynamic>)) var devices = (response['devices'] as List<dynamic>)
.toList(); .map((e) => UserDevice.fromJson(e as Map<String, dynamic>))
User.instance.devices = devices; .toList();
User.instance.devices = devices;
} else {
User.instance.devices = [];
}
Logger.info("加载设备 ${User.instance.devices.length}");
} }
devices.clear();
for (var device in User.instance.devices) { for (var device in User.instance.devices) {
if (!devices.containsKey(device.devicetype)) { if (!devices.containsKey(device.devicetype)) {
devices[device.devicetype] = []; devices[device.devicetype] = [];
} }
devices[device.devicetype]!.add(device); devices[device.devicetype]!.add(device);
} }
devices.refresh();
} }
@override @override
@ -59,12 +66,22 @@ class DevicesController extends GetxController {
final response = await Api.unbinddevice({ final response = await Api.unbinddevice({
"id": device.id, "id": device.id,
}); });
Logger.info("解绑设备 $response");
if (response != null) { if (response != null) {
// 响应式更新 // 响应式更新
devices[device.devicetype]?.remove(device); User.instance.devices.remove(device);
devices.refresh(); // 触发UI更新 devices.clear();
for (var device in User.instance.devices) {
if (device.devicemac == _bleManager.deviceInfo.value?.address) { if (!devices.containsKey(device.devicetype)) {
devices[device.devicetype] = [];
}
devices[device.devicetype]!.add(device);
}
devices.refresh();
Logger.info(
"解绑设备 ${device.devicemac} 当前使用设备:${_bleManager.currentDeviceAddress}");
if (device.devicemac == _bleManager.currentDeviceAddress) {
Logger.info("解绑设备 $device 重新匹配!");
User.instance.removeUserDevice(device.id); User.instance.removeUserDevice(device.id);
_bleManager.clearPairedDevices(); _bleManager.clearPairedDevices();
_bleManager.disconnect(); _bleManager.disconnect();

84
lib/modules/devices/views/devices_view.dart

@ -118,57 +118,51 @@ class DevicesView extends GetView<DevicesController> {
} }
Widget _buildDevicesList(bool isDarkMode) { Widget _buildDevicesList(bool isDarkMode) {
final devices = User.instance.devices; return Obx(() {
// 使用控制器的响应式数据,这会触发控制器的实例化
final groupedDevices = controller.devices;
if (devices.isEmpty) { if (groupedDevices.isEmpty) {
return _buildEmptyState(isDarkMode); return _buildEmptyState(isDarkMode);
}
// 按设备类型分组
final Map<DeviceType, List<UserDevice>> groupedDevices = {};
for (var device in devices) {
final deviceType = DeviceType.fromValue(device.devicetype);
if (!groupedDevices.containsKey(deviceType)) {
groupedDevices[deviceType] = [];
} }
groupedDevices[deviceType]!.add(device);
}
return ListView( return ListView(
padding: EdgeInsets.all(20.w), padding: EdgeInsets.all(20.w),
children: [ children: [
// 遍历每个设备类型 // 遍历每个设备类型
...groupedDevices.entries.map((entry) { ...groupedDevices.entries.map((entry) {
final deviceType = entry.key; final deviceTypeValue = entry.key;
final deviceList = entry.value; final deviceList = entry.value;
final deviceType = DeviceType.fromValue(deviceTypeValue);
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
// 设备类型标题 // 设备类型标题
Padding( Padding(
padding: EdgeInsets.only(bottom: 12.h), padding: EdgeInsets.only(bottom: 12.h),
child: Text( child: Text(
deviceType.displayName, deviceType.displayName,
style: TextStyle( style: TextStyle(
fontSize: 16.sp, fontSize: 16.sp,
fontWeight: FontWeight.w600, fontWeight: FontWeight.w600,
color: isDarkMode ? Colors.white : Colors.black87, color: isDarkMode ? Colors.white : Colors.black87,
),
), ),
), ),
), // 该类型下的设备列表
// 该类型下的设备列表 ...deviceList.map((device) => _buildDeviceCard(
...deviceList.map((device) => _buildDeviceCard( device: device,
device: device, deviceType: deviceType,
deviceType: deviceType, isDarkMode: isDarkMode,
isDarkMode: isDarkMode, )),
)), SizedBox(height: 20.h),
SizedBox(height: 20.h), ],
], );
); }).toList(),
}).toList(), ],
], );
); });
} }
Widget _buildEmptyState(bool isDarkMode) { Widget _buildEmptyState(bool isDarkMode) {

Loading…
Cancel
Save