完善遥控权限、控制发送和急停反馈

This commit is contained in:
2026-11-21 14:37:46 +08:00
parent c284aefc7d
commit 9a8a42a0a1
55 changed files with 5009 additions and 1308 deletions

View File

@@ -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;
}

View File

@@ -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('急停请求异常,执行结果未确认'));
}
}
}