第二次提交
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
|
||||
abstract class DeviceHttpDatasource {
|
||||
Future<List<DeviceEntity>> getUserDevices(String username);
|
||||
Future<int> bindDevice(String deviceId, String deviceAlias);
|
||||
Future<int> unbindDevice(String deviceId);
|
||||
Future<int> switchDevice(String platform, String deviceId);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:maibu_satabot_v2/core/consts/http_api_consts.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';
|
||||
|
||||
class DeviceHttpDatasourceImpl implements DeviceHttpDatasource {
|
||||
final Dio dio;
|
||||
|
||||
DeviceHttpDatasourceImpl(this.dio);
|
||||
|
||||
@override
|
||||
Future<int> bindDevice(String deviceId, String deviceAlias) async {
|
||||
var response = await dio.post(
|
||||
HttpApiConsts.bindDevice,
|
||||
data: {'deviceId': deviceId, 'deviceAlias': deviceAlias},
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败:${response.statusCode}');
|
||||
}
|
||||
|
||||
final responseData = response.data;
|
||||
|
||||
if (responseData['code'] != 200 || responseData['data'] != true) {
|
||||
throw Exception(responseData['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<DeviceEntity>> getUserDevices(String username) async {
|
||||
var response = await dio.get(
|
||||
HttpApiConsts.getUserDevicesList,
|
||||
queryParameters: {'tenantName': username},
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败:${response.statusCode}');
|
||||
}
|
||||
|
||||
final responseData = response.data;
|
||||
|
||||
if (responseData['code'] != 200) {
|
||||
throw Exception(responseData['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
if (responseData['total'] == 0) {
|
||||
return [];
|
||||
} else {
|
||||
List<DeviceEntity> devices = [];
|
||||
for (var item in responseData['rows']) {
|
||||
devices.add(DeviceEntity.fromJson(item));
|
||||
}
|
||||
return devices;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> switchDevice(String platform, String deviceId) async {
|
||||
var response = await dio.post(
|
||||
HttpApiConsts.switchDevice,
|
||||
data: {'platform': platform, 'deviceId': deviceId},
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('网络请求失败:${response.statusCode}');
|
||||
}
|
||||
|
||||
final responseData = response.data;
|
||||
|
||||
if (responseData['code'] != 200 || responseData['data'] != true) {
|
||||
throw Exception(responseData['msg'] ?? '业务异常');
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<int> unbindDevice(String deviceId) {
|
||||
// TODO: implement unbindDevice
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
77
lib/features/devices/data/models/device_model.dart
Normal file
77
lib/features/devices/data/models/device_model.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
import 'package:maibu_satabot_v2/core/data/base_model.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
|
||||
class DeviceModel extends DeviceEntity implements BaseModel {
|
||||
DeviceModel({
|
||||
required super.deviceName,
|
||||
required super.productId,
|
||||
required super.productName,
|
||||
required super.tenantId,
|
||||
required super.tenantName,
|
||||
required super.status,
|
||||
required super.activeTime,
|
||||
required super.deviceAlias,
|
||||
required super.isBind,
|
||||
required super.onlineStatus,
|
||||
});
|
||||
|
||||
factory DeviceModel.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceModel(
|
||||
deviceName: json['deviceName'],
|
||||
productId: json['productId'],
|
||||
productName: json['productName'],
|
||||
tenantId: json['tenantId'],
|
||||
tenantName: json['tenantName'],
|
||||
status: json['status'],
|
||||
activeTime: json['activeTime'],
|
||||
deviceAlias: json['deviceAlias'],
|
||||
isBind: json['isBind'],
|
||||
onlineStatus: json['onlineStatus'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'deviceName': deviceName,
|
||||
'productId': productId,
|
||||
'productName': productName,
|
||||
'tenantId': tenantId,
|
||||
'tenantName': tenantName,
|
||||
'status': status,
|
||||
'activeTime': activeTime,
|
||||
'deviceAlias': deviceAlias,
|
||||
'isBind': isBind,
|
||||
'onlineStatus': onlineStatus,
|
||||
};
|
||||
}
|
||||
|
||||
toEntity() {
|
||||
return DeviceEntity(
|
||||
deviceName: deviceName,
|
||||
productId: productId,
|
||||
productName: productName,
|
||||
tenantId: tenantId,
|
||||
tenantName: tenantName,
|
||||
status: status,
|
||||
activeTime: activeTime,
|
||||
deviceAlias: deviceAlias,
|
||||
isBind: isBind,
|
||||
onlineStatus: onlineStatus,
|
||||
);
|
||||
}
|
||||
|
||||
factory DeviceModel.fromEntity(DeviceEntity entity) {
|
||||
return DeviceModel(
|
||||
deviceName: entity.deviceName,
|
||||
productId: entity.productId,
|
||||
productName: entity.productName,
|
||||
tenantId: entity.tenantId,
|
||||
tenantName: entity.tenantName,
|
||||
status: entity.status,
|
||||
activeTime: entity.activeTime,
|
||||
deviceAlias: entity.deviceAlias,
|
||||
isBind: entity.isBind,
|
||||
onlineStatus: entity.onlineStatus,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
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/errors/device_failure.dart';
|
||||
|
||||
import '../../domain/repositories/device_repository.dart';
|
||||
|
||||
class DeviceRepositoryImpl implements DeviceRepository {
|
||||
DeviceHttpDatasource _deviceHttpDatasource;
|
||||
|
||||
DeviceRepositoryImpl(this._deviceHttpDatasource);
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, int>> bindDevice(
|
||||
String deviceId,
|
||||
String deviceAlias,
|
||||
) async {
|
||||
try {
|
||||
var result = await _deviceHttpDatasource.bindDevice(
|
||||
deviceId,
|
||||
deviceAlias,
|
||||
);
|
||||
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
|
||||
Future<Either<DeviceFailure, List<DeviceEntity>>> getUserDevice(
|
||||
String username,
|
||||
) async {
|
||||
try {
|
||||
var result = await _deviceHttpDatasource.getUserDevices(username);
|
||||
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
|
||||
Future<Either<DeviceFailure, int>> unBindDevice(String deviceId) {
|
||||
// TODO: implement unBindDevice
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, int>> switchDevice(
|
||||
String platform,
|
||||
String deviceId,
|
||||
) async {
|
||||
try {
|
||||
var result = await _deviceHttpDatasource.switchDevice(platform, deviceId);
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
65
lib/features/devices/domain/entities/device_entity.dart
Normal file
65
lib/features/devices/domain/entities/device_entity.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class DeviceEntity extends Equatable {
|
||||
final String deviceName;
|
||||
final int productId;
|
||||
final String productName;
|
||||
final int tenantId;
|
||||
final String tenantName;
|
||||
final int status;
|
||||
final String? activeTime;
|
||||
final String? deviceAlias;
|
||||
final int isBind; // 是否被绑定过
|
||||
final int onlineStatus; // 在线状态
|
||||
|
||||
DeviceEntity({
|
||||
required this.deviceName,
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
required this.tenantId,
|
||||
required this.tenantName,
|
||||
this.status = 0,
|
||||
this.activeTime,
|
||||
this.deviceAlias,
|
||||
this.isBind = 0,
|
||||
this.onlineStatus = 0,
|
||||
});
|
||||
|
||||
// 方便从后端 JSON 转换
|
||||
factory DeviceEntity.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceEntity(
|
||||
deviceName: json['deviceName'],
|
||||
productId: json['productId'] ?? -1,
|
||||
productName: json['productName'] ?? "割草机产品MC700",
|
||||
tenantId: json['tenantId'],
|
||||
tenantName: json['tenantName'],
|
||||
status: json['status'] ?? 0,
|
||||
activeTime: json['activeTime'],
|
||||
deviceAlias: json['deviceAlias'],
|
||||
isBind: json['isBind'] ?? 0,
|
||||
onlineStatus: json['onlineStatus'] ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
// 获取显示的名称(优先别名,没有则用设备名)
|
||||
String get displayName => (deviceAlias != null && deviceAlias!.isNotEmpty)
|
||||
? deviceAlias!
|
||||
: (deviceName ?? "未知设备");
|
||||
|
||||
// 辅助方法:判断是否在线
|
||||
bool get isOnline => onlineStatus == 1;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
deviceName,
|
||||
productId,
|
||||
productName,
|
||||
tenantId,
|
||||
tenantName,
|
||||
status,
|
||||
activeTime,
|
||||
deviceAlias,
|
||||
isBind,
|
||||
onlineStatus,
|
||||
];
|
||||
}
|
||||
18
lib/features/devices/domain/errors/device_failure.dart
Normal file
18
lib/features/devices/domain/errors/device_failure.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
import '../../../../core/error/failure.dart';
|
||||
|
||||
class DeviceFailure extends Failure implements Exception {
|
||||
final int? code;
|
||||
DeviceFailure(super.message, {this.code});
|
||||
}
|
||||
|
||||
class DeviceNotConnectedFailure extends DeviceFailure {
|
||||
DeviceNotConnectedFailure(super.message);
|
||||
}
|
||||
|
||||
class DeviceOfflineFailure extends DeviceFailure {
|
||||
DeviceOfflineFailure(super.message);
|
||||
}
|
||||
|
||||
class DeviceNotBoundFailure extends DeviceFailure {
|
||||
DeviceNotBoundFailure(super.message);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
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/errors/device_failure.dart';
|
||||
|
||||
abstract class DeviceRepository {
|
||||
Future<Either<DeviceFailure, List<DeviceEntity>>> getUserDevice(
|
||||
String userName,
|
||||
);
|
||||
Future<Either<DeviceFailure, int>> bindDevice(
|
||||
String deviceId,
|
||||
String deviceAlias,
|
||||
);
|
||||
Future<Either<DeviceFailure, int>> unBindDevice(String deviceId);
|
||||
Future<Either<DeviceFailure, int>> switchDevice(
|
||||
String platform,
|
||||
String deviceId,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/core/domain/usecases/base_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
|
||||
class BindDeviceUseCase implements BaseUseCase<int, BindDeviceParams> {
|
||||
final DeviceRepository deviceRepository;
|
||||
|
||||
BindDeviceUseCase(this.deviceRepository);
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, int>> call(BindDeviceParams params) async {
|
||||
return await deviceRepository.bindDevice(
|
||||
params.deviceId,
|
||||
params.deviceAlias,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class BindDeviceParams {
|
||||
final String deviceId;
|
||||
final String deviceAlias;
|
||||
|
||||
BindDeviceParams(this.deviceId, this.deviceAlias);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/core/domain/usecases/base_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
|
||||
class GetUserDeviceUseCase
|
||||
implements BaseUseCase<List<DeviceEntity>, GetUserDeviceParams> {
|
||||
final DeviceRepository repository;
|
||||
|
||||
GetUserDeviceUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, List<DeviceEntity>>> call(
|
||||
GetUserDeviceParams params,
|
||||
) async {
|
||||
return await repository.getUserDevice(params.tenantName);
|
||||
}
|
||||
}
|
||||
|
||||
class GetUserDeviceParams {
|
||||
final String tenantName;
|
||||
|
||||
GetUserDeviceParams(this.tenantName);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/core/domain/usecases/base_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/errors/device_failure.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
|
||||
class SwitchDeviceUseCase implements BaseUseCase<int, SwitchDeviceParams> {
|
||||
final DeviceRepository deviceRepository;
|
||||
|
||||
SwitchDeviceUseCase(this.deviceRepository);
|
||||
|
||||
@override
|
||||
Future<Either<DeviceFailure, int>> call(SwitchDeviceParams params) async {
|
||||
return await deviceRepository.switchDevice(
|
||||
params.platform,
|
||||
params.deviceId,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SwitchDeviceParams {
|
||||
final String platform;
|
||||
final String deviceId;
|
||||
|
||||
SwitchDeviceParams(this.platform, this.deviceId);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:maibu_satabot_v2/core/domain/usecases/base_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/core/error/failure.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
|
||||
|
||||
class UnbindDeviceUseCase implements BaseUseCase<int, UnbindDeviceParams> {
|
||||
final DeviceRepository repository;
|
||||
|
||||
UnbindDeviceUseCase(this.repository);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, int>> call(UnbindDeviceParams params) async {
|
||||
return await repository.unBindDevice(params.deviceId);
|
||||
}
|
||||
}
|
||||
|
||||
class UnbindDeviceParams {
|
||||
final String deviceId;
|
||||
|
||||
UnbindDeviceParams(this.deviceId);
|
||||
}
|
||||
78
lib/features/devices/presentation/bloc/devices_cubit.dart
Normal file
78
lib/features/devices/presentation/bloc/devices_cubit.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
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 'devices_state.dart';
|
||||
|
||||
class DevicesCubit extends Cubit<DevicesState> {
|
||||
final GetUserDeviceUseCase _getUserDeviceUseCase;
|
||||
|
||||
final DeviceRepository repository;
|
||||
|
||||
DevicesCubit(this.repository, this._getUserDeviceUseCase)
|
||||
: super(const DevicesState());
|
||||
|
||||
// 获取所有设备列表
|
||||
Future<void> fetchAllDevices(String username) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
// 模拟网络请求获取列表
|
||||
var resultEither = await _getUserDeviceUseCase.call(
|
||||
GetUserDeviceParams(username),
|
||||
);
|
||||
|
||||
resultEither.fold(
|
||||
(failure) => emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message, // 假设你的 Failure 类有 message 字段
|
||||
),
|
||||
),
|
||||
(deviceList) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
devices: deviceList,
|
||||
selectedDevice: deviceList.isNotEmpty ? deviceList.first : null,
|
||||
isLoading: false,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// 切换当前选中的设备
|
||||
void selectDevice(DeviceEntity device) {
|
||||
emit(state.copyWith(selectedDevice: device));
|
||||
}
|
||||
|
||||
// 更新单个设备的状态(例如从 Tcp 收到实时电量更新)
|
||||
void updateDeviceStatus(DeviceEntity updatedDevice) {
|
||||
final newList = state.devices.map((d) {
|
||||
return d.deviceName == updatedDevice.deviceName ? updatedDevice : d;
|
||||
}).toList();
|
||||
|
||||
// 如果更新的是当前选中的设备,也要同步更新 selectedDevice
|
||||
final newSelected =
|
||||
state.selectedDevice?.deviceName == updatedDevice.deviceName
|
||||
? updatedDevice
|
||||
: state.selectedDevice;
|
||||
|
||||
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
|
||||
}
|
||||
|
||||
Future<void> switchDevice(DeviceEntity device) async {
|
||||
// 保持现有列表,只改 loading
|
||||
emit(state.copyWith(isLoading: true));
|
||||
|
||||
final result = await repository.switchDevice("app", device.deviceName);
|
||||
|
||||
result.fold((l) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: l.message));
|
||||
selectDevice(device);
|
||||
}, (r) => emit(state.copyWith(isLoading: false, selectedDevice: device)));
|
||||
}
|
||||
}
|
||||
34
lib/features/devices/presentation/bloc/devices_state.dart
Normal file
34
lib/features/devices/presentation/bloc/devices_state.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
|
||||
class DevicesState extends Equatable {
|
||||
final List<DeviceEntity> devices; // 名下所有设备列表
|
||||
final DeviceEntity? selectedDevice; // 当前主界面选中的/操作的设备
|
||||
final bool isLoading; // 是否正在加载
|
||||
final String? errorMessage; // 错误信息
|
||||
|
||||
const DevicesState({
|
||||
this.devices = const [],
|
||||
this.selectedDevice,
|
||||
this.isLoading = false,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
// 使用 copyWith 方便局部更新状态
|
||||
DevicesState copyWith({
|
||||
List<DeviceEntity>? devices,
|
||||
DeviceEntity? selectedDevice,
|
||||
bool? isLoading,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return DevicesState(
|
||||
devices: devices ?? this.devices,
|
||||
selectedDevice: selectedDevice ?? this.selectedDevice,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage, // 错误信息通常每次更新都要重新赋值或清空
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [devices, selectedDevice, isLoading, errorMessage];
|
||||
}
|
||||
Reference in New Issue
Block a user