mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
push 3 gang batch control
This commit is contained in:
@ -16,13 +16,14 @@ class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
||||
Timer? _timer;
|
||||
|
||||
LivingRoomBloc({required this.deviceId}) : super(LivingRoomInitial()) {
|
||||
on<LivingRoomFetchDeviceStatus>(_onFetchDeviceStatus);
|
||||
on<LivingRoomFetchDeviceStatusEvent>(_onFetchDeviceStatus);
|
||||
on<LivingRoomControl>(_livingRoomControl);
|
||||
on<LivingRoomFetchBatchStatus>(_livingRoomBatchControl);
|
||||
on<LivingRoomBatchControl>(_livingRoomBatchControl);
|
||||
on<LivingRoomFetchBatchEvent>(_livingRoomFetchBatchControl);
|
||||
}
|
||||
|
||||
FutureOr<void> _onFetchDeviceStatus(
|
||||
LivingRoomFetchDeviceStatus event, Emitter<LivingRoomState> emit) async {
|
||||
FutureOr<void> _onFetchDeviceStatus(LivingRoomFetchDeviceStatusEvent event,
|
||||
Emitter<LivingRoomState> emit) async {
|
||||
emit(LivingRoomDeviceStatusLoading());
|
||||
try {
|
||||
final status =
|
||||
@ -49,28 +50,44 @@ class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
||||
value: event.value,
|
||||
oldValue: oldValue,
|
||||
emit: emit,
|
||||
isBatch: false,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _runDebounce({
|
||||
required String deviceId,
|
||||
required dynamic deviceId,
|
||||
required String code,
|
||||
required dynamic value,
|
||||
required dynamic oldValue,
|
||||
required Emitter<LivingRoomState> 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(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) {
|
||||
_revertValueAndEmit(deviceId, code, oldValue, emit);
|
||||
_revertValueAndEmit(id, code, oldValue, emit);
|
||||
}
|
||||
} catch (e) {
|
||||
_revertValueAndEmit(deviceId, code, oldValue, emit);
|
||||
_revertValueAndEmit(id, code, oldValue, emit);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -79,7 +96,6 @@ class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
||||
Emitter<LivingRoomState> emit) {
|
||||
_updateLocalValue(code, oldValue);
|
||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||
emit(const LivingRoomControlError('Failed to control the device.'));
|
||||
}
|
||||
|
||||
void _updateLocalValue(String code, dynamic value) {
|
||||
@ -118,19 +134,35 @@ class LivingRoomBloc extends Bloc<LivingRoomEvent, LivingRoomState> {
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _livingRoomBatchControl(
|
||||
LivingRoomFetchBatchStatus event, Emitter<LivingRoomState> emit) async {
|
||||
FutureOr<void> _livingRoomFetchBatchControl(
|
||||
LivingRoomFetchBatchEvent event, Emitter<LivingRoomState> emit) async {
|
||||
emit(LivingRoomDeviceStatusLoading());
|
||||
try {
|
||||
//TODO: get batch status from api
|
||||
/// for now sending one id and getting the same value from fetch status
|
||||
final status =
|
||||
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||
await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
||||
deviceStatus =
|
||||
LivingRoomStatusModel.fromJson(event.deviceId, status.status);
|
||||
LivingRoomStatusModel.fromJson(event.devicesIds.first, status.status);
|
||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||
} catch (e) {
|
||||
emit(LivingRoomDeviceManagementError(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _livingRoomBatchControl(
|
||||
LivingRoomBatchControl event, Emitter<LivingRoomState> emit) async {
|
||||
final oldValue = _getValueByCode(event.code);
|
||||
|
||||
_updateLocalValue(event.code, event.value);
|
||||
|
||||
emit(LivingRoomDeviceStatusLoaded(deviceStatus));
|
||||
|
||||
await _runDebounce(
|
||||
deviceId: event.devicesIds,
|
||||
code: event.code,
|
||||
value: event.value,
|
||||
oldValue: oldValue,
|
||||
emit: emit,
|
||||
isBatch: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -7,23 +7,23 @@ sealed class LivingRoomEvent extends Equatable {
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class LivingRoomFetchDeviceStatus extends LivingRoomEvent {
|
||||
class LivingRoomFetchDeviceStatusEvent extends LivingRoomEvent {
|
||||
final String deviceId;
|
||||
|
||||
const LivingRoomFetchDeviceStatus(this.deviceId);
|
||||
const LivingRoomFetchDeviceStatusEvent(this.deviceId);
|
||||
|
||||
@override
|
||||
List<Object> get props => [deviceId];
|
||||
}
|
||||
|
||||
//LivingRoomFetchBatchStatus
|
||||
class LivingRoomFetchBatchStatus extends LivingRoomEvent {
|
||||
final String deviceId;
|
||||
class LivingRoomFetchBatchEvent extends LivingRoomEvent {
|
||||
final List<String> devicesIds;
|
||||
|
||||
const LivingRoomFetchBatchStatus(this.deviceId);
|
||||
const LivingRoomFetchBatchEvent(this.devicesIds);
|
||||
|
||||
@override
|
||||
List<Object> get props => [deviceId];
|
||||
List<Object> get props => [devicesIds];
|
||||
}
|
||||
|
||||
class LivingRoomControl extends LivingRoomEvent {
|
||||
@ -39,13 +39,13 @@ class LivingRoomControl extends LivingRoomEvent {
|
||||
}
|
||||
|
||||
class LivingRoomBatchControl extends LivingRoomEvent {
|
||||
final List<String> deviceId;
|
||||
final List<String> devicesIds;
|
||||
final String code;
|
||||
final bool value;
|
||||
|
||||
const LivingRoomBatchControl(
|
||||
{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];
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ class LivingRoomBatchControlsView extends StatelessWidget
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => LivingRoomBloc(deviceId: deviceIds.first)
|
||||
..add(LivingRoomFetchBatchStatus(deviceIds.first)),
|
||||
..add(LivingRoomFetchBatchEvent(deviceIds)),
|
||||
child: BlocBuilder<LivingRoomBloc, LivingRoomState>(
|
||||
builder: (context, state) {
|
||||
if (state is LivingRoomDeviceStatusLoading) {
|
||||
@ -61,7 +61,45 @@ class LivingRoomBatchControlsView extends StatelessWidget
|
||||
code: 'switch_1',
|
||||
deviceId: deviceIds.first,
|
||||
label: 'Wall Light',
|
||||
onChange: (value) {},
|
||||
onChange: (value) {
|
||||
context.read<LivingRoomBloc>().add(
|
||||
LivingRoomBatchControl(
|
||||
devicesIds: deviceIds,
|
||||
code: 'switch_1',
|
||||
value: value,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
ToggleWidget(
|
||||
value: status.switch2,
|
||||
code: 'switch_2',
|
||||
deviceId: deviceIds.first,
|
||||
label: 'Ceiling Light',
|
||||
onChange: (value) {
|
||||
context.read<LivingRoomBloc>().add(
|
||||
LivingRoomBatchControl(
|
||||
devicesIds: deviceIds,
|
||||
code: 'switch_2',
|
||||
value: value,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
ToggleWidget(
|
||||
value: status.switch3,
|
||||
code: 'switch_2',
|
||||
deviceId: deviceIds.first,
|
||||
label: 'Spotlight',
|
||||
onChange: (value) {
|
||||
context.read<LivingRoomBloc>().add(
|
||||
LivingRoomBatchControl(
|
||||
devicesIds: deviceIds,
|
||||
code: 'switch_3',
|
||||
value: value,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
FirmwareUpdateWidget(
|
||||
deviceId: deviceIds.first,
|
||||
|
@ -15,7 +15,7 @@ class LivingRoomDeviceControlsView extends StatelessWidget
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => LivingRoomBloc(deviceId: deviceId)
|
||||
..add(LivingRoomFetchDeviceStatus(deviceId)),
|
||||
..add(LivingRoomFetchDeviceStatusEvent(deviceId)),
|
||||
child: BlocBuilder<LivingRoomBloc, LivingRoomState>(
|
||||
builder: (context, state) {
|
||||
if (state is LivingRoomDeviceStatusLoading) {
|
||||
|
Reference in New Issue
Block a user