Files
smart-serow/pi/ui/lib/screens/dashboard_screen.dart

282 lines
8.6 KiB
Dart
Raw Normal View History

2026-01-25 18:47:35 +09:00
import 'dart:async';
import 'dart:math' show sqrt, sin, cos, pi;
2026-01-25 18:47:35 +09:00
import 'package:flutter/material.dart';
2026-01-26 16:50:52 +09:00
import '../services/backend_service.dart';
import '../services/websocket_service.dart';
2026-01-25 19:23:03 +09:00
import '../services/pi_io.dart';
import '../theme/app_theme.dart';
import '../widgets/navigator_widget.dart';
2026-01-25 18:47:35 +09:00
import '../widgets/stat_box.dart';
2026-01-26 15:47:59 +09:00
import '../widgets/stat_box_main.dart';
import '../widgets/system_bar.dart';
2026-01-26 22:22:33 +09:00
import '../widgets/debug_console.dart';
import '../widgets/whiskey_mark.dart';
2026-02-04 23:19:24 +09:00
import '../widgets/accel_graph.dart';
2026-01-25 18:47:35 +09:00
// test service for triggers
import '../services/test_flipflop_service.dart';
2026-01-25 19:23:03 +09:00
/// Main dashboard - displays Pi vitals and placeholder stats
2026-01-25 18:47:35 +09:00
class DashboardScreen extends StatefulWidget {
const DashboardScreen({super.key});
@override
State<DashboardScreen> createState() => _DashboardScreenState();
}
class _DashboardScreenState extends State<DashboardScreen> {
static const _surpriseThreshold = 0.24; // G threshold for navigator surprise
final _navigatorKey = GlobalKey<NavigatorWidgetState>();
2026-01-25 18:47:35 +09:00
2026-01-26 16:50:52 +09:00
// Timer for Pi temp only (safety critical, direct file read)
Timer? _piTempTimer;
// WebSocket stream subscriptions
StreamSubscription<ArduinoData>? _arduinoSub;
StreamSubscription<GpsData>? _gpsSub;
StreamSubscription<WsConnectionState>? _connectionSub;
// Pi temperature - direct file read (safety critical)
2026-01-25 19:23:03 +09:00
double? _piTemp;
2026-01-26 16:50:52 +09:00
// From backend - Arduino data
int? _rpm;
double? _voltage;
int? _engineTemp;
int? _gear;
double? _roll;
double? _pitch;
2026-02-04 23:19:24 +09:00
double? _ax;
double? _ay;
double? _dynamicAx; // Gravity-compensated
double? _dynamicAy;
2026-01-26 16:50:52 +09:00
// From backend - GPS data
double? _gpsSpeed;
2026-01-26 15:47:59 +09:00
// Placeholder values for system bar
int? _gpsSatellites;
int? _lteSignal;
2026-01-25 18:47:35 +09:00
2026-01-26 16:50:52 +09:00
// WebSocket connection state
WsConnectionState _wsState = WsConnectionState.disconnected;
2026-01-25 18:47:35 +09:00
@override
void initState() {
super.initState();
2026-01-25 19:23:03 +09:00
2026-01-26 16:50:52 +09:00
// Connect to WebSocket
WebSocketService.instance.connect();
// Subscribe to Arduino data stream
_arduinoSub = WebSocketService.instance.arduinoStream.listen((data) {
// Gravity-compensated acceleration
// When tilted, gravity "leaks" into horizontal axes - subtract it out
final rollRad = (data.roll ?? 0) * pi / 180;
final pitchRad = (data.pitch ?? 0) * pi / 180;
// Subtract gravity leakage from measured acceleration
// Axes swapped for IMU mounting orientation
final dynamicAx = (data.ay ?? 0) + sin(rollRad);
final dynamicAy = (data.ax ?? 0) - (sin(pitchRad) * cos(rollRad));
2026-01-25 18:47:35 +09:00
setState(() {
2026-01-26 16:50:52 +09:00
_voltage = data.voltage;
_rpm = data.rpm;
_engineTemp = data.engTemp;
_gear = data.gear;
_roll = data.roll;
_pitch = data.pitch;
2026-02-04 23:19:24 +09:00
_ax = data.ax;
_ay = data.ay;
_dynamicAx = dynamicAx;
_dynamicAy = dynamicAy;
2026-01-26 16:50:52 +09:00
});
final gMagnitude = sqrt(dynamicAx * dynamicAx + dynamicAy * dynamicAy);
if (gMagnitude > _surpriseThreshold) {
_navigatorKey.currentState?.setEmotion('surprise');
}
2026-01-26 16:50:52 +09:00
});
2026-01-25 19:23:03 +09:00
2026-01-26 16:50:52 +09:00
// Subscribe to GPS data stream
_gpsSub = WebSocketService.instance.gpsStream.listen((data) {
setState(() {
_gpsSpeed = data.speed;
// Derive satellites from mode (placeholder logic)
_gpsSatellites = data.mode == 3 ? 8 : (data.mode == 2 ? 4 : 0);
});
});
2026-01-26 15:47:59 +09:00
2026-01-26 16:50:52 +09:00
// Subscribe to connection state
_connectionSub = WebSocketService.instance.connectionStream.listen((state) {
setState(() {
_wsState = state;
});
});
2026-01-26 15:47:59 +09:00
2026-01-26 16:50:52 +09:00
// Timer for Pi temp only (safety critical - bypasses backend)
_piTempTimer = Timer.periodic(const Duration(milliseconds: 500), (_) {
setState(() {
_piTemp = PiIO.instance.getTemperature();
2026-01-25 18:47:35 +09:00
});
});
2026-01-26 16:50:52 +09:00
// Initialize with any cached data from WebSocketService
final cachedArduino = WebSocketService.instance.latestArduino;
if (cachedArduino != null) {
_voltage = cachedArduino.voltage;
_rpm = cachedArduino.rpm;
_engineTemp = cachedArduino.engTemp;
_gear = cachedArduino.gear;
_roll = cachedArduino.roll;
_pitch = cachedArduino.pitch;
2026-02-04 23:19:24 +09:00
_ax = cachedArduino.ax;
_ay = cachedArduino.ay;
2026-01-26 16:50:52 +09:00
}
final cachedGps = WebSocketService.instance.latestGps;
if (cachedGps != null) {
_gpsSpeed = cachedGps.speed;
_gpsSatellites = cachedGps.mode == 3 ? 8 : (cachedGps.mode == 2 ? 4 : 0);
}
_wsState = WebSocketService.instance.connectionState;
// Placeholder: LTE signal (TODO: wire up when LTE service exists)
_lteSignal = null;
// DEBUG: flip-flop theme + navigator every 2s
TestFlipFlopService.instance.start(navigatorKey: _navigatorKey);
2026-01-25 18:47:35 +09:00
}
@override
void dispose() {
2026-01-26 16:50:52 +09:00
_piTempTimer?.cancel();
_arduinoSub?.cancel();
_gpsSub?.cancel();
_connectionSub?.cancel();
TestFlipFlopService.instance.stop();
2026-01-25 18:47:35 +09:00
super.dispose();
}
2026-01-26 16:50:52 +09:00
/// Format gear for display: null → "—", 0 → "N", 1-6 → "1"-"6"
String _formatGear(int? gear) {
if (gear == null) return '';
if (gear == 0) return 'N';
return gear.toString();
}
/// Format nullable int for display
String _formatInt(int? value) => value?.toString() ?? '';
/// Format nullable double for display with decimal places
String _formatDouble(double? value, [int decimals = 1]) {
if (value == null) return '';
return value.toStringAsFixed(decimals);
}
2026-01-25 18:47:35 +09:00
@override
Widget build(BuildContext context) {
final theme = AppTheme.of(context);
2026-01-25 18:47:35 +09:00
return Scaffold(
backgroundColor: theme.background,
2026-01-25 18:47:35 +09:00
body: Padding(
2026-01-26 15:47:59 +09:00
padding: const EdgeInsets.all(16),
child: Row(
2026-01-25 18:47:35 +09:00
children: [
// Left side: All dashboard widgets (flex: 2)
Expanded(
flex: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
2026-01-26 15:47:59 +09:00
// System status bar
SystemBar(
gpsSatellites: _gpsSatellites,
lteSignal: _lteSignal,
piTemp: _piTemp,
voltage: _voltage,
2026-01-26 16:50:52 +09:00
wsState: _wsState,
2026-01-25 18:47:35 +09:00
),
2026-02-08 00:26:59 +09:00
const SizedBox(height: 2),
2026-01-26 15:47:59 +09:00
// Main content area - big stat boxes
Expanded(
2026-02-08 00:26:59 +09:00
flex: 7,
2026-01-26 15:47:59 +09:00
child: Row(
children: [
// Attitude indicator (whiskey mark)
Expanded(
child: WhiskeyMark(
roll: _roll,
pitch: _pitch,
),
2026-01-26 15:47:59 +09:00
),
2026-02-04 23:19:24 +09:00
Expanded(
child: AccelGraph(
ax: _dynamicAx, // Gravity-compensated lateral
ay: _dynamicAy, // Gravity-compensated longitudinal
maxG: 0.8,
ghostTrackPeriod: const Duration(seconds: 4),
2026-02-04 23:19:24 +09:00
),
)
2026-01-26 15:47:59 +09:00
],
),
2026-01-25 18:47:35 +09:00
),
// Bottom stats row
2026-01-26 15:47:59 +09:00
Expanded(
2026-02-08 00:26:59 +09:00
flex: 3,
2026-01-26 15:47:59 +09:00
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
StatBox(value: _formatInt(_rpm), label: 'RPM', isWarning: () => (_rpm ?? 0) > 4000),
2026-02-08 00:26:59 +09:00
GpsCompass(heading: 147),
2026-01-26 16:50:52 +09:00
StatBox(value: _formatGear(_gear), label: 'GEAR'),
2026-01-26 15:47:59 +09:00
],
),
),
],
),
2026-01-25 18:47:35 +09:00
),
const SizedBox(width: 32),
2026-01-25 18:47:35 +09:00
2026-01-28 01:01:23 +09:00
// Right side: Navigator on top, debug console below
2026-01-25 18:47:35 +09:00
Expanded(
flex: 1,
2026-01-28 01:01:23 +09:00
child: Column(
2026-01-26 22:22:33 +09:00
children: [
2026-01-28 01:01:23 +09:00
// Navigator
Expanded(
flex: 3,
child: Center(
child: NavigatorWidget(key: _navigatorKey),
),
2026-01-26 22:22:33 +09:00
),
2026-01-28 01:01:23 +09:00
// Debug console
Expanded(
flex: 1,
child:
DebugConsole(
messageStream: WebSocketService.instance.debugStream,
initialMessages: WebSocketService.instance.debugMessages,
maxLines: 6,
title: 'WebSocket messages',
),
2026-01-26 22:22:33 +09:00
),
],
2026-01-25 18:47:35 +09:00
),
),
],
),
),
);
}
}