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
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;
WallSensorBloc({required this.deviceId}) : super(WallSensorInitialState()) {
on<WallSensorInitialEvent>(_fetchWallSensorStatus);
on<WallSensorBatchControlEvent>(_fetchWallSensorBatchControl);
on<WallSensorFetchStatusEvent>(_fetchWallSensorStatus);
on<WallSensorFetchBatchStatusEvent>(_fetchWallSensorBatchControl);
on<WallSensorChangeValueEvent>(_changeValue);
on<WallSensorBatchControlEvent>(_onBatchControl);
on<GetDeviceReportsEvent>(_getDeviceReports);
on<ShowDescriptionEvent>(_showDescription);
on<BackToGridViewEvent>(_backToGridView);
}
void _fetchWallSensorStatus(
WallSensorInitialEvent event, Emitter<WallSensorState> emit) async {
WallSensorFetchStatusEvent event, Emitter<WallSensorState> emit) async {
emit(WallSensorLoadingInitialState());
try {
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() {
// try {
// DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
@ -67,28 +83,63 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
}
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<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({
required String deviceId,
required dynamic deviceId,
required String code,
required dynamic value,
required Emitter<WallSensorState> 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<WallSensorEvent, WallSensorState> {
BackToGridViewEvent event, Emitter<WallSensorState> emit) {
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 => [];
}
class WallSensorInitialEvent extends WallSensorEvent {}
class WallSensorFetchStatusEvent extends WallSensorEvent {}
class WallSensorChangeValueEvent extends WallSensorEvent {
final int value;
@ -18,8 +18,12 @@ class WallSensorChangeValueEvent extends WallSensorEvent {
List<Object> get props => [value, code];
}
class WallSensorBatchControlEvent extends WallSensorEvent {
const WallSensorBatchControlEvent();
class WallSensorFetchBatchStatusEvent extends WallSensorEvent {
final List<String> devicesIds;
const WallSensorFetchBatchStatusEvent(this.devicesIds);
@override
List<Object> 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<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);
return BlocProvider(
create: (context) => WallSensorBloc(deviceId: devicesIds.first)
..add(const WallSensorBatchControlEvent()),
..add(WallSensorFetchBatchStatusEvent(devicesIds)),
child: BlocBuilder<WallSensorBloc, WallSensorState>(
builder: (context, state) {
if (state is WallSensorLoadingInitialState ||
@ -67,7 +67,8 @@ class WallSensorBatchControlView extends StatelessWidget
steps: 1,
action: (int value) {
context.read<WallSensorBloc>().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<WallSensorBloc>().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<WallSensorBloc>().add(WallSensorChangeValueEvent(
context.read<WallSensorBloc>().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<WallSensorBloc>().add(
WallSensorChangeValueEvent(
WallSensorBatchControlEvent(
deviceIds: devicesIds,
code: 'far_detection',
value: value,
),

View File

@ -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<WallSensorBloc, WallSensorState>(
builder: (context, state) {
if (state is WallSensorLoadingInitialState ||