接入设备故障告警和场站实时任务进度

This commit is contained in:
2026-11-21 14:43:24 +08:00
parent 9a8a42a0a1
commit a37c448391
53 changed files with 5033 additions and 489 deletions

View File

@@ -1,5 +1,9 @@
import 'dart:async';
import 'dart:math' as math;
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:maibu_satabot_v2/features/autonomous_work/data/site_device_realtime_subscriptions.dart';
import 'package:maibu_satabot_v2/features/v2/site/presentation/cubit/site_cubit.dart';
import 'pv_task_progress_content.dart';
import 'package:cc_ui_kit/cc_ui_kit.dart';
import 'package:flutter/material.dart';
@@ -1247,114 +1251,18 @@ class _PvManualControlViewState extends State<PvManualControlView>
);
}
/// 作业进度卡(浅色版):进度条单独一行,面积与时长两列在下一行,
/// 窄栏也不会因为三列硬排而拥挤。
Widget _progressCard() {
final tr = AppLocalizations.of(context);
return _panel(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(
child: Text(
tr.translate('pv_control.work_progress'),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: s(PvHudType.label, 11).copyWith(
color: PvHudColors.textSecondary,
),
),
),
Text(
'68%',
style: s(PvHudType.value, 12).copyWith(
color: PvHudColors.textPrimary,
),
),
],
),
SizedBox(height: d(5)),
ClipRRect(
borderRadius: BorderRadius.circular(d(3)),
child: LinearProgressIndicator(
value: 0.68,
minHeight: d(5),
backgroundColor: PvHudColors.pill,
valueColor: const AlwaysStoppedAnimation(PvHudColors.success),
),
),
SizedBox(height: d(8)),
Row(
children: [
Expanded(
child: _progressItem(
tr.translate('pv_control.work_area_size'),
'2.5',
unit: '/ 3.6 ha',
),
),
Container(
width: 1,
height: d(24),
color: PvHudColors.border,
),
Expanded(
child: _progressItem(
tr.translate('pv_control.work_duration'),
'01:42:18',
),
),
],
),
],
/// 作业进度来自场站共享任务状态,与自主作业使用同一数据源。
Widget _progressCard() => _panel(
child: BlocBuilder<SiteCubit, SiteState>(
bloc: sl<SiteCubit>(),
builder: (context, site) => PvTaskProgressContent(
subscriptions: sl<SiteDeviceRealtimeSubscriptions>(),
siteId: site.selectedSite?.id,
deviceSn: widget.deviceSn,
scale: _sc,
),
);
}
Widget _progressItem(String label, String value, {String? unit}) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: s(PvHudType.caption, 10).copyWith(
color: PvHudColors.textTertiary,
),
),
SizedBox(height: d(2)),
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
mainAxisSize: MainAxisSize.min,
children: [
Text(
value,
style: s(PvHudType.value, 12).copyWith(
color: PvHudColors.textPrimary,
),
),
if (unit != null) ...[
SizedBox(width: d(2)),
Flexible(
child: Text(
unit,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: s(PvHudType.caption, 10).copyWith(
color: PvHudColors.textTertiary,
),
),
),
],
],
),
],
);
}
),
);
Widget _statusCard() {
final tr = AppLocalizations.of(context);

View File

@@ -0,0 +1,201 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
import 'package:maibu_satabot_v2/core/network/mqtt/domain/entities/mower_task_status_entity.dart';
import 'package:maibu_satabot_v2/features/autonomous_work/data/site_device_realtime_subscriptions.dart';
import '../theme/pv_hud_theme.dart';
/// 遥控 HUD 的只读作业进度,复用场站订阅,不参与控制指令发送。
class PvTaskProgressContent extends StatefulWidget {
const PvTaskProgressContent({
super.key,
required this.subscriptions,
required this.siteId,
required this.deviceSn,
this.scale = 1,
});
final SiteDeviceRealtimeSubscriptions subscriptions;
final int? siteId;
final String deviceSn;
final double scale;
@override
State<PvTaskProgressContent> createState() => _PvTaskProgressContentState();
}
class _PvTaskProgressContentState extends State<PvTaskProgressContent> {
Stream<MowerTaskStatusEntity?>? _stream;
Timer? _clock;
double d(double value) => value * widget.scale;
TextStyle style(TextStyle base, double size) =>
base.copyWith(fontSize: d(size).clamp(8.0, 24.0));
@override
void initState() {
super.initState();
_bind();
// 只刷新这张小卡片的上报时间,不带动视频和摇杆重建。
_clock = Timer.periodic(const Duration(seconds: 1), (_) {
if (mounted) setState(() {});
});
}
void _bind() {
_stream = widget.deviceSn.trim().isEmpty
? null
: widget.subscriptions.watchTaskStatus(
siteId: widget.siteId,
deviceSn: widget.deviceSn,
);
}
@override
void didUpdateWidget(covariant PvTaskProgressContent oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.subscriptions != widget.subscriptions ||
oldWidget.siteId != widget.siteId ||
oldWidget.deviceSn != widget.deviceSn) {
_bind();
}
}
@override
void dispose() {
_clock?.cancel();
super.dispose();
}
String _duration(double seconds) {
final total = seconds.floor();
String pad(int value) => value.toString().padLeft(2, '0');
return '${pad(total ~/ 3600)}:${pad((total ~/ 60) % 60)}:${pad(total % 60)}';
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
final unknown = l10n.translate('common.unknown');
String taskLabel(String key) =>
l10n.translate('autonomous_work.task_status.$key');
return StreamBuilder<MowerTaskStatusEntity?>(
// 切换设备或场站时直接清空旧快照,不能显示上一台设备的数据。
key: ValueKey((widget.siteId, widget.deviceSn, widget.subscriptions)),
stream: _stream,
builder: (context, snapshot) {
final report = snapshot.data;
final progress = report?.progress;
final age = report == null
? null
: DateTime.now()
.difference(report.receivedAt)
.inSeconds
.clamp(0, 999999);
final stale = age != null && age >= 15;
final warning = snapshot.hasError || stale;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(
child: Text(
l10n.translate('pv_control.work_progress'),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: style(PvHudType.label, 11),
),
),
const SizedBox(width: 4),
Text(
progress == null
? unknown
: '${(progress * 100).toStringAsFixed(2)}%',
style: style(PvHudType.value, 12),
),
],
),
SizedBox(height: d(5)),
ClipRRect(
borderRadius: BorderRadius.circular(d(3)),
child: LinearProgressIndicator(
value: progress ?? 0,
minHeight: d(5),
backgroundColor: PvHudColors.pill,
color: warning || progress == null
? PvHudColors.textTertiary
: PvHudColors.success,
),
),
SizedBox(height: d(8)),
_pair(
// 当前协议没有面积字段;不按距离/进度推算或复用演示数据。
(l10n.translate('pv_control.work_area_size'), unknown),
(
l10n.translate('pv_control.work_duration'),
report?.elapsedSeconds == null
? unknown
: _duration(report!.elapsedSeconds!),
),
),
SizedBox(height: d(8)),
_pair(
(
taskLabel('eta_s'),
report?.etaSeconds == null
? unknown
: _duration(report!.etaSeconds!),
),
(
taskLabel('distance_remaining'),
report?.distanceRemaining == null
? unknown
: '${report!.distanceRemaining!.toStringAsFixed(2)} m',
),
),
SizedBox(height: d(6)),
Text(
snapshot.hasError
? taskLabel('unavailable')
: report == null
? taskLabel('waiting')
: stale
? taskLabel('stale')
: l10n.translate('autonomous_work.task_status.updated', {
'seconds': age,
}),
style: style(PvHudType.caption, 9).copyWith(
color: warning ? PvHudColors.danger : PvHudColors.textTertiary,
),
),
],
);
},
);
}
Widget _pair((String, String) left, (String, String) right) {
Widget item((String, String) value) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(value.$1, style: style(PvHudType.caption, 10)),
SizedBox(height: d(2)),
Text(value.$2, style: style(PvHudType.value, 12)),
],
);
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(child: item(left)),
SizedBox(width: d(8)),
Container(width: 1, height: d(24), color: PvHudColors.border),
SizedBox(width: d(8)),
Expanded(child: item(right)),
],
);
}
}