mirror of
https://github.com/SyncrowIOT/web.git
synced 2026-10-27 03:57:55 +00:00
making good progress on realtime data integration.
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
import 'package:syncrow_web/pages/analytics/services/realtime_device_service/realtime_device_service.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
||||
|
||||
class FirebaseRealtimeDeviceService implements RealtimeDeviceService {
|
||||
@override
|
||||
Stream<Status> subscribe(String deviceId) {
|
||||
try {
|
||||
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
|
||||
return ref.onValue.asyncMap((DatabaseEvent event) {
|
||||
final data = event.snapshot.value as Map<dynamic, dynamic>?;
|
||||
|
||||
if (data == null || data['status'] == null) {
|
||||
throw Exception('Invalid data received from Firebase');
|
||||
}
|
||||
|
||||
final statusMap = data['status'] as Map<dynamic, dynamic>;
|
||||
return Status(
|
||||
code: statusMap['code'] as String? ?? '',
|
||||
value: statusMap['value'] as String? ?? '',
|
||||
);
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Error subscribing to device status: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
||||
|
||||
abstract interface class RealtimeDeviceService {
|
||||
Stream<Status> subscribe(String deviceId);
|
||||
}
|
||||
Reference in New Issue
Block a user