diff --git a/lib/pages/device_managment/all_devices/helper/route_controls_based_code.dart b/lib/pages/device_managment/all_devices/helper/route_controls_based_code.dart index 81405451..5586a310 100644 --- a/lib/pages/device_managment/all_devices/helper/route_controls_based_code.dart +++ b/lib/pages/device_managment/all_devices/helper/route_controls_based_code.dart @@ -9,6 +9,7 @@ import 'package:syncrow_web/pages/device_managment/curtain/view/curtain_batch_st import 'package:syncrow_web/pages/device_managment/curtain/view/curtain_status_view.dart'; import 'package:syncrow_web/pages/device_managment/door_lock/view/door_lock_batch_control_view.dart'; import 'package:syncrow_web/pages/device_managment/door_lock/view/door_lock_control_view.dart'; +import 'package:syncrow_web/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_batch_control_view.dart'; import 'package:syncrow_web/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_control_view.dart'; import 'package:syncrow_web/pages/device_managment/garage_door/view/garage_door_batch_control_view.dart'; import 'package:syncrow_web/pages/device_managment/garage_door/view/garage_door_control_view.dart'; @@ -198,6 +199,10 @@ mixin RouteControlsBasedCode { return SOSBatchControlView( deviceIds: devices.where((e) => (e.productType == 'SOS')).map((e) => e.uuid!).toList(), ); + case 'NCPS': + return FlushMountedPresenceSensorBatchControlView( + devicesIds: devices.where((e) => (e.productType == 'NCPS')).map((e) => e.uuid!).toList(), + ); default: return const SizedBox(); } diff --git a/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart b/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart index 0bc15cd2..aea800dd 100644 --- a/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart +++ b/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_bloc.dart @@ -49,6 +49,9 @@ class FlushMountedPresenceSensorBloc on( _onFlushMountedPresenceSensorFactoryResetEvent, ); + on( + _onFlushMountedPresenceSensorStatusUpdatedEvent, + ); } void _onFlushMountedPresenceSensorFetchStatusEvent( @@ -60,7 +63,7 @@ class FlushMountedPresenceSensorBloc final response = await DevicesManagementApi().getDeviceStatus(deviceId); deviceStatus = FlushMountedPresenceSensorModel.fromJson(response.status); emit(FlushMountedPresenceSensorUpdateState(model: deviceStatus)); - _listenToChanges(emit, deviceId); + _listenToChanges(deviceId); } catch (e) { emit(FlushMountedPresenceSensorFailedState(error: e.toString())); return; @@ -81,31 +84,33 @@ class FlushMountedPresenceSensorBloc } } - Future _listenToChanges( - Emitter emit, - String deviceId, - ) async { - final ref = FirebaseDatabase.instance.ref( - 'device-status/$deviceId', - ); + void _listenToChanges(String deviceId) { + try { + final ref = FirebaseDatabase.instance.ref( + 'device-status/$deviceId', + ); + + ref.onValue.listen((event) { + final eventsMap = event.snapshot.value as Map; - await ref.onValue.listen( - (DatabaseEvent event) async { - Map usersMap = - event.snapshot.value as Map; List statusList = []; - - (usersMap['status'] as List?)?.forEach((element) { - statusList.add(Status(code: element['code'], value: element['value'])); + eventsMap['status'].forEach((element) { + statusList.add( + Status(code: element['code'], value: element['value']), + ); }); - + deviceStatus = FlushMountedPresenceSensorModel.fromJson(statusList); - if (!emit.isDone) { - emit(FlushMountedPresenceSensorLoadingNewSate(model: deviceStatus)); + if (!isClosed) { + add(FlushMountedPresenceSensorStatusUpdatedEvent(deviceStatus)); } - }, - onError: (error) => log(error.toString(), name: 'FirebaseDatabaseError'), - ).asFuture(); + }); + } catch (_) { + log( + 'Error listening to changes', + name: 'FlushMountedPresenceSensorBloc._listenToChanges', + ); + } } void _onFlushMountedPresenceSensorChangeValueEvent( @@ -234,4 +239,12 @@ class FlushMountedPresenceSensorBloc emit(FlushMountedPresenceSensorFailedState(error: e.toString())); } } + + void _onFlushMountedPresenceSensorStatusUpdatedEvent( + FlushMountedPresenceSensorStatusUpdatedEvent event, + Emitter emit, + ) { + deviceStatus = event.model; + emit(FlushMountedPresenceSensorUpdateState(model: deviceStatus)); + } } diff --git a/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_event.dart b/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_event.dart index f1636300..f70e7f3a 100644 --- a/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_event.dart +++ b/lib/pages/device_managment/flush_mounted_presence_sensor/bloc/flush_mounted_presence_sensor_event.dart @@ -10,6 +10,16 @@ sealed class FlushMountedPresenceSensorEvent extends Equatable { class FlushMountedPresenceSensorFetchStatusEvent extends FlushMountedPresenceSensorEvent {} +class FlushMountedPresenceSensorStatusUpdatedEvent + extends FlushMountedPresenceSensorEvent { + const FlushMountedPresenceSensorStatusUpdatedEvent(this.model); + + final FlushMountedPresenceSensorModel model; + + @override + List get props => [model]; +} + class FlushMountedPresenceSensorChangeValueEvent extends FlushMountedPresenceSensorEvent { final int value; diff --git a/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart b/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart index f1342eec..49fb517f 100644 --- a/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart +++ b/lib/pages/device_managment/flush_mounted_presence_sensor/factories/flush_mounted_presence_sensor_bloc_factory.dart @@ -16,6 +16,6 @@ abstract final class FlushMountedPresenceSensorBlocFactory { batchControlDevicesService: DebouncedBatchControlDevicesService( decoratee: RemoteBatchControlDevicesService(), ), - )..add(FlushMountedPresenceSensorFetchStatusEvent()); + ); } } diff --git a/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_batch_control_view.dart b/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_batch_control_view.dart index 7f1e726c..78db6e2e 100644 --- a/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_batch_control_view.dart +++ b/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_batch_control_view.dart @@ -22,7 +22,7 @@ class FlushMountedPresenceSensorBatchControlView extends StatelessWidget return BlocProvider( create: (context) => FlushMountedPresenceSensorBlocFactory.create( deviceId: devicesIds.first, - ), + )..add(FlushMountedPresenceSensorFetchBatchStatusEvent(devicesIds)), child: BlocBuilder( builder: (context, state) { @@ -67,7 +67,8 @@ class FlushMountedPresenceSensorBatchControlView extends StatelessWidget maxValue: 9, steps: 1, action: (int value) => context.read().add( - FlushMountedPresenceSensorChangeValueEvent( + FlushMountedPresenceSensorBatchControlEvent( + deviceIds: devicesIds, code: FlushMountedPresenceSensorModel.codeSensitivity, value: value, ), @@ -83,7 +84,8 @@ class FlushMountedPresenceSensorBatchControlView extends StatelessWidget valuesPercision: 1, action: (double value) => context.read().add( - FlushMountedPresenceSensorChangeValueEvent( + FlushMountedPresenceSensorBatchControlEvent( + deviceIds: devicesIds, code: FlushMountedPresenceSensorModel.codeNearDetection, value: (value * 100).toInt(), ), @@ -99,7 +101,8 @@ class FlushMountedPresenceSensorBatchControlView extends StatelessWidget valuesPercision: 1, action: (double value) => context.read().add( - FlushMountedPresenceSensorChangeValueEvent( + FlushMountedPresenceSensorBatchControlEvent( + deviceIds: devicesIds, code: FlushMountedPresenceSensorModel.codeFarDetection, value: (value * 100).toInt(), ), @@ -112,20 +115,22 @@ class FlushMountedPresenceSensorBatchControlView extends StatelessWidget maxValue: 3, steps: 1, action: (int value) => context.read().add( - FlushMountedPresenceSensorChangeValueEvent( + FlushMountedPresenceSensorBatchControlEvent( + deviceIds: devicesIds, code: FlushMountedPresenceSensorModel.codePresenceDelay, value: value, ), ), ), PresenceUpdateData( - value: (model.occurDistReduce.toDouble()), + value: model.occurDistReduce.toDouble(), title: 'Indent Level:', minValue: 0, maxValue: 3, steps: 1, action: (int value) => context.read().add( - FlushMountedPresenceSensorChangeValueEvent( + FlushMountedPresenceSensorBatchControlEvent( + deviceIds: devicesIds, code: FlushMountedPresenceSensorModel.codeOccurDistReduce, value: value, ), @@ -139,7 +144,8 @@ class FlushMountedPresenceSensorBatchControlView extends StatelessWidget maxValue: 3, steps: 1, action: (int value) => context.read().add( - FlushMountedPresenceSensorChangeValueEvent( + FlushMountedPresenceSensorBatchControlEvent( + deviceIds: devicesIds, code: FlushMountedPresenceSensorModel.codeSensiReduce, value: value, ), @@ -154,7 +160,8 @@ class FlushMountedPresenceSensorBatchControlView extends StatelessWidget steps: 1, action: (double value) => context.read().add( - FlushMountedPresenceSensorChangeValueEvent( + FlushMountedPresenceSensorBatchControlEvent( + deviceIds: devicesIds, code: FlushMountedPresenceSensorModel.codeNoneDelay, value: (value * 10).round(), ), diff --git a/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_control_view.dart b/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_control_view.dart index bdefa507..f0815c60 100644 --- a/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_control_view.dart +++ b/lib/pages/device_managment/flush_mounted_presence_sensor/views/flush_mounted_presence_sensor_control_view.dart @@ -24,7 +24,7 @@ class FlushMountedPresenceSensorControlView extends StatelessWidget return BlocProvider( create: (context) => FlushMountedPresenceSensorBlocFactory.create( deviceId: device.uuid ?? '-1', - ), + )..add(FlushMountedPresenceSensorFetchStatusEvent()), child: BlocBuilder( builder: (context, state) { diff --git a/lib/services/batch_control_devices_service.dart b/lib/services/batch_control_devices_service.dart index 76dbe480..de5af9ee 100644 --- a/lib/services/batch_control_devices_service.dart +++ b/lib/services/batch_control_devices_service.dart @@ -51,7 +51,7 @@ final class DebouncedBatchControlDevicesService DebouncedBatchControlDevicesService({ required this.decoratee, - this.debounceDuration = const Duration(milliseconds: 1500), + this.debounceDuration = const Duration(milliseconds: 800), }); @override diff --git a/lib/services/control_device_service.dart b/lib/services/control_device_service.dart index ab04a398..9913b52e 100644 --- a/lib/services/control_device_service.dart +++ b/lib/services/control_device_service.dart @@ -40,7 +40,7 @@ final class DebouncedControlDeviceService implements ControlDeviceService { DebouncedControlDeviceService({ required this.decoratee, - this.debounceDuration = const Duration(milliseconds: 1500), + this.debounceDuration = const Duration(milliseconds: 800), }); final _pendingRequests = <(String deviceUuid, Status status)>[];