From c354abbeca4d5f96b6030de9df7aa38550454064 Mon Sep 17 00:00:00 2001 From: ashrafzarkanisala Date: Wed, 18 Sep 2024 13:55:36 +0300 Subject: [PATCH] push wall sensor batch control --- .../ceiling_sensor/bloc/state.dart | 9 -- .../wall_sensor/bloc/bloc.dart | 82 ++++++++++++++----- .../wall_sensor/bloc/event.dart | 25 +++++- .../view/wall_sensor_batch_control.dart | 14 ++-- .../view/wall_sensor_conrtols.dart | 4 +- 5 files changed, 93 insertions(+), 41 deletions(-) diff --git a/lib/pages/device_managment/ceiling_sensor/bloc/state.dart b/lib/pages/device_managment/ceiling_sensor/bloc/state.dart index d9144474..0bd7e4ed 100644 --- a/lib/pages/device_managment/ceiling_sensor/bloc/state.dart +++ b/lib/pages/device_managment/ceiling_sensor/bloc/state.dart @@ -66,12 +66,3 @@ class ShowCeilingDescriptionState extends CeilingSensorState { @override List get props => [description]; } - -class CeilingBatchControlSuccessState extends CeilingSensorState { - final CeilingSensorModel ceilingSensorModel; - - const CeilingBatchControlSuccessState({required this.ceilingSensorModel}); - - @override - List get props => [ceilingSensorModel]; -} diff --git a/lib/pages/device_managment/wall_sensor/bloc/bloc.dart b/lib/pages/device_managment/wall_sensor/bloc/bloc.dart index 86e1d504..cc96955a 100644 --- a/lib/pages/device_managment/wall_sensor/bloc/bloc.dart +++ b/lib/pages/device_managment/wall_sensor/bloc/bloc.dart @@ -12,16 +12,17 @@ class WallSensorBloc extends Bloc { Timer? _timer; WallSensorBloc({required this.deviceId}) : super(WallSensorInitialState()) { - on(_fetchWallSensorStatus); - on(_fetchWallSensorBatchControl); + on(_fetchWallSensorStatus); + on(_fetchWallSensorBatchControl); on(_changeValue); + on(_onBatchControl); on(_getDeviceReports); on(_showDescription); on(_backToGridView); } void _fetchWallSensorStatus( - WallSensorInitialEvent event, Emitter emit) async { + WallSensorFetchStatusEvent event, Emitter emit) async { emit(WallSensorLoadingInitialState()); try { var response = await DevicesManagementApi().getDeviceStatus(deviceId); @@ -34,6 +35,21 @@ class WallSensorBloc extends Bloc { } } + // Fetch batch status + FutureOr _fetchWallSensorBatchControl( + WallSensorFetchBatchStatusEvent event, + Emitter emit) async { + emit(WallSensorLoadingInitialState()); + try { + var response = + await DevicesManagementApi().getBatchStatus(event.devicesIds); + deviceStatus = WallSensorModel.fromJson(response.status); + emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); + } catch (e) { + emit(WallSensorFailedState(error: e.toString())); + } + } + // _listenToChanges() { // try { // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId'); @@ -67,28 +83,63 @@ class WallSensorBloc extends Bloc { } emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); await _runDeBouncer( - deviceId: deviceId, code: event.code, value: event.value); + deviceId: deviceId, + code: event.code, + value: event.value, + isBatch: false, + emit: emit, + ); + } + + Future _onBatchControl( + WallSensorBatchControlEvent event, Emitter emit) async { + emit(WallSensorLoadingNewSate(wallSensorModel: deviceStatus)); + if (event.code == 'far_detection') { + deviceStatus.farDetection = event.value; + } else if (event.code == 'motionless_sensitivity') { + deviceStatus.motionlessSensitivity = event.value; + } else if (event.code == 'motion_sensitivity_value') { + deviceStatus.motionSensitivity = event.value; + } else if (event.code == 'no_one_time') { + deviceStatus.noBodyTime = event.value; + } + emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); + await _runDeBouncer( + deviceId: event.deviceIds, + code: event.code, + value: event.value, + emit: emit, + isBatch: true, + ); } _runDeBouncer({ - required String deviceId, + required dynamic deviceId, required String code, required dynamic value, + required Emitter emit, + required bool isBatch, }) { if (_timer != null) { _timer!.cancel(); } _timer = Timer(const Duration(seconds: 1), () async { try { - final response = await DevicesManagementApi() - .deviceControl(deviceId, Status(code: code, value: value)); + late bool response; + if (isBatch) { + response = await DevicesManagementApi() + .deviceBatchControl(deviceId, code, value); + } else { + response = await DevicesManagementApi() + .deviceControl(deviceId, Status(code: code, value: value)); + } if (!response) { - add(WallSensorInitialEvent()); + add(WallSensorFetchStatusEvent()); } } catch (_) { await Future.delayed(const Duration(milliseconds: 500)); - add(WallSensorInitialEvent()); + add(WallSensorFetchStatusEvent()); } }); } @@ -117,17 +168,4 @@ class WallSensorBloc extends Bloc { BackToGridViewEvent event, Emitter emit) { emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); } - - FutureOr _fetchWallSensorBatchControl( - WallSensorBatchControlEvent event, Emitter emit) async { - emit(WallSensorLoadingInitialState()); - try { - var response = await DevicesManagementApi().getDeviceStatus(deviceId); - deviceStatus = WallSensorModel.fromJson(response.status); - emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); - } catch (e) { - emit(WallSensorFailedState(error: e.toString())); - return; - } - } } diff --git a/lib/pages/device_managment/wall_sensor/bloc/event.dart b/lib/pages/device_managment/wall_sensor/bloc/event.dart index c40d5e3e..f09c7123 100644 --- a/lib/pages/device_managment/wall_sensor/bloc/event.dart +++ b/lib/pages/device_managment/wall_sensor/bloc/event.dart @@ -7,7 +7,7 @@ abstract class WallSensorEvent extends Equatable { List get props => []; } -class WallSensorInitialEvent extends WallSensorEvent {} +class WallSensorFetchStatusEvent extends WallSensorEvent {} class WallSensorChangeValueEvent extends WallSensorEvent { final int value; @@ -18,8 +18,12 @@ class WallSensorChangeValueEvent extends WallSensorEvent { List get props => [value, code]; } -class WallSensorBatchControlEvent extends WallSensorEvent { - const WallSensorBatchControlEvent(); +class WallSensorFetchBatchStatusEvent extends WallSensorEvent { + final List devicesIds; + const WallSensorFetchBatchStatusEvent(this.devicesIds); + + @override + List get props => [devicesIds]; } class GetDeviceReportsEvent extends WallSensorEvent { @@ -40,3 +44,18 @@ class ShowDescriptionEvent extends WallSensorEvent { } class BackToGridViewEvent extends WallSensorEvent {} + +class WallSensorBatchControlEvent extends WallSensorEvent { + final List deviceIds; + final String code; + final dynamic value; + + const WallSensorBatchControlEvent({ + required this.deviceIds, + required this.code, + required this.value, + }); + + @override + List get props => [deviceIds, code, value]; +} diff --git a/lib/pages/device_managment/wall_sensor/view/wall_sensor_batch_control.dart b/lib/pages/device_managment/wall_sensor/view/wall_sensor_batch_control.dart index 703d129e..dab1e152 100644 --- a/lib/pages/device_managment/wall_sensor/view/wall_sensor_batch_control.dart +++ b/lib/pages/device_managment/wall_sensor/view/wall_sensor_batch_control.dart @@ -22,7 +22,7 @@ class WallSensorBatchControlView extends StatelessWidget final isMedium = isMediumScreenSize(context); return BlocProvider( create: (context) => WallSensorBloc(deviceId: devicesIds.first) - ..add(const WallSensorBatchControlEvent()), + ..add(WallSensorFetchBatchStatusEvent(devicesIds)), child: BlocBuilder( builder: (context, state) { if (state is WallSensorLoadingInitialState || @@ -67,7 +67,8 @@ class WallSensorBatchControlView extends StatelessWidget steps: 1, action: (int value) { context.read().add( - WallSensorChangeValueEvent( + WallSensorBatchControlEvent( + deviceIds: devicesIds, code: 'motion_sensitivity_value', value: value, ), @@ -81,7 +82,8 @@ class WallSensorBatchControlView extends StatelessWidget maxValue: 5, steps: 1, action: (int value) => context.read().add( - WallSensorChangeValueEvent( + WallSensorBatchControlEvent( + deviceIds: devicesIds, code: 'motionless_sensitivity', value: value, ), @@ -95,7 +97,8 @@ class WallSensorBatchControlView extends StatelessWidget steps: 1, description: 'hr', action: (int value) => - context.read().add(WallSensorChangeValueEvent( + context.read().add(WallSensorBatchControlEvent( + deviceIds: devicesIds, code: 'no_one_time', value: value, ))), @@ -107,7 +110,8 @@ class WallSensorBatchControlView extends StatelessWidget steps: 75, description: 'cm', action: (int value) => context.read().add( - WallSensorChangeValueEvent( + WallSensorBatchControlEvent( + deviceIds: devicesIds, code: 'far_detection', value: value, ), diff --git a/lib/pages/device_managment/wall_sensor/view/wall_sensor_conrtols.dart b/lib/pages/device_managment/wall_sensor/view/wall_sensor_conrtols.dart index 375b01be..4f789477 100644 --- a/lib/pages/device_managment/wall_sensor/view/wall_sensor_conrtols.dart +++ b/lib/pages/device_managment/wall_sensor/view/wall_sensor_conrtols.dart @@ -26,8 +26,8 @@ class WallSensorControlsView extends StatelessWidget final isLarge = isLargeScreenSize(context); final isMedium = isMediumScreenSize(context); return BlocProvider( - create: (context) => - WallSensorBloc(deviceId: device.uuid!)..add(WallSensorInitialEvent()), + create: (context) => WallSensorBloc(deviceId: device.uuid!) + ..add(WallSensorFetchStatusEvent()), child: BlocBuilder( builder: (context, state) { if (state is WallSensorLoadingInitialState ||