push one gang switch

This commit is contained in:
ashrafzarkanisala
2024-09-18 14:33:01 +03:00
parent bc309adba7
commit 6d805ddfd7
3 changed files with 60 additions and 16 deletions

View File

@ -14,6 +14,7 @@ class WallLightSwitchBloc
on<WallLightSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
on<WallLightSwitchControl>(_onControl);
on<WallLightSwitchFetchBatchEvent>(_onFetchBatchStatus);
on<WallLightSwitchBatchControl>(_onBatchControl);
}
late WallLightStatusModel deviceStatus;
@ -49,30 +50,47 @@ class WallLightSwitchBloc
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<WallLightSwitchState> 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 (!status) {
_revertValueAndEmit(deviceId, code, oldValue, emit);
if (isBatch) {
response = await DevicesManagementApi()
.deviceBatchControl(deviceId, code, value);
} else {
response = await DevicesManagementApi()
.deviceControl(deviceId, Status(code: code, value: value));
}
if (!response) {
_revertValueAndEmit(id, code, oldValue, emit);
}
} catch (e) {
_revertValueAndEmit(deviceId, code, oldValue, emit);
_revertValueAndEmit(id, code, oldValue, emit);
}
});
}
@ -103,9 +121,9 @@ class WallLightSwitchBloc
emit(WallLightSwitchLoading());
try {
final status =
await DevicesManagementApi().getDeviceStatus(event.deviceId);
await DevicesManagementApi().getBatchStatus(event.devicesIds);
deviceStatus =
WallLightStatusModel.fromJson(event.deviceId, status.status);
WallLightStatusModel.fromJson(event.devicesIds.first, status.status);
emit(WallLightSwitchStatusLoaded(deviceStatus));
} catch (e) {
emit(WallLightSwitchError(e.toString()));
@ -117,4 +135,22 @@ class WallLightSwitchBloc
_timer?.cancel();
return super.close();
}
FutureOr<void> _onBatchControl(WallLightSwitchBatchControl event,
Emitter<WallLightSwitchState> emit) async {
final oldValue = _getValueByCode(event.code);
_updateLocalValue(event.code, event.value);
emit(WallLightSwitchStatusLoaded(deviceStatus));
await _runDebounce(
deviceId: event.devicesIds,
code: event.code,
value: event.value,
oldValue: oldValue,
emit: emit,
isBatch: true,
);
}
}

View File

@ -27,22 +27,22 @@ class WallLightSwitchControl extends WallLightSwitchEvent {
}
class WallLightSwitchFetchBatchEvent extends WallLightSwitchEvent {
final String deviceId;
final List<String> devicesIds;
WallLightSwitchFetchBatchEvent(this.deviceId);
WallLightSwitchFetchBatchEvent(this.devicesIds);
@override
List<Object> get props => [deviceId];
List<Object> get props => [devicesIds];
}
class WallLightSwitchBatchControl extends WallLightSwitchEvent {
final List<String> deviceId;
final List<String> devicesIds;
final String code;
final bool value;
WallLightSwitchBatchControl(
{required this.deviceId, required this.code, required this.value});
{required this.devicesIds, required this.code, required this.value});
@override
List<Object> get props => [deviceId, code, value];
List<Object> get props => [devicesIds, code, value];
}

View File

@ -19,7 +19,7 @@ class WallLightBatchControlView extends StatelessWidget
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => WallLightSwitchBloc(deviceId: deviceIds.first)
..add(WallLightSwitchFetchBatchEvent(deviceIds.first)),
..add(WallLightSwitchFetchBatchEvent(deviceIds)),
child: BlocBuilder<WallLightSwitchBloc, WallLightSwitchState>(
builder: (context, state) {
if (state is WallLightSwitchLoading) {
@ -63,7 +63,15 @@ class WallLightBatchControlView extends StatelessWidget
code: 'switch_1',
deviceId: deviceIds.first,
label: 'Wall Light',
onChange: (value) {},
onChange: (value) {
context.read<WallLightSwitchBloc>().add(
WallLightSwitchBatchControl(
devicesIds: deviceIds,
code: 'switch_1',
value: value,
),
);
},
),
FirmwareUpdateWidget(
deviceId: deviceIds.first,