接入设备故障告警和场站实时任务进度
This commit is contained in:
@@ -0,0 +1,424 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
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 'work_ui.dart';
|
||||
|
||||
/// 仅展示设备上报,不参与任务控制和任务生命周期状态的判定。
|
||||
class WorkTaskStatusCard extends StatefulWidget {
|
||||
const WorkTaskStatusCard({
|
||||
super.key,
|
||||
required this.subscriptions,
|
||||
required this.siteId,
|
||||
required this.deviceSn,
|
||||
required this.maxHeight,
|
||||
required this.onCollapse,
|
||||
this.expectedTaskId,
|
||||
this.compact = false,
|
||||
});
|
||||
|
||||
final SiteDeviceRealtimeSubscriptions subscriptions;
|
||||
final int? siteId;
|
||||
final String deviceSn;
|
||||
final String? expectedTaskId;
|
||||
final double maxHeight;
|
||||
final VoidCallback onCollapse;
|
||||
final bool compact;
|
||||
|
||||
@override
|
||||
State<WorkTaskStatusCard> createState() => _WorkTaskStatusCardState();
|
||||
}
|
||||
|
||||
class _WorkTaskStatusCardState extends State<WorkTaskStatusCard> {
|
||||
StreamSubscription<MowerTaskStatusEntity?>? _subscription;
|
||||
Timer? _clock;
|
||||
MowerTaskStatusEntity? _report;
|
||||
bool _failed = false;
|
||||
int _generation = 0;
|
||||
|
||||
AppLocalizations get _l10n => AppLocalizations.of(context);
|
||||
String get _unknown => _l10n.translate('common.unknown');
|
||||
String _label(String key) =>
|
||||
_l10n.translate('autonomous_work.task_status.$key');
|
||||
String _text(Object? value) => value?.toString() ?? _unknown;
|
||||
double get _font => widget.compact ? 10 : 12;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_listen();
|
||||
_clock = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (_report != null && mounted) setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant WorkTaskStatusCard oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.subscriptions != widget.subscriptions ||
|
||||
oldWidget.siteId != widget.siteId ||
|
||||
oldWidget.deviceSn != widget.deviceSn ||
|
||||
oldWidget.expectedTaskId != widget.expectedTaskId) {
|
||||
_report = null;
|
||||
_failed = false;
|
||||
_listen();
|
||||
}
|
||||
}
|
||||
|
||||
void _listen() {
|
||||
final generation = ++_generation;
|
||||
_subscription?.cancel();
|
||||
_subscription = widget.subscriptions
|
||||
.watchTaskStatus(siteId: widget.siteId, deviceSn: widget.deviceSn)
|
||||
.listen(
|
||||
(report) {
|
||||
if (!mounted || generation != _generation) return;
|
||||
if (report == null) {
|
||||
setState(() {
|
||||
_report = null;
|
||||
_failed = false;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (widget.expectedTaskId != null &&
|
||||
report.taskId != null &&
|
||||
report.taskId != widget.expectedTaskId) {
|
||||
return;
|
||||
}
|
||||
final previous = _report;
|
||||
if (previous != null &&
|
||||
previous.taskId == report.taskId &&
|
||||
previous.timestamp != null &&
|
||||
report.timestamp != null &&
|
||||
report.timestamp! < previous.timestamp!) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_report = report;
|
||||
_failed = false;
|
||||
});
|
||||
},
|
||||
onError: (Object error) {
|
||||
if (mounted && generation == _generation) {
|
||||
setState(() => _failed = true);
|
||||
}
|
||||
},
|
||||
onDone: () {
|
||||
if (mounted && generation == _generation) {
|
||||
setState(() => _failed = true);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_generation++;
|
||||
_clock?.cancel();
|
||||
_subscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String _number(double? value, String unit) =>
|
||||
value == null ? _unknown : '${value.toStringAsFixed(2)} $unit';
|
||||
|
||||
String _duration(double? seconds) {
|
||||
if (seconds == null) return _unknown;
|
||||
final duration = Duration(seconds: seconds.round());
|
||||
String pad(int n) => n.toString().padLeft(2, '0');
|
||||
final minutes = pad(duration.inMinutes.remainder(60));
|
||||
final tail = '$minutes:${pad(duration.inSeconds.remainder(60))}';
|
||||
return duration.inHours > 0 ? '${pad(duration.inHours)}:$tail' : tail;
|
||||
}
|
||||
|
||||
String _status(String? value) => switch (value?.toLowerCase()) {
|
||||
null => _unknown,
|
||||
'running' => _label('running'),
|
||||
'paused' => _label('paused'),
|
||||
'completed' || 'finished' => _label('completed'),
|
||||
'failed' => _label('failed'),
|
||||
'canceled' || 'cancelled' => _label('canceled'),
|
||||
'idle' => _label('idle'),
|
||||
_ => value!,
|
||||
};
|
||||
|
||||
String _timestamp(int? milliseconds) {
|
||||
if (milliseconds == null || milliseconds > 8640000000000000) {
|
||||
return _unknown;
|
||||
}
|
||||
final time = DateTime.fromMillisecondsSinceEpoch(milliseconds).toLocal();
|
||||
String pad(int n) => n.toString().padLeft(2, '0');
|
||||
return '${time.year}-${pad(time.month)}-${pad(time.day)} '
|
||||
'${pad(time.hour)}:${pad(time.minute)}:${pad(time.second)}';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final report = _report;
|
||||
final age = report == null
|
||||
? null
|
||||
: DateTime.now()
|
||||
.difference(report.receivedAt)
|
||||
.inSeconds
|
||||
.clamp(0, 999999);
|
||||
final stale = age != null && age >= 15;
|
||||
final warning = _failed || stale;
|
||||
final progress = report?.progress;
|
||||
final mode = report?.mode == 'coverage'
|
||||
? _label('coverage')
|
||||
: _text(report?.mode);
|
||||
final phaseName = report?.phaseName == 'COVERING'
|
||||
? _label('covering')
|
||||
: _text(report?.phaseName);
|
||||
|
||||
return Container(
|
||||
constraints: BoxConstraints(maxHeight: widget.maxHeight),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Color(0x1A1A2B4A),
|
||||
blurRadius: 18,
|
||||
offset: Offset(0, 6),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 4, 4, 0),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.assignment_outlined, size: 16, color: kWtBlue),
|
||||
const SizedBox(width: 6),
|
||||
Expanded(
|
||||
child: Text(
|
||||
_label('title'),
|
||||
style: TextStyle(
|
||||
fontSize: _font + 2,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: kWtNavy,
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: _label('collapse'),
|
||||
onPressed: widget.onCollapse,
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(
|
||||
Icons.keyboard_arrow_up_rounded,
|
||||
color: kWtBlue,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Flexible(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
_failed
|
||||
? _label('unavailable')
|
||||
: report == null
|
||||
? _label('waiting')
|
||||
: stale
|
||||
? _label('stale')
|
||||
: _l10n.translate(
|
||||
'autonomous_work.task_status.updated',
|
||||
{'seconds': age},
|
||||
),
|
||||
style: TextStyle(
|
||||
fontSize: _font,
|
||||
color: warning ? kWtWarn : kWtGray,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_row(_label('task_id'), _text(report?.taskId)),
|
||||
_row(_label('status'), _status(report?.status)),
|
||||
_row(_label('mode'), mode),
|
||||
_row(_label('phase_name'), phaseName),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
_label('progress'),
|
||||
style: TextStyle(fontSize: _font, color: kWtGray),
|
||||
),
|
||||
Text(
|
||||
progress == null
|
||||
? _unknown
|
||||
: '${(progress * 100).toStringAsFixed(2)}%',
|
||||
style: TextStyle(
|
||||
fontSize: _font + 12,
|
||||
fontWeight: FontWeight.w800,
|
||||
color: kWtNavy,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: LinearProgressIndicator(
|
||||
value: progress ?? 0,
|
||||
minHeight: 5,
|
||||
backgroundColor: kWtSegBg,
|
||||
color: progress == null || warning ? kWtGray : kWtGreen,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
_pair(
|
||||
(_label('elapsed_s'), _duration(report?.elapsedSeconds)),
|
||||
(_label('eta_s'), _duration(report?.etaSeconds)),
|
||||
),
|
||||
_pair(
|
||||
(
|
||||
_label('distance_traveled'),
|
||||
_number(report?.distanceTraveled, 'm'),
|
||||
),
|
||||
(
|
||||
_label('distance_remaining'),
|
||||
_number(report?.distanceRemaining, 'm'),
|
||||
),
|
||||
),
|
||||
_pair(
|
||||
(_label('speed'), _number(report?.speed, 'm/s')),
|
||||
(
|
||||
_label('number_of_recoveries'),
|
||||
_text(report?.numberOfRecoveries),
|
||||
),
|
||||
),
|
||||
_pair(
|
||||
// 面向用户的分区序号从 1 开始,原始索引在报文详情里保留。
|
||||
(
|
||||
_label('current_cell'),
|
||||
_text(
|
||||
report?.currentCellIndex == null
|
||||
? null
|
||||
: report!.currentCellIndex! + 1,
|
||||
),
|
||||
),
|
||||
(_label('total_cells'), _text(report?.totalCells)),
|
||||
),
|
||||
Theme(
|
||||
data: Theme.of(
|
||||
context,
|
||||
).copyWith(dividerColor: Colors.transparent),
|
||||
child: ExpansionTile(
|
||||
tilePadding: EdgeInsets.zero,
|
||||
childrenPadding: EdgeInsets.zero,
|
||||
dense: true,
|
||||
title: Text(
|
||||
_label('details'),
|
||||
style: TextStyle(fontSize: _font, color: kWtBlue),
|
||||
),
|
||||
children: [
|
||||
_row(
|
||||
_label('protocol_version'),
|
||||
_text(report?.protocolVersion),
|
||||
),
|
||||
_row(_label('message_id'), _text(report?.messageId)),
|
||||
_row(_label('sn'), _text(report?.sn)),
|
||||
_row(_label('command_id'), _text(report?.commandId)),
|
||||
_row(
|
||||
_label('timestamp'),
|
||||
_timestamp(report?.timestamp),
|
||||
),
|
||||
_row('timestamp', _text(report?.timestamp)),
|
||||
_row('phase', _text(report?.phase)),
|
||||
_row('task_phase', _text(report?.taskPhase)),
|
||||
_row(
|
||||
'current_cell_index',
|
||||
_text(report?.currentCellIndex),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: SelectableText(
|
||||
report == null
|
||||
? _unknown
|
||||
: const JsonEncoder.withIndent(
|
||||
' ',
|
||||
).convert(report.raw),
|
||||
style: TextStyle(fontSize: _font, color: kWtGray),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _row(String label, String value) => Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
flex: 4,
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(fontSize: _font, color: kWtGray),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
Expanded(
|
||||
flex: 5,
|
||||
child: Text(
|
||||
value,
|
||||
textAlign: TextAlign.right,
|
||||
style: TextStyle(
|
||||
fontSize: _font,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: kWtNavy,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Widget _pair((String, String) left, (String, String) right) {
|
||||
Widget metric((String, String) item) => Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
item.$1,
|
||||
style: TextStyle(fontSize: _font, color: kWtGray),
|
||||
),
|
||||
const SizedBox(height: 3),
|
||||
Text(
|
||||
item.$2,
|
||||
style: TextStyle(
|
||||
fontSize: _font + 2,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: kWtNavy,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(child: metric(left)),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(child: metric(right)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user