main.dart 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'services/bluetooth_service.dart';
  4. import 'widgets/temperature_chart.dart';
  5. import 'screens/settings_screen.dart';
  6. import 'screens/connection_screen.dart';
  7. import 'screens/diagnostics_screen.dart';
  8. void main() {
  9. runApp(const ProviderScope(child: MyApp()));
  10. }
  11. class MyApp extends StatelessWidget {
  12. const MyApp({super.key});
  13. @override
  14. Widget build(BuildContext context) {
  15. return MaterialApp(
  16. title: 'meBarista',
  17. theme: ThemeData(
  18. brightness: Brightness.dark,
  19. colorScheme: ColorScheme.fromSeed(
  20. seedColor: Colors.brown,
  21. brightness: Brightness.dark,
  22. ),
  23. useMaterial3: true,
  24. ),
  25. home: const MyHomePage(title: 'meBarista Dashboard'),
  26. );
  27. }
  28. }
  29. class MyHomePage extends ConsumerWidget {
  30. const MyHomePage({super.key, required this.title});
  31. final String title;
  32. @override
  33. Widget build(BuildContext context, WidgetRef ref) {
  34. final machineState = ref.watch(bluetoothServiceProvider);
  35. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  36. void connect() {
  37. if (bluetoothService.supportsClassic) {
  38. Navigator.of(
  39. context,
  40. ).push(MaterialPageRoute(builder: (_) => const ConnectionScreen()));
  41. } else {
  42. bluetoothService.startScan();
  43. }
  44. }
  45. ref.listen(bluetoothServiceProvider, (previous, next) {
  46. if (next.commandError != null &&
  47. next.commandError != previous?.commandError) {
  48. ScaffoldMessenger.of(
  49. context,
  50. ).showSnackBar(SnackBar(content: Text(next.commandError!)));
  51. }
  52. });
  53. return Scaffold(
  54. appBar: AppBar(
  55. title: Text(title),
  56. actions: [
  57. IconButton(
  58. tooltip: 'Diagnostics',
  59. icon: const Icon(Icons.receipt_long),
  60. onPressed: () {
  61. Navigator.of(context).push(
  62. MaterialPageRoute(builder: (_) => const DiagnosticsScreen()),
  63. );
  64. },
  65. ),
  66. IconButton(
  67. icon: const Icon(Icons.settings),
  68. onPressed: () {
  69. Navigator.of(context).push(
  70. MaterialPageRoute(builder: (context) => const SettingsScreen()),
  71. );
  72. },
  73. ),
  74. IconButton(
  75. onPressed: machineState.firmwareUpdating ? null : connect,
  76. icon: Icon(
  77. machineState.connectionStatus == 'Connected'
  78. ? Icons.bluetooth_connected
  79. : machineState.connectionStatus.startsWith('Error')
  80. ? Icons.bluetooth_disabled
  81. : Icons.bluetooth_searching,
  82. color: machineState.connectionStatus == 'Connected'
  83. ? Colors.blue
  84. : machineState.connectionStatus.startsWith('Error')
  85. ? Colors.redAccent
  86. : null,
  87. ),
  88. ),
  89. ],
  90. ),
  91. body: SafeArea(
  92. top: false,
  93. child: SingleChildScrollView(
  94. padding: const EdgeInsets.all(16.0),
  95. child: Column(
  96. crossAxisAlignment: CrossAxisAlignment.stretch,
  97. children: [
  98. if (machineState.connectionStatus == 'Connected' &&
  99. machineState.telemetryStale)
  100. Card(
  101. color: Colors.amber.withValues(alpha: 0.15),
  102. child: Padding(
  103. padding: const EdgeInsets.all(12),
  104. child: Text(
  105. machineState.lastTelemetryAt == null
  106. ? 'Waiting for temperature telemetry. Readings are unavailable.'
  107. : 'Telemetry stale: no temperature sample for 3 seconds. '
  108. 'Displayed readings are the last received values.',
  109. style: const TextStyle(color: Colors.amber),
  110. ),
  111. ),
  112. ),
  113. if (machineState.firmwareUpdating)
  114. Card(
  115. child: Padding(
  116. padding: const EdgeInsets.all(16),
  117. child: Column(
  118. children: [
  119. Text(machineState.firmwareUpdateStatus),
  120. const SizedBox(height: 8),
  121. LinearProgressIndicator(
  122. value: machineState.firmwareProgress,
  123. ),
  124. const Text(
  125. 'Keep controller power and Bluetooth connected.',
  126. ),
  127. ],
  128. ),
  129. ),
  130. ),
  131. // Temperature Display Card
  132. Card(
  133. child: Padding(
  134. padding: const EdgeInsets.all(20.0),
  135. child: Column(
  136. children: [
  137. Row(
  138. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  139. children: [
  140. const Text(
  141. 'Boiler Temperature',
  142. style: TextStyle(fontSize: 18),
  143. ),
  144. Icon(
  145. Icons.coffee_maker,
  146. size: 32,
  147. color:
  148. machineState.connectionStatus == 'Connected' &&
  149. !machineState.telemetryStale
  150. ? (machineState.isHeating
  151. ? Colors.redAccent
  152. : Colors.blueAccent)
  153. : Colors.grey,
  154. ),
  155. ],
  156. ),
  157. const SizedBox(height: 10),
  158. Text(
  159. '${machineState.temperature.toStringAsFixed(1)}°C',
  160. style: const TextStyle(
  161. fontSize: 64,
  162. fontWeight: FontWeight.bold,
  163. color: Colors.orangeAccent,
  164. ),
  165. ),
  166. // Second sensor reading (shown only when present)
  167. if (machineState.sensor2Temperature > 0)
  168. Padding(
  169. padding: const EdgeInsets.only(top: 4.0),
  170. child: Row(
  171. mainAxisAlignment: MainAxisAlignment.center,
  172. children: [
  173. const Icon(
  174. Icons.thermostat,
  175. size: 14,
  176. color: Colors.greenAccent,
  177. ),
  178. const SizedBox(width: 4),
  179. Text(
  180. 'Sensor 2: ${machineState.sensor2Temperature.toStringAsFixed(1)}°C',
  181. style: const TextStyle(
  182. fontSize: 13,
  183. color: Colors.greenAccent,
  184. ),
  185. ),
  186. ],
  187. ),
  188. ),
  189. const SizedBox(height: 10),
  190. // PID Power Indicator
  191. Row(
  192. children: [
  193. const Icon(
  194. Icons.bolt,
  195. size: 16,
  196. color: Colors.yellowAccent,
  197. ),
  198. Expanded(
  199. child: Padding(
  200. padding: const EdgeInsets.symmetric(
  201. horizontal: 8.0,
  202. ),
  203. child: LinearProgressIndicator(
  204. value: machineState.telemetryStale
  205. ? 0
  206. : machineState.isSteaming
  207. ? null
  208. : machineState.heatingPower,
  209. backgroundColor: Colors.grey.withValues(
  210. alpha: 0.2,
  211. ),
  212. color: Colors.redAccent,
  213. minHeight: 8,
  214. borderRadius: BorderRadius.circular(4),
  215. ),
  216. ),
  217. ),
  218. Text(
  219. machineState.telemetryStale
  220. ? 'Unavailable'
  221. : machineState.isSteaming
  222. ? 'Steam'
  223. : '${(machineState.heatingPower * 100).toStringAsFixed(0)}%',
  224. style: const TextStyle(
  225. fontSize: 12,
  226. color: Colors.grey,
  227. ),
  228. ),
  229. ],
  230. ),
  231. const SizedBox(height: 16),
  232. // Target Control
  233. Row(
  234. mainAxisAlignment: MainAxisAlignment.center,
  235. children: [
  236. IconButton(
  237. onPressed: () =>
  238. bluetoothService.decrementSetpoint(),
  239. icon: const Icon(
  240. Icons.remove_circle_outline,
  241. color: Colors.grey,
  242. ),
  243. ),
  244. Column(
  245. children: [
  246. const Text(
  247. 'BREW TARGET',
  248. style: TextStyle(
  249. fontSize: 10,
  250. color: Colors.grey,
  251. ),
  252. ),
  253. Text(
  254. '${machineState.brewTemperature.toStringAsFixed(1)}°C',
  255. style: const TextStyle(
  256. fontSize: 18,
  257. fontWeight: FontWeight.bold,
  258. ),
  259. ),
  260. ],
  261. ),
  262. IconButton(
  263. onPressed: () =>
  264. bluetoothService.incrementSetpoint(),
  265. icon: const Icon(
  266. Icons.add_circle_outline,
  267. color: Colors.grey,
  268. ),
  269. ),
  270. ],
  271. ),
  272. ],
  273. ),
  274. ),
  275. ),
  276. const SizedBox(height: 16),
  277. // Shot Timer (Visible when brewing or after a shot)
  278. if (machineState.isBrewing || machineState.shotTimer > 0)
  279. Card(
  280. color: machineState.isBrewing && !machineState.telemetryStale
  281. ? Colors.green.withValues(alpha: 0.2)
  282. : null,
  283. child: Padding(
  284. padding: const EdgeInsets.all(16.0),
  285. child: Row(
  286. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  287. children: [
  288. const Text(
  289. 'Shot Timer',
  290. style: TextStyle(
  291. fontSize: 18,
  292. fontWeight: FontWeight.bold,
  293. ),
  294. ),
  295. Text(
  296. '${machineState.shotTimer}s',
  297. style: TextStyle(
  298. fontSize: 32,
  299. fontWeight: FontWeight.bold,
  300. color:
  301. machineState.isBrewing &&
  302. !machineState.telemetryStale
  303. ? Colors.greenAccent
  304. : Colors.grey,
  305. ),
  306. ),
  307. ],
  308. ),
  309. ),
  310. ),
  311. const SizedBox(height: 16),
  312. // Status Indicators
  313. Row(
  314. children: [
  315. Expanded(
  316. child: _StatusCard(
  317. label: 'Heating',
  318. isActive:
  319. machineState.isHeating &&
  320. !machineState.telemetryStale,
  321. activeColor: Colors.redAccent,
  322. icon: Icons.hot_tub,
  323. ),
  324. ),
  325. const SizedBox(width: 16),
  326. Expanded(
  327. child: _StatusCard(
  328. label: 'Brewing',
  329. isActive:
  330. machineState.isBrewing &&
  331. !machineState.telemetryStale,
  332. activeColor: Colors.greenAccent,
  333. icon: Icons.coffee,
  334. ),
  335. ),
  336. ],
  337. ),
  338. const SizedBox(height: 16),
  339. // Temperature Chart
  340. Card(
  341. child: Padding(
  342. padding: const EdgeInsets.all(16.0),
  343. child: Column(
  344. crossAxisAlignment: CrossAxisAlignment.start,
  345. children: [
  346. Row(
  347. children: [
  348. const Text(
  349. 'Temperature Stability',
  350. style: TextStyle(fontSize: 16),
  351. ),
  352. const Spacer(),
  353. // Legend
  354. const _LegendDot(
  355. color: Colors.redAccent,
  356. label: 'Boiler',
  357. ),
  358. if (machineState
  359. .sensor2TemperatureHistory
  360. .isNotEmpty) ...[
  361. const SizedBox(width: 12),
  362. const _LegendDot(
  363. color: Colors.greenAccent,
  364. label: 'Sensor 2',
  365. ),
  366. ],
  367. const SizedBox(width: 12),
  368. _LegendDot(
  369. color: Colors.blue.withValues(alpha: 0.5),
  370. label: 'Target',
  371. ),
  372. ],
  373. ),
  374. const SizedBox(height: 20),
  375. SizedBox(
  376. height: 320,
  377. child: TemperatureChart(
  378. history: machineState.temperatureHistory,
  379. sensor2History:
  380. machineState.sensor2TemperatureHistory,
  381. targetTemperature: machineState.targetTemperature,
  382. samples: machineState.temperatureSamples,
  383. ),
  384. ),
  385. ],
  386. ),
  387. ),
  388. ),
  389. const SizedBox(height: 20),
  390. if (machineState.connectionStatus != 'Connected')
  391. ElevatedButton.icon(
  392. onPressed: machineState.connectionStatus == 'Scanning...'
  393. ? null
  394. : machineState.firmwareUpdating
  395. ? null
  396. : connect,
  397. icon: Icon(
  398. machineState.connectionStatus.startsWith('Error')
  399. ? Icons.bluetooth_disabled
  400. : Icons.search,
  401. ),
  402. style: machineState.connectionStatus.startsWith('Error')
  403. ? ElevatedButton.styleFrom(
  404. backgroundColor: Colors.redAccent.withValues(
  405. alpha: 0.2,
  406. ),
  407. )
  408. : null,
  409. label: Text(switch (machineState.connectionStatus) {
  410. 'Scanning...' => 'Scanning…',
  411. 'Reconnecting...' =>
  412. 'Reconnecting in 30s… (tap to retry now)',
  413. 'Not found' => 'Not found — tap to retry',
  414. String s when s.startsWith('Error') =>
  415. '${machineState.connectionStatus} — tap to retry',
  416. _ => 'Connect to meCoffee',
  417. }),
  418. )
  419. else
  420. OutlinedButton(
  421. onPressed: machineState.firmwareUpdating
  422. ? null
  423. : () => bluetoothService.disconnect(),
  424. child: const Text('Disconnect'),
  425. ),
  426. ],
  427. ),
  428. ),
  429. ),
  430. );
  431. }
  432. }
  433. class _StatusCard extends StatelessWidget {
  434. final String label;
  435. final bool isActive;
  436. final Color activeColor;
  437. final IconData icon;
  438. const _StatusCard({
  439. required this.label,
  440. required this.isActive,
  441. required this.activeColor,
  442. required this.icon,
  443. });
  444. @override
  445. Widget build(BuildContext context) {
  446. return Card(
  447. color: isActive ? activeColor.withValues(alpha: 0.2) : null,
  448. child: Padding(
  449. padding: const EdgeInsets.symmetric(vertical: 16.0),
  450. child: Column(
  451. children: [
  452. Icon(icon, color: isActive ? activeColor : Colors.grey),
  453. const SizedBox(height: 8),
  454. Text(
  455. label,
  456. style: TextStyle(
  457. color: isActive ? activeColor : Colors.grey,
  458. fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
  459. ),
  460. ),
  461. ],
  462. ),
  463. ),
  464. );
  465. }
  466. }
  467. class _LegendDot extends StatelessWidget {
  468. final Color color;
  469. final String label;
  470. const _LegendDot({required this.color, required this.label});
  471. @override
  472. Widget build(BuildContext context) {
  473. return Row(
  474. mainAxisSize: MainAxisSize.min,
  475. children: [
  476. Container(
  477. width: 10,
  478. height: 10,
  479. decoration: BoxDecoration(color: color, shape: BoxShape.circle),
  480. ),
  481. const SizedBox(width: 4),
  482. Text(label, style: const TextStyle(fontSize: 11, color: Colors.grey)),
  483. ],
  484. );
  485. }
  486. }