changes in threeGang

This commit is contained in:
mohammad
2025-02-26 10:58:32 +03:00
parent 7c5d7e1dda
commit d3068b8e14
2 changed files with 18 additions and 9 deletions

View File

@ -518,26 +518,35 @@ class DevicesCubit extends Cubit<DevicesState> {
}
final device = allDevices[deviceIndex];
final switches = ['switch_1', 'switch_2', 'switch_3'];
final anySwitchOff = device.status.any(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
);
final targetState = anySwitchOff ? true : false;
for (final switchCode in switches) {
final statusIndex =
device.status.indexWhere((s) => s.code == switchCode);
if (statusIndex != -1) {
final currentValue = device.status[statusIndex].value ?? false;
final toggledValue = !currentValue;
if (!targetState && !currentValue) {
continue;
}
final controlRequest = DeviceControlModel(
code: switchCode, value: toggledValue, deviceId: deviceUuid);
code: switchCode, value: targetState, deviceId: deviceUuid);
final response =
await DevicesAPI.controlDevice(controlRequest, deviceUuid);
if (response['success'] != true) {
throw Exception('Failed to toggle $switchCode');
}
device.status[statusIndex].value = toggledValue;
device.status[statusIndex].value = targetState;
}
}
final anySwitchOff = device.status.any(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
device.toggleStatus = device.status.every(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == true),
);
device.toggleStatus = !anySwitchOff;
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} catch (failure) {
@ -545,6 +554,7 @@ class DevicesCubit extends Cubit<DevicesState> {
}
}
Future<void> towGangToggle(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));

View File

@ -115,7 +115,6 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
}
}
StreamSubscription<DatabaseEvent>? _streamSubscription;
void _listenToChanges() {
@ -127,7 +126,7 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
_streamSubscription = stream.listen((DatabaseEvent event) async {
if (_timer != null) {
await Future.delayed(const Duration(seconds: 2));
await Future.delayed(const Duration(seconds: 1));
}
Map<dynamic, dynamic> usersMap =
event.snapshot.value as Map<dynamic, dynamic>;
@ -147,7 +146,7 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
@override
Future<void> close() async {
_streamSubscription?.cancel();
_streamSubscription = null;
_streamSubscription = null;
return super.close();
}