From da255177ee08efc5b0ddd591a3d591763efb9973 Mon Sep 17 00:00:00 2001 From: e32232340-Nadif <158453666+kleponijo@users.noreply.github.com> Date: Sun, 1 Mar 2026 22:01:03 +0700 Subject: [PATCH] Update wind_speed_monitoring_screen.dart --- .../wind_speed_monitoring_screen.dart | 60 ++++++++++++++----- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/lib/screens/monitoring/wind_speed_monitoring_screen.dart b/lib/screens/monitoring/wind_speed_monitoring_screen.dart index fe747a4..f619d54 100644 --- a/lib/screens/monitoring/wind_speed_monitoring_screen.dart +++ b/lib/screens/monitoring/wind_speed_monitoring_screen.dart @@ -25,6 +25,11 @@ class _WindSpeedMonitoringScreenState extends State { double _currentSpeed = 0.0; final List _dailySpeeds = List.filled(24, 0.0); + // hourly aggregation helpers (PRO version) + double _hourlyTotalSpeed = 0.0; + int _hourlyCount = 0; + int _currentHour = DateTime.now().hour; + bool get _isOnline { if (_lastUpdateTime == null) return false; final diff = DateTime.now().difference(_lastUpdateTime!); @@ -39,25 +44,52 @@ class _WindSpeedMonitoringScreenState extends State { } void _onRealtimeData(DatabaseEvent event) { - final data = event.snapshot.value; + final data = event.snapshot.value; - if (data is Map) { - final speed = (data['kecepatan'] ?? 0).toDouble(); - final timestamp = data['timestamp'] ?? 0; + if (data is Map) { + final speed = (data['kecepatan'] ?? 0).toDouble(); + final timestamp = data['timestamp'] ?? 0; - final waktu = DateTime.fromMillisecondsSinceEpoch( - timestamp * 1000, - ); + final waktu = DateTime.fromMillisecondsSinceEpoch( + timestamp * 1000, + ); - setState(() { - _currentSpeed = speed; - _lastUpdateTime = waktu; + setState(() { + _currentSpeed = speed; + _lastUpdateTime = waktu; - _dailySpeeds.removeAt(0); - _dailySpeeds.add(speed); - }); - } + final incomingHour = waktu.hour; + + // Jika jam berubah → simpan rata-rata jam sebelumnya + if (incomingHour != _currentHour) { + + if (_hourlyCount > 0) { + final avg = _hourlyTotalSpeed / _hourlyCount; + + // Simpan ke index sesuai jam sebelumnya + _dailySpeeds[_currentHour] = avg; + } + + // Reset agregasi + _hourlyTotalSpeed = 0.0; + _hourlyCount = 0; + + _currentHour = incomingHour; + + // Reset data saat masuk hari baru (00:00) + if (incomingHour == 0) { + for (int i = 0; i < 24; i++) { + _dailySpeeds[i] = 0.0; + } + } + } + + // Tambah akumulasi + _hourlyTotalSpeed += speed; + _hourlyCount++; + }); } +} @override void dispose() {