完善遥控权限、控制发送和急停反馈
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
import '../../../../core/logging/i_logger_service.dart';
|
||||
import '../../../../core/logging/remote_control_diagnostics.dart';
|
||||
import '../../../../core/network/protocol_decoder.dart';
|
||||
import '../../../../core/network/tcp/tcp_client.dart';
|
||||
import '../../../../core/protocol/machine_protocol_codec.dart';
|
||||
@@ -34,10 +36,14 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
}
|
||||
|
||||
@override
|
||||
bool sendControlMachineCmd(MachineControlStatusEntity status) {
|
||||
// 🔥 整个“换算 + 编码 + 写 socket”都包在 try 里:本方法被 200ms 控制循环
|
||||
Future<bool> sendControlMachineCmd(
|
||||
MachineControlStatusEntity status, {
|
||||
bool priority = false,
|
||||
bool Function()? isCurrent,
|
||||
}) async {
|
||||
// 🔥 整个“换算 + 编码 + 写 socket”都包在 try 里:本方法被 250ms 控制循环
|
||||
// 和停止风暴(一次 30 帧)高频调用,任何一帧抛异常都会变成 Timer 回调
|
||||
// 里的未捕获异常(每 200ms 刷一次),更糟的是会让 stopAllMovement 的
|
||||
// 里的未捕获异常(每 250ms 刷一次),更糟的是会让 stopAllMovement 的
|
||||
// 停止帧中途断掉——车就停不下来。失败一律返回 false,由调用方统计
|
||||
// “一共出网了几帧”(控制循环里本来就不关心返回值)。
|
||||
try {
|
||||
@@ -56,6 +62,26 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
emergency: status.isEmergency ? 1 : 0,
|
||||
);
|
||||
|
||||
try {
|
||||
final encoded = ByteData.sublistView(payload);
|
||||
RemoteControlDiagnostics.instance.event('conversion', {
|
||||
'x': status.originX,
|
||||
'y': status.originY,
|
||||
'linearMps': linear,
|
||||
'angularRadps': angular,
|
||||
'encodedLinearMps': encoded.getFloat64(0, Endian.little),
|
||||
'encodedAngularRadps': encoded.getFloat64(8, Endian.little),
|
||||
'payloadBytes': payload.length,
|
||||
'payloadHex': _hex(payload),
|
||||
'lift': status.chassisLift,
|
||||
'mower': status.mowerSpeed,
|
||||
'ignition': status.ignitionStatus,
|
||||
'emergency': status.isEmergency,
|
||||
});
|
||||
} catch (_) {
|
||||
// Observing the encoded payload cannot prevent its transmission.
|
||||
}
|
||||
|
||||
// 联调日志:三段式打印 ①滑块/摇杆原始数据 ②乘完系数的发送值 ③包装后的整帧字节(联调结束可移除)
|
||||
_logger.logWithLevel(
|
||||
'🔗 [联调-0x00] 遥控发送明细\n'
|
||||
@@ -73,9 +99,10 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
// 3. 调用 TcpClient 发送指令
|
||||
// 🔥 把写入结果如实上抛:false 代表本帧没交给系统 socket(未连接/写入异常),
|
||||
// 调用方(尤其 stopAllMovement 的停止风暴)需要知道“发出去了几个”。
|
||||
return _tcpClient.sendRaw(
|
||||
MachineProtocolConstants.cmdRemoteControl,
|
||||
return await _tcpClient.sendControlPayload(
|
||||
payload,
|
||||
priority: priority,
|
||||
isCurrent: isCurrent,
|
||||
);
|
||||
} catch (e, s) {
|
||||
debugPrint('❌ [RemoteControl] 遥控指令构造/发送异常: $e');
|
||||
@@ -96,63 +123,19 @@ class RemoteControlRepositoryImpl implements RemoteControlRepository {
|
||||
Stream<RawPacket> get responseStream => _tcpClient.packetStream;
|
||||
|
||||
@override
|
||||
void sendTcpPermissionRequest(String deviceName) {
|
||||
// 🔥 RemoteTcpDatasource.sendSwitchControlRequest 在 TCP 未连接时是 throw 的,
|
||||
// 而本方法是在 requestControlPermissionS 的 result.fold(async 回调) 里调的:
|
||||
// fold 不会 await 那个回调的 Future,异常从这条路径出去就是彻底的未捕获
|
||||
// 异步异常(点“获取控制权”时断着 TCP 就会撞上)。抢占请求发不出去最坏
|
||||
// 只是等不到 0x12 回包,不该崩,故就地吞掉并留 ERROR 痕迹。
|
||||
try {
|
||||
_remoteTcp.sendSwitchControlRequest(deviceName);
|
||||
_logger.logWithLevel(
|
||||
'🔑 [RemoteControl] TCP已发送权限请求',
|
||||
shouldLog: true,
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [RemoteControl] TCP 权限请求发送失败: $e');
|
||||
_logger.logWithLevel(
|
||||
'❌ [RemoteControl] TCP 权限请求发送失败(未连接): $e',
|
||||
level: 'ERROR',
|
||||
shouldLog: true,
|
||||
);
|
||||
}
|
||||
Future<void> sendTcpPermissionRequest(
|
||||
String deviceName, {
|
||||
bool Function()? isCurrent,
|
||||
}) async {
|
||||
// Await the actual write so failures reach the request UI.
|
||||
await _remoteTcp.sendSwitchControlRequest(deviceName, isCurrent: isCurrent);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> requestControlPermission(
|
||||
String deviceName,
|
||||
String deviceId,
|
||||
) async {
|
||||
try {
|
||||
// 1. 先发送 HTTP 权限请求检查权限
|
||||
_logger.logWithLevel(
|
||||
'🔑 [RemoteControl] 检查 HTTP 权限... deviceName=$deviceName, platform=$deviceId',
|
||||
shouldLog: true,
|
||||
);
|
||||
final result = await _remoteHttp.requestRemoteControlViaHttp(
|
||||
deviceName,
|
||||
deviceId,
|
||||
);
|
||||
|
||||
// 2. 如果没有权限或权限为 null,才发送 TCP 请求
|
||||
final hasPermission = result['hasPermission'] as bool? ?? false;
|
||||
_logger.logWithLevel(
|
||||
'🔑 [RemoteControl] HTTP权限查询结果: hasPermission=$hasPermission, owner=${result['owner']}',
|
||||
shouldLog: true,
|
||||
);
|
||||
if (!hasPermission) {
|
||||
_logger.logWithLevel('🔑 [RemoteControl] 无权限,发送 TCP 权限请求', shouldLog: true);
|
||||
_remoteTcp.sendSwitchControlRequest(deviceName);
|
||||
} else {
|
||||
_logger.logWithLevel('✅ [RemoteControl] 已有权限,无需 TCP 请求', shouldLog: true);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (e) {
|
||||
_logger.logWithLevel('❌ [RemoteControl] 权限请求失败:$e', level: 'ERROR', shouldLog: true);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
) => _remoteHttp.requestRemoteControlViaHttp(deviceName, deviceId);
|
||||
|
||||
@override
|
||||
void respondPermission(bool agreed, String deviceId) {
|
||||
|
||||
Reference in New Issue
Block a user