mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
push curtain batch control
This commit is contained in:
@ -1,9 +1,6 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_bloc.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_event.dart';
|
||||
import 'package:syncrow_web/utils/color_manager.dart';
|
||||
import 'package:syncrow_web/utils/constants/assets.dart';
|
||||
|
||||
@ -12,6 +9,7 @@ class CurtainToggle extends StatelessWidget {
|
||||
final String code;
|
||||
final String deviceId;
|
||||
final String label;
|
||||
final Null Function(dynamic value) onChanged;
|
||||
|
||||
const CurtainToggle({
|
||||
super.key,
|
||||
@ -19,6 +17,7 @@ class CurtainToggle extends StatelessWidget {
|
||||
required this.code,
|
||||
required this.deviceId,
|
||||
required this.label,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
@override
|
||||
@ -54,15 +53,7 @@ class CurtainToggle extends StatelessWidget {
|
||||
child: CupertinoSwitch(
|
||||
value: value,
|
||||
activeColor: ColorsManager.dialogBlueTitle,
|
||||
onChanged: (newValue) {
|
||||
context.read<CurtainBloc>().add(
|
||||
CurtainControl(
|
||||
deviceId: deviceId,
|
||||
code: code,
|
||||
value: newValue,
|
||||
),
|
||||
);
|
||||
},
|
||||
onChanged: onChanged,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
@ -14,6 +14,7 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
on<CurtainFetchDeviceStatus>(_onFetchDeviceStatus);
|
||||
on<CurtainFetchBatchStatus>(_onFetchBatchStatus);
|
||||
on<CurtainControl>(_onCurtainControl);
|
||||
on<CurtainBatchControl>(_onCurtainBatchControl);
|
||||
}
|
||||
|
||||
FutureOr<void> _onFetchDeviceStatus(
|
||||
@ -45,16 +46,26 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
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<CurtainState> emit,
|
||||
required bool isBatch,
|
||||
}) async {
|
||||
late String id;
|
||||
|
||||
if (deviceId is List) {
|
||||
id = deviceId.first;
|
||||
} else {
|
||||
id = deviceId;
|
||||
}
|
||||
|
||||
if (_timer != null) {
|
||||
_timer!.cancel();
|
||||
}
|
||||
@ -62,14 +73,20 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
try {
|
||||
final controlValue = value ? 'open' : 'close';
|
||||
|
||||
final response = await DevicesManagementApi()
|
||||
.deviceControl(deviceId, Status(code: code, value: controlValue));
|
||||
late bool response;
|
||||
if (isBatch) {
|
||||
response = await DevicesManagementApi()
|
||||
.deviceBatchControl(deviceId, code, controlValue);
|
||||
} else {
|
||||
response = await DevicesManagementApi()
|
||||
.deviceControl(deviceId, Status(code: code, value: controlValue));
|
||||
}
|
||||
|
||||
if (!response) {
|
||||
_revertValueAndEmit(deviceId, oldValue, emit);
|
||||
_revertValueAndEmit(id, oldValue, emit);
|
||||
}
|
||||
} catch (e) {
|
||||
_revertValueAndEmit(deviceId, oldValue, emit);
|
||||
_revertValueAndEmit(id, oldValue, emit);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -95,7 +112,7 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
emit(CurtainStatusLoading());
|
||||
try {
|
||||
final status =
|
||||
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||
await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
||||
|
||||
deviceStatus = _checkStatus(status.status[0].value);
|
||||
|
||||
@ -104,4 +121,22 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
emit(CurtainError(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _onCurtainBatchControl(
|
||||
CurtainBatchControl event, Emitter<CurtainState> emit) async {
|
||||
final oldValue = deviceStatus;
|
||||
|
||||
_updateLocalValue(event.value, emit);
|
||||
|
||||
emit(CurtainStatusLoaded(deviceStatus));
|
||||
|
||||
await _runDebounce(
|
||||
deviceId: event.devicesIds,
|
||||
code: event.code,
|
||||
value: event.value,
|
||||
oldValue: oldValue,
|
||||
emit: emit,
|
||||
isBatch: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -17,12 +17,12 @@ class CurtainFetchDeviceStatus extends CurtainEvent {
|
||||
}
|
||||
|
||||
class CurtainFetchBatchStatus extends CurtainEvent {
|
||||
final String deviceId;
|
||||
final List<String> devicesIds;
|
||||
|
||||
const CurtainFetchBatchStatus(this.deviceId);
|
||||
const CurtainFetchBatchStatus(this.devicesIds);
|
||||
|
||||
@override
|
||||
List<Object> get props => [deviceId];
|
||||
List<Object> get props => [devicesIds];
|
||||
}
|
||||
|
||||
class CurtainControl extends CurtainEvent {
|
||||
@ -36,3 +36,15 @@ class CurtainControl extends CurtainEvent {
|
||||
@override
|
||||
List<Object> get props => [deviceId, code, value];
|
||||
}
|
||||
|
||||
class CurtainBatchControl extends CurtainEvent {
|
||||
final List<String> devicesIds;
|
||||
final String code;
|
||||
final bool value;
|
||||
|
||||
const CurtainBatchControl(
|
||||
{required this.devicesIds, required this.code, required this.value});
|
||||
|
||||
@override
|
||||
List<Object> get props => [devicesIds, code, value];
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ class CurtainBatchStatusView extends StatelessWidget
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => CurtainBloc(deviceId: devicesIds.first)
|
||||
..add(CurtainFetchDeviceStatus(devicesIds.first)),
|
||||
..add(CurtainFetchBatchStatus(devicesIds)),
|
||||
child: BlocBuilder<CurtainBloc, CurtainState>(
|
||||
builder: (context, state) {
|
||||
if (state is CurtainStatusLoading) {
|
||||
@ -59,6 +59,13 @@ class CurtainBatchStatusView extends StatelessWidget
|
||||
code: 'control',
|
||||
deviceId: devicesIds.first,
|
||||
label: 'Curtains',
|
||||
onChanged: (value) {
|
||||
context.read<CurtainBloc>().add(CurtainBatchControl(
|
||||
devicesIds: devicesIds,
|
||||
code: 'control',
|
||||
value: value,
|
||||
));
|
||||
},
|
||||
),
|
||||
FirmwareUpdateWidget(deviceId: devicesIds.first, version: 5),
|
||||
FactoryResetWidget(deviceId: devicesIds.first),
|
||||
|
@ -58,6 +58,15 @@ class CurtainStatusControlsView extends StatelessWidget
|
||||
code: 'control',
|
||||
deviceId: deviceId,
|
||||
label: 'Curtains',
|
||||
onChanged: (value) {
|
||||
context.read<CurtainBloc>().add(
|
||||
CurtainControl(
|
||||
deviceId: deviceId,
|
||||
code: 'control',
|
||||
value: value,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox.shrink(),
|
||||
],
|
||||
|
@ -4,6 +4,7 @@ import 'package:syncrow_web/pages/device_managment/all_devices/models/device_rep
|
||||
import 'package:syncrow_web/pages/device_managment/shared/table/table_cell_widget.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/shared/table/table_header.dart';
|
||||
|
||||
// ignore: must_be_immutable
|
||||
class ReportsTable extends StatelessWidget {
|
||||
final DeviceReport report;
|
||||
final String? thirdColumnTitle;
|
||||
|
Reference in New Issue
Block a user