main.dart 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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.heatingPower,
  207. backgroundColor: Colors.grey.withValues(
  208. alpha: 0.2,
  209. ),
  210. color: Colors.redAccent,
  211. minHeight: 8,
  212. borderRadius: BorderRadius.circular(4),
  213. ),
  214. ),
  215. ),
  216. Text(
  217. machineState.telemetryStale
  218. ? 'Unavailable'
  219. : '${(machineState.heatingPower * 100).toStringAsFixed(0)}%',
  220. style: const TextStyle(
  221. fontSize: 12,
  222. color: Colors.grey,
  223. ),
  224. ),
  225. ],
  226. ),
  227. const SizedBox(height: 16),
  228. // Target Control
  229. Row(
  230. mainAxisAlignment: MainAxisAlignment.center,
  231. children: [
  232. IconButton(
  233. onPressed: () =>
  234. bluetoothService.decrementSetpoint(),
  235. icon: const Icon(
  236. Icons.remove_circle_outline,
  237. color: Colors.grey,
  238. ),
  239. ),
  240. Column(
  241. children: [
  242. const Text(
  243. 'BREW TARGET',
  244. style: TextStyle(
  245. fontSize: 10,
  246. color: Colors.grey,
  247. ),
  248. ),
  249. Text(
  250. '${machineState.brewTemperature.toStringAsFixed(1)}°C',
  251. style: const TextStyle(
  252. fontSize: 18,
  253. fontWeight: FontWeight.bold,
  254. ),
  255. ),
  256. ],
  257. ),
  258. IconButton(
  259. onPressed: () =>
  260. bluetoothService.incrementSetpoint(),
  261. icon: const Icon(
  262. Icons.add_circle_outline,
  263. color: Colors.grey,
  264. ),
  265. ),
  266. ],
  267. ),
  268. ],
  269. ),
  270. ),
  271. ),
  272. const SizedBox(height: 16),
  273. // Shot Timer (Visible when brewing or after a shot)
  274. if (machineState.isBrewing || machineState.shotTimer > 0)
  275. Card(
  276. color: machineState.isBrewing && !machineState.telemetryStale
  277. ? Colors.green.withValues(alpha: 0.2)
  278. : null,
  279. child: Padding(
  280. padding: const EdgeInsets.all(16.0),
  281. child: Row(
  282. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  283. children: [
  284. const Text(
  285. 'Shot Timer',
  286. style: TextStyle(
  287. fontSize: 18,
  288. fontWeight: FontWeight.bold,
  289. ),
  290. ),
  291. Text(
  292. '${machineState.shotTimer}s',
  293. style: TextStyle(
  294. fontSize: 32,
  295. fontWeight: FontWeight.bold,
  296. color:
  297. machineState.isBrewing &&
  298. !machineState.telemetryStale
  299. ? Colors.greenAccent
  300. : Colors.grey,
  301. ),
  302. ),
  303. ],
  304. ),
  305. ),
  306. ),
  307. const SizedBox(height: 16),
  308. // Status Indicators
  309. Row(
  310. children: [
  311. Expanded(
  312. child: _StatusCard(
  313. label: 'Heating',
  314. isActive:
  315. machineState.isHeating &&
  316. !machineState.telemetryStale,
  317. activeColor: Colors.redAccent,
  318. icon: Icons.hot_tub,
  319. ),
  320. ),
  321. const SizedBox(width: 16),
  322. Expanded(
  323. child: _StatusCard(
  324. label: 'Brewing',
  325. isActive:
  326. machineState.isBrewing &&
  327. !machineState.telemetryStale,
  328. activeColor: Colors.greenAccent,
  329. icon: Icons.coffee,
  330. ),
  331. ),
  332. ],
  333. ),
  334. const SizedBox(height: 16),
  335. // Temperature Chart
  336. Card(
  337. child: Padding(
  338. padding: const EdgeInsets.all(16.0),
  339. child: Column(
  340. crossAxisAlignment: CrossAxisAlignment.start,
  341. children: [
  342. Row(
  343. children: [
  344. const Text(
  345. 'Temperature Stability',
  346. style: TextStyle(fontSize: 16),
  347. ),
  348. const Spacer(),
  349. // Legend
  350. const _LegendDot(
  351. color: Colors.redAccent,
  352. label: 'Boiler',
  353. ),
  354. if (machineState
  355. .sensor2TemperatureHistory
  356. .isNotEmpty) ...[
  357. const SizedBox(width: 12),
  358. const _LegendDot(
  359. color: Colors.greenAccent,
  360. label: 'Sensor 2',
  361. ),
  362. ],
  363. const SizedBox(width: 12),
  364. _LegendDot(
  365. color: Colors.blue.withValues(alpha: 0.5),
  366. label: 'Target',
  367. ),
  368. ],
  369. ),
  370. const SizedBox(height: 20),
  371. SizedBox(
  372. height: 320,
  373. child: TemperatureChart(
  374. history: machineState.temperatureHistory,
  375. sensor2History:
  376. machineState.sensor2TemperatureHistory,
  377. targetTemperature: machineState.targetTemperature,
  378. samples: machineState.temperatureSamples,
  379. ),
  380. ),
  381. ],
  382. ),
  383. ),
  384. ),
  385. const SizedBox(height: 20),
  386. if (machineState.connectionStatus != 'Connected')
  387. ElevatedButton.icon(
  388. onPressed: machineState.connectionStatus == 'Scanning...'
  389. ? null
  390. : machineState.firmwareUpdating
  391. ? null
  392. : connect,
  393. icon: Icon(
  394. machineState.connectionStatus.startsWith('Error')
  395. ? Icons.bluetooth_disabled
  396. : Icons.search,
  397. ),
  398. style: machineState.connectionStatus.startsWith('Error')
  399. ? ElevatedButton.styleFrom(
  400. backgroundColor: Colors.redAccent.withValues(
  401. alpha: 0.2,
  402. ),
  403. )
  404. : null,
  405. label: Text(switch (machineState.connectionStatus) {
  406. 'Scanning...' => 'Scanning…',
  407. 'Reconnecting...' =>
  408. 'Reconnecting in 30s… (tap to retry now)',
  409. 'Not found' => 'Not found — tap to retry',
  410. String s when s.startsWith('Error') =>
  411. '${machineState.connectionStatus} — tap to retry',
  412. _ => 'Connect to meCoffee',
  413. }),
  414. )
  415. else
  416. OutlinedButton(
  417. onPressed: machineState.firmwareUpdating
  418. ? null
  419. : () => bluetoothService.disconnect(),
  420. child: const Text('Disconnect'),
  421. ),
  422. ],
  423. ),
  424. ),
  425. ),
  426. );
  427. }
  428. }
  429. class _StatusCard extends StatelessWidget {
  430. final String label;
  431. final bool isActive;
  432. final Color activeColor;
  433. final IconData icon;
  434. const _StatusCard({
  435. required this.label,
  436. required this.isActive,
  437. required this.activeColor,
  438. required this.icon,
  439. });
  440. @override
  441. Widget build(BuildContext context) {
  442. return Card(
  443. color: isActive ? activeColor.withValues(alpha: 0.2) : null,
  444. child: Padding(
  445. padding: const EdgeInsets.symmetric(vertical: 16.0),
  446. child: Column(
  447. children: [
  448. Icon(icon, color: isActive ? activeColor : Colors.grey),
  449. const SizedBox(height: 8),
  450. Text(
  451. label,
  452. style: TextStyle(
  453. color: isActive ? activeColor : Colors.grey,
  454. fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
  455. ),
  456. ),
  457. ],
  458. ),
  459. ),
  460. );
  461. }
  462. }
  463. class _LegendDot extends StatelessWidget {
  464. final Color color;
  465. final String label;
  466. const _LegendDot({required this.color, required this.label});
  467. @override
  468. Widget build(BuildContext context) {
  469. return Row(
  470. mainAxisSize: MainAxisSize.min,
  471. children: [
  472. Container(
  473. width: 10,
  474. height: 10,
  475. decoration: BoxDecoration(color: color, shape: BoxShape.circle),
  476. ),
  477. const SizedBox(width: 4),
  478. Text(label, style: const TextStyle(fontSize: 11, color: Colors.grey)),
  479. ],
  480. );
  481. }
  482. }