push wall sensor batch control

This commit is contained in:
ashrafzarkanisala
2024-09-18 13:55:36 +03:00
parent fd09db6835
commit c354abbeca
5 changed files with 93 additions and 41 deletions

View File

@ -66,12 +66,3 @@ class ShowCeilingDescriptionState extends CeilingSensorState {
@override @override
List<Object> get props => [description]; List<Object> get props => [description];
} }
class CeilingBatchControlSuccessState extends CeilingSensorState {
final CeilingSensorModel ceilingSensorModel;
const CeilingBatchControlSuccessState({required this.ceilingSensorModel});
@override
List<Object> get props => [ceilingSensorModel];
}

View File

@ -12,16 +12,17 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
Timer? _timer; Timer? _timer;
WallSensorBloc({required this.deviceId}) : super(WallSensorInitialState()) { WallSensorBloc({required this.deviceId}) : super(WallSensorInitialState()) {
on<WallSensorInitialEvent>(_fetchWallSensorStatus); on<WallSensorFetchStatusEvent>(_fetchWallSensorStatus);
on<WallSensorBatchControlEvent>(_fetchWallSensorBatchControl); on<WallSensorFetchBatchStatusEvent>(_fetchWallSensorBatchControl);
on<WallSensorChangeValueEvent>(_changeValue); on<WallSensorChangeValueEvent>(_changeValue);
on<WallSensorBatchControlEvent>(_onBatchControl);
on<GetDeviceReportsEvent>(_getDeviceReports); on<GetDeviceReportsEvent>(_getDeviceReports);
on<ShowDescriptionEvent>(_showDescription); on<ShowDescriptionEvent>(_showDescription);
on<BackToGridViewEvent>(_backToGridView); on<BackToGridViewEvent>(_backToGridView);
} }
void _fetchWallSensorStatus( void _fetchWallSensorStatus(
WallSensorInitialEvent event, Emitter<WallSensorState> emit) async { WallSensorFetchStatusEvent event, Emitter<WallSensorState> emit) async {
emit(WallSensorLoadingInitialState()); emit(WallSensorLoadingInitialState());
try { try {
var response = await DevicesManagementApi().getDeviceStatus(deviceId); var response = await DevicesManagementApi().getDeviceStatus(deviceId);
@ -34,6 +35,21 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
} }
} }
// Fetch batch status
FutureOr<void> _fetchWallSensorBatchControl(
WallSensorFetchBatchStatusEvent event,
Emitter<WallSensorState> 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() { // _listenToChanges() {
// try { // try {
// DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId'); // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
@ -67,28 +83,63 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
} }
emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); emit(WallSensorUpdateState(wallSensorModel: deviceStatus));
await _runDeBouncer( await _runDeBouncer(
deviceId: deviceId, code: event.code, value: event.value); deviceId: deviceId,
code: event.code,
value: event.value,
isBatch: false,
emit: emit,
);
}
Future<void> _onBatchControl(
WallSensorBatchControlEvent event, Emitter<WallSensorState> 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({ _runDeBouncer({
required String deviceId, required dynamic deviceId,
required String code, required String code,
required dynamic value, required dynamic value,
required Emitter<WallSensorState> emit,
required bool isBatch,
}) { }) {
if (_timer != null) { if (_timer != null) {
_timer!.cancel(); _timer!.cancel();
} }
_timer = Timer(const Duration(seconds: 1), () async { _timer = Timer(const Duration(seconds: 1), () async {
try { try {
final response = await DevicesManagementApi() late bool response;
.deviceControl(deviceId, Status(code: code, value: value)); if (isBatch) {
response = await DevicesManagementApi()
.deviceBatchControl(deviceId, code, value);
} else {
response = await DevicesManagementApi()
.deviceControl(deviceId, Status(code: code, value: value));
}
if (!response) { if (!response) {
add(WallSensorInitialEvent()); add(WallSensorFetchStatusEvent());
} }
} catch (_) { } catch (_) {
await Future.delayed(const Duration(milliseconds: 500)); await Future.delayed(const Duration(milliseconds: 500));
add(WallSensorInitialEvent()); add(WallSensorFetchStatusEvent());
} }
}); });
} }
@ -117,17 +168,4 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
BackToGridViewEvent event, Emitter<WallSensorState> emit) { BackToGridViewEvent event, Emitter<WallSensorState> emit) {
emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); emit(WallSensorUpdateState(wallSensorModel: deviceStatus));
} }
FutureOr<void> _fetchWallSensorBatchControl(
WallSensorBatchControlEvent event, Emitter<WallSensorState> 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;
}
}
} }

View File

@ -7,7 +7,7 @@ abstract class WallSensorEvent extends Equatable {
List<Object> get props => []; List<Object> get props => [];
} }
class WallSensorInitialEvent extends WallSensorEvent {} class WallSensorFetchStatusEvent extends WallSensorEvent {}
class WallSensorChangeValueEvent extends WallSensorEvent { class WallSensorChangeValueEvent extends WallSensorEvent {
final int value; final int value;
@ -18,8 +18,12 @@ class WallSensorChangeValueEvent extends WallSensorEvent {
List<Object> get props => [value, code]; List<Object> get props => [value, code];
} }
class WallSensorBatchControlEvent extends WallSensorEvent { class WallSensorFetchBatchStatusEvent extends WallSensorEvent {
const WallSensorBatchControlEvent(); final List<String> devicesIds;
const WallSensorFetchBatchStatusEvent(this.devicesIds);
@override
List<Object> get props => [devicesIds];
} }
class GetDeviceReportsEvent extends WallSensorEvent { class GetDeviceReportsEvent extends WallSensorEvent {
@ -40,3 +44,18 @@ class ShowDescriptionEvent extends WallSensorEvent {
} }
class BackToGridViewEvent extends WallSensorEvent {} class BackToGridViewEvent extends WallSensorEvent {}
class WallSensorBatchControlEvent extends WallSensorEvent {
final List<String> deviceIds;
final String code;
final dynamic value;
const WallSensorBatchControlEvent({
required this.deviceIds,
required this.code,
required this.value,
});
@override
List<Object> get props => [deviceIds, code, value];
}

View File

@ -22,7 +22,7 @@ class WallSensorBatchControlView extends StatelessWidget
final isMedium = isMediumScreenSize(context); final isMedium = isMediumScreenSize(context);
return BlocProvider( return BlocProvider(
create: (context) => WallSensorBloc(deviceId: devicesIds.first) create: (context) => WallSensorBloc(deviceId: devicesIds.first)
..add(const WallSensorBatchControlEvent()), ..add(WallSensorFetchBatchStatusEvent(devicesIds)),
child: BlocBuilder<WallSensorBloc, WallSensorState>( child: BlocBuilder<WallSensorBloc, WallSensorState>(
builder: (context, state) { builder: (context, state) {
if (state is WallSensorLoadingInitialState || if (state is WallSensorLoadingInitialState ||
@ -67,7 +67,8 @@ class WallSensorBatchControlView extends StatelessWidget
steps: 1, steps: 1,
action: (int value) { action: (int value) {
context.read<WallSensorBloc>().add( context.read<WallSensorBloc>().add(
WallSensorChangeValueEvent( WallSensorBatchControlEvent(
deviceIds: devicesIds,
code: 'motion_sensitivity_value', code: 'motion_sensitivity_value',
value: value, value: value,
), ),
@ -81,7 +82,8 @@ class WallSensorBatchControlView extends StatelessWidget
maxValue: 5, maxValue: 5,
steps: 1, steps: 1,
action: (int value) => context.read<WallSensorBloc>().add( action: (int value) => context.read<WallSensorBloc>().add(
WallSensorChangeValueEvent( WallSensorBatchControlEvent(
deviceIds: devicesIds,
code: 'motionless_sensitivity', code: 'motionless_sensitivity',
value: value, value: value,
), ),
@ -95,7 +97,8 @@ class WallSensorBatchControlView extends StatelessWidget
steps: 1, steps: 1,
description: 'hr', description: 'hr',
action: (int value) => action: (int value) =>
context.read<WallSensorBloc>().add(WallSensorChangeValueEvent( context.read<WallSensorBloc>().add(WallSensorBatchControlEvent(
deviceIds: devicesIds,
code: 'no_one_time', code: 'no_one_time',
value: value, value: value,
))), ))),
@ -107,7 +110,8 @@ class WallSensorBatchControlView extends StatelessWidget
steps: 75, steps: 75,
description: 'cm', description: 'cm',
action: (int value) => context.read<WallSensorBloc>().add( action: (int value) => context.read<WallSensorBloc>().add(
WallSensorChangeValueEvent( WallSensorBatchControlEvent(
deviceIds: devicesIds,
code: 'far_detection', code: 'far_detection',
value: value, value: value,
), ),

View File

@ -26,8 +26,8 @@ class WallSensorControlsView extends StatelessWidget
final isLarge = isLargeScreenSize(context); final isLarge = isLargeScreenSize(context);
final isMedium = isMediumScreenSize(context); final isMedium = isMediumScreenSize(context);
return BlocProvider( return BlocProvider(
create: (context) => create: (context) => WallSensorBloc(deviceId: device.uuid!)
WallSensorBloc(deviceId: device.uuid!)..add(WallSensorInitialEvent()), ..add(WallSensorFetchStatusEvent()),
child: BlocBuilder<WallSensorBloc, WallSensorState>( child: BlocBuilder<WallSensorBloc, WallSensorState>(
builder: (context, state) { builder: (context, state) {
if (state is WallSensorLoadingInitialState || if (state is WallSensorLoadingInitialState ||