all_devices and Restrict_user

This commit is contained in:
mohammad
2025-01-29 14:10:38 +03:00
parent d72253e3de
commit c578d63134
29 changed files with 1402 additions and 310 deletions
@@ -4,6 +4,7 @@ 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/acs_bloc/acs_event.dart';
import 'package:syncrow_app/features/devices/bloc/acs_bloc/acs_state.dart';
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
import 'package:syncrow_app/features/devices/model/ac_model.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
@@ -135,6 +136,8 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
ac.acSwitch = acSwitchValue;
}
}
_setAllAcsTempsAndSwitches();
_emitAcsStatus(emit);
} else {
@@ -433,9 +436,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
seconds = event.seconds;
final response = await DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: acId,
code: 'countdown_time',
value: event.duration),
deviceId: acId, code: 'countdown_time', value: event.duration),
acId);
if (response['success'] ?? false) {
@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -112,7 +114,7 @@ class CeilingSensorBloc extends Bloc<CeilingSensorEvent, CeilingSensorState> {
code: 'presence_state',
);
recordGroups = response;
// print('---${recordGroups.data!.first.eventTime}');
// print('---${jsonEncode(recordGroups.data.first.)}');
emit(FitchData());
} on DioException catch (e) {
final errorData = e.response!.data;
+378 -42
View File
@@ -31,6 +31,7 @@ class DevicesCubit extends Cubit<DevicesState> {
// Fetch groups based on the selected space ID
await fetchGroups(selectedSpace.id);
await fetchAllDevices(selectedSpace);
}
DevicesCubit._() : super(DevicesInitial()) {
@@ -109,17 +110,6 @@ class DevicesCubit extends Cubit<DevicesState> {
}
// Getter to retrieve all devices from HomeCubit
List<DeviceModel> get allDevices {
List<DeviceModel> devices = [];
if (HomeCubit.getInstance().selectedSpace != null) {
for (var room in HomeCubit.getInstance().selectedSpace!.subspaces) {
if (room.devices != null) {
devices.addAll(room.devices!);
}
}
}
return devices;
}
// DevicesCategoryModel? get chosenCategory {
// for (var category in allCategories!) {
@@ -267,36 +257,36 @@ class DevicesCubit extends Cubit<DevicesState> {
}
///////////////////////// API CALLS //////////////////////////
deviceControl(DeviceControlModel control, String deviceId) async {
emitSafe(DeviceControlLoading(
code: control.code,
));
try {
var response = await DevicesAPI.controlDevice(control, deviceId);
// deviceControl(DeviceControlModel control, String deviceId) async {
// emitSafe(DeviceControlLoading(
// code: control.code,
// ));
// try {
// var response = await DevicesAPI.controlDevice(control, deviceId);
if (response['success'] ?? false) {
emitSafe(DeviceControlSuccess(code: control.code));
//this delay is to give tuya server time to update the status
// Future.delayed(const Duration(milliseconds: 400), () {
fetchDevicesStatues(
deviceId,
HomeCubit.getInstance()
.selectedSpace!
.subspaces
.indexOf(HomeCubit.getInstance().selectedRoom!),
code: control.code);
// });
} else {
emitSafe(DeviceControlError('Failed to control the device'));
}
} catch (failure) {
emitSafe(DeviceControlError(failure.toString()));
return;
}
}
// if (response['success'] ?? false) {
// emitSafe(DeviceControlSuccess(code: control.code));
// //this delay is to give tuya server time to update the status
// // Future.delayed(const Duration(milliseconds: 400), () {
// fetchDevicesStatues(
// deviceId,
// HomeCubit.getInstance()
// .selectedSpace!
// .subspaces
// .indexOf(HomeCubit.getInstance().selectedRoom!),
// code: control.code);
// // });
// } else {
// emitSafe(DeviceControlError('Failed to control the device'));
// }
// } catch (failure) {
// emitSafe(DeviceControlError(failure.toString()));
// return;
// }
// }
fetchGroups(String spaceId) async {
emitSafe(DevicesCategoriesLoading());
emitSafe(GetDevicesLoading());
try {
allCategories = await DevicesAPI.fetchGroups(spaceId);
} catch (e) {
@@ -321,12 +311,15 @@ class DevicesCubit extends Cubit<DevicesState> {
try {
HomeCubit.getInstance().selectedSpace!.subspaces[roomIndex].devices =
await DevicesAPI.getDevicesByRoomId(
communityUuid: unit!.community.uuid, spaceUuid: unit.id, roomId: roomId);
communityUuid: unit!.community.uuid,
spaceUuid: unit.id,
roomId: roomId);
} catch (e) {
emitSafe(GetDevicesError(e.toString()));
return;
}
final devices = HomeCubit.getInstance().selectedSpace!.subspaces[roomIndex].devices;
final devices =
HomeCubit.getInstance().selectedSpace!.subspaces[roomIndex].devices;
emitSafe(GetDevicesSuccess(devices));
//get status for each device
@@ -356,8 +349,11 @@ class DevicesCubit extends Cubit<DevicesState> {
emitSafe(GetDeviceStatusError(e.toString()));
return;
}
HomeCubit.getInstance().selectedSpace!.subspaces[roomIndex].devices![deviceIndex].status =
statuses;
HomeCubit.getInstance()
.selectedSpace!
.subspaces[roomIndex]
.devices![deviceIndex]
.status = statuses;
emitSafe(GetDeviceStatusSuccess(code: code));
}
@@ -406,6 +402,346 @@ class DevicesCubit extends Cubit<DevicesState> {
// emitSafe(LightBrightnessChanged(value));
// }
// }
// List<DeviceModel> _fetchedDevices = [];
// List<DeviceModel> get allDevices => _fetchedDevices;
List<DeviceModel> allDevices = [];
Future<void> fetchAllDevices(SpaceModel? unit) async {
emitSafe(GetDevicesLoading());
try {
final devices = await DevicesAPI.getAllDevices(
communityUuid: unit!.community.uuid,
spaceUuid: unit.id,
);
allDevices = devices;
emitSafe(GetDevicesSuccess(allDevices));
} catch (e) {
emitSafe(GetDevicesError(e.toString()));
}
}
bool isDeviceOn(DeviceModel device) {
final switchStatuses = device.status.where(
(s) => (s.code?.startsWith('switch') ?? false),
);
final anySwitchFalse = switchStatuses.any((s) => s.value == false);
return !anySwitchFalse;
}
void updateDeviceStatus(String deviceId, bool newToggleStatus) {
emitSafe(GetDevicesLoading());
// Create a fresh copy of the device list.
final updatedDevices = List<DeviceModel>.from(allDevices);
for (int i = 0; i < updatedDevices.length; i++) {
final device = updatedDevices[i];
if (device.uuid == deviceId) {
updatedDevices[i] = DeviceModel(
activeTime: device.activeTime,
localKey: device.localKey,
model: device.model,
name: device.name,
icon: device.icon,
categoryName: device.categoryName,
type: device.type,
isOnline: device.isOnline,
status: device.status, // Make sure to keep the same status list
productName: device.productName,
timeZone: device.timeZone,
updateTime: device.updateTime,
uuid: device.uuid,
productUuid: device.productUuid,
productType: device.productType,
subspace: device.subspace,
toggleStatus: newToggleStatus,
);
break;
}
}
emit(GetDevicesSuccess(updatedDevices));
}
Future<void> threeGangToggle(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex == -1) {
throw Exception('Device not found');
}
final device = allDevices[deviceIndex];
final switches = ['switch_1', 'switch_2', 'switch_3'];
for (final switchCode in switches) {
final statusIndex =
device.status.indexWhere((s) => s.code == switchCode);
if (statusIndex != -1) {
final currentValue = device.status[statusIndex].value ?? false;
final toggledValue = !currentValue;
final controlRequest = DeviceControlModel(
code: switchCode, value: toggledValue, deviceId: deviceUuid);
final response =
await DevicesAPI.controlDevice(controlRequest, deviceUuid);
if (response['success'] != true) {
throw Exception('Failed to toggle $switchCode');
}
device.status[statusIndex].value = toggledValue;
}
}
final anySwitchOff = device.status.any(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
);
device.toggleStatus = !anySwitchOff;
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} catch (failure) {
emit(DeviceControlError(failure.toString()));
}
}
Future<void> towGangToggle(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex == -1) {
throw Exception('Device not found');
}
final device = allDevices[deviceIndex];
final switches = [
'switch_1',
'switch_2',
];
for (final switchCode in switches) {
final statusIndex =
device.status.indexWhere((s) => s.code == switchCode);
if (statusIndex != -1) {
final currentValue = device.status[statusIndex].value ?? false;
final toggledValue = !currentValue;
final controlRequest = DeviceControlModel(
code: switchCode, value: toggledValue, deviceId: deviceUuid);
final response =
await DevicesAPI.controlDevice(controlRequest, deviceUuid);
device.status[statusIndex].value = response['result'];
}
}
final anySwitchOff = device.status.any(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
);
device.toggleStatus = !anySwitchOff;
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} catch (failure) {
emit(DeviceControlError(failure.toString()));
}
}
Future<void> towGTGangToggle(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex == -1) {
throw Exception('Device not found');
}
final device = allDevices[deviceIndex];
final switches = [
'switch_1',
'switch_2',
];
for (final switchCode in switches) {
final statusIndex =
device.status.indexWhere((s) => s.code == switchCode);
if (statusIndex != -1) {
final currentValue = device.status[statusIndex].value ?? false;
final toggledValue = !currentValue;
final controlRequest = DeviceControlModel(
code: switchCode, value: toggledValue, deviceId: deviceUuid);
final response =
await DevicesAPI.controlDevice(controlRequest, deviceUuid);
device.status[statusIndex].value = response['result'];
}
}
final anySwitchOff = device.status.any(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
);
device.toggleStatus = !anySwitchOff;
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} catch (failure) {
emit(DeviceControlError(failure.toString()));
}
}
Future<void> oneGangToggle(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex == -1) {
throw Exception('Device not found');
}
final device = allDevices[deviceIndex];
final statusIndex = device.status.indexWhere((s) => s.code == 'switch_1');
if (statusIndex != -1) {
final currentValue = device.status[statusIndex].value ?? false;
final toggledValue = !currentValue;
final controlRequest = DeviceControlModel(
code: 'switch_1', value: toggledValue, deviceId: deviceUuid);
final response =
await DevicesAPI.controlDevice(controlRequest, deviceUuid);
print(response);
if (response['result'] != true) {
throw Exception('Failed to toggle switch_1');
}
device.status[statusIndex].value = response['result'];
}
final anySwitchOff = device.status.any(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
);
device.toggleStatus = !anySwitchOff;
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} catch (failure) {
emit(DeviceControlError(failure.toString()));
}
}
Future<void> oneGTGangToggle(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex == -1) {
throw Exception('Device not found');
}
final device = allDevices[deviceIndex];
final statusIndex = device.status.indexWhere((s) => s.code == 'switch_1');
if (statusIndex != -1) {
final currentValue = device.status[statusIndex].value ?? false;
final toggledValue = !currentValue;
final controlRequest = DeviceControlModel(
code: 'switch_1', value: toggledValue, deviceId: deviceUuid);
final response =
await DevicesAPI.controlDevice(controlRequest, deviceUuid);
if (response['result'] != true) {
throw Exception('Failed to toggle switch_1');
}
device.status[statusIndex].value = response['result'];
}
final anySwitchOff = device.status.any(
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
);
device.toggleStatus = !anySwitchOff;
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} catch (failure) {
emit(DeviceControlError(failure.toString()));
}
}
Future<void> deviceControl(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final response = await DevicesAPI.controlDevice(control, deviceUuid);
if (response['success'] == true) {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex != -1) {
final device = allDevices[deviceIndex];
final statusIndex =
device.status.indexWhere((s) => s.code == control.code);
if (statusIndex != -1) {
device.status[statusIndex].value = control.value;
}
final anySwitchOff = device.status.any(
(s) =>
(s.code?.startsWith('switch_') ?? false) && (s.value == false),
);
device.toggleStatus = !anySwitchOff;
allDevices[deviceIndex] = device;
}
emit(DeviceControlSuccess(code: control.code));
} else {
emit(DeviceControlError('Failed to control the device'));
}
} catch (failure) {
emit(DeviceControlError(failure.toString()));
}
}
Future<void> changeCurtainSwitch(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex == -1) {
throw Exception('Device not found');
}
final device = allDevices[deviceIndex];
final isOpen = control.value == "open";
final newValue = isOpen ? 0 : 100;
final response = await DevicesAPI.deviceBatchController(
code: 'percent_control',
devicesUuid: [deviceUuid],
value: newValue,
);
if (response['success'] == true) {
final statusIndex =
device.status.indexWhere((s) => s.code == 'percent_control');
if (statusIndex != -1) {
device.status[statusIndex].value = newValue;
}
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} else {
emit(DeviceControlError('Failed to toggle curtain.'));
}
} catch (error) {
emit(DeviceControlError(error.toString()));
}
}
Future<void> changeGarageSwitch(
DeviceControlModel control, String deviceUuid) async {
emit(SwitchControlLoading(code: control.code));
try {
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceUuid);
if (deviceIndex == -1) {
throw Exception('Device not found');
}
final device = allDevices[deviceIndex];
final isOpen = control.value == "open";
final newValue = isOpen ? 0 : 100;
final response = await DevicesAPI.deviceBatchController(
code: 'percent_control',
devicesUuid: [deviceUuid],
value: newValue,
);
if (response['success'] == true) {
final statusIndex =
device.status.indexWhere((s) => s.code == 'percent_control');
if (statusIndex != -1) {
device.status[statusIndex].value = newValue;
}
allDevices[deviceIndex] = device;
emit(DeviceControlSuccess(code: control.code));
} else {
emit(DeviceControlError('Failed to toggle curtain.'));
}
} catch (error) {
emit(DeviceControlError(error.toString()));
}
}
}
enum LightMode {
+10 -3
View File
@@ -34,6 +34,7 @@ class GetDeviceStatusError extends DevicesState {
}
class GetDevicesLoading extends DevicesState {}
class RoomLoading extends DevicesState {}
class GetDevicesSuccess extends DevicesState {
GetDevicesSuccess(this.devices);
@@ -55,10 +56,16 @@ class DeviceSwitchChanged extends DevicesState {}
class DeviceSelected extends DevicesState {}
// Device Control
class DeviceControlLoading extends DevicesState {
final String? code;
// class DeviceControlLoading extends DevicesState {
// final String? code;
DeviceControlLoading({this.code});
// DeviceControlLoading({this.code});
// }
class SwitchControlLoading extends DevicesState {
final String? code;
final String? deviceId;
SwitchControlLoading({this.deviceId, this.code});
}
class DeviceControlSuccess extends DevicesState {
@@ -40,7 +40,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
bool createSchedule = false;
List<ScheduleModel> listSchedule = [];
TwoTouchBloc({required this.twoTouchId, required this.switchCode}) : super(InitialState()) {
TwoTouchBloc({required this.twoTouchId, required this.switchCode})
: super(InitialState()) {
on<InitialEvent>(_fetchTwoTouchStatus);
on<TwoTouchUpdated>(_twoTouchUpdated);
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
@@ -71,13 +72,15 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
int selectedTabIndex = 0;
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<TwoTouchState> emit) {
void toggleSelectedIndex(
ToggleSelectedEvent event, Emitter<TwoTouchState> emit) {
emit(LoadingInitialState());
selectedTabIndex = event.index;
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
}
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<TwoTouchState> emit) {
void toggleCreateSchedule(
ToggleCreateScheduleEvent event, Emitter<TwoTouchState> emit) {
emit(LoadingInitialState());
createSchedule = !createSchedule;
selectedDays.clear();
@@ -85,7 +88,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
emit(UpdateCreateScheduleState(createSchedule));
}
void _fetchTwoTouchStatus(InitialEvent event, Emitter<TwoTouchState> emit) async {
void _fetchTwoTouchStatus(
InitialEvent event, Emitter<TwoTouchState> emit) async {
emit(LoadingInitialState());
try {
var response = await DevicesAPI.getDeviceStatus(twoTouchId);
@@ -104,18 +108,21 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
_listenToChanges() {
try {
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$twoTouchId');
DatabaseReference ref =
FirebaseDatabase.instance.ref('device-status/$twoTouchId');
Stream<DatabaseEvent> stream = ref.onValue;
stream.listen((DatabaseEvent event) async {
if (_timer != null) {
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 = [];
usersMap['status'].forEach((element) {
statusList.add(StatusModel(code: element['code'], value: element['value']));
statusList
.add(StatusModel(code: element['code'], value: element['value']));
});
deviceStatus = TwoTouchModel.fromJson(statusList);
@@ -130,7 +137,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
emit(UpdateState(twoTouchModel: deviceStatus));
}
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<TwoTouchState> emit) async {
void _changeFirstSwitch(
ChangeFirstSwitchStatusEvent event, Emitter<TwoTouchState> emit) async {
emit(LoadingNewSate(twoTouchModel: deviceStatus));
try {
deviceStatus.firstSwitch = !event.value;
@@ -141,7 +149,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
_timer = Timer(const Duration(milliseconds: 100), () async {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: twoTouchId, code: 'switch_1', value: !event.value),
DeviceControlModel(
deviceId: twoTouchId, code: 'switch_1', value: !event.value),
twoTouchId);
if (!response['success']) {
@@ -153,7 +162,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
}
void _changeSecondSwitch(ChangeSecondSwitchStatusEvent event, Emitter<TwoTouchState> emit) async {
void _changeSecondSwitch(
ChangeSecondSwitchStatusEvent event, Emitter<TwoTouchState> emit) async {
emit(LoadingNewSate(twoTouchModel: deviceStatus));
try {
deviceStatus.secondSwitch = !event.value;
@@ -163,7 +173,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
_timer = Timer(const Duration(milliseconds: 100), () async {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: twoTouchId, code: 'switch_2', value: !event.value),
DeviceControlModel(
deviceId: twoTouchId, code: 'switch_2', value: !event.value),
twoTouchId);
if (!response['success']) {
@@ -186,11 +197,15 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: twoTouchId, code: 'switch_1', value: deviceStatus.firstSwitch),
deviceId: twoTouchId,
code: 'switch_1',
value: deviceStatus.firstSwitch),
twoTouchId),
DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: twoTouchId, code: 'switch_2', value: deviceStatus.secondSwitch),
deviceId: twoTouchId,
code: 'switch_2',
value: deviceStatus.secondSwitch),
twoTouchId),
]);
@@ -213,11 +228,15 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: twoTouchId, code: 'switch_1', value: deviceStatus.firstSwitch),
deviceId: twoTouchId,
code: 'switch_1',
value: deviceStatus.firstSwitch),
twoTouchId),
DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: twoTouchId, code: 'switch_2', value: deviceStatus.secondSwitch),
deviceId: twoTouchId,
code: 'switch_2',
value: deviceStatus.secondSwitch),
twoTouchId),
]);
if (response.every((element) => !element['success'])) {
@@ -237,8 +256,10 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
groupTwoTouchList[i].firstSwitch = true;
groupTwoTouchList[i].secondSwitch = true;
}
emit(UpdateGroupState(twoTouchList: groupTwoTouchList, allSwitches: true));
List<String> allDeviceIds = groupTwoTouchList.map((device) => device.deviceId).toList();
emit(
UpdateGroupState(twoTouchList: groupTwoTouchList, allSwitches: true));
List<String> allDeviceIds =
groupTwoTouchList.map((device) => device.deviceId).toList();
final response1 = await DevicesAPI.deviceBatchController(
code: 'switch_1',
@@ -271,9 +292,11 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
groupTwoTouchList[i].secondSwitch = false;
}
emit(UpdateGroupState(twoTouchList: groupTwoTouchList, allSwitches: false));
emit(UpdateGroupState(
twoTouchList: groupTwoTouchList, allSwitches: false));
List<String> allDeviceIds = groupTwoTouchList.map((device) => device.deviceId).toList();
List<String> allDeviceIds =
groupTwoTouchList.map((device) => device.deviceId).toList();
final response1 = await DevicesAPI.deviceBatchController(
code: 'switch_1',
@@ -298,17 +321,20 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
}
void _changeSliding(ChangeSlidingSegment event, Emitter<TwoTouchState> emit) async {
void _changeSliding(
ChangeSlidingSegment event, Emitter<TwoTouchState> emit) async {
emit(ChangeSlidingSegmentState(value: event.value));
}
void _setCounterValue(SetCounterValue event, Emitter<TwoTouchState> emit) async {
void _setCounterValue(
SetCounterValue event, Emitter<TwoTouchState> emit) async {
emit(LoadingNewSate(twoTouchModel: deviceStatus));
int seconds = 0;
try {
seconds = event.duration.inSeconds;
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: twoTouchId, code: event.deviceCode, value: seconds),
DeviceControlModel(
deviceId: twoTouchId, code: event.deviceCode, value: seconds),
twoTouchId);
if (response['success'] ?? false) {
@@ -333,7 +359,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
}
void _getCounterValue(GetCounterEvent event, Emitter<TwoTouchState> emit) async {
void _getCounterValue(
GetCounterEvent event, Emitter<TwoTouchState> emit) async {
emit(LoadingInitialState());
try {
add(GetScheduleEvent());
@@ -441,7 +468,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
deviceId: twoTouchId,
);
List<dynamic> 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;
@@ -452,12 +480,13 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
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 toggleRepeat(ToggleScheduleEvent event, Emitter<TwoTouchState> emit) async {
Future toggleRepeat(
ToggleScheduleEvent event, Emitter<TwoTouchState> emit) async {
try {
emit(LoadingInitialState());
final response = await DevicesAPI.changeSchedule(
@@ -476,7 +505,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
}
Future deleteSchedule(DeleteScheduleEvent event, Emitter<TwoTouchState> emit) async {
Future deleteSchedule(
DeleteScheduleEvent event, Emitter<TwoTouchState> emit) async {
try {
emit(LoadingInitialState());
final response = await DevicesAPI.deleteSchedule(
@@ -496,7 +526,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
}
void _fetchTwoTouchWizardStatus(InitialWizardEvent event, Emitter<TwoTouchState> emit) async {
void _fetchTwoTouchWizardStatus(
InitialWizardEvent event, Emitter<TwoTouchState> emit) async {
emit(LoadingInitialState());
try {
devicesList = [];
@@ -506,7 +537,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
HomeCubit.getInstance().selectedSpace?.id ?? '', '2GT');
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 = [];
for (var status in response['status']) {
statusModelList.add(StatusModel.fromJson(status));
@@ -529,15 +561,16 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
return true;
});
}
emit(UpdateGroupState(twoTouchList: groupTwoTouchList, allSwitches: allSwitchesOn));
emit(UpdateGroupState(
twoTouchList: groupTwoTouchList, allSwitches: allSwitchesOn));
} catch (e) {
emit(FailedState(error: e.toString()));
return;
}
}
void _changeFirstWizardSwitch(
ChangeFirstWizardSwitchStatusEvent event, Emitter<TwoTouchState> emit) async {
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
Emitter<TwoTouchState> emit) async {
emit(LoadingNewSate(twoTouchModel: deviceStatus));
try {
bool allSwitchesValue = true;
@@ -550,7 +583,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
});
emit(UpdateGroupState(twoTouchList: groupTwoTouchList, allSwitches: allSwitchesValue));
emit(UpdateGroupState(
twoTouchList: groupTwoTouchList, allSwitches: allSwitchesValue));
final response = await DevicesAPI.deviceBatchController(
code: 'switch_1',
devicesUuid: [event.deviceId],
@@ -565,8 +599,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
}
void _changeSecondWizardSwitch(
ChangeSecondWizardSwitchStatusEvent event, Emitter<TwoTouchState> emit) async {
void _changeSecondWizardSwitch(ChangeSecondWizardSwitchStatusEvent event,
Emitter<TwoTouchState> emit) async {
emit(LoadingNewSate(twoTouchModel: deviceStatus));
try {
bool allSwitchesValue = true;
@@ -579,7 +613,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
}
});
emit(UpdateGroupState(twoTouchList: groupTwoTouchList, allSwitches: allSwitchesValue));
emit(UpdateGroupState(
twoTouchList: groupTwoTouchList, allSwitches: allSwitchesValue));
final response = await DevicesAPI.deviceBatchController(
code: 'switch_2',
@@ -598,7 +633,8 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
String statusSelected = '';
String optionSelected = '';
Future<void> _changeStatus(ChangeStatusEvent event, Emitter<TwoTouchState> emit) async {
Future<void> _changeStatus(
ChangeStatusEvent event, Emitter<TwoTouchState> emit) async {
try {
emit(LoadingInitialState());
final Map<String, Map<String, String>> controlMap = {
@@ -627,11 +663,15 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
final selectedControl = controlMap[optionSelected]?[statusSelected];
if (selectedControl != null) {
await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: twoTouchId, code: optionSelected, value: selectedControl),
DeviceControlModel(
deviceId: twoTouchId,
code: optionSelected,
value: selectedControl),
twoTouchId,
);
} else {
emit(const FailedState(error: 'Invalid statusSelected or optionSelected'));
emit(const FailedState(
error: 'Invalid statusSelected or optionSelected'));
}
} on DioException catch (e) {
final errorData = e.response!.data;