Files
flutterApp/lib/components/control_mode_text.dart
2026-11-06 08:34:19 +08:00

57 lines
2.4 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
/// 控制模式(`RunningStatusEntity.controlMode`)→ 界面文案的唯一映射。
///
/// 为什么要单独一个文件:这个值以前在 7 个页面各写一遍「== 3 ? 远程 : 本地」,
/// 而数据层又把「后端没给」的默认值写成 `'0'`,两个错误叠在一起,结果是
/// 设备从来没上报过控制模式时,界面一本正经地显示「本地控制」——假信号。
///
/// 协议定义(2026-11 后端重新确认,覆盖早先「0=无控制 / 2=蓝牙 / 4=其他接口」那版):
/// 空串 / null → 「未知」:后端根本没给这个字段
/// 0 → 未知
/// 1 → 本地控制
/// 2 → 自主任务
/// 3 → 远程控制(远程遥控)
/// 其它数字 → 「未知(6)」:协议外的值,不猜它是什么意思
/// 非数字字符串 → 原样显示:后端给什么展示什么
///
/// [controlModeKey] 只返回已定义的 0~4,其余为 null,供各页面沿用本页的
/// 翻译器与自定义文案接;需要一步拿到最终文本用 [controlModeDisplayText]。
/// 已定义的协议值 → 多语言 key;空值、非数字、协议外数字返回 null。
String? controlModeKey(String? raw) {
final int? n = controlModeValue(raw);
if (n == null) return null;
return switch (n) {
0 => 'common.unknown',
1 => 'running_status.local_control',
2 => 'running_status.autonomous_task',
3 => 'running_status.remote_control',
_ => null,
};
}
/// 原始值 → 协议数值。空串/非数字返回 null,表示「没有可信的控制模式数据」。
int? controlModeValue(String? raw) {
final String v = (raw ?? '').trim();
if (v.isEmpty) return null;
return int.tryParse(v);
}
/// 直接得到可显示文案(含未知/协议外/非数字的兜底)。
String controlModeDisplayText(AppLocalizations l10n, String? raw) {
final String? key = controlModeKey(raw);
if (key != null) return l10n.translate(key);
final String v = (raw ?? '').trim();
if (v.isEmpty) return l10n.translate('common.unknown');
final int? n = int.tryParse(v);
// 协议外的数字:带上原值,现场排查时能看出后端到底发了什么
if (n != null) {
return '${l10n.translate('common.unknown')}($n)';
}
// 后端给的不是数字:按它给的显示,不做任何猜测
return v;
}