317 lines
12 KiB
Dart
317 lines
12 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../../../../core/app/app_user_cubit.dart';
|
|
import '../../../../core/di/injection.dart';
|
|
import '../../../../core/network/net_message_dispatcher.dart';
|
|
import '../../../../core/network/protocol_decoder.dart';
|
|
import '../../../devices/presentation/bloc/devices_cubit.dart';
|
|
import '../../../devices/presentation/bloc/device_status_bloc.dart';
|
|
import '../../../devices/presentation/bloc/device_status_state.dart';
|
|
|
|
class RunningStatusPage extends StatefulWidget {
|
|
const RunningStatusPage({super.key});
|
|
|
|
@override
|
|
State<RunningStatusPage> createState() => _RunningStatusPageState();
|
|
}
|
|
|
|
class _RunningStatusPageState extends State<RunningStatusPage> {
|
|
bool _isCardView = true;
|
|
|
|
// 解析定位质量数值为文字描述
|
|
String _parseLocationQuality(int locationValue) {
|
|
switch (locationValue) {
|
|
case 0:
|
|
return '无效';
|
|
case 1:
|
|
return 'GPS 单点定位';
|
|
case 2:
|
|
return 'DGPS 伪距差分或 SBAS';
|
|
case 4:
|
|
return 'RTK 固定解';
|
|
case 5:
|
|
return 'RTK 浮点解';
|
|
default:
|
|
return '未知($locationValue)'; // 处理未定义的数值,避免显示空值
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final deviceState = context.read<DevicesCubit>().state;
|
|
final currentDevice = deviceState.selectedDevice;
|
|
|
|
if (currentDevice == null) {
|
|
return const Scaffold(body: Center(child: Text("加载中...")));
|
|
}
|
|
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFF7F7F7),
|
|
body: CustomScrollView(
|
|
physics: const BouncingScrollPhysics(),
|
|
slivers: [
|
|
SliverAppBar(
|
|
backgroundColor: const Color(0xFF1677FF),
|
|
pinned: true,
|
|
centerTitle: true,
|
|
elevation: 0,
|
|
title: const Text(
|
|
"设备数据监控",
|
|
style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold),
|
|
),
|
|
leading: IconButton(
|
|
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
),
|
|
|
|
BlocBuilder<DeviceStatusBloc, DeviceStatusState>(
|
|
builder: (context, state) {
|
|
String qual = '--';
|
|
String satelliteCnt = '--';
|
|
String headingStatus = "--";
|
|
|
|
if (state is DeviceStatusUpdated) {
|
|
headingStatus = state.status.headingStatus == 0 ? '未初始化' : '已初始化';
|
|
qual = _parseLocationQuality(state.status.qual);
|
|
//qual = int.parse(state.status.qual.toString());
|
|
satelliteCnt = state.status.satelliteCnt.toString();
|
|
} else if (state is DeviceStatusError) {
|
|
qual = '-';
|
|
satelliteCnt = '-';
|
|
headingStatus = '-';
|
|
}
|
|
|
|
return SliverToBoxAdapter(
|
|
child: Container(
|
|
color: Colors.white,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text("航向角状态:$headingStatus", style: const TextStyle(fontSize: 14)),
|
|
Text("定位质量:$qual", style: const TextStyle(fontSize: 14)),
|
|
Text("卫星数:$satelliteCnt", style: const TextStyle(fontSize: 14)),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
|
|
SliverToBoxAdapter(
|
|
child: Container(
|
|
color: Colors.white,
|
|
margin: const EdgeInsets.only(top: 8),
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () => setState(() => _isCardView = true),
|
|
child: _buildTab("卡片", isActive: _isCardView),
|
|
),
|
|
const SizedBox(width: 24),
|
|
GestureDetector(
|
|
onTap: () => setState(() => _isCardView = false),
|
|
child: _buildTab("图表", isActive: !_isCardView),
|
|
),
|
|
],
|
|
),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: const Color(0xFF1677FF),
|
|
foregroundColor: Colors.white,
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
),
|
|
onPressed: _refreshDeviceData,
|
|
child: const Text("刷新数据", style: TextStyle(fontSize: 14)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
SliverFillRemaining(
|
|
child: BlocBuilder<DeviceStatusBloc, DeviceStatusState>(
|
|
builder: (context, state) {
|
|
return Container(
|
|
margin: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8)),
|
|
child: _isCardView ? _buildCardContentView(state) : _buildChartContentView(),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildTab(String title, {required bool isActive}) {
|
|
return Text(
|
|
title,
|
|
style: TextStyle(
|
|
color: isActive ? const Color(0xFF1677FF) : const Color(0xFF666666),
|
|
fontSize: 18,
|
|
fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
|
|
decoration: isActive ? TextDecoration.underline : null,
|
|
decorationColor: const Color(0xFF1677FF),
|
|
decorationThickness: 2,
|
|
),
|
|
);
|
|
}
|
|
|
|
// 电机参数表格
|
|
Widget _buildMotorTable(DeviceStatusUpdated state) {
|
|
final status = state.status;
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 12),
|
|
child: Text("电机参数", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
|
),
|
|
Table(
|
|
border: TableBorder.all(color: const Color(0xFFE5E5E5)),
|
|
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
|
|
children: [
|
|
TableRow(
|
|
decoration: const BoxDecoration(color: Color(0xFFF5F5F5)),
|
|
children: [const SizedBox(), _buildTableCell("左轮", isHeader: true), _buildTableCell("右轮", isHeader: true)],
|
|
),
|
|
TableRow(
|
|
children: [
|
|
_buildTableCell("目标速度\n(rpm)"),
|
|
_buildTableCell(status.leftTargetSpeed.toStringAsFixed(2)),
|
|
_buildTableCell(status.rightTargetSpeed.toStringAsFixed(2)),
|
|
],
|
|
),
|
|
TableRow(
|
|
children: [
|
|
_buildTableCell("测量速度\n(rpm)"),
|
|
_buildTableCell(status.leftTargetSpeed.toStringAsFixed(2)),
|
|
_buildTableCell(status.rightTargetSpeed.toStringAsFixed(2)),
|
|
//_buildTableCell(status.leftMeasuredSpeed.toStringAsFixed(2)),
|
|
//_buildTableCell(status.rightMeasuredSpeed.toStringAsFixed(2)),
|
|
],
|
|
),
|
|
TableRow(
|
|
children: [
|
|
_buildTableCell("电流(A)"),
|
|
_buildTableCell(status.leftCurrent.toStringAsFixed(2)),
|
|
_buildTableCell(status.rightCurrent.toStringAsFixed(2)),
|
|
],
|
|
),
|
|
TableRow(
|
|
children: [
|
|
_buildTableCell("电机温度(°C)"),
|
|
_buildTableCell(status.leftMotorTemp.toStringAsFixed(2)),
|
|
_buildTableCell(status.rightMotorTemp.toStringAsFixed(2)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
// 其他参数表格
|
|
Widget _buildOtherParamsTable(DeviceStatusUpdated state) {
|
|
final status = state.status;
|
|
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 12),
|
|
child: Text("其他参数", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
|
),
|
|
Table(
|
|
border: TableBorder.all(color: const Color(0xFFE5E5E5)),
|
|
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
|
|
children: [
|
|
TableRow(
|
|
decoration: const BoxDecoration(color: Color(0xFFF5F5F5)),
|
|
children: [_buildTableCell("名称", isHeader: true), _buildTableCell("数值", isHeader: true)],
|
|
),
|
|
TableRow(children: [_buildTableCell("俯仰角(°)"), _buildTableCell(status.pitch.toStringAsFixed(2))]),
|
|
TableRow(children: [_buildTableCell("翻滚角(°)"), _buildTableCell(status.roll.toStringAsFixed(2))]),
|
|
TableRow(children: [_buildTableCell("航向角(°)"), _buildTableCell(status.yaw.toStringAsFixed(2))]),
|
|
TableRow(children: [_buildTableCell("电量(%)"), _buildTableCell(status.battery)]),
|
|
TableRow(children: [_buildTableCell("芯片温度(°C)"), _buildTableCell(status.chipTemp.toStringAsFixed(2))]),
|
|
TableRow(children: [_buildTableCell("割刀速度(rpm)"), _buildTableCell(status.knifeCuttingSpeed)]),
|
|
TableRow(children: [_buildTableCell("纬度(°)"), _buildTableCell(status.latitude.toStringAsFixed(6))]),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildTableCell(String text, {bool isHeader = false}) {
|
|
return TableCell(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
|
|
child: Text(
|
|
text,
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(
|
|
fontSize: 16,
|
|
color: isHeader ? const Color(0xFF333333) : const Color(0xFF666666),
|
|
fontWeight: isHeader ? FontWeight.bold : FontWeight.normal,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildCardContentView(DeviceStatusState state) {
|
|
if (state is DeviceStatusUpdated) {
|
|
return SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(children: [_buildMotorTable(state), const SizedBox(height: 20), _buildOtherParamsTable(state)]),
|
|
);
|
|
} else if (state is DeviceStatusError) {
|
|
return const Center(
|
|
child: Text("数据加载失败", style: TextStyle(color: Colors.red, fontSize: 16)),
|
|
);
|
|
} else {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
}
|
|
|
|
Widget _buildChartContentView() {
|
|
return const Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text("图表视图", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
SizedBox(height: 16),
|
|
Text("可接入 fl_chart / charts_flutter 实现折线图/柱状图", style: TextStyle(color: Color(0xFF999999))),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _refreshDeviceData() {
|
|
final deviceState = context.read<DevicesCubit>().state;
|
|
final currentDevice = deviceState.selectedDevice;
|
|
|
|
if (currentDevice != null) {
|
|
// 这里可以触发 Bloc 事件来刷新数据
|
|
// context.read<DeviceStatusBloc>().add(RefreshDeviceStatus(deviceId: currentDevice.id));
|
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("数据刷新成功!"), duration: Duration(seconds: 1)));
|
|
} else {
|
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("暂无设备,无法刷新"), duration: Duration(seconds: 1)));
|
|
}
|
|
}
|
|
}
|