对接远程遥控
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
@@ -19,11 +18,15 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
final TcpClient _tcpClient;
|
||||
final DiffSteerUseCase _diffSteer;
|
||||
final RemoteHttpDatasource _remoteHttp;
|
||||
final RemoteTcpDatasource _remoteTcp;
|
||||
final RemoteTcpDatasource _remoteTcp;
|
||||
final ILoggerService _logger = GetIt.I<ILoggerService>();
|
||||
|
||||
|
||||
RemoteControlRepositoryImpl(this._tcpClient, this._diffSteer, this._remoteHttp, this._remoteTcp);
|
||||
RemoteControlRepositoryImpl(
|
||||
this._tcpClient,
|
||||
this._diffSteer,
|
||||
this._remoteHttp,
|
||||
this._remoteTcp,
|
||||
);
|
||||
|
||||
@override
|
||||
void changeWebViewDirection(String direction) {
|
||||
@@ -34,7 +37,9 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
void sendControlMachineCmd(MachineControlStatusEntity status) {
|
||||
// 1. 调用算法:将摇杆坐标 (x, y) 转换为左右轮电机转速
|
||||
final speeds = _diffSteer.calculate(status.originX, status.originY);
|
||||
_logger.logWithLevel('⚙️ [电机速度] left: ${speeds['left']}, right: ${speeds['right']}');
|
||||
_logger.logWithLevel(
|
||||
'⚙️ [电机速度] left: ${speeds['left']}, right: ${speeds['right']}',
|
||||
);
|
||||
|
||||
// 2. 调用 Codec:生成协议要求的 8 字节 Payload
|
||||
final payload = MachineProtocolCodec.encodeRemoteControlPayload(
|
||||
@@ -45,41 +50,104 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
ignition: status.ignitionStatus,
|
||||
emergency: status.isEmergency ? 1 : 0,
|
||||
);
|
||||
|
||||
|
||||
// 3. 调用 TcpClient 发送指令
|
||||
_tcpClient.sendRaw(
|
||||
MachineProtocolConstants.cmdRemoteControl,
|
||||
payload,
|
||||
);
|
||||
_tcpClient.sendRaw(MachineProtocolConstants.cmdRemoteControl, payload);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<RawPacket> get responseStream => _tcpClient.packetStream;
|
||||
|
||||
@override
|
||||
Future<bool> requestControlPermission( String deviceName, String deviceId) async {
|
||||
void sendTcpPermissionRequest(String deviceName) {
|
||||
_remoteTcp.sendSwitchControlRequest(deviceName);
|
||||
_logger.logWithLevel('🔑 [RemoteControl] TCP已发送权限请求');
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> requestControlPermission(
|
||||
String deviceName,
|
||||
String deviceId,
|
||||
) async {
|
||||
try {
|
||||
_remoteTcp.sendSwitchControlRequest(deviceName);
|
||||
// debugPrint('🔑 [RemoteControl] TCP已发送权限请求');
|
||||
_logger.logWithLevel('🔑 [RemoteControl] TCP已发送权限请求');
|
||||
return await _remoteHttp.requestRemoteControlViaHttp(deviceName, deviceId);
|
||||
// 1. 先发送 HTTP 权限请求检查权限
|
||||
_logger.logWithLevel('🔑 [RemoteControl] 检查 HTTP 权限...');
|
||||
final result = await _remoteHttp.requestRemoteControlViaHttp(
|
||||
deviceName,
|
||||
deviceId,
|
||||
);
|
||||
|
||||
// 2. 如果没有权限或权限为 null,才发送 TCP 请求
|
||||
final hasPermission = result['hasPermission'] as bool? ?? false;
|
||||
if (!hasPermission) {
|
||||
_logger.logWithLevel('🔑 [RemoteControl] 无权限,发送 TCP 权限请求');
|
||||
_remoteTcp.sendSwitchControlRequest(deviceName);
|
||||
} else {
|
||||
_logger.logWithLevel('✅ [RemoteControl] 已有权限,无需 TCP 请求');
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
//sdebugPrint('❌ [RemoteControl] 权限请求失败:$e');
|
||||
_logger.logWithLevel('❌ [RemoteControl] 权限请求失败:$e');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void respondPermission(bool bool, String deviceId) {
|
||||
// TODO: implement respondPermission
|
||||
if (bool) {
|
||||
_remoteTcp.sendSwitchControlResponse(true, deviceId);
|
||||
} else {
|
||||
_remoteTcp.sendSwitchControlResponse(false, deviceId);
|
||||
void respondPermission(bool agreed, String deviceId) {
|
||||
final timePrefix = DateTime.now().toIso8601String();
|
||||
final logPrefix = '$timePrefix 📤 [RemoteControlRepository]';
|
||||
|
||||
debugPrint('$logPrefix =========================================');
|
||||
debugPrint('$logPrefix respondPermission 被调用');
|
||||
debugPrint('$logPrefix agreed: $agreed, deviceId: $deviceId');
|
||||
_logger.logWithLevel(
|
||||
'$logPrefix respondPermission 被调用 - agreed=$agreed, deviceId=$deviceId',
|
||||
shouldLog: true,
|
||||
);
|
||||
|
||||
try {
|
||||
// 🔥 检查 _remoteTcp 是否为空
|
||||
if (_remoteTcp == null) {
|
||||
debugPrint('$logPrefix ❌ _remoteTcp 为空,无法发送 TCP 指令');
|
||||
_logger.logWithLevel('$logPrefix ❌ _remoteTcp 为空', shouldLog: true);
|
||||
throw Exception('_remoteTcp 为空');
|
||||
}
|
||||
|
||||
// 🔥 检查 TCP 连接状态
|
||||
debugPrint('$logPrefix 📡 TCP连接状态: ${_remoteTcp.isConnected}');
|
||||
_logger.logWithLevel(
|
||||
'$logPrefix 📡 TCP连接状态: ${_remoteTcp.isConnected}',
|
||||
shouldLog: true,
|
||||
);
|
||||
|
||||
if (agreed) {
|
||||
debugPrint('$logPrefix ✅ 用户同意释放权限,发送同意响应');
|
||||
_logger.logWithLevel('$logPrefix ✅ 准备发送同意响应 TCP 指令', shouldLog: true);
|
||||
_remoteTcp.sendSwitchControlResponse(true, deviceId);
|
||||
} else {
|
||||
debugPrint('$logPrefix ❌ 用户拒绝释放权限,发送拒绝响应');
|
||||
_logger.logWithLevel('$logPrefix ❌ 准备发送拒绝响应 TCP 指令', shouldLog: true);
|
||||
_remoteTcp.sendSwitchControlResponse(false, deviceId);
|
||||
}
|
||||
|
||||
debugPrint('$logPrefix ✅ TCP 响应发送完成');
|
||||
_logger.logWithLevel('$logPrefix ✅ TCP 响应发送完成', shouldLog: true);
|
||||
} catch (e) {
|
||||
debugPrint('$logPrefix ❌ respondPermission 失败: $e');
|
||||
debugPrint('$logPrefix ❌ 错误类型: ${e.runtimeType}');
|
||||
_logger.logWithLevel(
|
||||
'$logPrefix ❌ respondPermission 失败: $e',
|
||||
shouldLog: true,
|
||||
);
|
||||
// 🔥 关键修复:重新抛出异常,让上层知道 TCP 发送失败了
|
||||
rethrow;
|
||||
}
|
||||
|
||||
debugPrint('$logPrefix =========================================');
|
||||
}
|
||||
///app退出远程控制后释放权限
|
||||
|
||||
///app退出远程控制后释放权限
|
||||
@override
|
||||
Future<bool> releasePermission(String platform) async {
|
||||
return await _remoteHttp.releaseRemoteControlViaHttp(platform);
|
||||
|
||||
Reference in New Issue
Block a user