This commit is contained in:
mohammad
2024-09-26 16:47:19 +03:00
parent 6e32f30970
commit bc1b92cd5c
2 changed files with 16 additions and 50 deletions

View File

@ -376,40 +376,31 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
void _groupAllOn(GroupAllOnEvent event, Emitter<TwoGangState> emit) async {
emit(LoadingNewSate(twoGangModel: deviceStatus));
try {
// Set all switches (firstSwitch and secondSwitch) based on the event value (on/off)
for (int i = 0; i < groupTwoGangList.length; i++) {
groupTwoGangList[i].firstSwitch = true;
groupTwoGangList[i].secondSwitch = true;
}
// Emit the state with updated values
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: true));
// Get a list of all device IDs
List<String> allDeviceIds =
groupTwoGangList.map((device) => device.deviceId).toList();
// First call for switch_1
final response1 = await DevicesAPI.deviceController(
code: 'switch_1', // Controls first switch for all devices
code: 'switch_1',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Second call for switch_2
final response2 = await DevicesAPI.deviceController(
code: 'switch_2', // Controls second switch for all devices
code: 'switch_2',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Check if either response is unsuccessful, then reset to initial state
if (!response1['success'] || !response2['success']) {
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
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
}
@ -418,40 +409,33 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
void _groupAllOff(GroupAllOffEvent event, Emitter<TwoGangState> emit) async {
emit(LoadingNewSate(twoGangModel: deviceStatus));
try {
// Set all switches (firstSwitch and secondSwitch) based on the event value (on/off)
for (int i = 0; i < groupTwoGangList.length; i++) {
groupTwoGangList[i].firstSwitch = false;
groupTwoGangList[i].secondSwitch = false;
}
// Emit the state with updated values
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: false));
// Get a list of all device IDs
List<String> allDeviceIds =
groupTwoGangList.map((device) => device.deviceId).toList();
// First call for switch_1
final response1 = await DevicesAPI.deviceController(
code: 'switch_1', // Controls first switch for all devices
code: 'switch_1',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Second call for switch_2
final response2 = await DevicesAPI.deviceController(
code: 'switch_2', // Controls second switch for all devices
code: 'switch_2',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Check if either response is unsuccessful, then reset to initial state
if (!response1['success'] || !response2['success']) {
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
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
}
@ -767,6 +751,4 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
add(InitialEvent(groupScreen: twoGangGroup));
}
}
}

View File

@ -360,7 +360,6 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
);
}
// List<DeviceModel> devicesList = [];
List<GroupWHModel> groupTwoGangList = [];
bool allSwitchesOn = true;
@ -438,39 +437,32 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
GroupAllOnEvent event, Emitter<WaterHeaterState> emit) async {
emit(LoadingNewSate(whModel: deviceStatus));
try {
// Set all switches (firstSwitch and secondSwitch) based on the event value (on/off)
for (int i = 0; i < groupTwoGangList.length; i++) {
groupTwoGangList[i].firstSwitch = true;
}
// Emit the state with updated values
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: true));
// Get a list of all device IDs
List<String> allDeviceIds =
groupTwoGangList.map((device) => device.deviceId).toList();
// First call for switch_1
final response1 = await DevicesAPI.deviceController(
code: 'switch_1', // Controls first switch for all devices
code: 'switch_1',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Second call for switch_2
final response2 = await DevicesAPI.deviceController(
code: 'switch_2', // Controls second switch for all devices
code: 'switch_2',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Check if either response is unsuccessful, then reset to initial state
if (!response1['success'] || !response2['success']) {
await Future.delayed(const Duration(milliseconds: 500));
add(InitialWizardEvent());
}
} catch (_) {
// In case of an error, delay and reset the screen to initial state
await Future.delayed(const Duration(milliseconds: 500));
add(InitialWizardEvent());
}
@ -480,39 +472,31 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
GroupAllOffEvent event, Emitter<WaterHeaterState> emit) async {
emit(LoadingNewSate(whModel: deviceStatus));
try {
// Set all switches (firstSwitch and secondSwitch) based on the event value (on/off)
for (int i = 0; i < groupTwoGangList.length; i++) {
groupTwoGangList[i].firstSwitch = false;
}
// Emit the state with updated values
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: false));
// Get a list of all device IDs
List<String> allDeviceIds =
groupTwoGangList.map((device) => device.deviceId).toList();
// First call for switch_1
final response1 = await DevicesAPI.deviceController(
code: 'switch_1', // Controls first switch for all devices
code: 'switch_1',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Second call for switch_2
final response2 = await DevicesAPI.deviceController(
code: 'switch_2', // Controls second switch for all devices
code: 'switch_2',
devicesUuid: allDeviceIds,
value: true, // true (on) or false (off) depending on the event value
value: true,
);
// Check if either response is unsuccessful, then reset to initial state
if (!response1['success'] || !response2['success']) {
await Future.delayed(const Duration(milliseconds: 500));
add(InitialWizardEvent());
}
} catch (_) {
// In case of an error, delay and reset the screen to initial state
await Future.delayed(const Duration(milliseconds: 500));
add(InitialWizardEvent());
}