路径规划作业列表 其余界面优化
This commit is contained in:
@@ -3,9 +3,8 @@ import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.
|
||||
abstract class DeviceHttpDatasource {
|
||||
Future<List<DeviceEntity>> getUserDevices(String username);
|
||||
Future<int> bindDevice(String deviceId, String deviceAlias);
|
||||
Future<int> unbindDevice(String deviceId);
|
||||
Future<int> unbindDevice(String deviceId, String deviceName);
|
||||
Future<int> switchDevice(String platform, String deviceId);
|
||||
|
||||
Future<dynamic> getDeviceLocation(String username) async {}
|
||||
|
||||
}
|
||||
|
||||
@@ -76,9 +76,23 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> unbindDevice(String deviceId) {
|
||||
// TODO: implement unbindDevice
|
||||
throw UnimplementedError();
|
||||
Future<int> unbindDevice(String deviceId, String deviceName) async {
|
||||
final response = await dio.post(
|
||||
'/forward/device/unbind',
|
||||
data: {"deviceId": deviceId, "deviceName": deviceName},
|
||||
);
|
||||
|
||||
final data = response.data;
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络错误');
|
||||
}
|
||||
|
||||
if (data['code'] != 200) {
|
||||
throw Exception(data['msg']);
|
||||
}
|
||||
|
||||
return data['data'];
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -101,14 +115,13 @@ class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data;
|
||||
if (data['code'] == 200) {
|
||||
//return data['data']; // 返回设备数据
|
||||
final locationData = data['data'];// 假设数据结构,测试时候如有不妥就修改结果的实例化
|
||||
//return data['data']; // 返回设备数据
|
||||
final locationData = data['data']; // 假设数据结构,测试时候如有不妥就修改结果的实例化
|
||||
return DeviceLocationEntity(
|
||||
deviceName: locationData['deviceName'],
|
||||
latitude: locationData['latitude'],
|
||||
longitude: locationData['longitude'],
|
||||
);
|
||||
|
||||
} else {
|
||||
throw Exception(data['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
@@ -49,9 +49,17 @@ class DeviceRepositoryImpl implements DeviceRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, int>> unBindDevice(String deviceId) {
|
||||
// TODO: implement unBindDevice
|
||||
throw UnimplementedError();
|
||||
Future<Either<DeviceFailure, int>> unBindDevice(String deviceId, String deviceName) async {
|
||||
try {
|
||||
var result = await _deviceHttpDatasource.unbindDevice(deviceId, deviceName);
|
||||
return Right(result);
|
||||
} on DioException catch (e) {
|
||||
final String serverMessage = e.response?.data['msg'] ?? "网络连接异常";
|
||||
return Left(DeviceFailure(serverMessage));
|
||||
} catch (e) {
|
||||
final cleanMessage = e.toString().replaceFirst('Exception: ', '');
|
||||
return Left(DeviceFailure(cleanMessage));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -11,11 +11,16 @@ abstract class DeviceRepository {
|
||||
String deviceId,
|
||||
String deviceAlias,
|
||||
);
|
||||
Future<Either<DeviceFailure, int>> unBindDevice(String deviceId);
|
||||
Future<Either<DeviceFailure, int>> unBindDevice(
|
||||
String deviceId,
|
||||
String deviceName,
|
||||
);
|
||||
Future<Either<DeviceFailure, int>> switchDevice(
|
||||
String platform,
|
||||
String deviceId,
|
||||
);
|
||||
// 获取设备位置
|
||||
Future<Either<DeviceFailure, DeviceLocationEntity>> getDeviceLocation(String username);
|
||||
Future<Either<DeviceFailure, DeviceLocationEntity>> getDeviceLocation(
|
||||
String username,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,12 +10,13 @@ class UnbindDeviceUseCase implements BaseUseCase<int, UnbindDeviceParams> {
|
||||
|
||||
@override
|
||||
Future<Either<Failure, int>> call(UnbindDeviceParams params) async {
|
||||
return await repository.unBindDevice(params.deviceId);
|
||||
return await repository.unBindDevice(params.deviceId, params.deviceName);
|
||||
}
|
||||
}
|
||||
|
||||
class UnbindDeviceParams {
|
||||
final String deviceId;
|
||||
final String deviceName;
|
||||
|
||||
UnbindDeviceParams(this.deviceId);
|
||||
UnbindDeviceParams(this.deviceId, this.deviceName);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
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 'package:maibu_satabot_v2/features/devices/domain/usecases/unbind_device_usecase.dart';
|
||||
|
||||
import '../../domain/usecases/delete_work_record_usecase.dart';
|
||||
import '../../domain/usecases/get_device_location_usecase.dart';
|
||||
@@ -14,15 +15,68 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
final DeviceRepository repository;
|
||||
final GetWorkRecordUseCase _getWorkRecordUseCase;
|
||||
final DeleteWorkRecordUseCase _deleteWorkRecordUseCase;
|
||||
final UnbindDeviceUseCase _unbindDeviceUseCase;
|
||||
|
||||
DevicesCubit(
|
||||
this.repository,
|
||||
this._getUserDeviceUseCase,
|
||||
this._getDeviceLocationUseCase,
|
||||
this._getWorkRecordUseCase,
|
||||
this._deleteWorkRecordUseCase,
|
||||
this._unbindDeviceUseCase,
|
||||
) : super(const DevicesState());
|
||||
|
||||
DevicesCubit(this.repository,
|
||||
this._getUserDeviceUseCase,
|
||||
this._getDeviceLocationUseCase,
|
||||
this._getWorkRecordUseCase,
|
||||
this._deleteWorkRecordUseCase,
|
||||
)
|
||||
: super(const DevicesState());
|
||||
Future<void> unbindDevice(String deviceId, String deviceName) async {
|
||||
// 1. 加载中状态
|
||||
emit(state.copyWith(isLoading: true, errorMessage: ''));
|
||||
|
||||
try {
|
||||
// 2. 构建参数
|
||||
final params = UnbindDeviceParams(deviceId, deviceName);
|
||||
|
||||
// 3. 调用 UseCase(核心:执行解绑逻辑)
|
||||
final result = await _unbindDeviceUseCase.call(params);
|
||||
|
||||
// 4. 处理 UseCase 返回的 Either 结果
|
||||
result.fold(
|
||||
// 失败:更新错误状态
|
||||
(failure) => emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '解绑设备失败',
|
||||
),
|
||||
),
|
||||
// 成功:更新状态(比如从设备列表中移除该设备)
|
||||
(successCode) {
|
||||
// 过滤掉已解绑的设备
|
||||
final updatedDevices = state.devices?.where((device) {
|
||||
return device.deviceAlias != deviceId &&
|
||||
device.deviceName != deviceName;
|
||||
}).toList();
|
||||
|
||||
// 更新状态:清空加载、更新设备列表、清空选中设备(如果解绑的是当前选中设备)
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
devices: updatedDevices,
|
||||
selectedDevice: state.selectedDevice?.deviceAlias == deviceId
|
||||
? null
|
||||
: state.selectedDevice,
|
||||
errorMessage: '',
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
// 捕获其他异常
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '解绑设备异常:${e.toString()}',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有设备列表
|
||||
Future<void> fetchAllDevices(String username) async {
|
||||
@@ -41,6 +95,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
),
|
||||
),
|
||||
(deviceList) {
|
||||
print("设备列表:$deviceList"); // 调试输出
|
||||
emit(
|
||||
state.copyWith(
|
||||
devices: deviceList,
|
||||
@@ -74,6 +129,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
|
||||
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
|
||||
}
|
||||
|
||||
/// 切换设备
|
||||
Future<void> switchDevice(DeviceEntity device) async {
|
||||
// 保持现有列表,只改 loading
|
||||
@@ -86,42 +142,50 @@ 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,
|
||||
)),
|
||||
);
|
||||
}
|
||||
///获取记录
|
||||
Future<void> loadWorkRecords(String userId) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _getWorkRecordUseCase(userId);
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
|
||||
(records) => emit(state.copyWith(isLoading: false, workRecords: records)),
|
||||
);
|
||||
}
|
||||
/// 删除工作记录
|
||||
Future<void> deleteWorkRecord(String workName) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _deleteWorkRecordUseCase(workName);
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
|
||||
(_) => emit(
|
||||
(failure) =>
|
||||
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
|
||||
(location) => emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
workRecords: state.workRecords?.where((record) => record != workName).toList(),
|
||||
deviceLatitude: location.latitude,
|
||||
deviceLongitude: location.longitude,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///获取记录
|
||||
Future<void> loadWorkRecords(String userId) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _getWorkRecordUseCase(userId);
|
||||
result.fold(
|
||||
(failure) =>
|
||||
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
|
||||
(records) => emit(state.copyWith(isLoading: false, workRecords: records)),
|
||||
);
|
||||
}
|
||||
|
||||
/// 删除工作记录
|
||||
Future<void> deleteWorkRecord(String workName) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _deleteWorkRecordUseCase(workName);
|
||||
result.fold(
|
||||
(failure) =>
|
||||
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
|
||||
(_) => emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
workRecords: state.workRecords
|
||||
?.where((record) => record != workName)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ class DevicesState extends Equatable {
|
||||
errorMessage: errorMessage, // 错误信息通常每次更新都要重新赋值或清空
|
||||
deviceLatitude: deviceLatitude ?? this.deviceLatitude,
|
||||
deviceLongitude: deviceLongitude ?? this.deviceLongitude,
|
||||
workRecords: workRecords ?? this.workRecords,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user