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<WallLightSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
on<WallLightSwitchControl>(_onControl); on<WallLightSwitchControl>(_onControl);
on<WallLightSwitchFetchBatchEvent>(_onFetchBatchStatus); on<WallLightSwitchFetchBatchEvent>(_onFetchBatchStatus);
on<WallLightSwitchBatchControl>(_onBatchControl);
} }
late WallLightStatusModel deviceStatus; late WallLightStatusModel deviceStatus;
@ -49,30 +50,47 @@ class WallLightSwitchBloc
value: event.value, value: event.value,
oldValue: oldValue, oldValue: oldValue,
emit: emit, emit: emit,
isBatch: false,
); );
} }
Future<void> _runDebounce({ Future<void> _runDebounce({
required String deviceId, required dynamic deviceId,
required String code, required String code,
required bool value, required bool value,
required bool oldValue, required bool oldValue,
required Emitter<WallLightSwitchState> emit, required Emitter<WallLightSwitchState> emit,
required bool isBatch,
}) async { }) async {
late String id;
if (deviceId is List) {
id = deviceId.first;
} else {
id = deviceId;
}
if (_timer != null) { if (_timer != null) {
_timer!.cancel(); _timer!.cancel();
} }
_timer = Timer(const Duration(milliseconds: 500), () async { _timer = Timer(const Duration(milliseconds: 500), () async {
try { try {
final status = await DevicesManagementApi() late bool response;
.deviceControl(deviceId, Status(code: code, value: value));
if (!status) { if (isBatch) {
_revertValueAndEmit(deviceId, code, oldValue, emit); 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) { } catch (e) {
_revertValueAndEmit(deviceId, code, oldValue, emit); _revertValueAndEmit(id, code, oldValue, emit);
} }
}); });
} }
@ -103,9 +121,9 @@ class WallLightSwitchBloc
emit(WallLightSwitchLoading()); emit(WallLightSwitchLoading());
try { try {
final status = final status =
await DevicesManagementApi().getDeviceStatus(event.deviceId); await DevicesManagementApi().getBatchStatus(event.devicesIds);
deviceStatus = deviceStatus =
WallLightStatusModel.fromJson(event.deviceId, status.status); WallLightStatusModel.fromJson(event.devicesIds.first, status.status);
emit(WallLightSwitchStatusLoaded(deviceStatus)); emit(WallLightSwitchStatusLoaded(deviceStatus));
} catch (e) { } catch (e) {
emit(WallLightSwitchError(e.toString())); emit(WallLightSwitchError(e.toString()));
@ -117,4 +135,22 @@ class WallLightSwitchBloc
_timer?.cancel(); _timer?.cancel();
return super.close(); 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 { class WallLightSwitchFetchBatchEvent extends WallLightSwitchEvent {
final String deviceId; final List<String> devicesIds;
WallLightSwitchFetchBatchEvent(this.deviceId); WallLightSwitchFetchBatchEvent(this.devicesIds);
@override @override
List<Object> get props => [deviceId]; List<Object> get props => [devicesIds];
} }
class WallLightSwitchBatchControl extends WallLightSwitchEvent { class WallLightSwitchBatchControl extends WallLightSwitchEvent {
final List<String> deviceId; final List<String> devicesIds;
final String code; final String code;
final bool value; final bool value;
WallLightSwitchBatchControl( WallLightSwitchBatchControl(
{required this.deviceId, required this.code, required this.value}); {required this.devicesIds, required this.code, required this.value});
@override @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) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (context) => WallLightSwitchBloc(deviceId: deviceIds.first) create: (context) => WallLightSwitchBloc(deviceId: deviceIds.first)
..add(WallLightSwitchFetchBatchEvent(deviceIds.first)), ..add(WallLightSwitchFetchBatchEvent(deviceIds)),
child: BlocBuilder<WallLightSwitchBloc, WallLightSwitchState>( child: BlocBuilder<WallLightSwitchBloc, WallLightSwitchState>(
builder: (context, state) { builder: (context, state) {
if (state is WallLightSwitchLoading) { if (state is WallLightSwitchLoading) {
@ -63,7 +63,15 @@ class WallLightBatchControlView extends StatelessWidget
code: 'switch_1', code: 'switch_1',
deviceId: deviceIds.first, deviceId: deviceIds.first,
label: 'Wall Light', label: 'Wall Light',
onChange: (value) {}, onChange: (value) {
context.read<WallLightSwitchBloc>().add(
WallLightSwitchBatchControl(
devicesIds: deviceIds,
code: 'switch_1',
value: value,
),
);
},
), ),
FirmwareUpdateWidget( FirmwareUpdateWidget(
deviceId: deviceIds.first, deviceId: deviceIds.first,