mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
push 2 G switch batch control
This commit is contained in:
@ -12,6 +12,7 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
|
|||||||
on<TwoGangSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
|
on<TwoGangSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
|
||||||
on<TwoGangSwitchControl>(_onControl);
|
on<TwoGangSwitchControl>(_onControl);
|
||||||
on<TwoGangSwitchFetchBatchEvent>(_onFetchBatchStatus);
|
on<TwoGangSwitchFetchBatchEvent>(_onFetchBatchStatus);
|
||||||
|
on<TwoGangSwitchBatchControl>(_onBatchControl);
|
||||||
}
|
}
|
||||||
|
|
||||||
late TwoGangStatusModel deviceStatus;
|
late TwoGangStatusModel deviceStatus;
|
||||||
@ -46,30 +47,46 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
|
|||||||
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<TwoGangSwitchState> emit,
|
required Emitter<TwoGangSwitchState> 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 (isBatch) {
|
||||||
|
response = await DevicesManagementApi()
|
||||||
|
.deviceBatchControl(deviceId, code, value);
|
||||||
|
} else {
|
||||||
|
response = await DevicesManagementApi()
|
||||||
|
.deviceControl(deviceId, Status(code: code, value: value));
|
||||||
|
}
|
||||||
|
|
||||||
if (!status) {
|
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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -106,8 +123,9 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
|
|||||||
emit(TwoGangSwitchLoading());
|
emit(TwoGangSwitchLoading());
|
||||||
try {
|
try {
|
||||||
final status =
|
final status =
|
||||||
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
||||||
deviceStatus = TwoGangStatusModel.fromJson(event.deviceId, status.status);
|
deviceStatus =
|
||||||
|
TwoGangStatusModel.fromJson(event.devicesIds.first, status.status);
|
||||||
emit(TwoGangSwitchStatusLoaded(deviceStatus));
|
emit(TwoGangSwitchStatusLoaded(deviceStatus));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(TwoGangSwitchError(e.toString()));
|
emit(TwoGangSwitchError(e.toString()));
|
||||||
@ -119,4 +137,22 @@ class TwoGangSwitchBloc extends Bloc<TwoGangSwitchEvent, TwoGangSwitchState> {
|
|||||||
_timer?.cancel();
|
_timer?.cancel();
|
||||||
return super.close();
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,12 +27,12 @@ class TwoGangSwitchControl extends TwoGangSwitchEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class TwoGangSwitchFetchBatchEvent extends TwoGangSwitchEvent {
|
class TwoGangSwitchFetchBatchEvent extends TwoGangSwitchEvent {
|
||||||
final String deviceId;
|
final List<String> devicesIds;
|
||||||
|
|
||||||
TwoGangSwitchFetchBatchEvent(this.deviceId);
|
TwoGangSwitchFetchBatchEvent(this.devicesIds);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [deviceId];
|
List<Object> get props => [devicesIds];
|
||||||
}
|
}
|
||||||
|
|
||||||
class TwoGangSwitchBatchControl extends TwoGangSwitchEvent {
|
class TwoGangSwitchBatchControl extends TwoGangSwitchEvent {
|
||||||
|
@ -19,7 +19,7 @@ class TwoGangBatchControlView extends StatelessWidget
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) => TwoGangSwitchBloc(deviceId: deviceIds.first)
|
create: (context) => TwoGangSwitchBloc(deviceId: deviceIds.first)
|
||||||
..add(TwoGangSwitchFetchBatchEvent(deviceIds.first)),
|
..add(TwoGangSwitchFetchBatchEvent(deviceIds)),
|
||||||
child: BlocBuilder<TwoGangSwitchBloc, TwoGangSwitchState>(
|
child: BlocBuilder<TwoGangSwitchBloc, TwoGangSwitchState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
if (state is TwoGangSwitchLoading) {
|
if (state is TwoGangSwitchLoading) {
|
||||||
@ -62,14 +62,26 @@ class TwoGangBatchControlView 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<TwoGangSwitchBloc>().add(TwoGangSwitchBatchControl(
|
||||||
|
deviceId: deviceIds,
|
||||||
|
code: 'switch_1',
|
||||||
|
value: value,
|
||||||
|
));
|
||||||
|
},
|
||||||
),
|
),
|
||||||
ToggleWidget(
|
ToggleWidget(
|
||||||
value: status.switch2,
|
value: status.switch2,
|
||||||
code: 'switch_2',
|
code: 'switch_2',
|
||||||
deviceId: deviceIds.first,
|
deviceId: deviceIds.first,
|
||||||
label: 'Ceiling Light',
|
label: 'Ceiling Light',
|
||||||
onChange: (value) {},
|
onChange: (value) {
|
||||||
|
context.read<TwoGangSwitchBloc>().add(TwoGangSwitchBatchControl(
|
||||||
|
deviceId: deviceIds,
|
||||||
|
code: 'switch_2',
|
||||||
|
value: value,
|
||||||
|
));
|
||||||
|
},
|
||||||
),
|
),
|
||||||
FirmwareUpdateWidget(
|
FirmwareUpdateWidget(
|
||||||
deviceId: deviceIds.first,
|
deviceId: deviceIds.first,
|
||||||
|
Reference in New Issue
Block a user