| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import 'services/bluetooth_service.dart';
- import 'widgets/temperature_chart.dart';
- import 'screens/settings_screen.dart';
- import 'screens/connection_screen.dart';
- import 'screens/diagnostics_screen.dart';
- void main() {
- runApp(const ProviderScope(child: MyApp()));
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'meBarista',
- theme: ThemeData(
- brightness: Brightness.dark,
- colorScheme: ColorScheme.fromSeed(
- seedColor: Colors.brown,
- brightness: Brightness.dark,
- ),
- useMaterial3: true,
- ),
- home: const MyHomePage(title: 'meBarista Dashboard'),
- );
- }
- }
- class MyHomePage extends ConsumerWidget {
- const MyHomePage({super.key, required this.title});
- final String title;
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final machineState = ref.watch(bluetoothServiceProvider);
- final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
- void connect() {
- if (bluetoothService.supportsClassic) {
- Navigator.of(
- context,
- ).push(MaterialPageRoute(builder: (_) => const ConnectionScreen()));
- } else {
- bluetoothService.startScan();
- }
- }
- ref.listen(bluetoothServiceProvider, (previous, next) {
- if (next.commandError != null &&
- next.commandError != previous?.commandError) {
- ScaffoldMessenger.of(
- context,
- ).showSnackBar(SnackBar(content: Text(next.commandError!)));
- }
- });
- return Scaffold(
- appBar: AppBar(
- title: Text(title),
- actions: [
- IconButton(
- tooltip: 'Diagnostics',
- icon: const Icon(Icons.receipt_long),
- onPressed: () {
- Navigator.of(context).push(
- MaterialPageRoute(builder: (_) => const DiagnosticsScreen()),
- );
- },
- ),
- IconButton(
- icon: const Icon(Icons.settings),
- onPressed: () {
- Navigator.of(context).push(
- MaterialPageRoute(builder: (context) => const SettingsScreen()),
- );
- },
- ),
- IconButton(
- onPressed: machineState.firmwareUpdating ? null : connect,
- icon: Icon(
- machineState.connectionStatus == 'Connected'
- ? Icons.bluetooth_connected
- : machineState.connectionStatus.startsWith('Error')
- ? Icons.bluetooth_disabled
- : Icons.bluetooth_searching,
- color: machineState.connectionStatus == 'Connected'
- ? Colors.blue
- : machineState.connectionStatus.startsWith('Error')
- ? Colors.redAccent
- : null,
- ),
- ),
- ],
- ),
- body: SafeArea(
- top: false,
- child: SingleChildScrollView(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- if (machineState.connectionStatus == 'Connected' &&
- machineState.telemetryStale)
- Card(
- color: Colors.amber.withValues(alpha: 0.15),
- child: Padding(
- padding: const EdgeInsets.all(12),
- child: Text(
- machineState.lastTelemetryAt == null
- ? 'Waiting for temperature telemetry. Readings are unavailable.'
- : 'Telemetry stale: no temperature sample for 3 seconds. '
- 'Displayed readings are the last received values.',
- style: const TextStyle(color: Colors.amber),
- ),
- ),
- ),
- if (machineState.firmwareUpdating)
- Card(
- child: Padding(
- padding: const EdgeInsets.all(16),
- child: Column(
- children: [
- Text(machineState.firmwareUpdateStatus),
- const SizedBox(height: 8),
- LinearProgressIndicator(
- value: machineState.firmwareProgress,
- ),
- const Text(
- 'Keep controller power and Bluetooth connected.',
- ),
- ],
- ),
- ),
- ),
- // Temperature Display Card
- Card(
- child: Padding(
- padding: const EdgeInsets.all(20.0),
- child: Column(
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text(
- 'Boiler Temperature',
- style: TextStyle(fontSize: 18),
- ),
- Icon(
- Icons.coffee_maker,
- size: 32,
- color:
- machineState.connectionStatus == 'Connected' &&
- !machineState.telemetryStale
- ? (machineState.isHeating
- ? Colors.redAccent
- : Colors.blueAccent)
- : Colors.grey,
- ),
- ],
- ),
- const SizedBox(height: 10),
- Text(
- '${machineState.temperature.toStringAsFixed(1)}°C',
- style: const TextStyle(
- fontSize: 64,
- fontWeight: FontWeight.bold,
- color: Colors.orangeAccent,
- ),
- ),
- // Second sensor reading (shown only when present)
- if (machineState.sensor2Temperature > 0)
- Padding(
- padding: const EdgeInsets.only(top: 4.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- const Icon(
- Icons.thermostat,
- size: 14,
- color: Colors.greenAccent,
- ),
- const SizedBox(width: 4),
- Text(
- 'Sensor 2: ${machineState.sensor2Temperature.toStringAsFixed(1)}°C',
- style: const TextStyle(
- fontSize: 13,
- color: Colors.greenAccent,
- ),
- ),
- ],
- ),
- ),
- const SizedBox(height: 10),
- // PID Power Indicator
- Row(
- children: [
- const Icon(
- Icons.bolt,
- size: 16,
- color: Colors.yellowAccent,
- ),
- Expanded(
- child: Padding(
- padding: const EdgeInsets.symmetric(
- horizontal: 8.0,
- ),
- child: LinearProgressIndicator(
- value: machineState.telemetryStale
- ? 0
- : machineState.heatingPower,
- backgroundColor: Colors.grey.withValues(
- alpha: 0.2,
- ),
- color: Colors.redAccent,
- minHeight: 8,
- borderRadius: BorderRadius.circular(4),
- ),
- ),
- ),
- Text(
- machineState.telemetryStale
- ? 'Unavailable'
- : '${(machineState.heatingPower * 100).toStringAsFixed(0)}%',
- style: const TextStyle(
- fontSize: 12,
- color: Colors.grey,
- ),
- ),
- ],
- ),
- const SizedBox(height: 16),
- // Target Control
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- IconButton(
- onPressed: () =>
- bluetoothService.decrementSetpoint(),
- icon: const Icon(
- Icons.remove_circle_outline,
- color: Colors.grey,
- ),
- ),
- Column(
- children: [
- const Text(
- 'BREW TARGET',
- style: TextStyle(
- fontSize: 10,
- color: Colors.grey,
- ),
- ),
- Text(
- '${machineState.brewTemperature.toStringAsFixed(1)}°C',
- style: const TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.bold,
- ),
- ),
- ],
- ),
- IconButton(
- onPressed: () =>
- bluetoothService.incrementSetpoint(),
- icon: const Icon(
- Icons.add_circle_outline,
- color: Colors.grey,
- ),
- ),
- ],
- ),
- ],
- ),
- ),
- ),
- const SizedBox(height: 16),
- // Shot Timer (Visible when brewing or after a shot)
- if (machineState.isBrewing || machineState.shotTimer > 0)
- Card(
- color: machineState.isBrewing && !machineState.telemetryStale
- ? Colors.green.withValues(alpha: 0.2)
- : null,
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- const Text(
- 'Shot Timer',
- style: TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.bold,
- ),
- ),
- Text(
- '${machineState.shotTimer}s',
- style: TextStyle(
- fontSize: 32,
- fontWeight: FontWeight.bold,
- color:
- machineState.isBrewing &&
- !machineState.telemetryStale
- ? Colors.greenAccent
- : Colors.grey,
- ),
- ),
- ],
- ),
- ),
- ),
- const SizedBox(height: 16),
- // Status Indicators
- Row(
- children: [
- Expanded(
- child: _StatusCard(
- label: 'Heating',
- isActive:
- machineState.isHeating &&
- !machineState.telemetryStale,
- activeColor: Colors.redAccent,
- icon: Icons.hot_tub,
- ),
- ),
- const SizedBox(width: 16),
- Expanded(
- child: _StatusCard(
- label: 'Brewing',
- isActive:
- machineState.isBrewing &&
- !machineState.telemetryStale,
- activeColor: Colors.greenAccent,
- icon: Icons.coffee,
- ),
- ),
- ],
- ),
- const SizedBox(height: 16),
- // Temperature Chart
- Card(
- child: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- children: [
- const Text(
- 'Temperature Stability',
- style: TextStyle(fontSize: 16),
- ),
- const Spacer(),
- // Legend
- const _LegendDot(
- color: Colors.redAccent,
- label: 'Boiler',
- ),
- if (machineState
- .sensor2TemperatureHistory
- .isNotEmpty) ...[
- const SizedBox(width: 12),
- const _LegendDot(
- color: Colors.greenAccent,
- label: 'Sensor 2',
- ),
- ],
- const SizedBox(width: 12),
- _LegendDot(
- color: Colors.blue.withValues(alpha: 0.5),
- label: 'Target',
- ),
- ],
- ),
- const SizedBox(height: 20),
- SizedBox(
- height: 320,
- child: TemperatureChart(
- history: machineState.temperatureHistory,
- sensor2History:
- machineState.sensor2TemperatureHistory,
- targetTemperature: machineState.targetTemperature,
- samples: machineState.temperatureSamples,
- ),
- ),
- ],
- ),
- ),
- ),
- const SizedBox(height: 20),
- if (machineState.connectionStatus != 'Connected')
- ElevatedButton.icon(
- onPressed: machineState.connectionStatus == 'Scanning...'
- ? null
- : machineState.firmwareUpdating
- ? null
- : connect,
- icon: Icon(
- machineState.connectionStatus.startsWith('Error')
- ? Icons.bluetooth_disabled
- : Icons.search,
- ),
- style: machineState.connectionStatus.startsWith('Error')
- ? ElevatedButton.styleFrom(
- backgroundColor: Colors.redAccent.withValues(
- alpha: 0.2,
- ),
- )
- : null,
- label: Text(switch (machineState.connectionStatus) {
- 'Scanning...' => 'Scanning…',
- 'Reconnecting...' =>
- 'Reconnecting in 30s… (tap to retry now)',
- 'Not found' => 'Not found — tap to retry',
- String s when s.startsWith('Error') =>
- '${machineState.connectionStatus} — tap to retry',
- _ => 'Connect to meCoffee',
- }),
- )
- else
- OutlinedButton(
- onPressed: machineState.firmwareUpdating
- ? null
- : () => bluetoothService.disconnect(),
- child: const Text('Disconnect'),
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
- class _StatusCard extends StatelessWidget {
- final String label;
- final bool isActive;
- final Color activeColor;
- final IconData icon;
- const _StatusCard({
- required this.label,
- required this.isActive,
- required this.activeColor,
- required this.icon,
- });
- @override
- Widget build(BuildContext context) {
- return Card(
- color: isActive ? activeColor.withValues(alpha: 0.2) : null,
- child: Padding(
- padding: const EdgeInsets.symmetric(vertical: 16.0),
- child: Column(
- children: [
- Icon(icon, color: isActive ? activeColor : Colors.grey),
- const SizedBox(height: 8),
- Text(
- label,
- style: TextStyle(
- color: isActive ? activeColor : Colors.grey,
- fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
- class _LegendDot extends StatelessWidget {
- final Color color;
- final String label;
- const _LegendDot({required this.color, required this.label});
- @override
- Widget build(BuildContext context) {
- return Row(
- mainAxisSize: MainAxisSize.min,
- children: [
- Container(
- width: 10,
- height: 10,
- decoration: BoxDecoration(color: color, shape: BoxShape.circle),
- ),
- const SizedBox(width: 4),
- Text(label, style: const TextStyle(fontSize: 11, color: Colors.grey)),
- ],
- );
- }
- }
|