117 lines
3.4 KiB
Dart
117 lines
3.4 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:get/get_rx/get_rx.dart';
|
||
import 'package:starcloud/entity/star_cloud_lock_list.dart';
|
||
import 'package:starcloud/sdk/sdk_device_operate_extension.dart';
|
||
import 'package:starcloud/sdk/starcloud.dart';
|
||
import 'package:starwork_flutter/base/app_logger.dart';
|
||
import 'package:starwork_flutter/base/base_controller.dart';
|
||
import 'package:starwork_flutter/common/constant/app_support_device_type.dart';
|
||
import 'package:starwork_flutter/common/events/refresh_device_list_event.dart';
|
||
import 'package:starwork_flutter/common/utils/event_bus_util.dart';
|
||
import 'package:starwork_flutter/views/main/main_controller.dart';
|
||
|
||
class DeviceManageController extends BaseController with GetSingleTickerProviderStateMixin {
|
||
final mainController = Get.find<MainController>();
|
||
|
||
// 选中状态 0:全部状态 1:在线 2:离线
|
||
final RxInt selectedStatusIndex = 0.obs;
|
||
|
||
// 选中设备类型
|
||
final selectedDeviceType = AppSupportDeviceType.lock.obs;
|
||
|
||
// 设备列表管理
|
||
final deviceList = <StarCloudLock>[].obs;
|
||
|
||
// 用于 UI 显示的过滤后列表
|
||
final filteredDeviceList = <StarCloudLock>[].obs;
|
||
|
||
// 在线设备数量
|
||
final onlineDeviceCount = 0.obs;
|
||
|
||
// 离线设备数量
|
||
final offlineDeviceCount = 0.obs;
|
||
|
||
// 刷新设备列表事件留监听
|
||
late StreamSubscription _refreshDeviceListSubscription;
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
initData();
|
||
|
||
// 监听刷新设备列表事件
|
||
_refreshDeviceListSubscription = EventBusUtil().instance.on<RefreshDeviceListEvent>().listen((event) async {
|
||
_requestTeamDeviceList();
|
||
});
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
super.onClose();
|
||
_refreshDeviceListSubscription.cancel();
|
||
}
|
||
|
||
void initData() async {
|
||
_requestTeamDeviceList();
|
||
}
|
||
|
||
/// 切换设备状态筛选
|
||
void changeStatusFilter(int index) {
|
||
selectedStatusIndex.value = index;
|
||
_updateFilteredList();
|
||
}
|
||
|
||
/// 根据当前 selectedStatusIndex 过滤设备列表
|
||
void _updateFilteredList() {
|
||
List<StarCloudLock> filtered = [];
|
||
|
||
switch (selectedStatusIndex.value) {
|
||
case 0: // 全部
|
||
filtered = deviceList;
|
||
break;
|
||
case 1: // 在线
|
||
filtered = deviceList.where((device) => device.isOnline == true).toList();
|
||
break;
|
||
case 2: // 离线
|
||
filtered = deviceList.where((device) => device.isOnline == false).toList();
|
||
break;
|
||
default:
|
||
filtered = deviceList;
|
||
}
|
||
filteredDeviceList.assignAll(filtered); // 更新响应式列表
|
||
}
|
||
|
||
void _requestTeamDeviceList() async {
|
||
showLoading();
|
||
await StarCloudSDK.instance.getDeviceList(
|
||
pageNo: 1,
|
||
pageSize: 999,
|
||
cloudUid: mainController.selectedTeam.value.teamCloudInfo!.uid!,
|
||
onSuccess: (StarCloudLockList list) {
|
||
filteredDeviceList.clear();
|
||
deviceList.clear();
|
||
for (var groupItem in list.groupList) {
|
||
for (var lock in groupItem.lockList) {
|
||
deviceList.add(lock);
|
||
if (lock.isOnline ?? false) {
|
||
onlineDeviceCount.value++;
|
||
} else {
|
||
offlineDeviceCount.value++;
|
||
}
|
||
}
|
||
}
|
||
// ✅ 加载完原始数据后,更新过滤列表
|
||
_updateFilteredList();
|
||
hideLoading();
|
||
},
|
||
onError: (err) {
|
||
AppLogger.error('获取设备列表失败:${err}');
|
||
hideLoading();
|
||
},
|
||
);
|
||
}
|
||
}
|