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

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

@@ -1,5 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
import '../../../remote_control/presentation/bloc/remote_control_cubit.dart';
import '../../../device_control_mode/presentation/widgets/mode_independent_control.dart';
import '../theme/pv_colors.dart';
@@ -48,9 +51,7 @@ class PvControlBar extends StatelessWidget {
onTap: onPauseTask,
),
),
Expanded(
child: PvEmergencyStopButton(onTap: onEmergencyStop),
),
Expanded(child: PvEmergencyStopButton(onTap: onEmergencyStop)),
Expanded(
child: _ControlButton(
icon: Icons.videocam,
@@ -160,9 +161,9 @@ class _PvEmergencyStopButtonState extends State<PvEmergencyStopButton>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
/// 是否处于「急停中」:默认 false(正常态、不呼吸)。
/// 点击一次进入急停(呼吸 + 「急停中」提示),再点一次解除恢复正常。
bool _active = false;
bool get _active =>
context.read<RemoteControlCubit>().state.isEmergency &&
context.read<RemoteControlCubit>().state.hasEmergencyStatus;
@override
void initState() {
@@ -179,19 +180,30 @@ class _PvEmergencyStopButtonState extends State<PvEmergencyStopButton>
super.dispose();
}
void _handleTap() {
setState(() => _active = !_active);
if (_active) {
@override
void didChangeDependencies() {
super.didChangeDependencies();
final state = context.watch<RemoteControlCubit>().state;
final active = state.isEmergency && state.hasEmergencyStatus;
if (active && !_controller.isAnimating) {
_controller.repeat(reverse: true);
} else {
} else if (!active && _controller.isAnimating) {
_controller.stop();
_controller.value = 0;
}
}
void _handleTap() {
if (context.read<RemoteControlCubit>().state.isEmergencyRequestPending)
return;
widget.onTap();
}
@override
Widget build(BuildContext context) {
Widget build(BuildContext context) =>
ModeIndependentControl(child: _buildButton(context));
Widget _buildButton(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, child) {
@@ -214,7 +226,9 @@ class _PvEmergencyStopButtonState extends State<PvEmergencyStopButton>
),
boxShadow: [
BoxShadow(
color: PvColors.danger.withValues(alpha: 0.5 + 0.35 * t),
color: PvColors.danger.withValues(
alpha: 0.5 + 0.35 * t,
),
blurRadius: 18 + 12 * t,
spreadRadius: 2 + 3 * t,
),
@@ -232,9 +246,11 @@ class _PvEmergencyStopButtonState extends State<PvEmergencyStopButton>
fit: BoxFit.scaleDown,
child: Text(
AppLocalizations.of(context).translate(
_active
? 'pv_control.emergency_stopping'
: 'pv_control.emergency_stop',
!context.read<RemoteControlCubit>().state.hasEmergencyStatus
? 'remote_control.emergency_unknown'
: _active
? 'remote_control.emergency_stopping'
: 'remote_control.emergency_inactive',
),
maxLines: 1,
textAlign: TextAlign.center,

View File

@@ -38,8 +38,9 @@ class PvInfoColumn extends StatelessWidget {
label: AppLocalizations.of(
context,
).translate('pv_control.current_speed'),
value: telemetry.speed.toStringAsFixed(1),
unit: 'm/s',
value: telemetry.speed?.toStringAsFixed(2) ??
AppLocalizations.of(context).translate('pv_control.unknown'),
unit: telemetry.speed == null ? '' : 'm/s',
)),
const SizedBox(width: 6),
Expanded(child: _MetricCard.icon(

View File

@@ -3,12 +3,14 @@ import 'dart:math' as math;
import 'package:cc_ui_kit/cc_ui_kit.dart';
import 'package:flutter/material.dart';
import 'package:maibu_satabot_v2/features/device_control_mode/presentation/widgets/mode_independent_control.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:maibu_satabot_v2/components/capsule_toast.dart';
import 'package:maibu_satabot_v2/components/control_mode_text.dart';
import 'package:maibu_satabot_v2/core/di/injection.dart';
import 'package:maibu_satabot_v2/core/logging/remote_control_diagnostics.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
import 'package:maibu_satabot_v2/features/remote_control/domain/entities/reverse_camera_data.dart';
import 'package:maibu_satabot_v2/features/remote_control/presentation/bloc/remote_control_cubit.dart';
@@ -22,6 +24,8 @@ import '../models/pv_video_source.dart';
import '../theme/pv_colors.dart';
import '../theme/pv_hud_theme.dart';
import '../channel/pv_robot_command_channel.dart';
import '../../../remote_control/presentation/widgets/remote_control_link_feedback.dart';
import '../../../remote_control/presentation/widgets/permission_request_feedback.dart';
import 'pv_control_bar.dart';
import 'pv_rear_view_card.dart';
import 'pv_local_rtsp_video.dart';
@@ -53,7 +57,7 @@ typedef _PvSheetSpec = ({
/// 计算缩放系数 [_sc],手机横屏(高约 360 → _sc≈0.64)时顶栏占比、字号、按钮
/// 全部等比缩小,平板(高约 600 → _sc≈1.07)时放大,从而在两种设备上还原版式。
///
/// AR 障碍框、遥测数值为写死的假数据;控制指令(摇杆 / 割刀 / 底盘 / 点火 / 急停)
/// 状态卡由实时遥测驱动;控制指令(摇杆 / 割刀 / 底盘 / 点火 / 急停)
/// 已通过 [PvRobotCommandChannel] 接入(TCP 实现委托远程遥控 Cubit,见 pv_robot_command_channel.dart)。
class PvManualControlView extends StatefulWidget {
const PvManualControlView({
@@ -615,6 +619,10 @@ class _PvManualControlViewState extends State<PvManualControlView>
SizedBox(width: d(8)),
// 「正在控制 / 未在控制」状态胶囊(与远程遥控顶栏同款、同逻辑)
_buildControlStatusChip(),
RemoteControlHeartbeatIndicator(
heartbeats: sl<RemoteControlCubit>().serverHeartbeats,
size: d(9),
),
// 中:链路信息(FittedBox 保证窄屏自动缩小不溢出)
Expanded(
child: Padding(
@@ -686,10 +694,12 @@ class _PvManualControlViewState extends State<PvManualControlView>
active: _showSidePips,
onTap: _toggleSidePips,
),
_navItem(
Icons.settings_outlined,
tr.translate('pv_control.nav_settings'),
onTap: widget.onSettings,
ModeIndependentControl(
child: _navItem(
Icons.settings_outlined,
tr.translate('pv_control.nav_settings'),
onTap: widget.onSettings,
),
),
SizedBox(width: d(4)),
],
@@ -752,17 +762,18 @@ class _PvManualControlViewState extends State<PvManualControlView>
initialData: cubit.state,
stream: cubit.stream,
builder: (context, snapshot) {
final hasPermission = snapshot.data?.hasPermission ?? false;
final tr = AppLocalizations.of(context);
final color = hasPermission
? PvHudColors.success
: PvHudColors.danger;
final permissionState = snapshot.data ?? cubit.state;
final hasPermission = permissionState.hasPermission;
final busy = permissionState.isPermissionRequestBusy;
final color = busy
? Colors.orange
: hasPermission ? PvHudColors.success : PvHudColors.danger;
final chip = Container(
padding: EdgeInsets.symmetric(horizontal: d(10), vertical: d(3)),
decoration: BoxDecoration(
color: hasPermission
? PvHudColors.successBg
: PvHudColors.dangerBg,
color: busy
? Colors.orange.withValues(alpha: 0.12)
: hasPermission ? PvHudColors.successBg : PvHudColors.dangerBg,
borderRadius: BorderRadius.circular(d(20)),
),
child: Row(
@@ -775,9 +786,7 @@ class _PvManualControlViewState extends State<PvManualControlView>
),
SizedBox(width: d(4)),
Text(
hasPermission
? tr.translate('remote_control.controlling')
: tr.translate('remote_control.not_controlling'),
permissionStatusText(context, permissionState),
maxLines: 1,
style: s(PvHudType.caption, 11).copyWith(
color: color,
@@ -787,19 +796,16 @@ class _PvManualControlViewState extends State<PvManualControlView>
],
),
);
return GestureDetector(
return PermissionRequestFeedback(
cubit: cubit,
child: Tooltip(
message: permissionStatusHint(context, permissionState),
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
// 已在控制:无需重复抢占。
if (hasPermission) return;
final targetDevice = cubit.state.targetDevice;
if (targetDevice == null) {
debugPrint('❌ [PV遥控] targetDevice 为空,无法请求权限');
return;
}
debugPrint('🔑 [PV遥控] 👆 点击“未在控制”,请求抢占控制权');
if (hasPermission || busy) return;
cubit.requestControlPermissionS(
targetDevice.deviceName,
cubit.state.targetDevice?.deviceName ?? '',
'app',
source: '手动点击',
grabControl: true,
@@ -816,6 +822,8 @@ class _PvManualControlViewState extends State<PvManualControlView>
),
child: chip,
),
),
),
);
},
);
@@ -1348,12 +1356,26 @@ class _PvManualControlViewState extends State<PvManualControlView>
);
}
/// 状态卡:图标 + 标签 + 数值的竖排列表(设计稿左栏第二张卡)。
Widget _statusCard() {
final tr = AppLocalizations.of(context);
final telemetry = widget.telemetry;
final unknown = tr.translate('pv_control.unknown');
String speedText(double? value, String unit) =>
value == null ? unknown : '${value.toStringAsFixed(2)} $unit';
final motionKey = telemetry.emergencyStop == true
? 'pv_control.emergency_stopping'
: switch (telemetry.motion) {
'moving' => 'pv_control.status_moving',
'stopped' => 'pv_control.status_stopped',
'blocked' => 'pv_control.status_blocked',
_ => 'pv_control.unknown',
};
final motionDanger = telemetry.emergencyStop == true ||
telemetry.motion == 'blocked';
final mowerRunning = telemetry.mowerRpm != null && telemetry.mowerRpm != 0;
final batteryText = widget.telemetry.battery == null
? tr.translate('pv_control.unknown')
: '${widget.telemetry.battery!.toStringAsFixed(0)}% (5.6 h)';
? unknown
: '${widget.telemetry.battery!.toStringAsFixed(0)}%';
final fixQuality = widget.telemetry.fixStatus == 4;
return _panel(
padding: EdgeInsets.symmetric(horizontal: d(10), vertical: d(6)),
@@ -1362,30 +1384,59 @@ class _PvManualControlViewState extends State<PvManualControlView>
_statusRow(
Icons.directions_car_filled_outlined,
tr.translate('pv_control.vehicle_status'),
tr.translate('pv_control.status_normal'),
valueColor: PvHudColors.success,
capsule: true,
tr.translate(motionKey),
valueColor: motionDanger
? PvHudColors.danger
: motionKey == 'pv_control.unknown'
? PvHudColors.textSecondary
: PvHudColors.success,
),
_statusRow(
Icons.speed,
tr.translate('pv_control.drive_speed'),
'1.2 m/s',
speedText(telemetry.speed, 'm/s'),
),
_statusRow(
Icons.speed,
tr.translate('pv_control.left_measured_rpm'),
speedText(telemetry.leftMeasuredRpm, 'rpm'),
),
_statusRow(
Icons.speed,
tr.translate('pv_control.right_measured_rpm'),
speedText(telemetry.rightMeasuredRpm, 'rpm'),
),
_statusRow(
Icons.speed,
tr.translate('pv_control.left_target_rpm'),
speedText(telemetry.leftTargetRpm, 'rpm'),
),
_statusRow(
Icons.speed,
tr.translate('pv_control.right_target_rpm'),
speedText(telemetry.rightTargetRpm, 'rpm'),
),
_statusRow(
Icons.settings_outlined,
tr.translate('pv_control.gear'),
tr.translate('pv_control.gear_low'),
unknown,
),
_statusRow(
Icons.content_cut,
tr.translate('pv_control.mower_status'),
tr.translate('pv_control.mower_running'),
valueColor: PvHudColors.success,
telemetry.mowerRpm == null
? unknown
: tr.translate(mowerRunning
? 'pv_control.mower_running'
: 'pv_control.mower_stopped'),
valueColor: mowerRunning
? PvHudColors.success
: PvHudColors.textSecondary,
),
_statusRow(
Icons.height,
tr.translate('pv_control.chassis_height'),
'120 mm',
unknown,
),
_statusRow(
Icons.battery_5_bar,
@@ -1684,7 +1735,9 @@ class _PvManualControlViewState extends State<PvManualControlView>
child: CCJoystick(
radius: radius,
axisHint: AxisHint.forwardBackward,
onDiagnostic: (event, fields) => RemoteControlDiagnostics.instance.event('joystick_' + event, fields),
skin: _joystickSkin(),
onPress: () => RemoteControlLinkNotice.showIfDisconnected(context),
onValueChanged: (v) {
final int y = v.y.toInt();
if (y == _lastDriveY) return;
@@ -1705,7 +1758,9 @@ class _PvManualControlViewState extends State<PvManualControlView>
child: CCJoystick(
radius: radius,
axisHint: AxisHint.leftRight,
onDiagnostic: (event, fields) => RemoteControlDiagnostics.instance.event('joystick_' + event, fields),
skin: _joystickSkin(),
onPress: () => RemoteControlLinkNotice.showIfDisconnected(context),
onValueChanged: (v) {
final int x = v.x.toInt().abs() < 1 ? 0 : v.x.toInt();
if (x == _lastDriveX) return;
@@ -1931,7 +1986,7 @@ class _PvManualControlViewState extends State<PvManualControlView>
mid: 'assets/svgs/remote_scissors.svg',
down: 'assets/svgs/remote_down.svg',
segUp: 'pv_control.seg_mower_up',
segMid: 'pv_control.seg_stop',
segMid: 'pv_control.seg_mower_idle',
segDown: 'pv_control.seg_mower_down',
turns: 0,
),
@@ -2065,6 +2120,7 @@ class _PvManualControlViewState extends State<PvManualControlView>
/// 长按起手:给一下中等强度震动做确认(手指按在按钮上就触发了,无反馈很
/// 容易被当成没响应),并亮气泡、从中位(停)开始。
void _onScrubStart(String sheetKey) {
RemoteControlLinkNotice.showIfDisconnected(context);
_sheetCloseTimer?.cancel();
_sheetCloseTimer = null;
HapticFeedback.mediumImpact();