// "Sync now" to the dashboard over the home network (the dashboard's sync.kick): right after the // band's rows went out, the Pi pulls them, imports them, rewrites the Health summaries and pushes // them at once — then the app pulls again. Away from home the Pi doesn't answer and nothing changes // (the usual 5-minute / hourly rhythm). The message is sealed with the data key, so only a device // of this account can send one. import 'dart:convert'; import 'dart:io'; import 'package:http/io_client.dart'; import 'client.dart'; import 'crypto.dart'; import 'store.dart'; /// The dashboard on the home network, e.g. http://192.168.1.50:8080: the Sync page (store meta pi_url), /// else the build's --dart-define=PI_URL=… (tool/publish.env); empty = no "sync now", the usual rhythm only. String piUrl(SyncStore store) => (store.metaText('pi_url') ?? const String.fromEnvironment('PI_URL')).trim().replaceAll(RegExp(r'/+$'), ''); const _connect = Duration(seconds: 3); // not home → give up quickly const _answer = Duration(minutes: 3); // the Pi 3: import + summaries ≈ 1 min /// → true when the Pi sent something back (pull now). Future kickPi(SyncSettings s, SyncStore store, SyncCrypto crypto) async { final pi = piUrl(store); if (pi.isEmpty) return false; final blob = crypto.sealBatch(s.key, {'kick': DateTime.now().millisecondsSinceEpoch}, s.account); final client = IOClient(HttpClient()..connectionTimeout = _connect); try { final r = await client .post(Uri.parse('$pi/api/sync/kick'), headers: {'Content-Type': 'application/octet-stream'}, body: blob) .timeout(_answer); if (r.statusCode != 200) return false; return ((jsonDecode(r.body) as Map)['pushed'] ?? 0) > 0; } catch (_) { return false; } finally { client.close(); } }