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