push 2 G switch batch control

This commit is contained in:
ashrafzarkanisala
2024-09-18 14:25:52 +03:00
parent 67667e4405
commit bc309adba7
3 changed files with 62 additions and 14 deletions

View File

@ -12,6 +12,7 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
on<TwoGangSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
on<TwoGangSwitchControl>(_onControl);
on<TwoGangSwitchFetchBatchEvent>(_onFetchBatchStatus);
on<TwoGangSwitchBatchControl>(_onBatchControl);
}
late TwoGangStatusModel deviceStatus;
@ -46,30 +47,46 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
value: event.value,
oldValue: oldValue,
emit: emit,
isBatch: false,
);
}
Future<void> _runDebounce({
required String deviceId,
required dynamic deviceId,
required String code,
required bool value,
required bool oldValue,
required Emitter<TwoGangSwitchState> emit,
required bool isBatch,
}) async {
late String id;
if (deviceId is List) {
id = deviceId.first;
} else {
id = deviceId;
}
if (_timer != null) {
_timer!.cancel();
}
_timer = Timer(const Duration(milliseconds: 500), () async {
try {
final status = 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 (!status) {
_revertValueAndEmit(deviceId, code, oldValue, emit);
if (!response) {
_revertValueAndEmit(id, code, oldValue, emit);
}
} catch (e) {
_revertValueAndEmit(deviceId, code, oldValue, emit);
_revertValueAndEmit(id, code, oldValue, emit);
}
});
}
@ -106,8 +123,9 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
emit(TwoGangSwitchLoading());
try {
final status =
await DevicesManagementApi().getDeviceStatus(event.deviceId);
deviceStatus = TwoGangStatusModel.fromJson(event.deviceId, status.status);
await DevicesManagementApi().getBatchStatus(event.devicesIds);
deviceStatus =
TwoGangStatusModel.fromJson(event.devicesIds.first, status.status);
emit(TwoGangSwitchStatusLoaded(deviceStatus));
} catch (e) {
emit(TwoGangSwitchError(e.toString()));
@ -119,4 +137,22 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
_timer?.cancel();
return super.close();
}
FutureOr<void> _onBatchControl(
TwoGangSwitchBatchControl event, Emitter<TwoGangSwitchState> emit) async {
final oldValue = _getValueByCode(event.code);
_updateLocalValue(event.code, event.value);
emit(TwoGangSwitchStatusLoaded(deviceStatus));
await _runDebounce(
deviceId: event.deviceId,
code: event.code,
value: event.value,
oldValue: oldValue,
emit: emit,
isBatch: true,
);
}
}

View File

@ -27,12 +27,12 @@ class TwoGangSwitchControl extends TwoGangSwitchEvent {
}
class TwoGangSwitchFetchBatchEvent extends TwoGangSwitchEvent {
final String deviceId;
final List<String> devicesIds;
TwoGangSwitchFetchBatchEvent(this.deviceId);
TwoGangSwitchFetchBatchEvent(this.devicesIds);
@override
List<Object> get props => [deviceId];
List<Object> get props => [devicesIds];
}
class TwoGangSwitchBatchControl extends TwoGangSwitchEvent {

View File

@ -19,7 +19,7 @@ class TwoGangBatchControlView extends StatelessWidget
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => TwoGangSwitchBloc(deviceId: deviceIds.first)
..add(TwoGangSwitchFetchBatchEvent(deviceIds.first)),
..add(TwoGangSwitchFetchBatchEvent(deviceIds)),
child: BlocBuilder<TwoGangSwitchBloc, TwoGangSwitchState>(
builder: (context, state) {
if (state is TwoGangSwitchLoading) {
@ -62,14 +62,26 @@ class TwoGangBatchControlView extends StatelessWidget
code: 'switch_1',
deviceId: deviceIds.first,
label: 'Wall Light',
onChange: (value) {},
onChange: (value) {
context.read<TwoGangSwitchBloc>().add(TwoGangSwitchBatchControl(
deviceId: deviceIds,
code: 'switch_1',
value: value,
));
},
),
ToggleWidget(
value: status.switch2,
code: 'switch_2',
deviceId: deviceIds.first,
label: 'Ceiling Light',
onChange: (value) {},
onChange: (value) {
context.read<TwoGangSwitchBloc>().add(TwoGangSwitchBatchControl(
deviceId: deviceIds,
code: 'switch_2',
value: value,
));
},
),
FirmwareUpdateWidget(
deviceId: deviceIds.first,