diff --git a/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart b/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart index 4b64206..1d431e9 100644 --- a/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart +++ b/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart @@ -29,7 +29,8 @@ class OneTouchBloc extends Bloc { bool oneTouchGroup = false; List devicesList = []; - OneTouchBloc({required this.oneTouchId, required this.switchCode}) : super(InitialState()) { + OneTouchBloc({required this.oneTouchId, required this.switchCode}) + : super(InitialState()) { on(_fetchOneTouchStatus); on(_oneTouchUpdated); on(_changeFirstSwitch); @@ -52,7 +53,8 @@ class OneTouchBloc extends Bloc { on(_changeStatus); } - void _fetchOneTouchStatus(InitialEvent event, Emitter emit) async { + void _fetchOneTouchStatus( + InitialEvent event, Emitter emit) async { emit(LoadingInitialState()); try { var response = await DevicesAPI.getDeviceStatus(oneTouchId); @@ -71,18 +73,21 @@ class OneTouchBloc extends Bloc { _listenToChanges() { try { - DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$oneTouchId'); + DatabaseReference ref = + FirebaseDatabase.instance.ref('device-status/$oneTouchId'); Stream stream = ref.onValue; stream.listen((DatabaseEvent event) async { if (_timer != null) { await Future.delayed(const Duration(seconds: 2)); } - Map usersMap = event.snapshot.value as Map; + Map usersMap = + event.snapshot.value as Map; List statusList = []; usersMap['status'].forEach((element) { - statusList.add(StatusModel(code: element['code'], value: element['value'])); + statusList + .add(StatusModel(code: element['code'], value: element['value'])); }); deviceStatus = OneTouchModel.fromJson(statusList); @@ -97,7 +102,8 @@ class OneTouchBloc extends Bloc { emit(UpdateState(oneTouchModel: deviceStatus)); } - void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter emit) async { + void _changeFirstSwitch( + ChangeFirstSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(oneTouchModel: deviceStatus)); try { deviceStatus.firstSwitch = !event.value; @@ -122,17 +128,20 @@ class OneTouchBloc extends Bloc { } } - void _changeSliding(ChangeSlidingSegment event, Emitter emit) async { + void _changeSliding( + ChangeSlidingSegment event, Emitter emit) async { emit(ChangeSlidingSegmentState(value: event.value)); } - void _setCounterValue(SetCounterValue event, Emitter emit) async { + void _setCounterValue( + SetCounterValue event, Emitter emit) async { emit(LoadingNewSate(oneTouchModel: deviceStatus)); int seconds = 0; try { seconds = event.duration.inSeconds; final response = await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: oneTouchId, code: event.deviceCode, value: seconds), + DeviceControlModel( + deviceId: oneTouchId, code: event.deviceCode, value: seconds), oneTouchId); if (response['success'] ?? false) { @@ -155,7 +164,8 @@ class OneTouchBloc extends Bloc { } } - void _getCounterValue(GetCounterEvent event, Emitter emit) async { + void _getCounterValue( + GetCounterEvent event, Emitter emit) async { emit(LoadingInitialState()); try { var response = await DevicesAPI.getDeviceStatus(oneTouchId); @@ -244,7 +254,8 @@ class OneTouchBloc extends Bloc { deviceId: oneTouchId, ); List jsonData = response; - listSchedule = jsonData.map((item) => ScheduleModel.fromJson(item)).toList(); + listSchedule = + jsonData.map((item) => ScheduleModel.fromJson(item)).toList(); emit(InitialState()); } on DioException catch (e) { final errorData = e.response!.data; @@ -255,12 +266,13 @@ class OneTouchBloc extends Bloc { int? getTimeStampWithoutSeconds(DateTime? dateTime) { if (dateTime == null) return null; - DateTime dateTimeWithoutSeconds = - DateTime(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute); + DateTime dateTimeWithoutSeconds = DateTime(dateTime.year, dateTime.month, + dateTime.day, dateTime.hour, dateTime.minute); return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000; } - Future toggleChange(ToggleScheduleEvent event, Emitter emit) async { + Future toggleChange( + ToggleScheduleEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final response = await DevicesAPI.changeSchedule( @@ -279,7 +291,8 @@ class OneTouchBloc extends Bloc { } } - Future deleteSchedule(DeleteScheduleEvent event, Emitter emit) async { + Future deleteSchedule( + DeleteScheduleEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final response = await DevicesAPI.deleteSchedule( @@ -299,7 +312,8 @@ class OneTouchBloc extends Bloc { } } - void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter emit) { + void toggleCreateSchedule( + ToggleCreateScheduleEvent event, Emitter emit) { emit(LoadingInitialState()); createSchedule = !createSchedule; selectedDays.clear(); @@ -328,7 +342,8 @@ class OneTouchBloc extends Bloc { int selectedTabIndex = 0; - void toggleSelectedIndex(ToggleSelectedEvent event, Emitter emit) { + void toggleSelectedIndex( + ToggleSelectedEvent event, Emitter emit) { emit(LoadingInitialState()); selectedTabIndex = event.index; emit(ChangeSlidingSegmentState(value: selectedTabIndex)); @@ -337,7 +352,8 @@ class OneTouchBloc extends Bloc { List groupOneTouchList = []; bool allSwitchesOn = true; - void _fetchOneTouchWizardStatus(InitialWizardEvent event, Emitter emit) async { + void _fetchOneTouchWizardStatus( + InitialWizardEvent event, Emitter emit) async { emit(LoadingInitialState()); try { devicesList = []; @@ -347,7 +363,8 @@ class OneTouchBloc extends Bloc { HomeCubit.getInstance().selectedSpace?.id ?? '', '1GT'); for (int i = 0; i < devicesList.length; i++) { - var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); + var response = + await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); List statusModelList = []; for (var status in response['status']) { statusModelList.add(StatusModel.fromJson(status)); @@ -368,15 +385,16 @@ class OneTouchBloc extends Bloc { return true; }); } - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: allSwitchesOn)); + emit(UpdateGroupState( + oneTouchList: groupOneTouchList, allSwitches: allSwitchesOn)); } catch (e) { emit(FailedState(error: e.toString())); return; } } - void _changeFirstWizardSwitch( - ChangeFirstWizardSwitchStatusEvent event, Emitter emit) async { + void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event, + Emitter emit) async { emit(LoadingNewSate(oneTouchModel: deviceStatus)); try { bool allSwitchesValue = true; @@ -394,8 +412,9 @@ class OneTouchBloc extends Bloc { value: !event.value, ); - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: allSwitchesValue)); - if (!response['success']) { + emit(UpdateGroupState( + oneTouchList: groupOneTouchList, allSwitches: allSwitchesValue)); + if (response['success']) { add(InitialEvent(groupScreen: oneTouchGroup)); } } catch (_) { @@ -412,10 +431,12 @@ class OneTouchBloc extends Bloc { } // Emit the state with updated values - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: true)); + emit( + UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: true)); // Get a list of all device IDs - List allDeviceIds = groupOneTouchList.map((device) => device.deviceId).toList(); + List allDeviceIds = + groupOneTouchList.map((device) => device.deviceId).toList(); // First call for switch_1 final response1 = await DevicesAPI.deviceBatchController( @@ -423,20 +444,12 @@ class OneTouchBloc extends Bloc { devicesUuid: allDeviceIds, value: true, // true (on) or false (off) depending on the event value ); - - // Second call for switch_2 - final response2 = await DevicesAPI.deviceBatchController( - code: 'switch_2', // Controls second switch for all devices - devicesUuid: allDeviceIds, - value: true, // true (on) or false (off) depending on the event value - ); - - // Check if either response is unsuccessful, then reset to initial state - if (!response1['success'] || !response2['success']) { + if (response1['failedResults'].toString() != '[]') { await Future.delayed(const Duration(milliseconds: 500)); add(const InitialEvent(groupScreen: true)); } } catch (_) { + emit(FailedState(error: _.toString())); // In case of an error, delay and reset the screen to initial state await Future.delayed(const Duration(milliseconds: 500)); add(const InitialEvent(groupScreen: true)); @@ -452,31 +465,27 @@ class OneTouchBloc extends Bloc { } // Emit the state with updated values - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: false)); + emit(UpdateGroupState( + oneTouchList: groupOneTouchList, allSwitches: false)); // Get a list of all device IDs - List allDeviceIds = groupOneTouchList.map((device) => device.deviceId).toList(); + List allDeviceIds = + groupOneTouchList.map((device) => device.deviceId).toList(); // First call for switch_1 final response1 = await DevicesAPI.deviceBatchController( code: 'switch_1', // Controls first switch for all devices devicesUuid: allDeviceIds, - value: true, // true (on) or false (off) depending on the event value + value: false, // true (on) or false (off) depending on the event value ); - // Second call for switch_2 - final response2 = await DevicesAPI.deviceBatchController( - code: 'switch_2', // Controls second switch for all devices - devicesUuid: allDeviceIds, - value: true, // true (on) or false (off) depending on the event value - ); // Check if either response is unsuccessful, then reset to initial state - if (!response1['success'] || !response2['success']) { + if (response1['failedResults'].toString() != '[]') { await Future.delayed(const Duration(milliseconds: 500)); add(const InitialEvent(groupScreen: true)); } } catch (_) { - // In case of an error, delay and reset the screen to initial state + emit(FailedState(error: _.toString())); await Future.delayed(const Duration(milliseconds: 500)); add(const InitialEvent(groupScreen: true)); } @@ -485,7 +494,8 @@ class OneTouchBloc extends Bloc { String statusSelected = ''; String optionSelected = ''; - Future _changeStatus(ChangeStatusEvent event, Emitter emit) async { + Future _changeStatus( + ChangeStatusEvent event, Emitter emit) async { try { emit(LoadingInitialState()); @@ -510,7 +520,10 @@ class OneTouchBloc extends Bloc { final selectedControl = controlMap[optionSelected]?[statusSelected]; if (selectedControl != null) { await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: oneTouchId, code: optionSelected, value: selectedControl), + DeviceControlModel( + deviceId: oneTouchId, + code: optionSelected, + value: selectedControl), oneTouchId, ); } else { diff --git a/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart b/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart index 242f2c5..af74e8d 100644 --- a/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart +++ b/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart @@ -107,7 +107,7 @@ class ThreeGangBloc extends Bloc { } deviceStatus = ThreeGangModel.fromJson(statusModelList); emit(UpdateState(threeGangModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } } catch (e) { emit(FailedState(error: e.toString())); @@ -368,33 +368,30 @@ class ThreeGangBloc extends Bloc { emit(UpdateGroupState( threeGangList: groupThreeGangList, allSwitches: true)); - for (int i = 0; i < groupThreeGangList.length; i++) { - final response = await Future.wait([ - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeGangList[i].deviceId, - code: 'switch_1', - value: true), - groupThreeGangList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeGangList[i].deviceId, - code: 'switch_2', - value: true), - groupThreeGangList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeGangList[i].deviceId, - code: 'switch_3', - value: true), - groupThreeGangList[i].deviceId), - ]); + List allDeviceIds = + groupThreeGangList.map((device) => device.deviceId).toList(); - if (response.every((element) => !element['success'])) { - await Future.delayed(const Duration(milliseconds: 500)); - add(const InitialEvent(groupScreen: true)); - break; - } + final response1 = await DevicesAPI.deviceBatchController( + code: 'switch_1', + devicesUuid: allDeviceIds, + value: true, + ); + final response2 = await DevicesAPI.deviceBatchController( + code: 'switch_2', + devicesUuid: allDeviceIds, + value: true, + ); + final response3 = await DevicesAPI.deviceBatchController( + code: 'switch_3', + devicesUuid: allDeviceIds, + value: true, + ); + + if (response1['failedResults'].toString() != '[]' || + response2['failedResults'].toString() != '[]' || + response3['failedResults'].toString() != '[]') { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); } } catch (_) { await Future.delayed(const Duration(milliseconds: 500)); @@ -413,34 +410,30 @@ class ThreeGangBloc extends Bloc { } emit(UpdateGroupState( threeGangList: groupThreeGangList, allSwitches: false)); + List allDeviceIds = + groupThreeGangList.map((device) => device.deviceId).toList(); - for (int i = 0; i < groupThreeGangList.length; i++) { - final response = await Future.wait([ - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeGangList[i].deviceId, - code: 'switch_1', - value: false), - groupThreeGangList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeGangList[i].deviceId, - code: 'switch_2', - value: false), - groupThreeGangList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeGangList[i].deviceId, - code: 'switch_3', - value: false), - groupThreeGangList[i].deviceId), - ]); + final response1 = await DevicesAPI.deviceBatchController( + code: 'switch_1', + devicesUuid: allDeviceIds, + value: false, + ); + final response2 = await DevicesAPI.deviceBatchController( + code: 'switch_2', + devicesUuid: allDeviceIds, + value: false, + ); + final response3 = await DevicesAPI.deviceBatchController( + code: 'switch_3', + devicesUuid: allDeviceIds, + value: false, + ); - if (response.every((element) => !element['success'])) { - await Future.delayed(const Duration(milliseconds: 500)); - add(const InitialEvent(groupScreen: true)); - break; - } + if (response1['failedResults'].toString() != '[]' || + response2['failedResults'].toString() != '[]' || + response3['failedResults'].toString() != '[]') { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); } } catch (_) { await Future.delayed(const Duration(milliseconds: 500)); @@ -592,8 +585,6 @@ class ThreeGangBloc extends Bloc { GetScheduleEvent event, Emitter emit, ) async { - print('getSchedule=${switchCode}'); - try { emit(LoadingInitialState()); final response = await DevicesAPI.getSchedule( diff --git a/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart b/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart index d4a59ea..8a4ac56 100644 --- a/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart +++ b/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart @@ -38,7 +38,8 @@ class ThreeTouchBloc extends Bloc { List groupThreeTouchList = []; bool allSwitchesOn = true; - ThreeTouchBloc({required this.threeTouchId, required this.switchCode}) : super(InitialState()) { + ThreeTouchBloc({required this.threeTouchId, required this.switchCode}) + : super(InitialState()) { on(_fetchThreeTouchStatus); on(_threeTouchUpdated); on(_changeFirstSwitch); @@ -53,19 +54,18 @@ class ThreeTouchBloc extends Bloc { on(_onClose); on(_groupAllOn); on(_groupAllOff); - on(toggleDaySelection); on(saveSchedule); on(getSchedule); on(toggleChange); on(deleteSchedule); - on(toggleSelectedIndex); on(toggleCreateSchedule); on(_changeStatus); } - void _fetchThreeTouchStatus(InitialEvent event, Emitter emit) async { + void _fetchThreeTouchStatus( + InitialEvent event, Emitter emit) async { emit(LoadingInitialState()); try { threeTouchGroup = event.groupScreen; @@ -77,7 +77,8 @@ class ThreeTouchBloc extends Bloc { HomeCubit.getInstance().selectedSpace?.id ?? '', '3GT'); for (int i = 0; i < devicesList.length; i++) { - var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); + var response = + await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); List statusModelList = []; for (var status in response['status']) { statusModelList.add(StatusModel.fromJson(status)); @@ -94,13 +95,16 @@ class ThreeTouchBloc extends Bloc { if (groupThreeTouchList.isNotEmpty) { groupThreeTouchList.firstWhere((element) { - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesOn = false; } return true; }); } - emit(UpdateGroupState(threeTouchList: groupThreeTouchList, allSwitches: allSwitchesOn)); + emit(UpdateGroupState( + threeTouchList: groupThreeTouchList, allSwitches: allSwitchesOn)); } else { var response = await DevicesAPI.getDeviceStatus(threeTouchId); List statusModelList = []; @@ -109,7 +113,7 @@ class ThreeTouchBloc extends Bloc { } deviceStatus = ThreeTouchModel.fromJson(statusModelList); emit(UpdateState(threeTouchModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } } catch (e) { emit(FailedState(error: e.toString())); @@ -119,18 +123,21 @@ class ThreeTouchBloc extends Bloc { _listenToChanges() { try { - DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeTouchId'); + DatabaseReference ref = + FirebaseDatabase.instance.ref('device-status/$threeTouchId'); Stream stream = ref.onValue; stream.listen((DatabaseEvent event) async { if (_timer != null) { await Future.delayed(const Duration(seconds: 2)); } - Map usersMap = event.snapshot.value as Map; + Map usersMap = + event.snapshot.value as Map; List statusList = []; usersMap['status'].forEach((element) { - statusList.add(StatusModel(code: element['code'], value: element['value'])); + statusList + .add(StatusModel(code: element['code'], value: element['value'])); }); deviceStatus = ThreeTouchModel.fromJson(statusList); @@ -145,7 +152,8 @@ class ThreeTouchBloc extends Bloc { emit(UpdateState(threeTouchModel: deviceStatus)); } - void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter emit) async { + void _changeFirstSwitch( + ChangeFirstSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(threeTouchModel: deviceStatus)); try { if (threeTouchGroup) { @@ -154,11 +162,15 @@ class ThreeTouchBloc extends Bloc { if (element.deviceId == event.deviceId) { element.firstSwitch = !event.value; } - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesValue = false; } }); - emit(UpdateGroupState(threeTouchList: groupThreeTouchList, allSwitches: allSwitchesValue)); + emit(UpdateGroupState( + threeTouchList: groupThreeTouchList, + allSwitches: allSwitchesValue)); } else { deviceStatus.firstSwitch = !event.value; emit(UpdateState(threeTouchModel: deviceStatus)); @@ -185,8 +197,8 @@ class ThreeTouchBloc extends Bloc { } } - void _changeSecondSwitch( - ChangeSecondSwitchStatusEvent event, Emitter emit) async { + void _changeSecondSwitch(ChangeSecondSwitchStatusEvent event, + Emitter emit) async { emit(LoadingNewSate(threeTouchModel: deviceStatus)); try { if (threeTouchGroup) { @@ -195,11 +207,15 @@ class ThreeTouchBloc extends Bloc { if (element.deviceId == event.deviceId) { element.secondSwitch = !event.value; } - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesValue = false; } }); - emit(UpdateGroupState(threeTouchList: groupThreeTouchList, allSwitches: allSwitchesValue)); + emit(UpdateGroupState( + threeTouchList: groupThreeTouchList, + allSwitches: allSwitchesValue)); } else { deviceStatus.secondSwitch = !event.value; emit(UpdateState(threeTouchModel: deviceStatus)); @@ -225,7 +241,8 @@ class ThreeTouchBloc extends Bloc { } } - void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter emit) async { + void _changeThirdSwitch( + ChangeThirdSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(threeTouchModel: deviceStatus)); try { if (threeTouchGroup) { @@ -234,11 +251,15 @@ class ThreeTouchBloc extends Bloc { if (element.deviceId == event.deviceId) { element.thirdSwitch = !event.value; } - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesValue = false; } }); - emit(UpdateGroupState(threeTouchList: groupThreeTouchList, allSwitches: allSwitchesValue)); + emit(UpdateGroupState( + threeTouchList: groupThreeTouchList, + allSwitches: allSwitchesValue)); } else { deviceStatus.thirdSwitch = !event.value; emit(UpdateState(threeTouchModel: deviceStatus)); @@ -277,15 +298,21 @@ class ThreeTouchBloc extends Bloc { final response = await Future.wait([ DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeTouchId, code: 'switch_1', value: deviceStatus.firstSwitch), + deviceId: threeTouchId, + code: 'switch_1', + value: deviceStatus.firstSwitch), threeTouchId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeTouchId, code: 'switch_2', value: deviceStatus.secondSwitch), + deviceId: threeTouchId, + code: 'switch_2', + value: deviceStatus.secondSwitch), threeTouchId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeTouchId, code: 'switch_3', value: deviceStatus.thirdSwitch), + deviceId: threeTouchId, + code: 'switch_3', + value: deviceStatus.thirdSwitch), threeTouchId), ]); @@ -311,15 +338,21 @@ class ThreeTouchBloc extends Bloc { final response = await Future.wait([ DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeTouchId, code: 'switch_1', value: deviceStatus.firstSwitch), + deviceId: threeTouchId, + code: 'switch_1', + value: deviceStatus.firstSwitch), threeTouchId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeTouchId, code: 'switch_2', value: deviceStatus.secondSwitch), + deviceId: threeTouchId, + code: 'switch_2', + value: deviceStatus.secondSwitch), threeTouchId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeTouchId, code: 'switch_3', value: deviceStatus.thirdSwitch), + deviceId: threeTouchId, + code: 'switch_3', + value: deviceStatus.thirdSwitch), threeTouchId), ]); @@ -341,29 +374,31 @@ class ThreeTouchBloc extends Bloc { groupThreeTouchList[i].secondSwitch = true; groupThreeTouchList[i].thirdSwitch = true; } - emit(UpdateGroupState(threeTouchList: groupThreeTouchList, allSwitches: true)); + emit(UpdateGroupState( + threeTouchList: groupThreeTouchList, allSwitches: true)); + List allDeviceIds = + groupThreeTouchList.map((device) => device.deviceId).toList(); + final response1 = await DevicesAPI.deviceBatchController( + code: 'switch_1', + devicesUuid: allDeviceIds, + value: true, + ); + final response2 = await DevicesAPI.deviceBatchController( + code: 'switch_2', + devicesUuid: allDeviceIds, + value: true, + ); + final response3 = await DevicesAPI.deviceBatchController( + code: 'switch_3', + devicesUuid: allDeviceIds, + value: true, + ); - for (int i = 0; i < groupThreeTouchList.length; i++) { - final response = await Future.wait([ - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeTouchList[i].deviceId, code: 'switch_1', value: true), - groupThreeTouchList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeTouchList[i].deviceId, code: 'switch_2', value: true), - groupThreeTouchList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeTouchList[i].deviceId, code: 'switch_3', value: true), - groupThreeTouchList[i].deviceId), - ]); - - if (response.every((element) => !element['success'])) { - await Future.delayed(const Duration(milliseconds: 500)); - add(const InitialEvent(groupScreen: true)); - break; - } + if (response1['failedResults'].toString() != '[]' || + response2['failedResults'].toString() != '[]' || + response3['failedResults'].toString() != '[]') { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); } } catch (_) { await Future.delayed(const Duration(milliseconds: 500)); @@ -371,7 +406,8 @@ class ThreeTouchBloc extends Bloc { } } - void _groupAllOff(GroupAllOffEvent event, Emitter emit) async { + void _groupAllOff( + GroupAllOffEvent event, Emitter emit) async { emit(LoadingNewSate(threeTouchModel: deviceStatus)); try { for (int i = 0; i < groupThreeTouchList.length; i++) { @@ -379,29 +415,31 @@ class ThreeTouchBloc extends Bloc { groupThreeTouchList[i].secondSwitch = false; groupThreeTouchList[i].thirdSwitch = false; } - emit(UpdateGroupState(threeTouchList: groupThreeTouchList, allSwitches: false)); + List allDeviceIds = + groupThreeTouchList.map((device) => device.deviceId).toList(); + emit(UpdateGroupState( + threeTouchList: groupThreeTouchList, allSwitches: false)); + final response1 = await DevicesAPI.deviceBatchController( + code: 'switch_1', + devicesUuid: allDeviceIds, + value: false, + ); + final response2 = await DevicesAPI.deviceBatchController( + code: 'switch_2', + devicesUuid: allDeviceIds, + value: false, + ); + final response3 = await DevicesAPI.deviceBatchController( + code: 'switch_3', + devicesUuid: allDeviceIds, + value: false, + ); - for (int i = 0; i < groupThreeTouchList.length; i++) { - final response = await Future.wait([ - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeTouchList[i].deviceId, code: 'switch_1', value: false), - groupThreeTouchList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeTouchList[i].deviceId, code: 'switch_2', value: false), - groupThreeTouchList[i].deviceId), - DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: groupThreeTouchList[i].deviceId, code: 'switch_3', value: false), - groupThreeTouchList[i].deviceId), - ]); - - if (response.every((element) => !element['success'])) { - await Future.delayed(const Duration(milliseconds: 500)); - add(const InitialEvent(groupScreen: true)); - break; - } + if (response1['failedResults'].toString() != '[]' || + response2['failedResults'].toString() != '[]' || + response3['failedResults'].toString() != '[]') { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); } } catch (_) { await Future.delayed(const Duration(milliseconds: 500)); @@ -409,17 +447,20 @@ class ThreeTouchBloc extends Bloc { } } - void _changeSliding(ChangeSlidingSegment event, Emitter emit) async { + void _changeSliding( + ChangeSlidingSegment event, Emitter emit) async { emit(ChangeSlidingSegmentState(value: event.value)); } - void _setCounterValue(SetCounterValue event, Emitter emit) async { + void _setCounterValue( + SetCounterValue event, Emitter emit) async { emit(LoadingNewSate(threeTouchModel: deviceStatus)); int seconds = 0; try { seconds = event.duration.inSeconds; final response = await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeTouchId, code: event.deviceCode, value: seconds), + DeviceControlModel( + deviceId: threeTouchId, code: event.deviceCode, value: seconds), threeTouchId); if (response['success'] ?? false) { @@ -446,7 +487,8 @@ class ThreeTouchBloc extends Bloc { } } - void _getCounterValue(GetCounterEvent event, Emitter emit) async { + void _getCounterValue( + GetCounterEvent event, Emitter emit) async { emit(LoadingInitialState()); try { var response = await DevicesAPI.getDeviceStatus(threeTouchId); @@ -556,7 +598,8 @@ class ThreeTouchBloc extends Bloc { deviceId: threeTouchId, ); List jsonData = response; - listSchedule = jsonData.map((item) => ScheduleModel.fromJson(item)).toList(); + listSchedule = + jsonData.map((item) => ScheduleModel.fromJson(item)).toList(); emit(InitialState()); } on DioException catch (e) { final errorData = e.response!.data; @@ -567,12 +610,13 @@ class ThreeTouchBloc extends Bloc { int? getTimeStampWithoutSeconds(DateTime? dateTime) { if (dateTime == null) return null; - DateTime dateTimeWithoutSeconds = - DateTime(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute); + DateTime dateTimeWithoutSeconds = DateTime(dateTime.year, dateTime.month, + dateTime.day, dateTime.hour, dateTime.minute); return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000; } - Future toggleChange(ToggleScheduleEvent event, Emitter emit) async { + Future toggleChange( + ToggleScheduleEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final response = await DevicesAPI.changeSchedule( @@ -591,7 +635,8 @@ class ThreeTouchBloc extends Bloc { } } - Future deleteSchedule(DeleteScheduleEvent event, Emitter emit) async { + Future deleteSchedule( + DeleteScheduleEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final response = await DevicesAPI.deleteSchedule( @@ -611,13 +656,15 @@ class ThreeTouchBloc extends Bloc { } } - void toggleSelectedIndex(ToggleSelectedEvent event, Emitter emit) { + void toggleSelectedIndex( + ToggleSelectedEvent event, Emitter emit) { emit(LoadingInitialState()); selectedTabIndex = event.index; emit(ChangeSlidingSegmentState(value: selectedTabIndex)); } - void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter emit) { + void toggleCreateSchedule( + ToggleCreateScheduleEvent event, Emitter emit) { emit(LoadingInitialState()); createSchedule = !createSchedule; selectedDays.clear(); @@ -635,7 +682,8 @@ class ThreeTouchBloc extends Bloc { String statusSelected = ''; String optionSelected = ''; - Future _changeStatus(ChangeStatusEvent event, Emitter emit) async { + Future _changeStatus( + ChangeStatusEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final Map> controlMap = { @@ -669,7 +717,10 @@ class ThreeTouchBloc extends Bloc { final selectedControl = controlMap[optionSelected]?[statusSelected]; if (selectedControl != null) { await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeTouchId, code: optionSelected, value: selectedControl), + DeviceControlModel( + deviceId: threeTouchId, + code: optionSelected, + value: selectedControl), threeTouchId, ); } else { diff --git a/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart b/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart index 0f4c794..ff32edf 100644 --- a/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart +++ b/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart @@ -3,7 +3,6 @@ import 'package:dio/dio.dart'; import 'package:firebase_database/firebase_database.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; - import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_event.dart'; import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_state.dart'; import 'package:syncrow_app/features/devices/model/device_control_model.dart'; @@ -30,14 +29,14 @@ class TwoTouchBloc extends Bloc { relay_status_2: status.off, ); Timer? _timer; - + bool twoTouchGroup = false; List devicesList = []; List groupTwoTouchList = []; bool allSwitchesOn = true; bool toggleSchedule = true; List selectedDays = []; - + bool createSchedule = false; List listSchedule = []; @@ -586,12 +585,9 @@ class TwoTouchBloc extends Bloc { emit(UpdateGroupState( twoTouchList: groupTwoTouchList, allSwitches: allSwitchesValue)); - - List allDeviceIds = - groupTwoTouchList.map((device) => device.deviceId).toList(); final response = await DevicesAPI.deviceBatchController( code: 'switch_1', - devicesUuid: allDeviceIds, + devicesUuid: [event.deviceId], value: !event.value, ); @@ -616,15 +612,13 @@ class TwoTouchBloc extends Bloc { allSwitchesValue = false; } }); - List allDeviceIds = - groupTwoTouchList.map((device) => device.deviceId).toList(); emit(UpdateGroupState( twoTouchList: groupTwoTouchList, allSwitches: allSwitchesValue)); final response = await DevicesAPI.deviceBatchController( code: 'switch_2', - devicesUuid: allDeviceIds, + devicesUuid: [event.deviceId], value: !event.value, ); diff --git a/lib/services/api/devices_api.dart b/lib/services/api/devices_api.dart index c7305db..e277df4 100644 --- a/lib/services/api/devices_api.dart +++ b/lib/services/api/devices_api.dart @@ -385,11 +385,13 @@ class DevicesAPI { String? code, bool? value, }) async { + print({"devicesUuid": devicesUuid, "code": code, "value": value}); final response = await _httpService.post( path: ApiEndpoints.controlBatch, body: {"devicesUuid": devicesUuid, "code": code, "value": value}, showServerMessage: true, expectedResponseModel: (json) { + print('json===$json'); return json; }, );