完善遥控权限、控制发送和急停反馈
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../core/consts/http_api_consts.dart';
|
||||
import '../../domain/entities/emergency_stop_target.dart';
|
||||
|
||||
class EmergencyStopRemoteDatasource {
|
||||
const EmergencyStopRemoteDatasource(this.dio);
|
||||
// 注入统一 Dio,沿用登录鉴权和测试/生产环境配置。
|
||||
final Dio dio;
|
||||
|
||||
Future<void> request(String deviceId, EmergencyStopTarget target) async {
|
||||
final response = await dio.get<dynamic>(
|
||||
HttpApiConsts.hostEmergencyStopChange,
|
||||
queryParameters: {'deviceId': deviceId, 'eStop': target.value},
|
||||
options: Options(
|
||||
receiveTimeout: const Duration(seconds: 10),
|
||||
headers: {'Cache-Control': 'no-store', 'Pragma': 'no-cache'},
|
||||
),
|
||||
);
|
||||
final body = response.data;
|
||||
// 沿用相邻 /iot/host 接口的业务响应约定;未知响应不视为成功。
|
||||
if (response.statusCode != 200 || body is! Map || body['code'] == null) {
|
||||
throw const EmergencyStopRemoteException('急停接口响应格式错误或请求失败');
|
||||
}
|
||||
final data = body['data'];
|
||||
if ('${body['code']}' != '200' ||
|
||||
data == false ||
|
||||
body['accepted'] == false ||
|
||||
(data is Map && data['accepted'] == false)) {
|
||||
final message = body['msg'];
|
||||
throw EmergencyStopRemoteException(
|
||||
message is String && message.trim().isNotEmpty ? message : '急停请求未被接受',
|
||||
rejected: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class EmergencyStopRemoteException implements Exception {
|
||||
const EmergencyStopRemoteException(this.message, {this.rejected = false});
|
||||
final String message;
|
||||
final bool rejected;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../../domain/entities/emergency_stop_target.dart';
|
||||
import '../../domain/entities/emergency_stop_failure.dart';
|
||||
import '../../domain/repositories/device_emergency_stop_repository.dart';
|
||||
import '../datasources/emergency_stop_remote_datasource.dart';
|
||||
|
||||
class DeviceEmergencyStopRepositoryImpl
|
||||
implements DeviceEmergencyStopRepository {
|
||||
const DeviceEmergencyStopRepositoryImpl(this.remote);
|
||||
final EmergencyStopRemoteDatasource remote;
|
||||
|
||||
@override
|
||||
Future<Either<Failure, Unit>> request(
|
||||
String deviceId,
|
||||
EmergencyStopTarget target,
|
||||
) async {
|
||||
try {
|
||||
await remote.request(deviceId, target);
|
||||
return const Right(unit);
|
||||
} on EmergencyStopRemoteException catch (error) {
|
||||
return Left(
|
||||
error.rejected
|
||||
? EmergencyStopRejectedFailure(error.message)
|
||||
: ServerFailure(error.message),
|
||||
);
|
||||
} on DioException catch (error) {
|
||||
final status = error.response?.statusCode;
|
||||
if (status == 401 || status == 403) {
|
||||
return Left(EmergencyStopRejectedFailure('登录已失效或无权执行急停操作'));
|
||||
}
|
||||
if (error.type == DioExceptionType.connectionTimeout ||
|
||||
error.type == DioExceptionType.sendTimeout ||
|
||||
error.type == DioExceptionType.receiveTimeout) {
|
||||
return Left(NetworkFailure('急停请求超时,执行结果未确认'));
|
||||
}
|
||||
if (error.response != null) {
|
||||
return Left(ServerFailure('急停接口请求失败'));
|
||||
}
|
||||
return Left(NetworkFailure('急停请求连接异常,执行结果未确认'));
|
||||
} catch (_) {
|
||||
return Left(Failure('急停请求异常,执行结果未确认'));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import '../../../../core/error/failure.dart';
|
||||
|
||||
/// 后端明确返回业务失败,区别于断线/超时导致的结果未知。
|
||||
class EmergencyStopRejectedFailure extends ServerFailure {
|
||||
EmergencyStopRejectedFailure(super.message);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/// 请求目标,不代表设备已经执行;实际急停状态仍以设备反馈为准。
|
||||
enum EmergencyStopTarget {
|
||||
engage(1),
|
||||
release(0);
|
||||
|
||||
const EmergencyStopTarget(this.value);
|
||||
final int value;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../entities/emergency_stop_target.dart';
|
||||
|
||||
abstract class DeviceEmergencyStopRepository {
|
||||
/// 成功仅表示接口接受请求,不确认车辆实际急停或解除。
|
||||
Future<Either<Failure, Unit>> request(
|
||||
String deviceId,
|
||||
EmergencyStopTarget target,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
|
||||
import '../../../../core/error/failure.dart';
|
||||
import '../entities/emergency_stop_target.dart';
|
||||
import '../repositories/device_emergency_stop_repository.dart';
|
||||
|
||||
class RequestDeviceEmergencyStopUseCase {
|
||||
const RequestDeviceEmergencyStopUseCase(this.repository);
|
||||
final DeviceEmergencyStopRepository repository;
|
||||
|
||||
Future<Either<Failure, Unit>> call(
|
||||
String deviceId,
|
||||
EmergencyStopTarget target,
|
||||
) {
|
||||
final id = deviceId.trim();
|
||||
if (id.isEmpty) return Future.value(Left(Failure('设备标识不能为空')));
|
||||
return repository.request(id, target);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user