绑定设备的位置的更新和获取接口
This commit is contained in:
@@ -14,4 +14,11 @@ class HttpApiConsts {
|
||||
static const String unbindDevice = "$baseUrl/forward/device/unbind";
|
||||
// 切换设备
|
||||
static const String switchDevice = "$baseUrl/forward/device/switchDevice";
|
||||
|
||||
|
||||
// 获取设备位置
|
||||
static const String getDeviceLocation = "$baseUrl/iot/device/userDevice";
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,4 +5,7 @@ abstract class DeviceHttpDatasource {
|
||||
Future<int> bindDevice(String deviceId, String deviceAlias);
|
||||
Future<int> unbindDevice(String deviceId);
|
||||
Future<int> switchDevice(String platform, String deviceId);
|
||||
|
||||
Future<dynamic> getDeviceLocation(String username) async {}
|
||||
|
||||
}
|
||||
|
||||
@@ -78,4 +78,38 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
// TODO: implement unbindDevice
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<dynamic> getDeviceLocation(String username) async {
|
||||
// TODO: implement getDeviceLocation
|
||||
try {
|
||||
// 发起 GET 请求
|
||||
final response = await dio.get(
|
||||
'https://serviceri.satabot.com/iot/device/userDevice',
|
||||
queryParameters: {'tenantName': username},
|
||||
// options: Options(
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json',
|
||||
// 'Authorization': 'Bearer ${await _getToken()}', // 获取 Token
|
||||
// },
|
||||
// ),
|
||||
);
|
||||
|
||||
// 检查响应状态码
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data;
|
||||
if (data['code'] == 200) {
|
||||
return data['data']; // 返回设备数据
|
||||
} else {
|
||||
throw Exception(data['msg'] ?? '业务异常');
|
||||
}
|
||||
} else {
|
||||
throw Exception('网络请求失败: ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
throw Exception('获取设备位置失败: $e');
|
||||
}
|
||||
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:dio/dio.dart';
|
||||
import 'package:fpdart/src/either.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/data/datasources/device_http_datasource.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_location_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
|
||||
|
||||
import '../../domain/repositories/device_repository.dart';
|
||||
@@ -69,4 +70,12 @@ class DeviceRepositoryImpl implements DeviceRepository {
|
||||
return Left(DeviceFailure(cleanMessage));
|
||||
}
|
||||
}
|
||||
// TODO: implement switchDevice 实现获取设备的列表
|
||||
@override
|
||||
Future<Either<DeviceFailure, DeviceLocationEntity>>getDeviceLocation(String username) async {
|
||||
// TODO: implement getDeviceLocation
|
||||
var result = await _deviceHttpDatasource.getDeviceLocation(username);
|
||||
return Right(result);
|
||||
// throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
class DeviceLocationEntity {
|
||||
final String deviceName;
|
||||
final double latitude; // 新增字段
|
||||
final double longitude; // 新增字段
|
||||
|
||||
DeviceLocationEntity({
|
||||
required this.deviceName,
|
||||
required this.latitude,
|
||||
required this.longitude,
|
||||
});
|
||||
}
|
||||
@@ -11,4 +11,6 @@ abstract class DeviceHostrityWorkRepositoryRepository {
|
||||
required String deviceId,
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_location_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
|
||||
|
||||
abstract class DeviceRepository {
|
||||
@@ -15,4 +16,6 @@ abstract class DeviceRepository {
|
||||
String platform,
|
||||
String deviceId,
|
||||
);
|
||||
// 获取设备位置
|
||||
Future<Either<DeviceFailure, DeviceLocationEntity>> getDeviceLocation(String username);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// features/devices/domain/usecases/get_device_location_usecase.dart
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_location_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
|
||||
|
||||
class GetDeviceLocationUseCase {
|
||||
final DeviceRepository repository;
|
||||
|
||||
GetDeviceLocationUseCase(this.repository);
|
||||
//获取设备的位置
|
||||
Future<Either<DeviceFailure, DeviceLocationEntity>> call(String username) async {
|
||||
return await repository.getDeviceLocation(username);
|
||||
}
|
||||
}
|
||||
@@ -3,21 +3,22 @@ import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/usecases/get_user_device_usecase.dart';
|
||||
|
||||
import '../../domain/usecases/get_device_location_usecase.dart';
|
||||
import 'devices_state.dart';
|
||||
|
||||
class DevicesCubit extends Cubit<DevicesState> {
|
||||
final GetUserDeviceUseCase _getUserDeviceUseCase;
|
||||
|
||||
final GetDeviceLocationUseCase _getDeviceLocationUseCase;
|
||||
final DeviceRepository repository;
|
||||
|
||||
DevicesCubit(this.repository, this._getUserDeviceUseCase)
|
||||
DevicesCubit(this.repository, this._getUserDeviceUseCase, this._getDeviceLocationUseCase)
|
||||
: super(const DevicesState());
|
||||
|
||||
// 获取所有设备列表
|
||||
Future<void> fetchAllDevices(String username) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
// 模拟网络请求获取列表
|
||||
// 网络请求获取列表
|
||||
var resultEither = await _getUserDeviceUseCase.call(
|
||||
GetUserDeviceParams(username),
|
||||
);
|
||||
@@ -63,7 +64,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
|
||||
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
|
||||
}
|
||||
|
||||
// 切换设备
|
||||
Future<void> switchDevice(DeviceEntity device) async {
|
||||
// 保持现有列表,只改 loading
|
||||
emit(state.copyWith(isLoading: true));
|
||||
@@ -75,4 +76,19 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
selectDevice(device);
|
||||
}, (r) => emit(state.copyWith(isLoading: false, selectedDevice: device)));
|
||||
}
|
||||
// 获取设备位置
|
||||
Future<void> getDeviceLocation(DeviceEntity device) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _getDeviceLocationUseCase.call(device.deviceName);
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
|
||||
(location) => emit(state.copyWith(
|
||||
isLoading: false,
|
||||
deviceLatitude: location.latitude,
|
||||
deviceLongitude: location.longitude,
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -6,12 +6,16 @@ class DevicesState extends Equatable {
|
||||
final DeviceEntity? selectedDevice; // 当前主界面选中的/操作的设备
|
||||
final bool isLoading; // 是否正在加载
|
||||
final String? errorMessage; // 错误信息
|
||||
final double? deviceLatitude; // 新增字段
|
||||
final double? deviceLongitude; // 新增字段
|
||||
|
||||
const DevicesState({
|
||||
this.devices = const [],
|
||||
this.selectedDevice,
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
this.deviceLatitude,
|
||||
this.deviceLongitude,
|
||||
});
|
||||
|
||||
// 使用 copyWith 方便局部更新状态
|
||||
@@ -20,15 +24,23 @@ class DevicesState extends Equatable {
|
||||
DeviceEntity? selectedDevice,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
double? deviceLatitude,
|
||||
double? deviceLongitude,
|
||||
}) {
|
||||
return DevicesState(
|
||||
devices: devices ?? this.devices,
|
||||
selectedDevice: selectedDevice ?? this.selectedDevice,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage, // 错误信息通常每次更新都要重新赋值或清空
|
||||
deviceLatitude: deviceLatitude ?? this.deviceLatitude,
|
||||
deviceLongitude: deviceLongitude ?? this.deviceLongitude,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [devices, selectedDevice, isLoading, errorMessage];
|
||||
List<Object?> get props => [devices, selectedDevice, isLoading,
|
||||
errorMessage,
|
||||
deviceLatitude,
|
||||
deviceLongitude,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user