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

View File

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