283 lines
9.8 KiB
Dart
283 lines
9.8 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';
|
|
|
|
class RunningStatusPage extends StatefulWidget {
|
|
const RunningStatusPage({super.key});
|
|
|
|
@override
|
|
State<RunningStatusPage> createState() => _RunningStatusPageState();
|
|
}
|
|
|
|
class _RunningStatusPageState extends State<RunningStatusPage> {
|
|
|
|
final _dispatcher = sl<NetMessageDispatcher>();
|
|
late StreamSubscription<RawPacket> _sub;
|
|
|
|
// 实时设备状态数据
|
|
String _yaw = '--';
|
|
String _satelliteCnt = '--';
|
|
String _qual = '--';
|
|
String _leftSpeed = '--';
|
|
String _rightSpeed = '--';
|
|
String _voltage = '--';
|
|
// 实时设备状态数据
|
|
String _pitch = '--';
|
|
String _roll = '--';
|
|
String _latitude = '--';
|
|
String _longitude = '--';
|
|
String _timeStamp = '--';
|
|
String _knifeCuttingSpeed = '--';
|
|
String _controlMode = '--';
|
|
String _workingArea = '--';
|
|
String _battery = '--';
|
|
String _obstacleFlag = '--';
|
|
// 视图切换状态
|
|
bool _isCardView = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_sub = _dispatcher.onCommand(0x02).listen(_onDeviceData);
|
|
}
|
|
|
|
void _onDeviceData(RawPacket packet) {
|
|
try {
|
|
final csv = utf8.decode(packet.payload);
|
|
final fields = csv.split(',');
|
|
// 至少需 16 个字段(索引 10=航向角, 13=卫星数, 14=定位质量)
|
|
if (fields.length >= 16) {
|
|
setState(() {
|
|
_voltage = fields[0]; // 电压
|
|
_leftSpeed = fields[1]; // 左轮目标速度
|
|
_rightSpeed = fields[2]; // 右轮目标速度
|
|
_pitch = fields[11]; // 俯仰角
|
|
_roll = fields[12]; // 翻滚角
|
|
_latitude = fields[16]; // 纬度
|
|
_longitude = fields[17]; // 经度
|
|
_timeStamp = '${fields[16]} ${fields[17]}'; // 时间戳(合并字段)
|
|
_knifeCuttingSpeed = fields[19]; // 割刀速度
|
|
_controlMode = fields[20]; // 控制模式
|
|
_workingArea = fields[21]; // 作业面积
|
|
_battery = fields[22]; // 电量
|
|
_obstacleFlag = fields[23]; // 障碍物标志
|
|
_yaw = fields[10]; // 航向角
|
|
_satelliteCnt = fields[13]; // 卫星数
|
|
_qual = fields[14]; // 定位质量
|
|
});
|
|
}
|
|
} catch (_) {
|
|
// 解析失败时忽略,避免崩溃
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_sub.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void _refreshDeviceData() {
|
|
final deviceState = context.read<DevicesCubit>().state;
|
|
final currentDevice = deviceState.selectedDevice;
|
|
|
|
if (currentDevice != null) {
|
|
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)),
|
|
);
|
|
}
|
|
}
|
|
|
|
@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: [
|
|
// 1. 顶部导航栏
|
|
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),
|
|
),
|
|
),
|
|
|
|
// 2. 状态信息行(动态更新)
|
|
SliverToBoxAdapter(
|
|
child: Container(
|
|
color: Colors.white,
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text("航向角状态: $_yaw°", style: const TextStyle(fontSize: 14)),
|
|
Text("定位质量: $_qual", style: const TextStyle(fontSize: 14)),
|
|
Text("卫星数: $_satelliteCnt", style: const TextStyle(fontSize: 14)),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
// 3. 选项卡 + 刷新按钮
|
|
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)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
|
|
// 4. 内容区域
|
|
SliverFillRemaining(
|
|
child: Container(
|
|
margin: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: _isCardView ? _buildCardContentView() : _buildChartContentView(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildTab(String title, {required bool isActive}) {
|
|
return Text(
|
|
title,
|
|
style: TextStyle(
|
|
color: isActive ? const Color(0xFF1677FF) : const Color(0xFF666666),
|
|
fontSize: 16,
|
|
fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
|
|
decoration: isActive ? TextDecoration.underline : null,
|
|
decorationColor: const Color(0xFF1677FF),
|
|
decorationThickness: 2,
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildCardContentView() {
|
|
return Padding(
|
|
padding: const EdgeInsets.all(20.0),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text("设备实时状态", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
|
const SizedBox(height: 16),
|
|
_CardItem(title: "电压", value: "$_voltage V"),
|
|
_CardItem(title: "左轮速度", value: "$_leftSpeed rpm"),
|
|
_CardItem(title: "右轮速度", value: "$_rightSpeed rpm"),
|
|
_CardItem(title: "航向角", value: "$_yaw°"),
|
|
_CardItem(title: "俯仰角", value: "$_pitch°"),
|
|
_CardItem(title: "翻滚角", value: "$_roll°"),
|
|
_CardItem(title: "卫星数", value: _satelliteCnt),
|
|
_CardItem(title: "定位质量", value: _qual),
|
|
_CardItem(title: "纬度", value: _latitude),
|
|
_CardItem(title: "经度", value: _longitude),
|
|
_CardItem(title: "时间戳", value: _timeStamp),
|
|
_CardItem(title: "割刀速度", value: "$_knifeCuttingSpeed rpm"),
|
|
_CardItem(title: "控制模式", value: _controlMode),
|
|
_CardItem(title: "作业面积", value: "$_workingArea m²"),
|
|
_CardItem(title: "电量", value: "$_battery%"),
|
|
_CardItem(title: "障碍物标志", value: _obstacleFlag),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
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))),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _CardItem extends StatelessWidget {
|
|
final String title;
|
|
final String value;
|
|
|
|
const _CardItem({required this.title, required this.value});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 10),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text(title, style: const TextStyle(fontSize: 16, color: Color(0xFF333333))),
|
|
Text(value, style: const TextStyle(fontSize: 16, color: Color(0xFF666666))),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|