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