按 docs/下位机交互数据模型.md 重构串口协议层: 协议层 - 新增 DeviceMessage 模型,对应 message_id/type/ack/need_ack/data - 新增 JsonProtocolService,4 字节大端长度前缀 + UTF-8 JSON 帧 - 删除原二进制协议(serial_protocol.dart) 服务层 - 新增 DeviceMessageService,集中收发并按 type 分发 - 重写 SerialRunner 为 JsonSerialRunner,使用 create_task/control 消息 数据模型 - DeviceState 增加 doorStatus/lightStatus/taskStatus/lastInfoAt - 新增 DeviceInfoNotifier 订阅 device_info 上行 - 灯光按钮接通 light_control 消息 测试 - 新增 device_protocol_test.dart(14 用例) - 修复 models_test.dart 残留的 Step mixSpeed/blowSpeed 错误
157 lines
4.4 KiB
Dart
157 lines
4.4 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../../core/localization/app_localizations.dart';
|
|
import '../../../core/theme/app_theme.dart';
|
|
import '../../../shared/widgets/status_indicator.dart';
|
|
import '../../device/providers/device_info_provider.dart';
|
|
|
|
/// 状态栏组件 - 明亮工业风格
|
|
/// 显示设备名称、实时时钟、系统状态、照明控制
|
|
class StatusBar extends ConsumerStatefulWidget {
|
|
final bool isRunning;
|
|
final VoidCallback? onLightToggle;
|
|
|
|
const StatusBar({
|
|
super.key,
|
|
this.isRunning = false,
|
|
this.onLightToggle,
|
|
});
|
|
|
|
@override
|
|
ConsumerState<StatusBar> createState() => _StatusBarState();
|
|
}
|
|
|
|
class _StatusBarState extends ConsumerState<StatusBar> {
|
|
String _currentTime = '';
|
|
Timer? _timer;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_updateTime();
|
|
_timer = Timer.periodic(const Duration(seconds: 1), (_) => _updateTime());
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_timer?.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
void _updateTime() {
|
|
final now = DateTime.now();
|
|
_currentTime =
|
|
'${now.year}-${_twoDigits(now.month)}-${_twoDigits(now.day)} '
|
|
'${_twoDigits(now.hour)}:${_twoDigits(now.minute)}:${_twoDigits(now.second)}';
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
String _twoDigits(int n) => n.toString().padLeft(2, '0');
|
|
|
|
Future<void> _onLightTap() async {
|
|
widget.onLightToggle?.call();
|
|
await ref.read(deviceInfoProvider.notifier).toggleLight();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
final deviceInfo = ref.watch(deviceInfoProvider);
|
|
|
|
return Container(
|
|
height: 56,
|
|
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryColor,
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withValues(alpha: 0.08),
|
|
blurRadius: 4,
|
|
offset: const Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(Icons.precision_manufacturing, color: Colors.white, size: 22),
|
|
const SizedBox(width: 10),
|
|
Text(
|
|
l10n?.deviceName ?? '污水毒品前处理一体机',
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 17,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const Spacer(),
|
|
_LightToggleButton(
|
|
isOn: deviceInfo.lightingOn,
|
|
onTap: _onLightTap,
|
|
),
|
|
const SizedBox(width: 20),
|
|
StatusIndicator(
|
|
text: widget.isRunning
|
|
? (l10n?.running ?? '运行中')
|
|
: (l10n?.idle ?? '未运行'),
|
|
status: widget.isRunning
|
|
? DeviceStatusType.running
|
|
: DeviceStatusType.idle,
|
|
),
|
|
const SizedBox(width: 20),
|
|
Text(
|
|
_currentTime,
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 13,
|
|
fontFamily: 'monospace',
|
|
fontWeight: FontWeight.normal,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _LightToggleButton extends StatelessWidget {
|
|
final bool isOn;
|
|
final VoidCallback? onTap;
|
|
const _LightToggleButton({this.isOn = false, this.onTap});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
onTap: onTap,
|
|
borderRadius: BorderRadius.circular(20),
|
|
child: Container(
|
|
width: 40,
|
|
height: 40,
|
|
decoration: BoxDecoration(
|
|
color: isOn
|
|
? Colors.white.withValues(alpha: 0.25)
|
|
: Colors.white.withValues(alpha: 0.1),
|
|
borderRadius: BorderRadius.circular(20),
|
|
border: Border.all(
|
|
color: Colors.white.withValues(alpha: 0.3),
|
|
width: 1,
|
|
),
|
|
),
|
|
child: Icon(
|
|
isOn ? Icons.lightbulb : Icons.lightbulb_outline_rounded,
|
|
color: isOn ? Colors.yellowAccent : Colors.white.withValues(alpha: 0.8),
|
|
size: 20,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|