woke
This commit is contained in:
parent
c0e7a1f1d6
commit
0162607d62
|
|
@ -1,113 +0,0 @@
|
||||||
import 'dart:async';
|
|
||||||
import 'package:bloc/bloc.dart';
|
|
||||||
import 'package:equatable/equatable.dart';
|
|
||||||
import 'package:monitoring_repository/monitoring_repository.dart';
|
|
||||||
|
|
||||||
part 'atmospheric_conditions_event.dart';
|
|
||||||
part 'atmospheric_conditions_state.dart';
|
|
||||||
|
|
||||||
class AtmosphericConditionsBloc extends Bloc<AtmosphericConditionsEvent, AtmosphericConditionsState> {
|
|
||||||
final MonitoringRepository _repository;
|
|
||||||
StreamSubscription<AtmosphericConditions>? _subscription;
|
|
||||||
static const int _maxHistoryItems = 1500;
|
|
||||||
|
|
||||||
AtmosphericConditionsBloc({required MonitoringRepository repository})
|
|
||||||
: _repository = repository,
|
|
||||||
super(const AtmosphericConditionsState()) {
|
|
||||||
on<WatchAtmosphericConditionsStarted>(_onStarted);
|
|
||||||
on<_AtmosphericConditionsUpdated>(_onUpdated);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 🚀 START
|
|
||||||
Future<void> _onStarted(
|
|
||||||
WatchAtmosphericConditionsStarted event,
|
|
||||||
Emitter<AtmosphericConditionsState> emit,
|
|
||||||
) async {
|
|
||||||
emit(state.copyWith(isLoading: true));
|
|
||||||
|
|
||||||
await _subscription?.cancel();
|
|
||||||
|
|
||||||
// Preload today's history from Firebase history table.
|
|
||||||
final now = DateTime.now();
|
|
||||||
final historyFromFirebase = await _repository.getSensorHistory(
|
|
||||||
'/sensor/history',
|
|
||||||
(json) => AtmosphericConditions.fromJson(json),
|
|
||||||
);
|
|
||||||
|
|
||||||
final todayHistory = historyFromFirebase.where((item) => _isSameDay(item.timestamp, now)).toList()..sort((a, b) => a.timestamp.compareTo(b.timestamp));
|
|
||||||
|
|
||||||
final trimmedHistory = todayHistory.length > _maxHistoryItems ? todayHistory.sublist(todayHistory.length - _maxHistoryItems) : todayHistory;
|
|
||||||
|
|
||||||
final latestFromHistory = trimmedHistory.isNotEmpty ? trimmedHistory.last : null;
|
|
||||||
|
|
||||||
emit(state.copyWith(
|
|
||||||
temperature: latestFromHistory?.temperature ?? state.temperature,
|
|
||||||
humidity: latestFromHistory?.humidity ?? state.humidity,
|
|
||||||
pressure: latestFromHistory?.pressure ?? state.pressure,
|
|
||||||
altitude: latestFromHistory?.altitude ?? state.altitude,
|
|
||||||
timeMs: latestFromHistory?.timeMs ?? state.timeMs,
|
|
||||||
history: trimmedHistory,
|
|
||||||
isLoading: false,
|
|
||||||
));
|
|
||||||
|
|
||||||
_subscription = _repository
|
|
||||||
.getSensorStream(
|
|
||||||
'/sensor/latest',
|
|
||||||
(json) => AtmosphericConditions.fromJson(json),
|
|
||||||
)
|
|
||||||
.listen((data) {
|
|
||||||
add(_AtmosphericConditionsUpdated(data));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ⚡ REALTIME UPDATE
|
|
||||||
void _onUpdated(
|
|
||||||
_AtmosphericConditionsUpdated event,
|
|
||||||
Emitter<AtmosphericConditionsState> emit,
|
|
||||||
) {
|
|
||||||
final now = DateTime.now();
|
|
||||||
final updatedHistory = state.history.where((item) => _isSameDay(item.timestamp, now)).toList();
|
|
||||||
|
|
||||||
final shouldAppend = updatedHistory.isEmpty || updatedHistory.last.timeMs != event.data.timeMs || updatedHistory.last.pressure != event.data.pressure || updatedHistory.last.timestamp != event.data.timestamp;
|
|
||||||
|
|
||||||
if (shouldAppend) {
|
|
||||||
updatedHistory.add(event.data);
|
|
||||||
|
|
||||||
if (updatedHistory.length > _maxHistoryItems) {
|
|
||||||
updatedHistory.removeAt(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
emit(state.copyWith(
|
|
||||||
temperature: event.data.temperature,
|
|
||||||
humidity: event.data.humidity,
|
|
||||||
pressure: event.data.pressure,
|
|
||||||
altitude: event.data.altitude,
|
|
||||||
timeMs: event.data.timeMs,
|
|
||||||
history: updatedHistory,
|
|
||||||
isLoading: false,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool _isSameDay(DateTime a, DateTime b) {
|
|
||||||
return a.year == b.year && a.month == b.month && a.day == b.day;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> close() async {
|
|
||||||
await _subscription?.cancel();
|
|
||||||
return super.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// INTERNAL EVENT
|
|
||||||
class _AtmosphericConditionsUpdated extends AtmosphericConditionsEvent {
|
|
||||||
final AtmosphericConditions data;
|
|
||||||
|
|
||||||
const _AtmosphericConditionsUpdated(this.data);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object> get props => [
|
|
||||||
data
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
part of 'atmospheric_conditions_bloc.dart';
|
|
||||||
|
|
||||||
abstract class AtmosphericConditionsEvent extends Equatable {
|
|
||||||
const AtmosphericConditionsEvent();
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object> get props => [];
|
|
||||||
}
|
|
||||||
|
|
||||||
class WatchAtmosphericConditionsStarted extends AtmosphericConditionsEvent {}
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
part of 'atmospheric_conditions_bloc.dart';
|
|
||||||
|
|
||||||
class AtmosphericConditionsState extends Equatable {
|
|
||||||
final double temperature;
|
|
||||||
final double humidity;
|
|
||||||
final double pressure;
|
|
||||||
final double altitude;
|
|
||||||
final int timeMs;
|
|
||||||
final List<AtmosphericConditions> history;
|
|
||||||
|
|
||||||
final bool isLoading;
|
|
||||||
|
|
||||||
const AtmosphericConditionsState({
|
|
||||||
this.temperature = 0.0,
|
|
||||||
this.humidity = 0.0,
|
|
||||||
this.pressure = 0.0,
|
|
||||||
this.altitude = 0.0,
|
|
||||||
this.timeMs = 0,
|
|
||||||
this.history = const [],
|
|
||||||
this.isLoading = true,
|
|
||||||
});
|
|
||||||
|
|
||||||
AtmosphericConditionsState copyWith({
|
|
||||||
double? temperature,
|
|
||||||
double? humidity,
|
|
||||||
double? pressure,
|
|
||||||
double? altitude,
|
|
||||||
int? timeMs,
|
|
||||||
List<AtmosphericConditions>? history,
|
|
||||||
bool? isLoading,
|
|
||||||
}) {
|
|
||||||
return AtmosphericConditionsState(
|
|
||||||
temperature: temperature ?? this.temperature,
|
|
||||||
humidity: humidity ?? this.humidity,
|
|
||||||
pressure: pressure ?? this.pressure,
|
|
||||||
altitude: altitude ?? this.altitude,
|
|
||||||
timeMs: timeMs ?? this.timeMs,
|
|
||||||
history: history ?? this.history,
|
|
||||||
isLoading: isLoading ?? this.isLoading,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object> get props => [
|
|
||||||
temperature,
|
|
||||||
humidity,
|
|
||||||
pressure,
|
|
||||||
altitude,
|
|
||||||
timeMs,
|
|
||||||
history,
|
|
||||||
isLoading,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
@ -1,393 +0,0 @@
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
||||||
import 'package:fl_chart/fl_chart.dart';
|
|
||||||
import 'package:monitoring_repository/monitoring_repository.dart';
|
|
||||||
import '../blocs/atmospheric_conditions_bloc.dart';
|
|
||||||
import '../../shared/utils/excel/excel_export_service.dart';
|
|
||||||
import '../../shared/widgets/export_excel_button.dart';
|
|
||||||
|
|
||||||
String formatUptimeShort(int timeMs) {
|
|
||||||
final duration = Duration(milliseconds: timeMs);
|
|
||||||
final minutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
|
|
||||||
final seconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
|
|
||||||
return "$minutes:$seconds";
|
|
||||||
}
|
|
||||||
|
|
||||||
String formatUptimeLong(int timeMs) {
|
|
||||||
final duration = Duration(milliseconds: timeMs);
|
|
||||||
final hours = duration.inHours.toString().padLeft(2, '0');
|
|
||||||
final minutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
|
|
||||||
final seconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
|
|
||||||
return "$hours:$minutes:$seconds";
|
|
||||||
}
|
|
||||||
|
|
||||||
String formatClockTime(DateTime timestamp) {
|
|
||||||
final h = timestamp.hour.toString().padLeft(2, '0');
|
|
||||||
final m = timestamp.minute.toString().padLeft(2, '0');
|
|
||||||
final s = timestamp.second.toString().padLeft(2, '0');
|
|
||||||
return "$h:$m:$s";
|
|
||||||
}
|
|
||||||
|
|
||||||
class AtmosphericScreen extends StatelessWidget {
|
|
||||||
const AtmosphericScreen({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Scaffold(
|
|
||||||
backgroundColor: Colors.grey.shade100,
|
|
||||||
appBar: AppBar(
|
|
||||||
title: const Text(
|
|
||||||
"Kondisi Atmosfer",
|
|
||||||
style: TextStyle(fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
centerTitle: true,
|
|
||||||
backgroundColor: Colors.transparent,
|
|
||||||
elevation: 0,
|
|
||||||
foregroundColor: Colors.black,
|
|
||||||
),
|
|
||||||
body: BlocBuilder<AtmosphericConditionsBloc, AtmosphericConditionsState>(
|
|
||||||
builder: (context, state) {
|
|
||||||
if (state.isLoading) {
|
|
||||||
return const Center(child: CircularProgressIndicator());
|
|
||||||
}
|
|
||||||
|
|
||||||
return SingleChildScrollView(
|
|
||||||
padding: const EdgeInsets.all(20),
|
|
||||||
child: Column(
|
|
||||||
children: [
|
|
||||||
_mainPressure(state),
|
|
||||||
const SizedBox(height: 24),
|
|
||||||
_historyChart(state),
|
|
||||||
const SizedBox(height: 24),
|
|
||||||
_HistoryTableCard(history: state.history),
|
|
||||||
const SizedBox(height: 24),
|
|
||||||
ExportExcelButton(
|
|
||||||
onExport: () {
|
|
||||||
final historyData = state.history
|
|
||||||
.map((e) => {
|
|
||||||
'timeMs': e.timeMs,
|
|
||||||
'pressure': e.pressure,
|
|
||||||
'timestamp': e.timestamp,
|
|
||||||
})
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
return ExcelExportService.atmospheric(
|
|
||||||
pressure: state.pressure,
|
|
||||||
timeMs: state.timeMs,
|
|
||||||
timestamp: state.history.isNotEmpty ? state.history.last.timestamp : DateTime.now(),
|
|
||||||
historyData: historyData,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
label: 'Export Excel Hari Ini',
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// =========================
|
|
||||||
/// 🔵 PRESSURE (HERO CARD)
|
|
||||||
/// =========================
|
|
||||||
Widget _mainPressure(AtmosphericConditionsState state) {
|
|
||||||
return Container(
|
|
||||||
width: double.infinity,
|
|
||||||
padding: const EdgeInsets.symmetric(vertical: 40),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
gradient: LinearGradient(
|
|
||||||
colors: [
|
|
||||||
const Color.fromARGB(255, 38, 255, 222),
|
|
||||||
const Color.fromARGB(255, 53, 132, 229)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
borderRadius: BorderRadius.circular(25),
|
|
||||||
boxShadow: [
|
|
||||||
BoxShadow(
|
|
||||||
color: const Color.fromARGB(255, 0, 191, 255).withOpacity(0.3),
|
|
||||||
blurRadius: 20,
|
|
||||||
offset: const Offset(0, 10),
|
|
||||||
)
|
|
||||||
],
|
|
||||||
),
|
|
||||||
child: Column(
|
|
||||||
children: [
|
|
||||||
const Icon(Icons.speed, color: Colors.white, size: 50),
|
|
||||||
const SizedBox(height: 10),
|
|
||||||
Text(
|
|
||||||
state.pressure.toStringAsFixed(1),
|
|
||||||
style: const TextStyle(
|
|
||||||
fontSize: 70,
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
color: Colors.white,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const Text(
|
|
||||||
"hPa",
|
|
||||||
style: TextStyle(color: Colors.white70, fontSize: 18),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _historyChart(AtmosphericConditionsState state) {
|
|
||||||
final history = state.history;
|
|
||||||
|
|
||||||
if (history.isEmpty) {
|
|
||||||
return _emptyCard("Grafik histori tekanan hari ini belum ada data");
|
|
||||||
}
|
|
||||||
|
|
||||||
final points = <FlSpot>[];
|
|
||||||
double minY = history.first.pressure;
|
|
||||||
double maxY = history.first.pressure;
|
|
||||||
|
|
||||||
for (int i = 0; i < history.length; i++) {
|
|
||||||
final pressure = history[i].pressure;
|
|
||||||
points.add(FlSpot(i.toDouble(), pressure));
|
|
||||||
|
|
||||||
if (pressure < minY) {
|
|
||||||
minY = pressure;
|
|
||||||
}
|
|
||||||
if (pressure > maxY) {
|
|
||||||
maxY = pressure;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
final range = (maxY - minY).abs();
|
|
||||||
final padding = range < 1 ? 0.5 : range * 0.15;
|
|
||||||
|
|
||||||
return Container(
|
|
||||||
width: double.infinity,
|
|
||||||
padding: const EdgeInsets.all(16),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.white,
|
|
||||||
borderRadius: BorderRadius.circular(20),
|
|
||||||
),
|
|
||||||
child: Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
const Text(
|
|
||||||
"Grafik Histori Tekanan Hari Ini",
|
|
||||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
SizedBox(
|
|
||||||
height: 220,
|
|
||||||
child: LineChart(
|
|
||||||
LineChartData(
|
|
||||||
minX: 0,
|
|
||||||
maxX: (history.length - 1).toDouble(),
|
|
||||||
minY: minY - padding,
|
|
||||||
maxY: maxY + padding,
|
|
||||||
gridData: FlGridData(show: true),
|
|
||||||
borderData: FlBorderData(show: false),
|
|
||||||
titlesData: FlTitlesData(
|
|
||||||
topTitles: AxisTitles(
|
|
||||||
sideTitles: SideTitles(showTitles: false),
|
|
||||||
),
|
|
||||||
rightTitles: AxisTitles(
|
|
||||||
sideTitles: SideTitles(showTitles: false),
|
|
||||||
),
|
|
||||||
leftTitles: AxisTitles(
|
|
||||||
sideTitles: SideTitles(
|
|
||||||
showTitles: true,
|
|
||||||
reservedSize: 44,
|
|
||||||
interval: range < 1 ? 0.5 : null,
|
|
||||||
getTitlesWidget: (value, meta) => Text(
|
|
||||||
value.toStringAsFixed(1),
|
|
||||||
style: const TextStyle(fontSize: 10),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
bottomTitles: AxisTitles(
|
|
||||||
sideTitles: SideTitles(
|
|
||||||
showTitles: true,
|
|
||||||
interval: history.length > 6 ? (history.length / 5).ceilToDouble() : 1,
|
|
||||||
getTitlesWidget: (value, meta) {
|
|
||||||
final index = value.toInt();
|
|
||||||
if (index < 0 || index >= history.length) {
|
|
||||||
return const SizedBox.shrink();
|
|
||||||
}
|
|
||||||
return Padding(
|
|
||||||
padding: const EdgeInsets.only(top: 6),
|
|
||||||
child: Text(
|
|
||||||
formatClockTime(history[index].timestamp),
|
|
||||||
style: const TextStyle(fontSize: 10),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
lineBarsData: [
|
|
||||||
LineChartBarData(
|
|
||||||
spots: points,
|
|
||||||
isCurved: true,
|
|
||||||
color: Colors.blue,
|
|
||||||
barWidth: 3,
|
|
||||||
dotData: FlDotData(show: history.length <= 10),
|
|
||||||
belowBarData: BarAreaData(
|
|
||||||
show: true,
|
|
||||||
color: Colors.blue.withOpacity(0.15),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _emptyCard(String text) {
|
|
||||||
return Container(
|
|
||||||
width: double.infinity,
|
|
||||||
padding: const EdgeInsets.all(20),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.white,
|
|
||||||
borderRadius: BorderRadius.circular(20),
|
|
||||||
),
|
|
||||||
child: Text(
|
|
||||||
text,
|
|
||||||
style: const TextStyle(color: Colors.grey),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class _HistoryTableCard extends StatefulWidget {
|
|
||||||
final List<AtmosphericConditions> history;
|
|
||||||
|
|
||||||
const _HistoryTableCard({required this.history});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<_HistoryTableCard> createState() => _HistoryTableCardState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _HistoryTableCardState extends State<_HistoryTableCard> {
|
|
||||||
static const int _rowsPerPage = 10;
|
|
||||||
int _currentPage = 0;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void didUpdateWidget(covariant _HistoryTableCard oldWidget) {
|
|
||||||
super.didUpdateWidget(oldWidget);
|
|
||||||
|
|
||||||
final totalPages = _totalPages;
|
|
||||||
if (totalPages > 0 && _currentPage >= totalPages) {
|
|
||||||
_currentPage = totalPages - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int get _totalPages {
|
|
||||||
if (widget.history.isEmpty) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return (widget.history.length / _rowsPerPage).ceil();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<AtmosphericConditions> get _pageItems {
|
|
||||||
final reversedHistory = widget.history.reversed.toList();
|
|
||||||
final start = _currentPage * _rowsPerPage;
|
|
||||||
final end = (start + _rowsPerPage).clamp(0, reversedHistory.length);
|
|
||||||
return reversedHistory.sublist(start, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _goToPreviousPage() {
|
|
||||||
if (_currentPage > 0) {
|
|
||||||
setState(() => _currentPage--);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void _goToNextPage() {
|
|
||||||
if (_currentPage < _totalPages - 1) {
|
|
||||||
setState(() => _currentPage++);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
if (widget.history.isEmpty) {
|
|
||||||
return Container(
|
|
||||||
width: double.infinity,
|
|
||||||
padding: const EdgeInsets.all(20),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.white,
|
|
||||||
borderRadius: BorderRadius.circular(20),
|
|
||||||
),
|
|
||||||
child: const Text(
|
|
||||||
"Tabel histori tekanan hari ini belum ada data",
|
|
||||||
style: TextStyle(color: Colors.grey),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
final pageItems = _pageItems;
|
|
||||||
final hasMultiplePages = _totalPages > 1;
|
|
||||||
|
|
||||||
return Container(
|
|
||||||
width: double.infinity,
|
|
||||||
padding: const EdgeInsets.all(16),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.white,
|
|
||||||
borderRadius: BorderRadius.circular(20),
|
|
||||||
),
|
|
||||||
child: Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
const Text(
|
|
||||||
"Tabel Histori Tekanan Hari Ini",
|
|
||||||
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 4),
|
|
||||||
Text(
|
|
||||||
"Halaman ${_currentPage + 1} dari $_totalPages · ${widget.history.length} data",
|
|
||||||
style: const TextStyle(color: Colors.grey, fontSize: 12),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
SingleChildScrollView(
|
|
||||||
scrollDirection: Axis.horizontal,
|
|
||||||
child: DataTable(
|
|
||||||
columns: const [
|
|
||||||
DataColumn(label: Text("No")),
|
|
||||||
DataColumn(label: Text("Waktu")),
|
|
||||||
DataColumn(label: Text("Tekanan")),
|
|
||||||
],
|
|
||||||
rows: List<DataRow>.generate(pageItems.length, (index) {
|
|
||||||
final item = pageItems[index];
|
|
||||||
final absoluteIndex = (_currentPage * _rowsPerPage) + index + 1;
|
|
||||||
return DataRow(
|
|
||||||
cells: [
|
|
||||||
DataCell(Text(absoluteIndex.toString())),
|
|
||||||
DataCell(Text(formatClockTime(item.timestamp))),
|
|
||||||
DataCell(Text("${item.pressure.toStringAsFixed(1)} hPa")),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
if (hasMultiplePages) ...[
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
||||||
children: [
|
|
||||||
TextButton.icon(
|
|
||||||
onPressed: _currentPage == 0 ? null : _goToPreviousPage,
|
|
||||||
icon: const Icon(Icons.chevron_left),
|
|
||||||
label: const Text('Sebelumnya'),
|
|
||||||
),
|
|
||||||
TextButton.icon(
|
|
||||||
onPressed: _currentPage >= _totalPages - 1 ? null : _goToNextPage,
|
|
||||||
icon: const Icon(Icons.chevron_right),
|
|
||||||
label: const Text('Berikutnya'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -10,6 +10,7 @@ class AtmosphericConditionsBloc
|
||||||
extends Bloc<AtmosphericConditionsEvent, AtmosphericConditionsState> {
|
extends Bloc<AtmosphericConditionsEvent, AtmosphericConditionsState> {
|
||||||
final MonitoringRepository _repository;
|
final MonitoringRepository _repository;
|
||||||
StreamSubscription<AtmosphericConditions>? _subscription;
|
StreamSubscription<AtmosphericConditions>? _subscription;
|
||||||
|
static const int _maxHistoryItems = 1500;
|
||||||
|
|
||||||
AtmosphericConditionsBloc({required MonitoringRepository repository})
|
AtmosphericConditionsBloc({required MonitoringRepository repository})
|
||||||
: _repository = repository,
|
: _repository = repository,
|
||||||
|
|
@ -24,9 +25,35 @@ class AtmosphericConditionsBloc
|
||||||
Emitter<AtmosphericConditionsState> emit,
|
Emitter<AtmosphericConditionsState> emit,
|
||||||
) async {
|
) async {
|
||||||
emit(state.copyWith(isLoading: true));
|
emit(state.copyWith(isLoading: true));
|
||||||
|
|
||||||
await _subscription?.cancel();
|
await _subscription?.cancel();
|
||||||
|
|
||||||
|
final historyFromFirebase = await _repository.getSensorHistory(
|
||||||
|
'sensor/history',
|
||||||
|
(json) => AtmosphericConditions.fromJson(json),
|
||||||
|
);
|
||||||
|
|
||||||
|
final today = DateTime.now();
|
||||||
|
final todayHistory = historyFromFirebase
|
||||||
|
.where((item) => _isSameDay(item.timestamp, today))
|
||||||
|
.toList()
|
||||||
|
..sort((a, b) => a.timestamp.compareTo(b.timestamp));
|
||||||
|
|
||||||
|
final trimmedHistory = todayHistory.length > _maxHistoryItems
|
||||||
|
? todayHistory.sublist(todayHistory.length - _maxHistoryItems)
|
||||||
|
: todayHistory;
|
||||||
|
|
||||||
|
final latestFromHistory =
|
||||||
|
trimmedHistory.isNotEmpty ? trimmedHistory.last : null;
|
||||||
|
|
||||||
|
emit(state.copyWith(
|
||||||
|
temperature: latestFromHistory?.temperature ?? state.temperature,
|
||||||
|
humidity: latestFromHistory?.humidity ?? state.humidity,
|
||||||
|
pressure: latestFromHistory?.pressure ?? state.pressure,
|
||||||
|
altitude: latestFromHistory?.altitude ?? state.altitude,
|
||||||
|
history: trimmedHistory,
|
||||||
|
isLoading: false,
|
||||||
|
));
|
||||||
|
|
||||||
_subscription = _repository
|
_subscription = _repository
|
||||||
.getSensorStream(
|
.getSensorStream(
|
||||||
'sensor/latest',
|
'sensor/latest',
|
||||||
|
|
@ -42,15 +69,36 @@ class AtmosphericConditionsBloc
|
||||||
_AtmosphericConditionsUpdated event,
|
_AtmosphericConditionsUpdated event,
|
||||||
Emitter<AtmosphericConditionsState> emit,
|
Emitter<AtmosphericConditionsState> emit,
|
||||||
) {
|
) {
|
||||||
|
final now = DateTime.now();
|
||||||
|
final updatedHistory =
|
||||||
|
state.history.where((item) => _isSameDay(item.timestamp, now)).toList();
|
||||||
|
|
||||||
|
final isDuplicate = updatedHistory.isNotEmpty &&
|
||||||
|
updatedHistory.last.timestamp == event.data.timestamp &&
|
||||||
|
updatedHistory.last.pressure == event.data.pressure &&
|
||||||
|
updatedHistory.last.humidity == event.data.humidity;
|
||||||
|
|
||||||
|
if (!isDuplicate) {
|
||||||
|
updatedHistory.add(event.data);
|
||||||
|
if (updatedHistory.length > _maxHistoryItems) {
|
||||||
|
updatedHistory.removeAt(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
temperature: event.data.temperature,
|
temperature: event.data.temperature,
|
||||||
humidity: event.data.humidity,
|
humidity: event.data.humidity,
|
||||||
pressure: event.data.pressure,
|
pressure: event.data.pressure,
|
||||||
altitude: event.data.altitude,
|
altitude: event.data.altitude,
|
||||||
|
history: updatedHistory,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool _isSameDay(DateTime a, DateTime b) {
|
||||||
|
return a.year == b.year && a.month == b.month && a.day == b.day;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> close() async {
|
Future<void> close() async {
|
||||||
await _subscription?.cancel();
|
await _subscription?.cancel();
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ class AtmosphericConditionsState extends Equatable {
|
||||||
final double humidity;
|
final double humidity;
|
||||||
final double pressure;
|
final double pressure;
|
||||||
final double altitude;
|
final double altitude;
|
||||||
|
final List<AtmosphericConditions> history;
|
||||||
final bool isLoading;
|
final bool isLoading;
|
||||||
|
|
||||||
const AtmosphericConditionsState({
|
const AtmosphericConditionsState({
|
||||||
|
|
@ -13,6 +13,7 @@ class AtmosphericConditionsState extends Equatable {
|
||||||
this.humidity = 0.0,
|
this.humidity = 0.0,
|
||||||
this.pressure = 0.0,
|
this.pressure = 0.0,
|
||||||
this.altitude = 0.0,
|
this.altitude = 0.0,
|
||||||
|
this.history = const [],
|
||||||
this.isLoading = true,
|
this.isLoading = true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -21,6 +22,7 @@ class AtmosphericConditionsState extends Equatable {
|
||||||
double? humidity,
|
double? humidity,
|
||||||
double? pressure,
|
double? pressure,
|
||||||
double? altitude,
|
double? altitude,
|
||||||
|
List<AtmosphericConditions>? history,
|
||||||
bool? isLoading,
|
bool? isLoading,
|
||||||
}) {
|
}) {
|
||||||
return AtmosphericConditionsState(
|
return AtmosphericConditionsState(
|
||||||
|
|
@ -28,6 +30,7 @@ class AtmosphericConditionsState extends Equatable {
|
||||||
humidity: humidity ?? this.humidity,
|
humidity: humidity ?? this.humidity,
|
||||||
pressure: pressure ?? this.pressure,
|
pressure: pressure ?? this.pressure,
|
||||||
altitude: altitude ?? this.altitude,
|
altitude: altitude ?? this.altitude,
|
||||||
|
history: history ?? this.history,
|
||||||
isLoading: isLoading ?? this.isLoading,
|
isLoading: isLoading ?? this.isLoading,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -38,6 +41,7 @@ class AtmosphericConditionsState extends Equatable {
|
||||||
humidity,
|
humidity,
|
||||||
pressure,
|
pressure,
|
||||||
altitude,
|
altitude,
|
||||||
|
history,
|
||||||
isLoading,
|
isLoading,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,18 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:fl_chart/fl_chart.dart';
|
||||||
|
import 'package:monitoring_repository/monitoring_repository.dart';
|
||||||
import '../blocs/atmospheric_conditions_bloc.dart';
|
import '../blocs/atmospheric_conditions_bloc.dart';
|
||||||
import '../../shared/utils/pdf/pdf_export_service.dart';
|
import '../../shared/utils/pdf/pdf_export_service.dart';
|
||||||
import '../../shared/widgets/export_pdf_button.dart';
|
import '../../shared/widgets/export_pdf_button.dart';
|
||||||
|
|
||||||
|
String formatClockTime(DateTime timestamp) {
|
||||||
|
final h = timestamp.hour.toString().padLeft(2, '0');
|
||||||
|
final m = timestamp.minute.toString().padLeft(2, '0');
|
||||||
|
final s = timestamp.second.toString().padLeft(2, '0');
|
||||||
|
return '$h:$m:$s';
|
||||||
|
}
|
||||||
|
|
||||||
class AtmosphericScreen extends StatelessWidget {
|
class AtmosphericScreen extends StatelessWidget {
|
||||||
const AtmosphericScreen({super.key});
|
const AtmosphericScreen({super.key});
|
||||||
|
|
||||||
|
|
@ -13,7 +22,7 @@ class AtmosphericScreen extends StatelessWidget {
|
||||||
backgroundColor: Colors.grey.shade100,
|
backgroundColor: Colors.grey.shade100,
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text(
|
title: const Text(
|
||||||
"Kondisi Atmosfer",
|
'Kondisi Atmosfer',
|
||||||
style: TextStyle(fontWeight: FontWeight.bold),
|
style: TextStyle(fontWeight: FontWeight.bold),
|
||||||
),
|
),
|
||||||
centerTitle: true,
|
centerTitle: true,
|
||||||
|
|
@ -32,20 +41,31 @@ class AtmosphericScreen extends StatelessWidget {
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
_mainTemperature(state),
|
_mainTemperature(state),
|
||||||
const SizedBox(height: 25),
|
const SizedBox(height: 24),
|
||||||
_gridInfo(state),
|
_gridInfo(state),
|
||||||
const SizedBox(height: 25),
|
const SizedBox(height: 24),
|
||||||
|
_historyChart(state),
|
||||||
// ← Tombol Export PDF
|
const SizedBox(height: 24),
|
||||||
|
_HistoryTableCard(history: state.history),
|
||||||
|
const SizedBox(height: 24),
|
||||||
ExportPdfButton(
|
ExportPdfButton(
|
||||||
onExport: () => PdfExportService.atmospheric(
|
onExport: () => PdfExportService.atmospheric(
|
||||||
temperature: state.temperature,
|
temperature: state.temperature,
|
||||||
humidity: state.humidity,
|
humidity: state.humidity,
|
||||||
pressure: state.pressure,
|
pressure: state.pressure,
|
||||||
altitude: state.altitude,
|
altitude: state.altitude,
|
||||||
timestamp: DateTime.now(),
|
timestamp: state.history.isNotEmpty
|
||||||
// atmospheric state belum punya history,
|
? state.history.last.timestamp
|
||||||
// kalau nanti ditambah tinggal isi historyData di sini
|
: DateTime.now(),
|
||||||
|
historyData: state.history
|
||||||
|
.map((entry) => {
|
||||||
|
'timestamp': entry.timestamp,
|
||||||
|
'temperature': entry.temperature,
|
||||||
|
'humidity': entry.humidity,
|
||||||
|
'pressure': entry.pressure,
|
||||||
|
'altitude': entry.altitude,
|
||||||
|
})
|
||||||
|
.toList(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
@ -64,18 +84,18 @@ class AtmosphericScreen extends StatelessWidget {
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
padding: const EdgeInsets.symmetric(vertical: 40),
|
padding: const EdgeInsets.symmetric(vertical: 40),
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
gradient: LinearGradient(
|
gradient: const LinearGradient(
|
||||||
colors: [
|
colors: [
|
||||||
const Color.fromARGB(255, 38, 255, 222),
|
Color.fromARGB(255, 38, 255, 222),
|
||||||
const Color.fromARGB(255, 53, 132, 229)
|
Color.fromARGB(255, 53, 132, 229),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
borderRadius: BorderRadius.circular(25),
|
borderRadius: BorderRadius.circular(25),
|
||||||
boxShadow: [
|
boxShadow: const [
|
||||||
BoxShadow(
|
BoxShadow(
|
||||||
color: const Color.fromARGB(255, 0, 191, 255).withOpacity(0.3),
|
color: Color.fromRGBO(0, 191, 255, 0.3),
|
||||||
blurRadius: 20,
|
blurRadius: 20,
|
||||||
offset: const Offset(0, 10),
|
offset: Offset(0, 10),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|
@ -92,7 +112,7 @@ class AtmosphericScreen extends StatelessWidget {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const Text(
|
const Text(
|
||||||
"°C",
|
'°C',
|
||||||
style: TextStyle(color: Colors.white70, fontSize: 18),
|
style: TextStyle(color: Colors.white70, fontSize: 18),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
@ -113,20 +133,20 @@ class AtmosphericScreen extends StatelessWidget {
|
||||||
childAspectRatio: 1.6,
|
childAspectRatio: 1.6,
|
||||||
children: [
|
children: [
|
||||||
_infoCard(
|
_infoCard(
|
||||||
"Kelembapan",
|
'Kelembapan',
|
||||||
"${state.humidity.toStringAsFixed(1)} %",
|
'${state.humidity.toStringAsFixed(1)} %',
|
||||||
Icons.water_drop,
|
Icons.water_drop,
|
||||||
Colors.blue,
|
Colors.blue,
|
||||||
),
|
),
|
||||||
_infoCard(
|
_infoCard(
|
||||||
"Tekanan",
|
'Tekanan',
|
||||||
"${state.pressure.toStringAsFixed(1)} hPa",
|
'${state.pressure.toStringAsFixed(1)} hPa',
|
||||||
Icons.speed,
|
Icons.speed,
|
||||||
Colors.green,
|
Colors.green,
|
||||||
),
|
),
|
||||||
_infoCard(
|
_infoCard(
|
||||||
"Ketinggian",
|
'Ketinggian',
|
||||||
"${state.altitude.toStringAsFixed(1)} m",
|
'${state.altitude.toStringAsFixed(1)} m',
|
||||||
Icons.terrain,
|
Icons.terrain,
|
||||||
Colors.brown,
|
Colors.brown,
|
||||||
),
|
),
|
||||||
|
|
@ -158,7 +178,204 @@ class AtmosphericScreen extends StatelessWidget {
|
||||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _historyChart(AtmosphericConditionsState state) {
|
||||||
|
final history = state.history;
|
||||||
|
|
||||||
|
if (history.isEmpty) {
|
||||||
|
return _emptyCard('Grafik histori tekanan hari ini belum ada data');
|
||||||
|
}
|
||||||
|
|
||||||
|
final points = <FlSpot>[];
|
||||||
|
double minY = history.first.pressure;
|
||||||
|
double maxY = history.first.pressure;
|
||||||
|
|
||||||
|
for (var i = 0; i < history.length; i++) {
|
||||||
|
final pressure = history[i].pressure;
|
||||||
|
points.add(FlSpot(i.toDouble(), pressure));
|
||||||
|
minY = pressure < minY ? pressure : minY;
|
||||||
|
maxY = pressure > maxY ? pressure : maxY;
|
||||||
|
}
|
||||||
|
|
||||||
|
final range = (maxY - minY).abs();
|
||||||
|
final padding = range < 1 ? 0.5 : range * 0.15;
|
||||||
|
|
||||||
|
return Container(
|
||||||
|
width: double.infinity,
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.white,
|
||||||
|
borderRadius: BorderRadius.circular(20),
|
||||||
|
),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'Grafik Histori Tekanan Hari Ini',
|
||||||
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
SizedBox(
|
||||||
|
height: 220,
|
||||||
|
child: LineChart(
|
||||||
|
LineChartData(
|
||||||
|
minX: 0,
|
||||||
|
maxX: history.length > 1 ? (history.length - 1).toDouble() : 1,
|
||||||
|
minY: minY - padding,
|
||||||
|
maxY: maxY + padding,
|
||||||
|
gridData: const FlGridData(show: true),
|
||||||
|
borderData: FlBorderData(show: false),
|
||||||
|
titlesData: FlTitlesData(
|
||||||
|
topTitles: const AxisTitles(
|
||||||
|
sideTitles: SideTitles(showTitles: false)),
|
||||||
|
rightTitles: const AxisTitles(
|
||||||
|
sideTitles: SideTitles(showTitles: false)),
|
||||||
|
leftTitles: AxisTitles(
|
||||||
|
sideTitles: SideTitles(
|
||||||
|
showTitles: true,
|
||||||
|
reservedSize: 44,
|
||||||
|
interval: range < 1 ? 0.5 : null,
|
||||||
|
getTitlesWidget: (value, meta) => Text(
|
||||||
|
value.toStringAsFixed(1),
|
||||||
|
style: const TextStyle(fontSize: 10),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
bottomTitles: AxisTitles(
|
||||||
|
sideTitles: SideTitles(
|
||||||
|
showTitles: true,
|
||||||
|
interval: history.length > 6
|
||||||
|
? (history.length / 5).ceilToDouble()
|
||||||
|
: 1,
|
||||||
|
getTitlesWidget: (value, meta) {
|
||||||
|
final index = value.toInt();
|
||||||
|
if (index < 0 || index >= history.length) {
|
||||||
|
return const SizedBox.shrink();
|
||||||
|
}
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 6),
|
||||||
|
child: Text(
|
||||||
|
formatClockTime(history[index].timestamp),
|
||||||
|
style: const TextStyle(fontSize: 10),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
lineBarsData: [
|
||||||
|
LineChartBarData(
|
||||||
|
spots: points,
|
||||||
|
isCurved: true,
|
||||||
|
color: Colors.blue,
|
||||||
|
barWidth: 3,
|
||||||
|
dotData: FlDotData(show: history.length <= 10),
|
||||||
|
belowBarData: BarAreaData(
|
||||||
|
show: true,
|
||||||
|
color: const Color.fromRGBO(33, 150, 243, 0.15),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _emptyCard(String text) {
|
||||||
|
return Container(
|
||||||
|
width: double.infinity,
|
||||||
|
padding: const EdgeInsets.all(20),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.white,
|
||||||
|
borderRadius: BorderRadius.circular(20),
|
||||||
|
),
|
||||||
|
child: Text(
|
||||||
|
text,
|
||||||
|
style: const TextStyle(color: Colors.grey),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _HistoryTableCard extends StatelessWidget {
|
||||||
|
final List<AtmosphericConditions> history;
|
||||||
|
|
||||||
|
const _HistoryTableCard({required this.history});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Container(
|
||||||
|
width: double.infinity,
|
||||||
|
padding: const EdgeInsets.all(16),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.white,
|
||||||
|
borderRadius: BorderRadius.circular(20),
|
||||||
|
),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'Riwayat Tekanan Hari Ini',
|
||||||
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
if (history.isEmpty)
|
||||||
|
const Text('Belum ada data histori hari ini.')
|
||||||
|
else ...[
|
||||||
|
Table(
|
||||||
|
columnWidths: const {
|
||||||
|
0: FlexColumnWidth(2),
|
||||||
|
1: FlexColumnWidth(1),
|
||||||
|
},
|
||||||
|
children: [
|
||||||
|
TableRow(
|
||||||
|
decoration: BoxDecoration(color: Colors.grey.shade100),
|
||||||
|
children: const [
|
||||||
|
Padding(
|
||||||
|
padding: EdgeInsets.symmetric(vertical: 8),
|
||||||
|
child: Text('Waktu',
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold)),
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: EdgeInsets.symmetric(vertical: 8),
|
||||||
|
child: Text('Tekanan',
|
||||||
|
style: TextStyle(fontWeight: FontWeight.bold)),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
...history.reversed.take(10).map(
|
||||||
|
(item) => TableRow(
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
|
child: Text(formatClockTime(item.timestamp)),
|
||||||
|
),
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
|
child:
|
||||||
|
Text('${item.pressure.toStringAsFixed(1)} hPa'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
if (history.length > 10) ...[
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
Text(
|
||||||
|
'Menampilkan 10 data terakhir dari ${history.length} data.',
|
||||||
|
style: const TextStyle(color: Colors.grey, fontSize: 12),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -113,17 +113,21 @@ class EvaporasiBloc extends Bloc<EvaporasiEvent, EvaporasiState> {
|
||||||
Emitter<EvaporasiState> emit,
|
Emitter<EvaporasiState> emit,
|
||||||
) {
|
) {
|
||||||
// Hindari update dobel: jika timestamp event sama dengan yang terakhir, jangan ubah bucket.
|
// Hindari update dobel: jika timestamp event sama dengan yang terakhir, jangan ubah bucket.
|
||||||
final previous = state.history.isNotEmpty ? state.history.last.timestamp : null;
|
final previous =
|
||||||
|
state.history.isNotEmpty ? state.history.last.timestamp : null;
|
||||||
|
|
||||||
|
final updatedHistory = List<Evaporasi>.from(state.history)..add(event.data);
|
||||||
|
|
||||||
final isDuplicate =
|
final isDuplicate =
|
||||||
previous != null && event.data.timestamp.toUtc() == previous.toUtc();
|
previous != null && event.data.timestamp.toUtc() == previous.toUtc();
|
||||||
|
|
||||||
// Update bucket berdasarkan timestamp event (bukan jam lokal sekarang).
|
// Update bucket berdasarkan timestamp event (bukan jam lokal sekarang).
|
||||||
final updated = isDuplicate ? state.dailyValues : List<double>.from(state.dailyValues);
|
final updated =
|
||||||
|
isDuplicate ? state.dailyValues : List<double>.from(state.dailyValues);
|
||||||
final updatedTemp = isDuplicate
|
final updatedTemp = isDuplicate
|
||||||
? state.dailyTemperatures
|
? state.dailyTemperatures
|
||||||
: List<double>.from(state.dailyTemperatures);
|
: List<double>.from(state.dailyTemperatures);
|
||||||
|
|
||||||
|
|
||||||
final eventTime = event.data.timestamp;
|
final eventTime = event.data.timestamp;
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
|
|
||||||
|
|
@ -143,6 +147,8 @@ class EvaporasiBloc extends Bloc<EvaporasiEvent, EvaporasiState> {
|
||||||
_emitEvaporasiAlert(status, rain, event.data.evaporasi);
|
_emitEvaporasiAlert(status, rain, event.data.evaporasi);
|
||||||
|
|
||||||
emit(state.copyWith(
|
emit(state.copyWith(
|
||||||
|
history: updatedHistory,
|
||||||
|
listData: updatedHistory,
|
||||||
currentValue: event.data.evaporasi,
|
currentValue: event.data.evaporasi,
|
||||||
temperature: event.data.suhu,
|
temperature: event.data.suhu,
|
||||||
waterLevel: event.data.tinggiAir,
|
waterLevel: event.data.tinggiAir,
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,7 @@ class FirebaseMonitoringRepo implements MonitoringRepository {
|
||||||
final Map<dynamic, dynamic> data = snapshot.value as Map;
|
final Map<dynamic, dynamic> data = snapshot.value as Map;
|
||||||
|
|
||||||
final list = data.values.map((item) {
|
final list = data.values.map((item) {
|
||||||
|
print(item);
|
||||||
return mapper(item as Map<dynamic, dynamic>);
|
return mapper(item as Map<dynamic, dynamic>);
|
||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,65 @@ class AtmosphericConditions {
|
||||||
});
|
});
|
||||||
|
|
||||||
factory AtmosphericConditions.fromJson(Map<dynamic, dynamic> json) {
|
factory AtmosphericConditions.fromJson(Map<dynamic, dynamic> json) {
|
||||||
|
double toDoubleSafe(dynamic value) {
|
||||||
|
if (value is num) return value.toDouble();
|
||||||
|
if (value is String) {
|
||||||
|
final normalized = value.trim().replaceAll(',', '.');
|
||||||
|
final match = RegExp(r'[-+]?[0-9]*\.?[0-9]+').firstMatch(normalized);
|
||||||
|
if (match != null) {
|
||||||
|
return double.tryParse(match.group(0)!) ?? 0.0;
|
||||||
|
}
|
||||||
|
return double.tryParse(normalized) ?? 0.0;
|
||||||
|
}
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DateTime parseTimestamp(dynamic rawTimestamp) {
|
||||||
|
if (rawTimestamp is int) {
|
||||||
|
return DateTime.fromMillisecondsSinceEpoch(rawTimestamp);
|
||||||
|
}
|
||||||
|
if (rawTimestamp is double) {
|
||||||
|
return DateTime.fromMillisecondsSinceEpoch(rawTimestamp.toInt());
|
||||||
|
}
|
||||||
|
if (rawTimestamp is String) {
|
||||||
|
final trimmed = rawTimestamp.trim();
|
||||||
|
final unixValue = int.tryParse(trimmed);
|
||||||
|
if (unixValue != null) {
|
||||||
|
if (unixValue < 1000000000000) {
|
||||||
|
return DateTime.fromMillisecondsSinceEpoch(unixValue * 1000);
|
||||||
|
}
|
||||||
|
return DateTime.fromMillisecondsSinceEpoch(unixValue);
|
||||||
|
}
|
||||||
|
final parsed = DateTime.tryParse(trimmed);
|
||||||
|
if (parsed != null) return parsed;
|
||||||
|
}
|
||||||
|
return DateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
final temperature = toDoubleSafe(
|
||||||
|
json['temperature'] ?? json['temp'] ?? json['t'] ?? 0,
|
||||||
|
);
|
||||||
|
final humidity = toDoubleSafe(json['humidity'] ?? json['hum'] ?? 0);
|
||||||
|
final pressure = toDoubleSafe(
|
||||||
|
json['pressure'] ?? json['press'] ?? json['tekanan'] ?? 0,
|
||||||
|
);
|
||||||
|
final altitude = toDoubleSafe(
|
||||||
|
json['altitude'] ?? json['alt'] ?? json['height'] ?? 0,
|
||||||
|
);
|
||||||
|
|
||||||
|
DateTime timestamp = DateTime.now();
|
||||||
|
final rawTimestamp =
|
||||||
|
json['timestamp'] ?? json['time'] ?? json['waktu'] ?? json['datetime'];
|
||||||
|
if (rawTimestamp != null) {
|
||||||
|
timestamp = parseTimestamp(rawTimestamp);
|
||||||
|
}
|
||||||
|
|
||||||
return AtmosphericConditions(
|
return AtmosphericConditions(
|
||||||
temperature: (json['temperature'] ?? 0).toDouble(),
|
temperature: temperature,
|
||||||
humidity: (json['humidity'] ?? 0).toDouble(),
|
humidity: humidity,
|
||||||
pressure: (json['pressure'] ?? 0).toDouble(),
|
pressure: pressure,
|
||||||
altitude: (json['altitude'] ?? 0).toDouble(),
|
altitude: altitude,
|
||||||
timestamp: DateTime.now(), // karena kamu pakai latest
|
timestamp: timestamp,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,20 @@ class Evaporasi {
|
||||||
json['evaporasi_k'],
|
json['evaporasi_k'],
|
||||||
);
|
);
|
||||||
|
|
||||||
final suhuVal = toDoubleSafe(
|
/// =========================
|
||||||
|
/// SUHU
|
||||||
|
/// =========================
|
||||||
|
final suhuParsed = toDoubleSafe(
|
||||||
|
json['suhu'] ??
|
||||||
|
json['suhu_air'] ??
|
||||||
|
json['suhuAir'] ??
|
||||||
|
json['temp'] ??
|
||||||
|
json['temperature'],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Filter nilai rusak
|
||||||
|
final suhuVal = (suhuParsed < -50 || suhuParsed > 100) ? 0.0 : suhuParsed;
|
||||||
|
toDoubleSafe(
|
||||||
json['suhu'] ??
|
json['suhu'] ??
|
||||||
json['suhu_air'] ??
|
json['suhu_air'] ??
|
||||||
json['suhuAir'] ??
|
json['suhuAir'] ??
|
||||||
|
|
@ -58,7 +71,7 @@ class Evaporasi {
|
||||||
// Banyak kemungkinan penamaan field tinggi air.
|
// Banyak kemungkinan penamaan field tinggi air.
|
||||||
// Pakai beberapa alias agar tidak default 0.
|
// Pakai beberapa alias agar tidak default 0.
|
||||||
final tinggiVal = toDoubleSafe(
|
final tinggiVal = toDoubleSafe(
|
||||||
json['tinggi_air_cm'] ??
|
json['tinggi'] ??
|
||||||
json['tinggi_air'] ??
|
json['tinggi_air'] ??
|
||||||
json['tinggiAir'] ??
|
json['tinggiAir'] ??
|
||||||
json['tinggiAir_cm'] ??
|
json['tinggiAir_cm'] ??
|
||||||
|
|
@ -70,17 +83,14 @@ class Evaporasi {
|
||||||
json['tinggiAir_m'],
|
json['tinggiAir_m'],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
// Default timestamp: fallback now (kalau field waktu tidak ada).
|
// Default timestamp: fallback now (kalau field waktu tidak ada).
|
||||||
// Catatan: untuk Firebase seharusnya timestamp dikirim konsisten (ms atau ISO string).
|
// Catatan: untuk Firebase seharusnya timestamp dikirim konsisten (ms atau ISO string).
|
||||||
DateTime timestamp = DateTime.now();
|
DateTime timestamp = DateTime.now();
|
||||||
|
|
||||||
// dukung beberapa kemungkinan penamaan timestamp
|
// dukung beberapa kemungkinan penamaan timestamp
|
||||||
final rawTimestamp =
|
final rawTimestamp = json['timestamp'] ?? json['time'] ?? json['datetime'];
|
||||||
json['timestamp'] ?? json['time'] ?? json['waktu'] ?? json['datetime'];
|
|
||||||
|
|
||||||
if (rawTimestamp != null) {
|
if (rawTimestamp != null) {
|
||||||
|
|
||||||
if (rawTimestamp is int) {
|
if (rawTimestamp is int) {
|
||||||
timestamp = DateTime.fromMillisecondsSinceEpoch(rawTimestamp);
|
timestamp = DateTime.fromMillisecondsSinceEpoch(rawTimestamp);
|
||||||
} else if (rawTimestamp is double) {
|
} else if (rawTimestamp is double) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue