// At home: the dashboard's measurement stations (termix sensors), from Weather's home button — each sensor's // latest values, then temperature and humidity by the hour (24 h / 7 days / 30 days). Synced one way from // the Pi (plugins/stations: tables now + hourly, db.phone_*), so it shows offline too; the phone keeps // stationDays of hours. import 'package:flutter/material.dart'; import '../app/sync_service.dart'; import '../app/theme.dart'; import '../widgets/line_chart.dart'; import 'weather_page.dart' show tempColour; const stationDays = 30; const _colors = [Color(0xFF5CC8FF), Color(0xFFFFB33A), Color(0xFF7FD6A3), Color(0xFFC792EA), Color(0xFFFF8FC8), Color(0xFFFF9A9A)]; /// A now row's sensor: its id ("now:2") — device_id is the Pi table's integer key, which the sync leaves out. int _dev(Map r) => (r['device_id'] as int?) ?? int.parse((r['_u'] as String).split(':')[1]); /// Local time in the form the charts use (their day / hour ticks are UTC dates). DateTime _chartTime(DateTime t) { final l = t.toLocal(); return DateTime.utc(l.year, l.month, l.day, l.hour, l.minute); } class StationsPage extends StatefulWidget { const StationsPage({super.key, required this.sync}); final SyncService sync; @override State createState() => _StationsPageState(); } class _StationsPageState extends State { int days = 1; @override Widget build(BuildContext context) => Scaffold( appBar: AppBar(title: const Text('At home')), body: ListenableBuilder(listenable: widget.sync, builder: (context, _) => _body()), ); Widget _body() { final store = widget.sync.store; final now = store.rows('stations.now')..sort((a, b) => _dev(a).compareTo(_dev(b))); if (now.isEmpty) { return const Center(child: Padding( padding: EdgeInsets.all(24), child: Text('No sensors yet — they come from the dashboard once it has synced.', textAlign: TextAlign.center, style: TextStyle(color: muted)), )); } final nowS = DateTime.now().millisecondsSinceEpoch ~/ 1000; final from = DateTime.now().subtract(Duration(days: days)); final hours = store.rows('stations.hourly', where: "json_extract(data, '\$.t') >= ?", args: [from.millisecondsSinceEpoch ~/ 1000]); String ago(int t) { final m = (nowS - t) ~/ 60; return m < 2 ? 'now' : m < 60 ? '$m min ago' : m < 1440 ? '${m ~/ 60} h ago' : '${m ~/ 1440} d ago'; } List series(String k) => [ for (var i = 0; i < now.length; i++) Series(now[i]['name'] as String? ?? '', [ for (final h in hours.where((h) => h['device_id'] == _dev(now[i]))) if (h[k] != null) (_chartTime(DateTime.fromMillisecondsSinceEpoch((h['t'] as int) * 1000 + 1800000)), (h[k] as num).toDouble()) ]..sort((a, b) => a.$1.compareTo(b.$1)), _colors[i % _colors.length]), ]; Widget chart(String title, String k, String unit, int decimals) => Card(child: Padding( padding: const EdgeInsets.fromLTRB(14, 10, 10, 10), child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text(title, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), const SizedBox(height: 8), LineChart(height: 170, from: _chartTime(from), to: _chartTime(DateTime.now()), unit: unit, decimals: decimals, series: series(k)), ]), )); return ListView(padding: const EdgeInsets.fromLTRB(12, 8, 12, 24), children: [ Card(child: Padding( padding: const EdgeInsets.fromLTRB(14, 8, 14, 8), child: Column(children: [ for (var i = 0; i < now.length; i++) Padding( padding: const EdgeInsets.symmetric(vertical: 6), child: Row(children: [ Container(width: 9, height: 9, decoration: BoxDecoration(color: _colors[i % _colors.length], shape: BoxShape.circle)), const SizedBox(width: 10), Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(now[i]['name'] as String? ?? '', style: const TextStyle(fontSize: 15)), Text(ago(now[i]['t'] as int), style: const TextStyle(color: muted, fontSize: 12)), ])), if (now[i]['temp'] != null) Text('${(now[i]['temp'] as num).toStringAsFixed(1)}°', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, color: tempColour(now[i]['temp'] as num))), if (now[i]['hum'] != null) SizedBox(width: 74, child: Row(mainAxisAlignment: MainAxisAlignment.end, children: [ const Icon(Icons.water_drop_outlined, size: 16, color: Color(0xFF5CC8FF)), Text(' ${(now[i]['hum'] as num).round()}%', style: const TextStyle(fontSize: 15)), ])), ]), ), ]), )), const SizedBox(height: 8), Row(children: [ for (final (d, l) in [(1, '24 h'), (7, '7 days'), (30, '30 days')]) Padding( padding: const EdgeInsets.only(right: 8), child: ChoiceChip(label: Text(l), selected: days == d, showCheckmark: false, labelStyle: TextStyle(color: days == d ? accent : Colors.white), onSelected: (_) => setState(() => days = d)), ), ]), const SizedBox(height: 8), chart('Temperature', 'temp', '°C', 1), const SizedBox(height: 12), chart('Humidity', 'hum', '%', 0), ]); } }