pembaruan firebase service untuk manual mode
This commit is contained in:
parent
b4ac6fcaa8
commit
e0a3d3cf25
|
@ -18,6 +18,7 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
double humidity = 0.0;
|
||||
bool fanOn = false;
|
||||
bool pumpOn = false;
|
||||
bool isManual = false;
|
||||
int _selectedIndex = 0;
|
||||
late AnimationController _controller;
|
||||
late Animation<double> _fadeAnim;
|
||||
|
@ -54,6 +55,8 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
setState(() {
|
||||
fanOn = status;
|
||||
});
|
||||
// Sinkronkan ke control/kipas
|
||||
_firebaseService.setControlFanFromStatus(status);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -63,6 +66,17 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
setState(() {
|
||||
pumpOn = status;
|
||||
});
|
||||
// Sinkronkan ke control/pompa
|
||||
_firebaseService.setControlPumpFromStatus(status);
|
||||
}
|
||||
});
|
||||
|
||||
// Listen to manual mode
|
||||
_firebaseService.getManualMode().listen((manual) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
isManual = manual;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -128,12 +142,15 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
required String label,
|
||||
required IconData icon,
|
||||
required Color color,
|
||||
required bool enabled,
|
||||
}) {
|
||||
return Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: onTap,
|
||||
onTap: enabled ? onTap : null,
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
child: Opacity(
|
||||
opacity: enabled ? 1.0 : 0.5,
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
|
@ -199,6 +216,7 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -434,6 +452,26 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
),
|
||||
],
|
||||
),
|
||||
// Tambahkan switch manual mode di sini
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Text(
|
||||
'Mode Manual',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
Switch(
|
||||
value: isManual,
|
||||
onChanged: (val) {
|
||||
_firebaseService.setManualMode(val);
|
||||
},
|
||||
activeColor: greenPrimary,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SizedBox(height: gridPadding),
|
||||
// Grid 2x2
|
||||
Expanded(
|
||||
|
@ -444,7 +482,7 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
mainAxisSpacing: gridSpacing,
|
||||
crossAxisSpacing: gridSpacing,
|
||||
childAspectRatio: 1,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: [
|
||||
// Temperature
|
||||
_buildSensorCard(
|
||||
|
@ -469,6 +507,7 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
label: 'Kipas',
|
||||
icon: Icons.ac_unit_rounded,
|
||||
color: greenPrimary,
|
||||
enabled: isManual,
|
||||
),
|
||||
// Pump Control
|
||||
_buildControlButton(
|
||||
|
@ -477,6 +516,7 @@ class _DashboardPageState extends State<DashboardPage>
|
|||
label: 'Pompa',
|
||||
icon: Icons.water_rounded,
|
||||
color: greenPrimary,
|
||||
enabled: isManual,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -80,4 +80,24 @@ class FirebaseService {
|
|||
Future<void> deleteHistory(String id) async {
|
||||
await _database.child('riwayat').child(id).remove();
|
||||
}
|
||||
|
||||
// Stream untuk mendapatkan mode manual
|
||||
Stream<bool> getManualMode() {
|
||||
return _database.child('control/manual').onValue.map((event) {
|
||||
final data = event.snapshot.value as bool?;
|
||||
return data ?? false;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> setManualMode(bool value) async {
|
||||
await _database.child('control/manual').set(value);
|
||||
}
|
||||
|
||||
Future<void> setControlFanFromStatus(bool status) async {
|
||||
await _database.child('control/kipas').set(status ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
Future<void> setControlPumpFromStatus(bool status) async {
|
||||
await _database.child('control/pompa').set(status ? "ON" : "OFF");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue