mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-16 01:56:19 +00:00
Compare commits
27 Commits
c159ecdb8a
...
bugifx/emp
Author | SHA1 | Date | |
---|---|---|---|
ef5e7c3154 | |||
a64a41548f | |||
0d50aa68fa | |||
88aac86b10 | |||
0673548745 | |||
c2fc8fa0ae | |||
cbf10bbf78 | |||
d95588ce84 | |||
7e2e591d71 | |||
2ec81bda9c | |||
3b0f51473c | |||
13757d89ee | |||
d3068b8e14 | |||
7c5d7e1dda | |||
0a97a4867d | |||
6e55b488aa | |||
22789234fe | |||
8393ec353a | |||
0b45d61b25 | |||
450b773921 | |||
3f7f7ce49f | |||
2dc4e16a75 | |||
cd41720244 | |||
67209961b4 | |||
9098276223 | |||
36dfe2c85e | |||
efed5f55a7 |
3
.env.dev
3
.env.dev
@ -1,2 +1,3 @@
|
||||
ENV_NAME=development
|
||||
BASE_URL=https://syncrow-dev.azurewebsites.net
|
||||
BASE_URL=https://syncrow-dev.azurewebsites.net
|
||||
PROJECT_ID=0e62577c-06fa-41b9-8a92-99a21fbaf51c
|
||||
|
@ -1,2 +1,3 @@
|
||||
ENV_NAME=production
|
||||
BASE_URL=https://syncrow-staging.azurewebsites.net
|
||||
BASE_URL=https://syncrow-staging.azurewebsites.net
|
||||
PROJECT_ID=bcda711e-9fc2-4168-a05e-171b4026d1ff
|
||||
|
@ -1,2 +1,3 @@
|
||||
ENV_NAME=staging
|
||||
BASE_URL=https://syncrow-staging.azurewebsites.net
|
||||
BASE_URL=https://syncrow-staging.azurewebsites.net
|
||||
PROJECT_ID=bcda711e-9fc2-4168-a05e-171b4026d1ff
|
||||
|
@ -10,6 +10,7 @@ import 'package:share_plus/share_plus.dart';
|
||||
import 'package:syncrow_app/features/app_layout/model/permission_model.dart';
|
||||
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||
import 'package:syncrow_app/features/app_layout/view/widgets/app_bar_home_dropdown.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
|
||||
import 'package:syncrow_app/features/devices/model/subspace_model.dart';
|
||||
@ -28,6 +29,7 @@ import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||
import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/profile_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||
part 'home_state.dart';
|
||||
@ -67,6 +69,8 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
var uuid =
|
||||
await const FlutterSecureStorage().read(key: UserModel.userUuidKey);
|
||||
user = await ProfileApi().fetchUserInfo(uuid);
|
||||
project = user?.project;
|
||||
|
||||
emit(HomeUserInfoLoaded(user!));
|
||||
} catch (e) {
|
||||
return;
|
||||
@ -175,6 +179,8 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
|
||||
SubSpaceModel? selectedRoom;
|
||||
|
||||
Project? project;
|
||||
|
||||
PageController devicesPageController = PageController();
|
||||
|
||||
PageController roomsPageController = PageController();
|
||||
@ -324,8 +330,8 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
//////////////////////////////////////// API ////////////////////////////////////////
|
||||
generateInvitation(SpaceModel unit) async {
|
||||
try {
|
||||
final invitationCode =
|
||||
await SpacesAPI.generateInvitationCode(unit.id, unit.community.uuid);
|
||||
final invitationCode = await SpacesAPI.generateInvitationCode(unit.id,
|
||||
unit.community.uuid, project?.uuid ?? TempConst.projectIdDev);
|
||||
if (invitationCode.isNotEmpty) {
|
||||
Share.share('The invitation code is $invitationCode');
|
||||
CustomSnackBar.displaySnackBar(
|
||||
@ -380,8 +386,10 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
fetchRoomsByUnitId(SpaceModel space) async {
|
||||
emitSafe(GetSpaceRoomsLoading());
|
||||
try {
|
||||
space.subspaces =
|
||||
await SpacesAPI.getSubSpaceBySpaceId(space.community.uuid, space.id);
|
||||
space.subspaces = await SpacesAPI.getSubSpaceBySpaceId(
|
||||
space.community.uuid,
|
||||
space.id,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
} catch (failure) {
|
||||
emitSafe(GetSpaceRoomsError(failure.toString()));
|
||||
return;
|
||||
@ -414,7 +422,6 @@ class HomeCubit extends Cubit<HomeState> {
|
||||
emitSafe(ActivationError(errMessage: errorMsg));
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/////////////////////////////////////// Nav ///////////////////////////////////////
|
||||
|
27
lib/features/auth/model/project_model.dart
Normal file
27
lib/features/auth/model/project_model.dart
Normal file
@ -0,0 +1,27 @@
|
||||
class Project {
|
||||
final String uuid;
|
||||
final String name;
|
||||
final String description;
|
||||
|
||||
const Project({
|
||||
required this.uuid,
|
||||
required this.name,
|
||||
required this.description,
|
||||
});
|
||||
|
||||
factory Project.fromJson(Map<String, dynamic> json) {
|
||||
return Project(
|
||||
uuid: json['uuid'] as String,
|
||||
name: json['name'] as String,
|
||||
description: json['description'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'uuid': uuid,
|
||||
'name': name,
|
||||
'description': description,
|
||||
};
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/auth/model/token.dart';
|
||||
|
||||
class UserModel {
|
||||
@ -20,6 +21,7 @@ class UserModel {
|
||||
final bool? hasAcceptedAppAgreement;
|
||||
final DateTime? appAgreementAcceptedAt;
|
||||
final Role? role;
|
||||
final Project? project;
|
||||
|
||||
UserModel({
|
||||
required this.uuid,
|
||||
@ -38,6 +40,7 @@ class UserModel {
|
||||
required this.hasAcceptedAppAgreement,
|
||||
required this.appAgreementAcceptedAt,
|
||||
required this.role,
|
||||
required this.project,
|
||||
});
|
||||
|
||||
factory UserModel.fromJson(Map<String, dynamic> json) {
|
||||
@ -62,33 +65,35 @@ class UserModel {
|
||||
? DateTime.parse(json['appAgreementAcceptedAt'])
|
||||
: null,
|
||||
role: json['role'] != null ? Role.fromJson(json['role']) : null,
|
||||
project:
|
||||
json['project'] != null ? Project.fromJson(json['project']) : null,
|
||||
);
|
||||
}
|
||||
|
||||
factory UserModel.fromToken(Token token) {
|
||||
Map<String, dynamic> tempJson = Token.decodeToken(token.accessToken);
|
||||
return UserModel(
|
||||
uuid: tempJson['uuid'].toString(),
|
||||
email: tempJson['email'],
|
||||
lastName: tempJson['lastName'],
|
||||
firstName: tempJson['firstName'],
|
||||
profilePicture: UserModel.decodeBase64Image(tempJson['profilePicture']),
|
||||
phoneNumber: null,
|
||||
isEmailVerified: null,
|
||||
isAgreementAccepted: null,
|
||||
regionUuid: null,
|
||||
regionName: tempJson['region']?['regionName'],
|
||||
timeZone: tempJson['timezone']?['timeZoneOffset'],
|
||||
hasAcceptedWebAgreement: tempJson['hasAcceptedWebAgreement'],
|
||||
webAgreementAcceptedAt: tempJson['webAgreementAcceptedAt'] != null
|
||||
? DateTime.parse(tempJson['webAgreementAcceptedAt'])
|
||||
: null,
|
||||
hasAcceptedAppAgreement: tempJson['hasAcceptedAppAgreement'],
|
||||
appAgreementAcceptedAt: tempJson['appAgreementAcceptedAt'] != null
|
||||
? DateTime.parse(tempJson['appAgreementAcceptedAt'])
|
||||
: null,
|
||||
role: tempJson['role'] != null ? Role.fromJson(tempJson['role']) : null,
|
||||
);
|
||||
uuid: tempJson['uuid'].toString(),
|
||||
email: tempJson['email'],
|
||||
lastName: tempJson['lastName'],
|
||||
firstName: tempJson['firstName'],
|
||||
profilePicture: UserModel.decodeBase64Image(tempJson['profilePicture']),
|
||||
phoneNumber: null,
|
||||
isEmailVerified: null,
|
||||
isAgreementAccepted: null,
|
||||
regionUuid: null,
|
||||
regionName: tempJson['region']?['regionName'],
|
||||
timeZone: tempJson['timezone']?['timeZoneOffset'],
|
||||
hasAcceptedWebAgreement: tempJson['hasAcceptedWebAgreement'],
|
||||
webAgreementAcceptedAt: tempJson['webAgreementAcceptedAt'] != null
|
||||
? DateTime.parse(tempJson['webAgreementAcceptedAt'])
|
||||
: null,
|
||||
hasAcceptedAppAgreement: tempJson['hasAcceptedAppAgreement'],
|
||||
appAgreementAcceptedAt: tempJson['appAgreementAcceptedAt'] != null
|
||||
? DateTime.parse(tempJson['appAgreementAcceptedAt'])
|
||||
: null,
|
||||
role: tempJson['role'] != null ? Role.fromJson(tempJson['role']) : null,
|
||||
project: null);
|
||||
}
|
||||
|
||||
static Uint8List? decodeBase64Image(String? base64String) {
|
||||
@ -137,8 +142,10 @@ class Role {
|
||||
factory Role.fromJson(Map<String, dynamic> json) {
|
||||
return Role(
|
||||
uuid: json['uuid'],
|
||||
createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null,
|
||||
updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null,
|
||||
createdAt:
|
||||
json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null,
|
||||
updatedAt:
|
||||
json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null,
|
||||
type: json['type'],
|
||||
);
|
||||
}
|
||||
|
19
lib/features/common/bloc/project_cubit.dart
Normal file
19
lib/features/common/bloc/project_cubit.dart
Normal file
@ -0,0 +1,19 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
class ProjectCubit extends Cubit<String?> {
|
||||
final FlutterSecureStorage storage;
|
||||
static const String projectKey = "selected_project_uuid";
|
||||
|
||||
ProjectCubit(this.storage) : super(null);
|
||||
|
||||
Future<void> setProjectUUID(String newUUID) async {
|
||||
await storage.write(key: projectKey, value: newUUID);
|
||||
emit(newUUID);
|
||||
}
|
||||
|
||||
Future<void> clearProjectUUID() async {
|
||||
await storage.delete(key: projectKey);
|
||||
emit(null);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/6_scene_switch_bloc/6_scene_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/6_scene_switch_bloc/6_scene_state.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||
@ -16,6 +17,7 @@ import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/services/api/scene_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
|
||||
class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
|
||||
@ -112,8 +114,9 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
|
||||
FetchRoomsEvent event, Emitter<SixSceneState> emit) async {
|
||||
try {
|
||||
emit(SixSceneLoadingState());
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
roomsList = await SpacesAPI.getSubSpaceBySpaceId(
|
||||
event.unit.community.uuid, event.unit.id);
|
||||
event.unit.community.uuid, event.unit.id, project?.uuid ?? TempConst.projectIdDev);
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
} catch (e) {
|
||||
emit(SixSceneFailedState(errorMessage: e.toString()));
|
||||
@ -125,12 +128,14 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
|
||||
try {
|
||||
emit(SixSceneLoadingState());
|
||||
if (_hasSelectionChanged) {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
await HomeManagementAPI.assignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, sixSceneId);
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, sixSceneId, project?.uuid ?? TempConst.projectIdDev);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
List<String> allDevicesIds = [];
|
||||
allDevices.forEach((element) {
|
||||
allDevicesIds.add(element.uuid!);
|
||||
@ -341,8 +346,10 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
|
||||
emit(SixSceneLoadingState());
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
allScenes = await SceneApi.getScenesByUnitId(
|
||||
event.unitId, event.unit.community.uuid,
|
||||
event.unitId, event.unit.community.uuid, project?.uuid ?? TempConst.projectIdDev,
|
||||
showInDevice: event.showInDevice);
|
||||
|
||||
filteredScenes = allScenes;
|
||||
|
@ -4,7 +4,6 @@ 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';
|
||||
@ -28,7 +27,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
bool allAcsPage = false;
|
||||
bool allAcsOn = true;
|
||||
bool allTempSame = true;
|
||||
int globalTemp = 25;
|
||||
int globalTemp = 250;
|
||||
Timer? _timer;
|
||||
|
||||
ACsBloc({required this.acId}) : super(AcsInitialState()) {
|
||||
@ -69,11 +68,10 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
}
|
||||
deviceStatus =
|
||||
AcStatusModel.fromJson(response['productUuid'], statusModelList);
|
||||
deviceStatus = AcStatusModel.fromJson(response['productUuid'], statusModelList);
|
||||
emit(GetAcStatusState(acStatusModel: deviceStatus));
|
||||
Future.delayed(const Duration(milliseconds: 500));
|
||||
// _listenToChanges();
|
||||
_listenToChanges(acId);
|
||||
}
|
||||
} catch (e) {
|
||||
emit(AcsFailedState(errorMessage: e.toString()));
|
||||
@ -81,29 +79,38 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
|
||||
void _listenToChanges(acId) {
|
||||
try {
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$acId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) {
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) {
|
||||
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 =
|
||||
AcStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||
|
||||
add(AcUpdated());
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_onAcUpdated(AcUpdated event, Emitter<AcsState> emit) {
|
||||
emit(GetAcStatusState(acStatusModel: deviceStatus));
|
||||
}
|
||||
@ -115,15 +122,16 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
HomeCubit.getInstance().selectedSpace?.id ?? '', 'AC');
|
||||
|
||||
for (int i = 0; i < devicesList.length; i++) {
|
||||
_listenToChanges(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));
|
||||
}
|
||||
deviceStatusList.add(
|
||||
AcStatusModel.fromJson(response['productUuid'], statusModelList));
|
||||
deviceStatusList.add(AcStatusModel.fromJson(devicesList[i].uuid ?? '', statusModelList));
|
||||
}
|
||||
|
||||
_setAllAcsTempsAndSwitches();
|
||||
}
|
||||
|
||||
@ -132,7 +140,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
if (allAcsPage) {
|
||||
emit(AcsLoadingState());
|
||||
for (AcStatusModel ac in deviceStatusList) {
|
||||
if (ac.uuid == event.productId) {
|
||||
if (ac.uuid == event.deviceId) {
|
||||
ac.acSwitch = acSwitchValue;
|
||||
}
|
||||
}
|
||||
@ -145,8 +153,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
emit(AcModifyingState(acStatusModel: deviceStatus));
|
||||
}
|
||||
|
||||
await _runDeBouncerForOneDevice(
|
||||
deviceId: event.deviceId, code: 'switch', value: acSwitchValue);
|
||||
await _runDeBouncerForOneDevice(deviceId: event.deviceId, code: 'switch', value: acSwitchValue);
|
||||
}
|
||||
|
||||
void _changeAllAcSwitch(ChangeAllSwitch event, Emitter<AcsState> emit) async {
|
||||
@ -207,8 +214,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
deviceStatus.childLock = lockValue;
|
||||
emit(AcModifyingState(acStatusModel: deviceStatus));
|
||||
|
||||
await _runDeBouncerForOneDevice(
|
||||
deviceId: acId, code: 'child_lock', value: lockValue);
|
||||
await _runDeBouncerForOneDevice(deviceId: acId, code: 'child_lock', value: lockValue);
|
||||
}
|
||||
|
||||
void _increaseCoolTo(IncreaseCoolToTemp event, Emitter<AcsState> emit) async {
|
||||
@ -224,7 +230,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
if (allAcsPage) {
|
||||
emit(AcsLoadingState());
|
||||
for (AcStatusModel ac in deviceStatusList) {
|
||||
if (ac.uuid == event.productId) {
|
||||
if (ac.uuid == event.deviceId) {
|
||||
ac.tempSet = value;
|
||||
}
|
||||
}
|
||||
@ -236,8 +242,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
emit(AcModifyingState(acStatusModel: deviceStatus));
|
||||
}
|
||||
|
||||
await _runDeBouncerForOneDevice(
|
||||
deviceId: event.deviceId, code: 'temp_set', value: value);
|
||||
await _runDeBouncerForOneDevice(deviceId: event.deviceId, code: 'temp_set', value: value);
|
||||
}
|
||||
|
||||
void _decreaseCoolTo(DecreaseCoolToTemp event, Emitter<AcsState> emit) async {
|
||||
@ -253,7 +258,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
if (allAcsPage) {
|
||||
emit(AcsLoadingState());
|
||||
for (AcStatusModel ac in deviceStatusList) {
|
||||
if (ac.uuid == event.productId) {
|
||||
if (ac.uuid == event.deviceId) {
|
||||
ac.tempSet = value;
|
||||
}
|
||||
}
|
||||
@ -265,8 +270,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
emit(AcModifyingState(acStatusModel: deviceStatus));
|
||||
}
|
||||
|
||||
await _runDeBouncerForOneDevice(
|
||||
deviceId: event.deviceId, code: 'temp_set', value: value);
|
||||
await _runDeBouncerForOneDevice(deviceId: event.deviceId, code: 'temp_set', value: value);
|
||||
}
|
||||
|
||||
void _changeAcMode(ChangeAcMode event, Emitter<AcsState> emit) async {
|
||||
@ -274,7 +278,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
if (allAcsPage) {
|
||||
emit(AcsLoadingState());
|
||||
for (AcStatusModel ac in deviceStatusList) {
|
||||
if (ac.uuid == event.productId) {
|
||||
if (ac.uuid == event.deviceId) {
|
||||
ac.modeString = getACModeString(tempMode);
|
||||
ac.acMode = AcStatusModel.getACMode(getACModeString(tempMode));
|
||||
}
|
||||
@ -288,9 +292,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
}
|
||||
|
||||
await _runDeBouncerForOneDevice(
|
||||
deviceId: event.deviceId,
|
||||
code: 'mode',
|
||||
value: getACModeString(tempMode));
|
||||
deviceId: event.deviceId, code: 'mode', value: getACModeString(tempMode));
|
||||
}
|
||||
|
||||
void _changeFanSpeed(ChangeFanSpeed event, Emitter<AcsState> emit) async {
|
||||
@ -301,25 +303,21 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
if (allAcsPage) {
|
||||
emit(AcsLoadingState());
|
||||
for (AcStatusModel ac in deviceStatusList) {
|
||||
if (ac.uuid == event.productId) {
|
||||
if (ac.uuid == event.deviceId) {
|
||||
ac.fanSpeedsString = getNextFanSpeedKey(fanSpeed);
|
||||
ac.acFanSpeed =
|
||||
AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed));
|
||||
ac.acFanSpeed = AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed));
|
||||
}
|
||||
}
|
||||
_emitAcsStatus(emit);
|
||||
} else {
|
||||
emit(AcChangeLoading(acStatusModel: deviceStatus));
|
||||
deviceStatus.fanSpeedsString = getNextFanSpeedKey(fanSpeed);
|
||||
deviceStatus.acFanSpeed =
|
||||
AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed));
|
||||
deviceStatus.acFanSpeed = AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed));
|
||||
emit(AcModifyingState(acStatusModel: deviceStatus));
|
||||
}
|
||||
|
||||
await _runDeBouncerForOneDevice(
|
||||
deviceId: event.deviceId,
|
||||
code: 'level',
|
||||
value: getNextFanSpeedKey(fanSpeed));
|
||||
deviceId: event.deviceId, code: 'level', value: getNextFanSpeedKey(fanSpeed));
|
||||
}
|
||||
|
||||
String getACModeString(TempModes value) {
|
||||
@ -338,17 +336,15 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
allAcsOn = true;
|
||||
allTempSame = true;
|
||||
if (deviceStatusList.isNotEmpty) {
|
||||
int temp = deviceStatusList[0].tempSet;
|
||||
deviceStatusList.firstWhere((element) {
|
||||
int temp = deviceStatusList.first.tempSet;
|
||||
for (var element in deviceStatusList) {
|
||||
if (!element.acSwitch) {
|
||||
allAcsOn = false;
|
||||
}
|
||||
if (element.tempSet != temp) {
|
||||
allTempSame = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
if (allTempSame) {
|
||||
globalTemp = temp;
|
||||
}
|
||||
@ -364,8 +360,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
for (int i = 0; i < deviceStatusList.length; i++) {
|
||||
try {
|
||||
await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: devicesList[i].uuid, code: code, value: value),
|
||||
DeviceControlModel(deviceId: devicesList[i].uuid, code: code, value: value),
|
||||
devicesList[i].uuid ?? '');
|
||||
} catch (_) {
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
@ -387,10 +382,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
_timer = Timer(const Duration(seconds: 1), () async {
|
||||
try {
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: allAcsPage ? deviceId : acId,
|
||||
code: code,
|
||||
value: value),
|
||||
DeviceControlModel(deviceId: allAcsPage ? deviceId : acId, code: code, value: value),
|
||||
allAcsPage ? deviceId : acId);
|
||||
|
||||
if (!response['success']) {
|
||||
@ -407,8 +399,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
if (value >= 20 && value <= 30) {
|
||||
return true;
|
||||
} else {
|
||||
emit(const AcsFailedState(
|
||||
errorMessage: 'The temperature must be between 20 and 30'));
|
||||
emit(const AcsFailedState(errorMessage: 'The temperature must be between 20 and 30'));
|
||||
emit(GetAllAcsStatusState(
|
||||
allAcsStatues: deviceStatusList,
|
||||
allAcs: devicesList,
|
||||
@ -434,9 +425,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
try {
|
||||
seconds = event.seconds;
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: acId, code: 'countdown_time', value: event.duration),
|
||||
acId);
|
||||
DeviceControlModel(deviceId: acId, code: 'countdown_time', value: event.duration), acId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
deviceStatus.countdown1 = seconds;
|
||||
@ -463,8 +452,7 @@ class ACsBloc extends Bloc<AcsEvent, AcsState> {
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
}
|
||||
deviceStatus =
|
||||
AcStatusModel.fromJson(response['productUuid'], statusModelList);
|
||||
deviceStatus = AcStatusModel.fromJson(response['productUuid'], statusModelList);
|
||||
deviceStatus.countdown1;
|
||||
var duration;
|
||||
if (deviceStatus.countdown1 == 5) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
@ -35,35 +35,49 @@ class CeilingSensorBloc extends Bloc<CeilingSensorEvent, CeilingSensorState> {
|
||||
}
|
||||
deviceStatus = CeilingSensorModel.fromJson(statusModelList);
|
||||
emit(UpdateState(ceilingSensorModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
Timer? _timer;
|
||||
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) {
|
||||
_streamSubscription = 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>;
|
||||
List<StatusModel> statusList = [];
|
||||
|
||||
usersMap['status'].forEach((element) {
|
||||
statusList
|
||||
.add(StatusModel(code: element['code'], value: element['value']));
|
||||
});
|
||||
|
||||
deviceStatus = CeilingSensorModel.fromJson(statusList);
|
||||
add(CeilingSensorUpdated());
|
||||
if (!isClosed) {
|
||||
add(CeilingSensorUpdated());
|
||||
}
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_onCeilingSensorUpdated(
|
||||
CeilingSensorUpdated event, Emitter<CeilingSensorState> emit) {
|
||||
emit(UpdateState(ceilingSensorModel: deviceStatus));
|
||||
|
@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
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/curtain_bloc/curtain_event.dart';
|
||||
@ -30,6 +32,7 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
on<InitialWizardEvent>(_fetchWizardStatus);
|
||||
on<GroupAllOffEvent>(_groupAllOff);
|
||||
on<GroupAllOnEvent>(_groupAllOn);
|
||||
on<UpdateCurtainEvent>(_updateCurtain);
|
||||
}
|
||||
|
||||
Future<void> _onOpenCurtain(
|
||||
@ -161,7 +164,10 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
void _fetchStatus(InitCurtain event, Emitter<CurtainState> emit) async {
|
||||
try {
|
||||
emit(CurtainLoadingState());
|
||||
_listenToChanges(curtainId);
|
||||
|
||||
var response = await DevicesAPI.getDeviceStatus(curtainId);
|
||||
|
||||
List<StatusModel> statusModelList = [];
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
@ -169,7 +175,6 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
openPercentage = double.tryParse(statusModelList[1].value.toString())!;
|
||||
curtainWidth = 270 - (openPercentage / 100) * curtainOpeningSpace;
|
||||
blindHeight = 310 - (openPercentage / 100) * blindOpeningSpace;
|
||||
|
||||
emit(CurtainsOpening(
|
||||
curtainWidth: curtainWidth,
|
||||
blindHeight: blindHeight,
|
||||
@ -181,6 +186,76 @@ class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
||||
}
|
||||
}
|
||||
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
|
||||
void _listenToChanges(curtainId) {
|
||||
try {
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$curtainId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) {
|
||||
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']));
|
||||
});
|
||||
openPercentage = double.tryParse(statusList[1].value.toString())!;
|
||||
curtainWidth = 270 - (openPercentage / 100) * curtainOpeningSpace;
|
||||
blindHeight = 310 - (openPercentage / 100) * blindOpeningSpace;
|
||||
add(UpdateCurtainEvent());
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
// _listenToChanges(curtainId) {
|
||||
// try {
|
||||
// print('curtainId=$curtainId');
|
||||
// DatabaseReference ref =
|
||||
// FirebaseDatabase.instance.ref('device-status/$curtainId');
|
||||
// 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>;
|
||||
// List<StatusModel> statusModelList = [];
|
||||
// for (var status in usersMap['status']) {
|
||||
// statusModelList.add(StatusModel.fromJson(status));
|
||||
// print('statusModelList==${statusModelList}');
|
||||
// }
|
||||
|
||||
// openPercentage = double.tryParse(statusModelList[1].value.toString())!;
|
||||
// curtainWidth = 270 - (openPercentage / 100) * curtainOpeningSpace;
|
||||
// blindHeight = 310 - (openPercentage / 100) * blindOpeningSpace;
|
||||
// add(UpdateCurtainEvent());
|
||||
// });
|
||||
// } catch (_) {}
|
||||
// }
|
||||
|
||||
_updateCurtain(UpdateCurtainEvent event, Emitter<CurtainState> emit) {
|
||||
curtainWidth = 270 - (openPercentage / 100) * curtainOpeningSpace;
|
||||
blindHeight = 310 - (openPercentage / 100) * blindOpeningSpace;
|
||||
emit(CurtainsOpening(
|
||||
curtainWidth: curtainWidth,
|
||||
blindHeight: blindHeight,
|
||||
openPercentage: openPercentage,
|
||||
));
|
||||
}
|
||||
|
||||
List<GroupCurtainModel> groupList = [];
|
||||
bool allSwitchesOn = true;
|
||||
List<DeviceModel> devicesList = [];
|
||||
|
@ -33,6 +33,7 @@ class InitCurtain extends CurtainEvent {}
|
||||
class PauseCurtain extends CurtainEvent {}
|
||||
class useCurtainEvent extends CurtainEvent {}
|
||||
class InitialWizardEvent extends CurtainEvent {}
|
||||
class UpdateCurtainEvent extends CurtainEvent {}
|
||||
|
||||
|
||||
class ChangeFirstWizardSwitchStatusEvent extends CurtainEvent {
|
||||
|
@ -2,6 +2,8 @@ import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
|
||||
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_state.dart';
|
||||
@ -10,6 +12,7 @@ import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
|
||||
import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||
|
||||
class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
@ -27,11 +30,16 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
|
||||
static List<DevicesCategoryModel>? allCategories;
|
||||
|
||||
Future<void> _onFetchAllDevices(FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
|
||||
Future<void> _onFetchAllDevices(
|
||||
FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
|
||||
emit(state.copyWith(loading: true));
|
||||
try {
|
||||
final allDevices = await HomeManagementAPI.fetchDevicesByUnitId();
|
||||
emit(state.copyWith(devices: _getOnlyImplementedDevices(allDevices), loading: false));
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final allDevices = await HomeManagementAPI.fetchDevicesByUnitId(
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
emit(state.copyWith(
|
||||
devices: _getOnlyImplementedDevices(allDevices), loading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: e.toString(), loading: false));
|
||||
}
|
||||
@ -41,26 +49,31 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
FetchDevicesByRoomId event, Emitter<DeviceManagerState> emit) async {
|
||||
emit(state.copyWith(loading: true));
|
||||
try {
|
||||
final devices = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId,
|
||||
);
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
emit(state.copyWith(devices: _getOnlyImplementedDevices(devices), loading: false));
|
||||
final devices = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
|
||||
emit(state.copyWith(
|
||||
devices: _getOnlyImplementedDevices(devices), loading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: e.toString(), loading: false));
|
||||
}
|
||||
}
|
||||
|
||||
void _onSelectCategory(SelectCategory event, Emitter<DeviceManagerState> emit) {
|
||||
void _onSelectCategory(
|
||||
SelectCategory event, Emitter<DeviceManagerState> emit) {
|
||||
for (var i = 0; i < allCategories!.length; i++) {
|
||||
allCategories![i].isSelected = i == event.index;
|
||||
}
|
||||
emit(state.copyWith(categoryChanged: true));
|
||||
}
|
||||
|
||||
void _onUnselectAllCategories(UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
|
||||
void _onUnselectAllCategories(
|
||||
UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
|
||||
for (var category in allCategories!) {
|
||||
category.isSelected = false;
|
||||
}
|
||||
@ -104,7 +117,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
_updateDevicesStatus(category, emit);
|
||||
}
|
||||
|
||||
void _onTurnOnOffDevice(TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
|
||||
void _onTurnOnOffDevice(
|
||||
TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
|
||||
var device = event.device;
|
||||
device.isOnline = !device.isOnline!;
|
||||
DevicesCategoryModel category = allCategories!.firstWhere((category) {
|
||||
@ -127,7 +141,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
emit(state.copyWith(categoryChanged: true)); // Set category changed state
|
||||
}
|
||||
|
||||
void _updateDevicesStatus(DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
|
||||
void _updateDevicesStatus(
|
||||
DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
|
||||
if (category.devices != null && category.devices!.isNotEmpty) {
|
||||
bool? tempStatus = category.devices![0].isOnline;
|
||||
for (var device in category.devices!) {
|
||||
@ -147,7 +162,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
try {
|
||||
final deviceFunctions = await DevicesAPI.deviceFunctions(event.deviceId);
|
||||
|
||||
emit(state.copyWith(functionsLoading: false, deviceFunctions: deviceFunctions));
|
||||
emit(state.copyWith(
|
||||
functionsLoading: false, deviceFunctions: deviceFunctions));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(functionsLoading: false, error: e.toString()));
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/device_settings_bloc/device_scene_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/device_settings_bloc/device_scene_state.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
@ -18,6 +19,7 @@ import 'package:syncrow_app/generated/assets.dart';
|
||||
import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
|
||||
class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
|
||||
@ -349,13 +351,20 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
|
||||
AssignRoomEvent event, Emitter<DeviceSettingState> emit) async {
|
||||
try {
|
||||
emit(DeviceSettingLoadingState());
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
if (_hasSelectionChanged) {
|
||||
await HomeManagementAPI.assignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, deviceId);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
event.roomId,
|
||||
deviceId,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
List<String> allDevicesIds = [];
|
||||
allDevices.forEach((element) {
|
||||
allDevicesIds.add(element.uuid!);
|
||||
@ -375,8 +384,12 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
|
||||
FetchRoomsEvent event, Emitter<DeviceSettingState> emit) async {
|
||||
try {
|
||||
emit(DeviceSettingLoadingState());
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
roomsList = await SpacesAPI.getSubSpaceBySpaceId(
|
||||
event.unit.community.uuid, event.unit.id);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
} catch (e) {
|
||||
emit(
|
||||
|
@ -1,10 +1,14 @@
|
||||
// ignore_for_file: constant_identifier_names, unused_import
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
import 'package:flutter/material.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/model/space_model.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
@ -19,6 +23,7 @@ import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/services/api/network_exception.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||
part 'devices_state.dart';
|
||||
|
||||
@ -61,6 +66,43 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
}
|
||||
|
||||
Timer? _timer;
|
||||
|
||||
final Map<String, StreamSubscription<DatabaseEvent>> _deviceSubscriptions =
|
||||
{};
|
||||
|
||||
void _listenToChanges(deviceId) {
|
||||
try {
|
||||
if (_deviceSubscriptions.containsKey(deviceId)) {
|
||||
_deviceSubscriptions[deviceId]?.cancel();
|
||||
_deviceSubscriptions.remove(deviceId);
|
||||
}
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
final subscription = 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>;
|
||||
// print('object-----${usersMap['status']}');
|
||||
List<StatusModel> statusList = [];
|
||||
usersMap['status'].forEach((element) {
|
||||
statusList
|
||||
.add(StatusModel(code: element['code'], value: element['value']));
|
||||
});
|
||||
emitSafe(GetDevicesLoading());
|
||||
final deviceIndex = allDevices.indexWhere((d) => d.uuid == deviceId);
|
||||
if (deviceIndex != -1) {
|
||||
allDevices[deviceIndex].status = statusList;
|
||||
}
|
||||
emitSafe(GetDevicesSuccess(allDevices));
|
||||
});
|
||||
_deviceSubscriptions[deviceId] = subscription;
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static DevicesCubit get(context) => BlocProvider.of(context);
|
||||
|
||||
List<DevicesCategoryModel>? allCategories;
|
||||
@ -309,11 +351,14 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
.subspaces
|
||||
.indexWhere((element) => element.id == roomId);
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
HomeCubit.getInstance().selectedSpace!.subspaces[roomIndex].devices =
|
||||
await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: unit!.community.uuid,
|
||||
spaceUuid: unit.id,
|
||||
roomId: roomId);
|
||||
roomId: roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
} catch (e) {
|
||||
emitSafe(GetDevicesError(e.toString()));
|
||||
return;
|
||||
@ -410,11 +455,26 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
Future<void> fetchAllDevices(SpaceModel? unit) async {
|
||||
emitSafe(GetDevicesLoading());
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final devices = await DevicesAPI.getAllDevices(
|
||||
communityUuid: unit!.community.uuid,
|
||||
spaceUuid: unit.id,
|
||||
);
|
||||
communityUuid: unit!.community.uuid,
|
||||
spaceUuid: unit.id,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
allDevices = devices;
|
||||
for (var deviceId in allDevices) {
|
||||
if (deviceId.type == "3G" ||
|
||||
deviceId.type == "2G" ||
|
||||
deviceId.type == "1G" ||
|
||||
deviceId.type == "3GT" ||
|
||||
deviceId.type == "2GT" ||
|
||||
deviceId.type == "1GT" ||
|
||||
deviceId.type == "WH" ||
|
||||
deviceId.type == "CUR" ||
|
||||
deviceId.type == "AC") {
|
||||
_listenToChanges(deviceId.uuid);
|
||||
}
|
||||
}
|
||||
emitSafe(GetDevicesSuccess(allDevices));
|
||||
} catch (e) {
|
||||
emitSafe(GetDevicesError(e.toString()));
|
||||
@ -473,25 +533,35 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
final device = allDevices[deviceIndex];
|
||||
final switches = ['switch_1', 'switch_2', 'switch_3'];
|
||||
final anySwitchOff = device.status.any(
|
||||
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
|
||||
);
|
||||
final targetState = anySwitchOff ? true : false;
|
||||
|
||||
for (final switchCode in switches) {
|
||||
final statusIndex =
|
||||
device.status.indexWhere((s) => s.code == switchCode);
|
||||
if (statusIndex != -1) {
|
||||
final toggledValue = control.value;
|
||||
final currentValue = device.status[statusIndex].value ?? false;
|
||||
|
||||
if (!targetState && !currentValue) {
|
||||
continue;
|
||||
}
|
||||
final controlRequest = DeviceControlModel(
|
||||
code: switchCode, value: toggledValue, deviceId: deviceUuid);
|
||||
code: switchCode, value: targetState, deviceId: deviceUuid);
|
||||
final response =
|
||||
await DevicesAPI.controlDevice(controlRequest, deviceUuid);
|
||||
if (response['success'] != true) {
|
||||
throw Exception('Failed to toggle $switchCode');
|
||||
}
|
||||
device.status[statusIndex].value = toggledValue;
|
||||
|
||||
device.status[statusIndex].value = targetState;
|
||||
}
|
||||
}
|
||||
final anySwitchOff = device.status.any(
|
||||
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false),
|
||||
device.toggleStatus = device.status.every(
|
||||
(s) => (s.code?.startsWith('switch_') ?? false) && (s.value == true),
|
||||
);
|
||||
device.toggleStatus = !anySwitchOff;
|
||||
|
||||
allDevices[deviceIndex] = device;
|
||||
emit(DeviceControlSuccess(code: control.code));
|
||||
} catch (failure) {
|
||||
@ -590,7 +660,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
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');
|
||||
}
|
||||
|
@ -21,13 +21,14 @@ class DoorSensorBloc extends Bloc<DoorSensorEvent, DoorSensorState> {
|
||||
on<ToggleClosingReminderEvent>(_toggleClosingReminder);
|
||||
on<ToggleDoorAlarmEvent>(_toggleDoorAlarm);
|
||||
}
|
||||
Timer? _timer;
|
||||
bool lowBattery = false;
|
||||
bool closingReminder = false;
|
||||
bool doorAlarm = false;
|
||||
DoorSensorModel deviceStatus = DoorSensorModel(doorContactState: false, batteryPercentage: 0);
|
||||
DoorSensorModel deviceStatus =
|
||||
DoorSensorModel(doorContactState: false, batteryPercentage: 0);
|
||||
|
||||
void _fetchStatus(DoorSensorInitial event, Emitter<DoorSensorState> emit) async {
|
||||
void _fetchStatus(
|
||||
DoorSensorInitial event, Emitter<DoorSensorState> emit) async {
|
||||
emit(DoorSensorLoadingState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(DSId);
|
||||
@ -40,7 +41,7 @@ class DoorSensorBloc extends Bloc<DoorSensorEvent, DoorSensorState> {
|
||||
);
|
||||
emit(UpdateState(doorSensor: deviceStatus));
|
||||
Future.delayed(const Duration(milliseconds: 500));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
} catch (e) {
|
||||
emit(DoorSensorFailedState(errorMessage: e.toString()));
|
||||
return;
|
||||
@ -48,7 +49,8 @@ class DoorSensorBloc extends Bloc<DoorSensorEvent, DoorSensorState> {
|
||||
}
|
||||
|
||||
// Toggle functions for each switch
|
||||
void _toggleLowBattery(ToggleLowBatteryEvent event, Emitter<DoorSensorState> emit) async {
|
||||
void _toggleLowBattery(
|
||||
ToggleLowBatteryEvent event, Emitter<DoorSensorState> emit) async {
|
||||
emit(LoadingNewSate(doorSensor: deviceStatus));
|
||||
try {
|
||||
lowBattery = event.isLowBatteryEnabled;
|
||||
@ -89,7 +91,8 @@ class DoorSensorBloc extends Bloc<DoorSensorEvent, DoorSensorState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _toggleDoorAlarm(ToggleDoorAlarmEvent event, Emitter<DoorSensorState> emit) async {
|
||||
void _toggleDoorAlarm(
|
||||
ToggleDoorAlarmEvent event, Emitter<DoorSensorState> emit) async {
|
||||
emit(LoadingNewSate(doorSensor: deviceStatus));
|
||||
try {
|
||||
doorAlarm = event.isDoorAlarmEnabled;
|
||||
@ -108,9 +111,11 @@ class DoorSensorBloc extends Bloc<DoorSensorEvent, DoorSensorState> {
|
||||
}
|
||||
}
|
||||
|
||||
DeviceReport recordGroups = DeviceReport(startTime: '0', endTime: '0', data: []);
|
||||
DeviceReport recordGroups =
|
||||
DeviceReport(startTime: '0', endTime: '0', data: []);
|
||||
|
||||
Future<void> fetchLogsForLastMonth(ReportLogsInitial event, Emitter<DoorSensorState> emit) async {
|
||||
Future<void> fetchLogsForLastMonth(
|
||||
ReportLogsInitial event, Emitter<DoorSensorState> emit) async {
|
||||
DateTime now = DateTime.now();
|
||||
|
||||
DateTime lastMonth = DateTime(now.year, now.month - 1, now.day);
|
||||
@ -133,22 +138,27 @@ class DoorSensorBloc extends Bloc<DoorSensorEvent, DoorSensorState> {
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
// real-time database
|
||||
Timer? _timer;
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$DSId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$DSId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) async {
|
||||
_streamSubscription = 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: true));
|
||||
statusList
|
||||
.add(StatusModel(code: element['code'], value: element['value']));
|
||||
});
|
||||
|
||||
deviceStatus = DoorSensorModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
add(
|
||||
@ -158,4 +168,11 @@ class DoorSensorBloc extends Bloc<DoorSensorEvent, DoorSensorState> {
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/four_scene_bloc/four_scene_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/four_scene_bloc/four_scene_state.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||
@ -14,6 +16,7 @@ import 'package:syncrow_app/features/devices/model/subspace_model.dart';
|
||||
import 'package:syncrow_app/features/scene/model/scenes_model.dart';
|
||||
import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/scene_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
|
||||
class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
@ -230,6 +233,7 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
deviceStatus = FourSceneModelState.fromJson(
|
||||
statusModelList,
|
||||
);
|
||||
// _listenToChanges();
|
||||
add(const FourSceneSwitchInitial());
|
||||
} catch (e) {
|
||||
emit(FourSceneFailedState(errorMessage: e.toString()));
|
||||
@ -299,9 +303,11 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
LoadScenes event, Emitter<FourSceneState> emit) async {
|
||||
emit(FourSceneLoadingState());
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
if (event.unitId.isNotEmpty) {
|
||||
allScenes = await SceneApi.getScenesByUnitId(
|
||||
event.unitId, event.unit.community.uuid,
|
||||
event.unitId, event.unit.community.uuid, project?.uuid ?? TempConst.projectIdDev,
|
||||
showInDevice: event.showInDevice);
|
||||
|
||||
filteredScenes = allScenes;
|
||||
@ -327,4 +333,33 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
|
||||
}).toList();
|
||||
emit(SearchResultsState());
|
||||
}
|
||||
// Real-time database
|
||||
// Timer? _timer;
|
||||
// _listenToChanges() {
|
||||
// try {
|
||||
// DatabaseReference ref =
|
||||
// FirebaseDatabase.instance.ref('device-status/$fourSceneId');
|
||||
// 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>;
|
||||
// List<StatusModel> statusList = [];
|
||||
|
||||
// usersMap['status'].forEach((element) {
|
||||
// statusList.add(StatusModel(code: element['code'], value: true));
|
||||
// });
|
||||
|
||||
// deviceStatus = FourSceneModelState.fromJson(statusList);
|
||||
// // if (!isClosed) {
|
||||
// // add(
|
||||
// // DoorSensorSwitch(switchD: deviceStatus.doorContactState),
|
||||
// // );
|
||||
// // }
|
||||
// });
|
||||
// } catch (_) {}
|
||||
// }
|
||||
}
|
||||
|
@ -42,7 +42,8 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
|
||||
on<ChangeFirstWizardSwitchStatusEvent>(_changeFirstWizardSwitch);
|
||||
on<ToggleAlarmEvent>(_toggleAlarmEvent);
|
||||
on<DeleteScheduleEvent>(deleteSchedule);
|
||||
//_toggleAlarmEvent
|
||||
|
||||
on<UpdateStateEvent>(_updateState);
|
||||
}
|
||||
void _onClose(OnClose event, Emitter<GarageDoorSensorState> emit) {
|
||||
_timer?.cancel();
|
||||
@ -81,7 +82,7 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
|
||||
toggleDoor = deviceStatus.switch1;
|
||||
emit(UpdateState(garageSensor: deviceStatus));
|
||||
Future.delayed(const Duration(milliseconds: 500));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
} catch (e) {
|
||||
emit(GarageDoorFailedState(errorMessage: e.toString()));
|
||||
return;
|
||||
@ -180,33 +181,59 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
// Real-time db
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$GDId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) async {
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) async {
|
||||
if (_timer != null) {
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
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: true));
|
||||
});
|
||||
|
||||
deviceStatus = GarageDoorModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
// add(
|
||||
// DoorSensorSwitch(switchD: deviceStatus.doorContactState),
|
||||
// );
|
||||
if (event.snapshot.value != null) {
|
||||
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']));
|
||||
});
|
||||
deviceStatus.tr_timecon = statusList
|
||||
.firstWhere((status) => status.code == "tr_timecon",
|
||||
orElse: () => StatusModel(code: "tr_timecon", value: 0))
|
||||
.value as int;
|
||||
|
||||
deviceStatus.switch1 = statusList
|
||||
.firstWhere((status) => status.code == "switch_1",
|
||||
orElse: () => StatusModel(code: "switch_1", value: false))
|
||||
.value as bool;
|
||||
secondSelected = deviceStatus.tr_timecon;
|
||||
toggleDoor = deviceStatus.switch1;
|
||||
add(UpdateStateEvent());
|
||||
}
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
Future<void> _updateState(
|
||||
UpdateStateEvent event, Emitter<GarageDoorSensorState> emit) async {
|
||||
emit(GarageDoorLoadingState());
|
||||
emit(UpdateState(garageSensor: deviceStatus));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
List<Map<String, String>> days = [
|
||||
{"day": "Sun", "key": "Sun"},
|
||||
{"day": "Mon", "key": "Mon"},
|
||||
|
@ -109,6 +109,8 @@ class GetScheduleEvent extends GarageDoorEvent {}
|
||||
|
||||
class ScheduleSaveapp extends GarageDoorEvent {}
|
||||
|
||||
class UpdateStateEvent extends GarageDoorEvent {}
|
||||
|
||||
class ToggleScheduleEvent extends GarageDoorEvent {
|
||||
final String id;
|
||||
final bool toggle;
|
||||
|
@ -26,7 +26,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
bool oneGangGroup = false;
|
||||
List<DeviceModel> devicesList = [];
|
||||
|
||||
OneGangBloc({required this.oneGangId, required this.switchCode}) : super(InitialState()) {
|
||||
OneGangBloc({required this.oneGangId, required this.switchCode})
|
||||
: super(InitialState()) {
|
||||
on<InitialEvent>(_fetchOneGangStatus);
|
||||
on<OneGangUpdated>(_oneGangUpdated);
|
||||
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
|
||||
@ -49,7 +50,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
on<GroupAllOffEvent>(_groupAllOff);
|
||||
}
|
||||
|
||||
void _fetchOneGangStatus(InitialEvent event, Emitter<OneGangState> emit) async {
|
||||
void _fetchOneGangStatus(
|
||||
InitialEvent event, Emitter<OneGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(oneGangId);
|
||||
@ -59,42 +61,51 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
}
|
||||
deviceStatus = OneGangModel.fromJson(statusModelList);
|
||||
emit(UpdateState(oneGangModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges(oneGangId);
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
// Real-time db
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
void _listenToChanges(String id) {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$oneGangId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$id');
|
||||
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>;
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) {
|
||||
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 = OneGangModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
add(OneGangUpdated());
|
||||
}
|
||||
add(OneGangUpdated());
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_oneGangUpdated(OneGangUpdated event, Emitter<OneGangState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
emit(UpdateState(oneGangModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<OneGangState> emit) async {
|
||||
void _changeFirstSwitch(
|
||||
ChangeFirstSwitchStatusEvent event, Emitter<OneGangState> emit) async {
|
||||
emit(LoadingNewSate(oneGangModel: deviceStatus));
|
||||
try {
|
||||
deviceStatus.firstSwitch = !event.value;
|
||||
@ -119,17 +130,20 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _changeSliding(ChangeSlidingSegment event, Emitter<OneGangState> emit) async {
|
||||
void _changeSliding(
|
||||
ChangeSlidingSegment event, Emitter<OneGangState> emit) async {
|
||||
emit(ChangeSlidingSegmentState(value: event.value));
|
||||
}
|
||||
|
||||
void _setCounterValue(SetCounterValue event, Emitter<OneGangState> emit) async {
|
||||
void _setCounterValue(
|
||||
SetCounterValue event, Emitter<OneGangState> emit) async {
|
||||
emit(LoadingNewSate(oneGangModel: deviceStatus));
|
||||
int seconds = 0;
|
||||
try {
|
||||
seconds = event.duration.inSeconds;
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: oneGangId, code: event.deviceCode, value: seconds),
|
||||
DeviceControlModel(
|
||||
deviceId: oneGangId, code: event.deviceCode, value: seconds),
|
||||
oneGangId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
@ -152,7 +166,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _getCounterValue(GetCounterEvent event, Emitter<OneGangState> emit) async {
|
||||
void _getCounterValue(
|
||||
GetCounterEvent event, Emitter<OneGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(oneGangId);
|
||||
@ -241,7 +256,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
deviceId: oneGangId,
|
||||
);
|
||||
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;
|
||||
@ -252,12 +268,13 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
|
||||
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<OneGangState> emit) async {
|
||||
Future toggleChange(
|
||||
ToggleScheduleEvent event, Emitter<OneGangState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.changeSchedule(
|
||||
@ -276,7 +293,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future deleteSchedule(DeleteScheduleEvent event, Emitter<OneGangState> emit) async {
|
||||
Future deleteSchedule(
|
||||
DeleteScheduleEvent event, Emitter<OneGangState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.deleteSchedule(
|
||||
@ -296,7 +314,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<OneGangState> emit) {
|
||||
void toggleCreateSchedule(
|
||||
ToggleCreateScheduleEvent event, Emitter<OneGangState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
createSchedule = !createSchedule;
|
||||
selectedDays.clear();
|
||||
@ -325,7 +344,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
|
||||
int selectedTabIndex = 0;
|
||||
|
||||
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<OneGangState> emit) {
|
||||
void toggleSelectedIndex(
|
||||
ToggleSelectedEvent event, Emitter<OneGangState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
selectedTabIndex = event.index;
|
||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||
@ -334,7 +354,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
List<GroupOneGangModel> groupOneGangList = [];
|
||||
bool allSwitchesOn = true;
|
||||
|
||||
void _fetchOneGangWizardStatus(InitialWizardEvent event, Emitter<OneGangState> emit) async {
|
||||
void _fetchOneGangWizardStatus(
|
||||
InitialWizardEvent event, Emitter<OneGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
devicesList = [];
|
||||
@ -344,7 +365,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
HomeCubit.getInstance().selectedSpace?.id ?? '', '1G');
|
||||
|
||||
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));
|
||||
@ -365,15 +387,16 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
emit(UpdateGroupState(oneGangList: groupOneGangList, allSwitches: allSwitchesOn));
|
||||
emit(UpdateGroupState(
|
||||
oneGangList: groupOneGangList, allSwitches: allSwitchesOn));
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _changeFirstWizardSwitch(
|
||||
ChangeFirstWizardSwitchStatusEvent event, Emitter<OneGangState> emit) async {
|
||||
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
|
||||
Emitter<OneGangState> emit) async {
|
||||
emit(LoadingNewSate(oneGangModel: deviceStatus));
|
||||
try {
|
||||
bool allSwitchesValue = true;
|
||||
@ -386,7 +409,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
}
|
||||
});
|
||||
|
||||
emit(UpdateGroupState(oneGangList: groupOneGangList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
oneGangList: groupOneGangList, allSwitches: allSwitchesValue));
|
||||
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
@ -414,7 +438,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
emit(UpdateGroupState(oneGangList: groupOneGangList, allSwitches: true));
|
||||
|
||||
// Get a list of all device IDs
|
||||
List<String> allDeviceIds = groupOneGangList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupOneGangList.map((device) => device.deviceId).toList();
|
||||
|
||||
// First call for switch_1
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
@ -446,7 +471,8 @@ class OneGangBloc extends Bloc<OneGangEvent, OneGangState> {
|
||||
emit(UpdateGroupState(oneGangList: groupOneGangList, allSwitches: false));
|
||||
|
||||
// Get a list of all device IDs
|
||||
List<String> allDeviceIds = groupOneGangList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupOneGangList.map((device) => device.deviceId).toList();
|
||||
|
||||
// First call for switch_1
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
|
@ -30,7 +30,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
bool oneTouchGroup = false;
|
||||
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<OneTouchUpdated>(_oneTouchUpdated);
|
||||
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
|
||||
@ -53,7 +54,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
on<ChangeStatusEvent>(_changeStatus);
|
||||
}
|
||||
|
||||
void _fetchOneTouchStatus(InitialEvent event, Emitter<OneTouchState> emit) async {
|
||||
void _fetchOneTouchStatus(
|
||||
InitialEvent event, Emitter<OneTouchState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(oneTouchId);
|
||||
@ -62,43 +64,59 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
}
|
||||
deviceStatus = OneTouchModel.fromJson(statusModelList);
|
||||
_listenToChanges(oneTouchId);
|
||||
|
||||
emit(UpdateState(oneTouchModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
void _listenToChanges(String id) {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$oneTouchId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$id');
|
||||
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>;
|
||||
List<StatusModel> statusList = [];
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) {
|
||||
if (event.snapshot.value != null) {
|
||||
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']));
|
||||
});
|
||||
usersMap['status'].forEach((element) {
|
||||
statusList.add(
|
||||
StatusModel(code: element['code'], value: element['value']));
|
||||
});
|
||||
var switchStatus = statusList.firstWhere(
|
||||
(status) => status.code == "switch_1",
|
||||
orElse: () => StatusModel(code: "switch_1", value: false));
|
||||
|
||||
deviceStatus = OneTouchModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
deviceStatus.firstSwitch = switchStatus.value as bool;
|
||||
add(OneTouchUpdated());
|
||||
}
|
||||
});
|
||||
} catch (_) {}
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_oneTouchUpdated(OneTouchUpdated event, Emitter<OneTouchState> emit) {
|
||||
emit(LoadingNewSate(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));
|
||||
try {
|
||||
deviceStatus.firstSwitch = !event.value;
|
||||
@ -123,17 +141,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));
|
||||
}
|
||||
|
||||
void _setCounterValue(SetCounterValue event, Emitter<OneTouchState> emit) async {
|
||||
void _setCounterValue(
|
||||
SetCounterValue event, Emitter<OneTouchState> 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) {
|
||||
@ -156,7 +177,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());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(oneTouchId);
|
||||
@ -245,7 +267,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
deviceId: oneTouchId,
|
||||
);
|
||||
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;
|
||||
@ -256,12 +279,13 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
|
||||
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<OneTouchState> emit) async {
|
||||
Future toggleChange(
|
||||
ToggleScheduleEvent event, Emitter<OneTouchState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.changeSchedule(
|
||||
@ -280,7 +304,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future deleteSchedule(DeleteScheduleEvent event, Emitter<OneTouchState> emit) async {
|
||||
Future deleteSchedule(
|
||||
DeleteScheduleEvent event, Emitter<OneTouchState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.deleteSchedule(
|
||||
@ -300,7 +325,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
}
|
||||
}
|
||||
|
||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<OneTouchState> emit) {
|
||||
void toggleCreateSchedule(
|
||||
ToggleCreateScheduleEvent event, Emitter<OneTouchState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
createSchedule = !createSchedule;
|
||||
selectedDays.clear();
|
||||
@ -329,7 +355,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
|
||||
int selectedTabIndex = 0;
|
||||
|
||||
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<OneTouchState> emit) {
|
||||
void toggleSelectedIndex(
|
||||
ToggleSelectedEvent event, Emitter<OneTouchState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
selectedTabIndex = event.index;
|
||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||
@ -338,7 +365,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
List<GroupOneTouchModel> groupOneTouchList = [];
|
||||
bool allSwitchesOn = true;
|
||||
|
||||
void _fetchOneTouchWizardStatus(InitialWizardEvent event, Emitter<OneTouchState> emit) async {
|
||||
void _fetchOneTouchWizardStatus(
|
||||
InitialWizardEvent event, Emitter<OneTouchState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
devicesList = [];
|
||||
@ -348,7 +376,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
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<StatusModel> statusModelList = [];
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
@ -369,15 +398,16 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
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<OneTouchState> emit) async {
|
||||
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
|
||||
Emitter<OneTouchState> emit) async {
|
||||
emit(LoadingNewSate(oneTouchModel: deviceStatus));
|
||||
try {
|
||||
bool allSwitchesValue = true;
|
||||
@ -395,7 +425,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
value: !event.value,
|
||||
);
|
||||
|
||||
emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
oneTouchList: groupOneTouchList, allSwitches: allSwitchesValue));
|
||||
if (response['success']) {
|
||||
add(InitialEvent(groupScreen: oneTouchGroup));
|
||||
}
|
||||
@ -413,10 +444,12 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
}
|
||||
|
||||
// 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<String> allDeviceIds = groupOneTouchList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupOneTouchList.map((device) => device.deviceId).toList();
|
||||
|
||||
// First call for switch_1
|
||||
final response1 = await DevicesAPI.deviceBatchController(
|
||||
@ -445,10 +478,12 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
}
|
||||
|
||||
// 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<String> allDeviceIds = groupOneTouchList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupOneTouchList.map((device) => device.deviceId).toList();
|
||||
|
||||
// First call for switch_1
|
||||
final response1 = await DevicesAPI.deviceBatchController(
|
||||
@ -472,7 +507,8 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
String statusSelected = '';
|
||||
String optionSelected = '';
|
||||
|
||||
Future<void> _changeStatus(ChangeStatusEvent event, Emitter<OneTouchState> emit) async {
|
||||
Future<void> _changeStatus(
|
||||
ChangeStatusEvent event, Emitter<OneTouchState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
|
||||
@ -497,7 +533,10 @@ class OneTouchBloc extends Bloc<OneTouchEvent, OneTouchState> {
|
||||
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 {
|
||||
|
@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@ -37,7 +38,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
on<SelectTimeEvent>(selectTimeOfLinePassword);
|
||||
on<SelectTimeOnlinePasswordEvent>(selectTimeOnlinePassword);
|
||||
on<DeletePasswordEvent>(deletePassword);
|
||||
on<GenerateAndSavePasswordTimeLimitEvent>(generateAndSavePasswordTimeLimited);
|
||||
on<GenerateAndSavePasswordTimeLimitEvent>(
|
||||
generateAndSavePasswordTimeLimited);
|
||||
on<GenerateAndSavePasswordOneTimeEvent>(generateAndSavePasswordOneTime);
|
||||
on<ToggleDaySelectionEvent>(toggleDaySelection);
|
||||
on<RenamePasswordEvent>(_renamePassword);
|
||||
@ -59,7 +61,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
List<OfflinePasswordModel>? oneTimePasswords = [];
|
||||
List<OfflinePasswordModel>? timeLimitPasswords = [];
|
||||
|
||||
Future generate7DigitNumber(GeneratePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
Future generate7DigitNumber(
|
||||
GeneratePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
passwordController.clear();
|
||||
Random random = Random();
|
||||
@ -71,7 +74,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
|
||||
Future generateAndSavePasswordOneTime(
|
||||
GenerateAndSavePasswordOneTimeEvent event, Emitter<SmartDoorState> emit) async {
|
||||
GenerateAndSavePasswordOneTimeEvent event,
|
||||
Emitter<SmartDoorState> emit) async {
|
||||
try {
|
||||
if (isSavingPassword) return;
|
||||
isSavingPassword = true;
|
||||
@ -92,7 +96,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _fetchSmartDoorStatus(InitialEvent event, Emitter<SmartDoorState> emit) async {
|
||||
void _fetchSmartDoorStatus(
|
||||
InitialEvent event, Emitter<SmartDoorState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
var response = await DevicesAPI.getDeviceStatus(deviceId);
|
||||
@ -102,42 +107,63 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
deviceStatus = SmartDoorModel.fromJson(statusModelList);
|
||||
emit(UpdateState(smartDoorModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
} catch (e) {
|
||||
emit(FailedState(errorMessage: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
Timer? _timer;
|
||||
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) {
|
||||
Map<dynamic, dynamic> usersMap = event.snapshot.value as Map<dynamic, dynamic>;
|
||||
_streamSubscription = 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>;
|
||||
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 = SmartDoorModel.fromJson(statusList);
|
||||
add(DoorLockUpdated());
|
||||
if (!isClosed) {
|
||||
add(DoorLockUpdated());
|
||||
}
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
|
||||
_doorLockUpdated(DoorLockUpdated event, Emitter<SmartDoorState> emit) {
|
||||
unlockRequest = deviceStatus.unlockRequest;
|
||||
emit(UpdateState(smartDoorModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _renamePassword(RenamePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
void _renamePassword(
|
||||
RenamePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
await DevicesAPI.renamePass(
|
||||
name: passwordNameController.text, doorLockUuid: deviceId, passwordId: passwordId);
|
||||
name: passwordNameController.text,
|
||||
doorLockUuid: deviceId,
|
||||
passwordId: passwordId);
|
||||
add(InitialOneTimePassword());
|
||||
add(InitialTimeLimitPassword());
|
||||
emit(UpdateState(smartDoorModel: deviceStatus));
|
||||
@ -147,46 +173,58 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
}
|
||||
|
||||
void getTemporaryPasswords(InitialPasswordsPage event, Emitter<SmartDoorState> emit) async {
|
||||
void getTemporaryPasswords(
|
||||
InitialPasswordsPage event, Emitter<SmartDoorState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
var response = await DevicesAPI.getTemporaryPasswords(
|
||||
deviceId,
|
||||
);
|
||||
if (response is List) {
|
||||
temporaryPasswords = response.map((item) => TemporaryPassword.fromJson(item)).toList();
|
||||
} else if (response is Map && response.containsKey('data')) {
|
||||
temporaryPasswords =
|
||||
(response['data'] as List).map((item) => TemporaryPassword.fromJson(item)).toList();
|
||||
response.map((item) => TemporaryPassword.fromJson(item)).toList();
|
||||
} else if (response is Map && response.containsKey('data')) {
|
||||
temporaryPasswords = (response['data'] as List)
|
||||
.map((item) => TemporaryPassword.fromJson(item))
|
||||
.toList();
|
||||
}
|
||||
emit(TemporaryPasswordsLoadedState(temporaryPassword: temporaryPasswords!));
|
||||
emit(TemporaryPasswordsLoadedState(
|
||||
temporaryPassword: temporaryPasswords!));
|
||||
} catch (e) {
|
||||
emit(FailedState(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
void getOneTimePasswords(InitialOneTimePassword event, Emitter<SmartDoorState> emit) async {
|
||||
void getOneTimePasswords(
|
||||
InitialOneTimePassword event, Emitter<SmartDoorState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
var response = await DevicesAPI.getOneTimePasswords(deviceId);
|
||||
if (response is List) {
|
||||
oneTimePasswords = response.map((item) => OfflinePasswordModel.fromJson(item)).toList();
|
||||
oneTimePasswords = response
|
||||
.map((item) => OfflinePasswordModel.fromJson(item))
|
||||
.toList();
|
||||
}
|
||||
emit(TemporaryPasswordsLoadedState(temporaryPassword: temporaryPasswords!));
|
||||
emit(TemporaryPasswordsLoadedState(
|
||||
temporaryPassword: temporaryPasswords!));
|
||||
} catch (e) {
|
||||
emit(FailedState(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
void getTimeLimitPasswords(InitialTimeLimitPassword event, Emitter<SmartDoorState> emit) async {
|
||||
void getTimeLimitPasswords(
|
||||
InitialTimeLimitPassword event, Emitter<SmartDoorState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
var response = await DevicesAPI.getTimeLimitPasswords(deviceId);
|
||||
if (response is List) {
|
||||
timeLimitPasswords = response.map((item) => OfflinePasswordModel.fromJson(item)).toList();
|
||||
timeLimitPasswords = response
|
||||
.map((item) => OfflinePasswordModel.fromJson(item))
|
||||
.toList();
|
||||
}
|
||||
|
||||
emit(TemporaryPasswordsLoadedState(temporaryPassword: temporaryPasswords!));
|
||||
emit(TemporaryPasswordsLoadedState(
|
||||
temporaryPassword: temporaryPasswords!));
|
||||
} catch (e) {
|
||||
emit(FailedState(errorMessage: e.toString()));
|
||||
}
|
||||
@ -207,7 +245,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
return repeat;
|
||||
}
|
||||
|
||||
bool setStartEndTime(SetStartEndTimeEvent event, Emitter<SmartDoorState> emit) {
|
||||
bool setStartEndTime(
|
||||
SetStartEndTimeEvent event, Emitter<SmartDoorState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
isStartEndTime = event.val;
|
||||
emit(IsStartEndState(isStartEndTime: isStartEndTime));
|
||||
@ -230,7 +269,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
emit(UpdateState(smartDoorModel: deviceStatus));
|
||||
}
|
||||
|
||||
Future<void> selectTimeOfLinePassword(SelectTimeEvent event, Emitter<SmartDoorState> emit) async {
|
||||
Future<void> selectTimeOfLinePassword(
|
||||
SelectTimeEvent event, Emitter<SmartDoorState> emit) async {
|
||||
emit(ChangeTimeState());
|
||||
final DateTime? picked = await showDatePicker(
|
||||
context: event.context,
|
||||
@ -260,20 +300,27 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
).millisecondsSinceEpoch ~/
|
||||
1000; // Divide by 1000 to remove milliseconds
|
||||
if (event.isEffective) {
|
||||
if (expirationTimeTimeStamp != null && selectedTimestamp > expirationTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar('Effective Time cannot be later than Expiration Time.');
|
||||
if (expirationTimeTimeStamp != null &&
|
||||
selectedTimestamp > expirationTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'Effective Time cannot be later than Expiration Time.');
|
||||
} else {
|
||||
effectiveTime =
|
||||
selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds
|
||||
effectiveTime = selectedDateTime
|
||||
.toString()
|
||||
.split('.')
|
||||
.first; // Remove seconds and milliseconds
|
||||
effectiveTimeTimeStamp = selectedTimestamp;
|
||||
}
|
||||
} else {
|
||||
if (effectiveTimeTimeStamp != null && selectedTimestamp < effectiveTimeTimeStamp!) {
|
||||
if (effectiveTimeTimeStamp != null &&
|
||||
selectedTimestamp < effectiveTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'Expiration Time cannot be earlier than Effective Time.');
|
||||
} else {
|
||||
expirationTime =
|
||||
selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds
|
||||
expirationTime = selectedDateTime
|
||||
.toString()
|
||||
.split('.')
|
||||
.first; // Remove seconds and milliseconds
|
||||
expirationTimeTimeStamp = selectedTimestamp;
|
||||
}
|
||||
}
|
||||
@ -329,20 +376,27 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
).millisecondsSinceEpoch ~/
|
||||
1000; // Divide by 1000 to remove milliseconds
|
||||
if (event.isEffective) {
|
||||
if (expirationTimeTimeStamp != null && selectedTimestamp > expirationTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar('Effective Time cannot be later than Expiration Time.');
|
||||
if (expirationTimeTimeStamp != null &&
|
||||
selectedTimestamp > expirationTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'Effective Time cannot be later than Expiration Time.');
|
||||
} else {
|
||||
effectiveTime =
|
||||
selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds
|
||||
effectiveTime = selectedDateTime
|
||||
.toString()
|
||||
.split('.')
|
||||
.first; // Remove seconds and milliseconds
|
||||
effectiveTimeTimeStamp = selectedTimestamp;
|
||||
}
|
||||
} else {
|
||||
if (effectiveTimeTimeStamp != null && selectedTimestamp < effectiveTimeTimeStamp!) {
|
||||
if (effectiveTimeTimeStamp != null &&
|
||||
selectedTimestamp < effectiveTimeTimeStamp!) {
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'Expiration Time cannot be earlier than Effective Time.');
|
||||
} else {
|
||||
expirationTime =
|
||||
selectedDateTime.toString().split('.').first; // Remove seconds and milliseconds
|
||||
expirationTime = selectedDateTime
|
||||
.toString()
|
||||
.split('.')
|
||||
.first; // Remove seconds and milliseconds
|
||||
expirationTimeTimeStamp = selectedTimestamp;
|
||||
}
|
||||
}
|
||||
@ -351,7 +405,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> savePassword(SavePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
Future<void> savePassword(
|
||||
SavePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
if (_validateInputs() || isSavingPassword) return;
|
||||
try {
|
||||
isSavingPassword = true;
|
||||
@ -381,7 +436,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
|
||||
Future<void> generateAndSavePasswordTimeLimited(
|
||||
GenerateAndSavePasswordTimeLimitEvent event, Emitter<SmartDoorState> emit) async {
|
||||
GenerateAndSavePasswordTimeLimitEvent event,
|
||||
Emitter<SmartDoorState> emit) async {
|
||||
if (timeLimitValidate() || isSavingPassword) return;
|
||||
try {
|
||||
isSavingPassword = true;
|
||||
@ -407,10 +463,12 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deletePassword(DeletePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
Future<void> deletePassword(
|
||||
DeletePasswordEvent event, Emitter<SmartDoorState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
await DevicesAPI.deletePassword(deviceId: deviceId, passwordId: event.passwordId)
|
||||
await DevicesAPI.deletePassword(
|
||||
deviceId: deviceId, passwordId: event.passwordId)
|
||||
.then((value) async {
|
||||
add(InitialPasswordsPage());
|
||||
});
|
||||
@ -445,7 +503,8 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
}
|
||||
|
||||
if (repeat == true && (endTime == null || startTime == null)) {
|
||||
CustomSnackBar.displaySnackBar('Start Time and End time and the days required ');
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'Start Time and End time and the days required ');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@ -1,10 +1,12 @@
|
||||
import 'dart:async';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
import 'package:flutter/material.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/model/community_model.dart';
|
||||
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/sos_bloc/sos_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/sos_bloc/sos_state.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||
@ -19,6 +21,7 @@ import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||
import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
|
||||
class SosBloc extends Bloc<SosEvent, SosState> {
|
||||
@ -171,6 +174,7 @@ class SosBloc extends Bloc<SosEvent, SosState> {
|
||||
);
|
||||
emit(UpdateState(sensor: deviceStatus));
|
||||
Future.delayed(const Duration(milliseconds: 500));
|
||||
// _listenToChanges();
|
||||
} catch (e) {
|
||||
emit(SosFailedState(errorMessage: e.toString()));
|
||||
return;
|
||||
@ -184,8 +188,12 @@ class SosBloc extends Bloc<SosEvent, SosState> {
|
||||
FetchRoomsEvent event, Emitter<SosState> emit) async {
|
||||
try {
|
||||
emit(SosLoadingState());
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
roomsList = await SpacesAPI.getSubSpaceBySpaceId(
|
||||
event.unit.community.uuid, event.unit.id);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
} catch (e) {
|
||||
emit(const SosFailedState(errorMessage: 'Something went wrong'));
|
||||
@ -223,13 +231,19 @@ class SosBloc extends Bloc<SosEvent, SosState> {
|
||||
void _assignDevice(AssignRoomEvent event, Emitter<SosState> emit) async {
|
||||
try {
|
||||
emit(SosLoadingState());
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
await HomeManagementAPI.assignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, sosId);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
event.roomId,
|
||||
sosId,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
|
||||
List<String> allDevicesIds = [];
|
||||
|
||||
@ -249,13 +263,16 @@ class SosBloc extends Bloc<SosEvent, SosState> {
|
||||
void _unassignDevice(UnassignRoomEvent event, Emitter<SosState> emit) async {
|
||||
try {
|
||||
Map<String, bool> roomDevicesId = {};
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
emit(SosLoadingState());
|
||||
await HomeManagementAPI.unAssignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, sosId);
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, sosId, project?.uuid ?? TempConst.projectIdDev);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
|
||||
List<String> allDevicesIds = [];
|
||||
|
||||
@ -419,4 +436,39 @@ class SosBloc extends Bloc<SosEvent, SosState> {
|
||||
emit(SosFailedState(errorMessage: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//real-time db
|
||||
// StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
// Timer? _timer;
|
||||
|
||||
// void _listenToChanges() {
|
||||
// try {
|
||||
// _streamSubscription?.cancel();
|
||||
// DatabaseReference ref =
|
||||
// FirebaseDatabase.instance.ref('device-status/$sosId');
|
||||
// Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
// _streamSubscription = 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>;
|
||||
// List<StatusModel> statusList = [];
|
||||
// usersMap['status'].forEach((element) {
|
||||
// statusList
|
||||
// .add(StatusModel(code: element['code'], value: element['value']));
|
||||
// });
|
||||
// deviceStatus = SosModel.fromJson(statusList);
|
||||
// });
|
||||
// } catch (_) {}
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Future<void> close() async {
|
||||
// _streamSubscription?.cancel();
|
||||
// _streamSubscription = null;
|
||||
// return super.close();
|
||||
// }
|
||||
}
|
||||
|
@ -31,7 +31,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
List<GroupThreeGangModel> groupThreeGangList = [];
|
||||
bool allSwitchesOn = true;
|
||||
|
||||
ThreeGangBloc({required this.threeGangId, required this.switchCode}) : super(InitialState()) {
|
||||
ThreeGangBloc({required this.threeGangId, required this.switchCode})
|
||||
: super(InitialState()) {
|
||||
on<InitialEvent>(_fetchThreeGangStatus);
|
||||
on<ThreeGangUpdated>(_threeGangUpdated);
|
||||
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
|
||||
@ -57,7 +58,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
on<ToggleCreateScheduleEvent>(toggleCreateSchedule);
|
||||
}
|
||||
|
||||
void _fetchThreeGangStatus(InitialEvent event, Emitter<ThreeGangState> emit) async {
|
||||
void _fetchThreeGangStatus(
|
||||
InitialEvent event, Emitter<ThreeGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
threeGangGroup = event.groupScreen;
|
||||
@ -69,7 +71,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
HomeCubit.getInstance().selectedSpace?.id ?? '', '3G');
|
||||
|
||||
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));
|
||||
@ -86,13 +89,16 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
|
||||
if (groupThreeGangList.isNotEmpty) {
|
||||
groupThreeGangList.firstWhere((element) {
|
||||
if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) {
|
||||
if (!element.firstSwitch ||
|
||||
!element.secondSwitch ||
|
||||
!element.thirdSwitch) {
|
||||
allSwitchesOn = false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesOn));
|
||||
emit(UpdateGroupState(
|
||||
threeGangList: groupThreeGangList, allSwitches: allSwitchesOn));
|
||||
} else {
|
||||
var response = await DevicesAPI.getDeviceStatus(threeGangId);
|
||||
List<StatusModel> statusModelList = [];
|
||||
@ -101,7 +107,7 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
deviceStatus = ThreeGangModel.fromJson(statusModelList);
|
||||
emit(UpdateState(threeGangModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
}
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
@ -109,22 +115,26 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeGangId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$threeGangId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) async {
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) async {
|
||||
if (_timer != null) {
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
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 = ThreeGangModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
add(ThreeGangUpdated());
|
||||
@ -133,11 +143,19 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_threeGangUpdated(ThreeGangUpdated event, Emitter<ThreeGangState> emit) {
|
||||
emit(UpdateState(threeGangModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
|
||||
void _changeFirstSwitch(
|
||||
ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
|
||||
emit(LoadingNewSate(threeGangModel: deviceStatus));
|
||||
try {
|
||||
if (threeGangGroup) {
|
||||
@ -146,11 +164,14 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
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(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
|
||||
} else {
|
||||
deviceStatus.firstSwitch = !event.value;
|
||||
emit(UpdateState(threeGangModel: deviceStatus));
|
||||
@ -187,11 +208,14 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
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(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
|
||||
} else {
|
||||
deviceStatus.secondSwitch = !event.value;
|
||||
emit(UpdateState(threeGangModel: deviceStatus));
|
||||
@ -217,7 +241,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
|
||||
void _changeThirdSwitch(
|
||||
ChangeThirdSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
|
||||
emit(LoadingNewSate(threeGangModel: deviceStatus));
|
||||
try {
|
||||
if (threeGangGroup) {
|
||||
@ -226,11 +251,14 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
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(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
|
||||
} else {
|
||||
deviceStatus.thirdSwitch = !event.value;
|
||||
emit(UpdateState(threeGangModel: deviceStatus));
|
||||
@ -269,15 +297,21 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
final response = await Future.wait([
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
||||
deviceId: threeGangId,
|
||||
code: 'switch_1',
|
||||
value: deviceStatus.firstSwitch),
|
||||
threeGangId),
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
|
||||
deviceId: threeGangId,
|
||||
code: 'switch_2',
|
||||
value: deviceStatus.secondSwitch),
|
||||
threeGangId),
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch),
|
||||
deviceId: threeGangId,
|
||||
code: 'switch_3',
|
||||
value: deviceStatus.thirdSwitch),
|
||||
threeGangId),
|
||||
]);
|
||||
|
||||
@ -303,15 +337,21 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
final response = await Future.wait([
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
||||
deviceId: threeGangId,
|
||||
code: 'switch_1',
|
||||
value: deviceStatus.firstSwitch),
|
||||
threeGangId),
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
|
||||
deviceId: threeGangId,
|
||||
code: 'switch_2',
|
||||
value: deviceStatus.secondSwitch),
|
||||
threeGangId),
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch),
|
||||
deviceId: threeGangId,
|
||||
code: 'switch_3',
|
||||
value: deviceStatus.thirdSwitch),
|
||||
threeGangId),
|
||||
]);
|
||||
|
||||
@ -333,9 +373,11 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
groupThreeGangList[i].secondSwitch = true;
|
||||
groupThreeGangList[i].thirdSwitch = true;
|
||||
}
|
||||
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: true));
|
||||
emit(UpdateGroupState(
|
||||
threeGangList: groupThreeGangList, allSwitches: true));
|
||||
|
||||
List<String> allDeviceIds = groupThreeGangList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupThreeGangList.map((device) => device.deviceId).toList();
|
||||
|
||||
final response1 = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
@ -365,7 +407,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _groupAllOff(GroupAllOffEvent event, Emitter<ThreeGangState> emit) async {
|
||||
void _groupAllOff(
|
||||
GroupAllOffEvent event, Emitter<ThreeGangState> emit) async {
|
||||
emit(LoadingNewSate(threeGangModel: deviceStatus));
|
||||
try {
|
||||
for (int i = 0; i < groupThreeGangList.length; i++) {
|
||||
@ -373,8 +416,10 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
groupThreeGangList[i].secondSwitch = false;
|
||||
groupThreeGangList[i].thirdSwitch = false;
|
||||
}
|
||||
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: false));
|
||||
List<String> allDeviceIds = groupThreeGangList.map((device) => device.deviceId).toList();
|
||||
emit(UpdateGroupState(
|
||||
threeGangList: groupThreeGangList, allSwitches: false));
|
||||
List<String> allDeviceIds =
|
||||
groupThreeGangList.map((device) => device.deviceId).toList();
|
||||
|
||||
final response1 = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
@ -404,17 +449,20 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _changeSliding(ChangeSlidingSegment event, Emitter<ThreeGangState> emit) async {
|
||||
void _changeSliding(
|
||||
ChangeSlidingSegment event, Emitter<ThreeGangState> emit) async {
|
||||
emit(ChangeSlidingSegmentState(value: event.value));
|
||||
}
|
||||
|
||||
void _setCounterValue(SetCounterValue event, Emitter<ThreeGangState> emit) async {
|
||||
void _setCounterValue(
|
||||
SetCounterValue event, Emitter<ThreeGangState> emit) async {
|
||||
emit(LoadingNewSate(threeGangModel: deviceStatus));
|
||||
int seconds = 0;
|
||||
try {
|
||||
seconds = event.duration.inSeconds;
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: threeGangId, code: event.deviceCode, value: seconds),
|
||||
DeviceControlModel(
|
||||
deviceId: threeGangId, code: event.deviceCode, value: seconds),
|
||||
threeGangId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
@ -441,7 +489,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _getCounterValue(GetCounterEvent event, Emitter<ThreeGangState> emit) async {
|
||||
void _getCounterValue(
|
||||
GetCounterEvent event, Emitter<ThreeGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(threeGangId);
|
||||
@ -551,7 +600,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
deviceId: threeGangId,
|
||||
);
|
||||
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;
|
||||
@ -562,12 +612,13 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
|
||||
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<ThreeGangState> emit) async {
|
||||
Future toggleChange(
|
||||
ToggleScheduleEvent event, Emitter<ThreeGangState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.changeSchedule(
|
||||
@ -586,7 +637,8 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future deleteSchedule(DeleteScheduleEvent event, Emitter<ThreeGangState> emit) async {
|
||||
Future deleteSchedule(
|
||||
DeleteScheduleEvent event, Emitter<ThreeGangState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.deleteSchedule(
|
||||
@ -606,13 +658,15 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<ThreeGangState> emit) {
|
||||
void toggleSelectedIndex(
|
||||
ToggleSelectedEvent event, Emitter<ThreeGangState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
selectedTabIndex = event.index;
|
||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||
}
|
||||
|
||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<ThreeGangState> emit) {
|
||||
void toggleCreateSchedule(
|
||||
ToggleCreateScheduleEvent event, Emitter<ThreeGangState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
createSchedule = !createSchedule;
|
||||
selectedDays.clear();
|
||||
|
@ -38,7 +38,8 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
List<GroupThreeTouchModel> groupThreeTouchList = [];
|
||||
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<ThreeTouchUpdated>(_threeTouchUpdated);
|
||||
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
|
||||
@ -63,7 +64,8 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
on<ChangeStatusEvent>(_changeStatus);
|
||||
}
|
||||
|
||||
void _fetchThreeTouchStatus(InitialEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
void _fetchThreeTouchStatus(
|
||||
InitialEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
threeTouchGroup = event.groupScreen;
|
||||
@ -75,7 +77,8 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
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<StatusModel> statusModelList = [];
|
||||
for (var status in response['status']) {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
@ -92,13 +95,16 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
|
||||
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<StatusModel> statusModelList = [];
|
||||
@ -107,7 +113,7 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
}
|
||||
deviceStatus = ThreeTouchModel.fromJson(statusModelList);
|
||||
emit(UpdateState(threeTouchModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
}
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
@ -117,18 +123,21 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
|
||||
_listenToChanges() {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeTouchId');
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$threeTouchId');
|
||||
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 = ThreeTouchModel.fromJson(statusList);
|
||||
@ -143,7 +152,8 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
emit(UpdateState(threeTouchModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
void _changeFirstSwitch(
|
||||
ChangeFirstSwitchStatusEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
emit(LoadingNewSate(threeTouchModel: deviceStatus));
|
||||
try {
|
||||
if (threeTouchGroup) {
|
||||
@ -152,11 +162,15 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
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));
|
||||
@ -183,8 +197,8 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _changeSecondSwitch(
|
||||
ChangeSecondSwitchStatusEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
void _changeSecondSwitch(ChangeSecondSwitchStatusEvent event,
|
||||
Emitter<ThreeTouchState> emit) async {
|
||||
emit(LoadingNewSate(threeTouchModel: deviceStatus));
|
||||
try {
|
||||
if (threeTouchGroup) {
|
||||
@ -193,11 +207,15 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
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));
|
||||
@ -223,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));
|
||||
try {
|
||||
if (threeTouchGroup) {
|
||||
@ -232,11 +251,15 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
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));
|
||||
@ -275,15 +298,21 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
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),
|
||||
]);
|
||||
|
||||
@ -309,15 +338,21 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
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),
|
||||
]);
|
||||
|
||||
@ -339,8 +374,10 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
groupThreeTouchList[i].secondSwitch = true;
|
||||
groupThreeTouchList[i].thirdSwitch = true;
|
||||
}
|
||||
emit(UpdateGroupState(threeTouchList: groupThreeTouchList, allSwitches: true));
|
||||
List<String> allDeviceIds = groupThreeTouchList.map((device) => device.deviceId).toList();
|
||||
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,
|
||||
@ -369,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));
|
||||
try {
|
||||
for (int i = 0; i < groupThreeTouchList.length; i++) {
|
||||
@ -377,8 +415,10 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
groupThreeTouchList[i].secondSwitch = false;
|
||||
groupThreeTouchList[i].thirdSwitch = false;
|
||||
}
|
||||
List<String> allDeviceIds = groupThreeTouchList.map((device) => device.deviceId).toList();
|
||||
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,
|
||||
@ -407,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));
|
||||
}
|
||||
|
||||
void _setCounterValue(SetCounterValue event, Emitter<ThreeTouchState> emit) async {
|
||||
void _setCounterValue(
|
||||
SetCounterValue event, Emitter<ThreeTouchState> 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) {
|
||||
@ -444,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());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(threeTouchId);
|
||||
@ -554,7 +598,8 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
deviceId: threeTouchId,
|
||||
);
|
||||
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;
|
||||
@ -565,12 +610,13 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
|
||||
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<ThreeTouchState> emit) async {
|
||||
Future toggleChange(
|
||||
ToggleScheduleEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.changeSchedule(
|
||||
@ -589,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 {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.deleteSchedule(
|
||||
@ -609,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());
|
||||
selectedTabIndex = event.index;
|
||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||
}
|
||||
|
||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<ThreeTouchState> emit) {
|
||||
void toggleCreateSchedule(
|
||||
ToggleCreateScheduleEvent event, Emitter<ThreeTouchState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
createSchedule = !createSchedule;
|
||||
selectedDays.clear();
|
||||
@ -633,7 +682,8 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
String statusSelected = '';
|
||||
String optionSelected = '';
|
||||
|
||||
Future<void> _changeStatus(ChangeStatusEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
Future<void> _changeStatus(
|
||||
ChangeStatusEvent event, Emitter<ThreeTouchState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final Map<String, Map<String, String>> controlMap = {
|
||||
@ -667,11 +717,15 @@ class ThreeTouchBloc extends Bloc<ThreeTouchEvent, ThreeTouchState> {
|
||||
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 {
|
||||
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;
|
||||
|
@ -35,7 +35,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
bool createSchedule = false;
|
||||
List<ScheduleModel> listSchedule = [];
|
||||
|
||||
TwoGangBloc({required this.twoGangId, required this.switchCode}) : super(InitialState()) {
|
||||
TwoGangBloc({required this.twoGangId, required this.switchCode})
|
||||
: super(InitialState()) {
|
||||
on<InitialEvent>(_fetchTwoGangStatus);
|
||||
on<TwoGangUpdated>(_twoGangUpdated);
|
||||
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
|
||||
@ -65,13 +66,15 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
|
||||
int selectedTabIndex = 0;
|
||||
|
||||
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<TwoGangState> emit) {
|
||||
void toggleSelectedIndex(
|
||||
ToggleSelectedEvent event, Emitter<TwoGangState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
selectedTabIndex = event.index;
|
||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||
}
|
||||
|
||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<TwoGangState> emit) {
|
||||
void toggleCreateSchedule(
|
||||
ToggleCreateScheduleEvent event, Emitter<TwoGangState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
createSchedule = !createSchedule;
|
||||
selectedDays.clear();
|
||||
@ -79,7 +82,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
emit(UpdateCreateScheduleState(createSchedule));
|
||||
}
|
||||
|
||||
void _fetchTwoGangStatus(InitialEvent event, Emitter<TwoGangState> emit) async {
|
||||
void _fetchTwoGangStatus(
|
||||
InitialEvent event, Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(twoGangId);
|
||||
@ -89,42 +93,50 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
deviceStatus = TwoGangModel.fromJson(statusModelList);
|
||||
emit(UpdateState(twoGangModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges(twoGangId);
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
void _listenToChanges(String id) {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$twoGangId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$id');
|
||||
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>;
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) {
|
||||
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 = TwoGangModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
add(TwoGangUpdated());
|
||||
}
|
||||
add(TwoGangUpdated());
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_twoGangUpdated(TwoGangUpdated event, Emitter<TwoGangState> emit) {
|
||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||
|
||||
emit(UpdateState(twoGangModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||
void _changeFirstSwitch(
|
||||
ChangeFirstSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||
try {
|
||||
deviceStatus.firstSwitch = !event.value;
|
||||
@ -135,7 +147,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
|
||||
_timer = Timer(const Duration(milliseconds: 100), () async {
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: twoGangId, code: 'switch_1', value: !event.value),
|
||||
DeviceControlModel(
|
||||
deviceId: twoGangId, code: 'switch_1', value: !event.value),
|
||||
twoGangId);
|
||||
|
||||
if (!response['success']) {
|
||||
@ -147,7 +160,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _changeSecondSwitch(ChangeSecondSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||
void _changeSecondSwitch(
|
||||
ChangeSecondSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||
try {
|
||||
deviceStatus.secondSwitch = !event.value;
|
||||
@ -157,7 +171,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
_timer = Timer(const Duration(milliseconds: 100), () async {
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: twoGangId, code: 'switch_2', value: !event.value),
|
||||
DeviceControlModel(
|
||||
deviceId: twoGangId, code: 'switch_2', value: !event.value),
|
||||
twoGangId);
|
||||
|
||||
if (!response['success']) {
|
||||
@ -180,11 +195,15 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
final response = await Future.wait([
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: twoGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
||||
deviceId: twoGangId,
|
||||
code: 'switch_1',
|
||||
value: deviceStatus.firstSwitch),
|
||||
twoGangId),
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: twoGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
|
||||
deviceId: twoGangId,
|
||||
code: 'switch_2',
|
||||
value: deviceStatus.secondSwitch),
|
||||
twoGangId),
|
||||
]);
|
||||
|
||||
@ -207,11 +226,15 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
final response = await Future.wait([
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: twoGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
||||
deviceId: twoGangId,
|
||||
code: 'switch_1',
|
||||
value: deviceStatus.firstSwitch),
|
||||
twoGangId),
|
||||
DevicesAPI.controlDevice(
|
||||
DeviceControlModel(
|
||||
deviceId: twoGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
|
||||
deviceId: twoGangId,
|
||||
code: 'switch_2',
|
||||
value: deviceStatus.secondSwitch),
|
||||
twoGangId),
|
||||
]);
|
||||
if (response.every((element) => !element['success'])) {
|
||||
@ -232,7 +255,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
groupTwoGangList[i].secondSwitch = true;
|
||||
}
|
||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: true));
|
||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
|
||||
final response1 = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
@ -267,7 +291,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
|
||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: false));
|
||||
|
||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
|
||||
final response1 = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
@ -292,17 +317,20 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _changeSliding(ChangeSlidingSegment event, Emitter<TwoGangState> emit) async {
|
||||
void _changeSliding(
|
||||
ChangeSlidingSegment event, Emitter<TwoGangState> emit) async {
|
||||
emit(ChangeSlidingSegmentState(value: event.value));
|
||||
}
|
||||
|
||||
void _setCounterValue(SetCounterValue event, Emitter<TwoGangState> emit) async {
|
||||
void _setCounterValue(
|
||||
SetCounterValue event, Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||
int seconds = 0;
|
||||
try {
|
||||
seconds = event.duration.inSeconds;
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: twoGangId, code: event.deviceCode, value: seconds),
|
||||
DeviceControlModel(
|
||||
deviceId: twoGangId, code: event.deviceCode, value: seconds),
|
||||
twoGangId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
@ -327,7 +355,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _getCounterValue(GetCounterEvent event, Emitter<TwoGangState> emit) async {
|
||||
void _getCounterValue(
|
||||
GetCounterEvent event, Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
add(GetScheduleEvent());
|
||||
@ -435,7 +464,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
deviceId: twoGangId,
|
||||
);
|
||||
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;
|
||||
@ -446,12 +476,13 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
|
||||
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<TwoGangState> emit) async {
|
||||
Future toggleRepeat(
|
||||
ToggleScheduleEvent event, Emitter<TwoGangState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.changeSchedule(
|
||||
@ -470,7 +501,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future deleteSchedule(DeleteScheduleEvent event, Emitter<TwoGangState> emit) async {
|
||||
Future deleteSchedule(
|
||||
DeleteScheduleEvent event, Emitter<TwoGangState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
final response = await DevicesAPI.deleteSchedule(
|
||||
@ -490,7 +522,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _fetchTwoGangWizardStatus(InitialWizardEvent event, Emitter<TwoGangState> emit) async {
|
||||
void _fetchTwoGangWizardStatus(
|
||||
InitialWizardEvent event, Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
devicesList = [];
|
||||
@ -500,7 +533,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
HomeCubit.getInstance().selectedSpace?.id ?? '', '2G');
|
||||
|
||||
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));
|
||||
@ -523,15 +557,16 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: allSwitchesOn));
|
||||
emit(UpdateGroupState(
|
||||
twoGangList: groupTwoGangList, allSwitches: allSwitchesOn));
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _changeFirstWizardSwitch(
|
||||
ChangeFirstWizardSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
|
||||
Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||
try {
|
||||
bool allSwitchesValue = true;
|
||||
@ -544,9 +579,11 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
});
|
||||
|
||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
||||
|
||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
devicesUuid: allDeviceIds,
|
||||
@ -561,8 +598,8 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _changeSecondWizardSwitch(
|
||||
ChangeSecondWizardSwitchStatusEvent event, Emitter<TwoGangState> emit) async {
|
||||
void _changeSecondWizardSwitch(ChangeSecondWizardSwitchStatusEvent event,
|
||||
Emitter<TwoGangState> emit) async {
|
||||
emit(LoadingNewSate(twoGangModel: deviceStatus));
|
||||
try {
|
||||
bool allSwitchesValue = true;
|
||||
@ -574,9 +611,11 @@ class TwoGangBloc extends Bloc<TwoGangEvent, TwoGangState> {
|
||||
allSwitchesValue = false;
|
||||
}
|
||||
});
|
||||
List<String> allDeviceIds = groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupTwoGangList.map((device) => device.deviceId).toList();
|
||||
|
||||
emit(UpdateGroupState(twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
twoGangList: groupTwoGangList, allSwitches: allSwitchesValue));
|
||||
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_2',
|
||||
|
@ -99,41 +99,61 @@ class TwoTouchBloc extends Bloc<TwoTouchEvent, TwoTouchState> {
|
||||
}
|
||||
deviceStatus = TwoTouchModel.fromJson(statusModelList);
|
||||
emit(UpdateState(twoTouchModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$twoTouchId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) async {
|
||||
_streamSubscription = stream.listen((DatabaseEvent event) async {
|
||||
if (_timer != null) {
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
}
|
||||
Map<dynamic, dynamic> usersMap =
|
||||
event.snapshot.value as Map<dynamic, dynamic>;
|
||||
List<StatusModel> statusList = [];
|
||||
if (event.snapshot.value != null) {
|
||||
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']));
|
||||
});
|
||||
usersMap['status'].forEach((element) {
|
||||
statusList.add(
|
||||
StatusModel(code: element['code'], value: element['value']));
|
||||
});
|
||||
var switch1Status = statusList.firstWhere(
|
||||
(status) => status.code == "switch_1",
|
||||
orElse: () => StatusModel(code: "switch_1", value: false));
|
||||
|
||||
var switch2Status = statusList.firstWhere(
|
||||
(status) => status.code == "switch_2",
|
||||
orElse: () => StatusModel(code: "switch_2", value: false));
|
||||
deviceStatus.firstSwitch = switch1Status.value as bool;
|
||||
deviceStatus.secondSwitch = switch2Status.value as bool;
|
||||
|
||||
deviceStatus = TwoTouchModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
add(TwoTouchUpdated());
|
||||
}
|
||||
});
|
||||
} catch (_) {}
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_twoTouchUpdated(TwoTouchUpdated event, Emitter<TwoTouchState> emit) {
|
||||
emit(LoadingInitialState());
|
||||
emit(UpdateState(twoTouchModel: deviceStatus));
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/wall_sensor_bloc/wall_sensor_state.dart';
|
||||
@ -21,7 +23,8 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
|
||||
on<GetDeviceReportsEvent>(_getDeviceReports);
|
||||
}
|
||||
|
||||
void _fetchCeilingSensorStatus(InitialEvent event, Emitter<WallSensorState> emit) async {
|
||||
void _fetchCeilingSensorStatus(
|
||||
InitialEvent event, Emitter<WallSensorState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(deviceId);
|
||||
@ -31,41 +34,62 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
|
||||
}
|
||||
deviceStatus = WallSensorModel.fromJson(statusModelList);
|
||||
emit(UpdateState(wallSensorModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
Timer? _timer;
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) {
|
||||
Map<dynamic, dynamic> usersMap = event.snapshot.value as Map<dynamic, dynamic>;
|
||||
_streamSubscription = 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>;
|
||||
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 = WallSensorModel.fromJson(statusList);
|
||||
add(WallSensorUpdatedEvent());
|
||||
if (!isClosed) {
|
||||
add(WallSensorUpdatedEvent());
|
||||
}
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
_wallSensorUpdated(WallSensorUpdatedEvent event, Emitter<WallSensorState> emit) {
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_wallSensorUpdated(
|
||||
WallSensorUpdatedEvent event, Emitter<WallSensorState> emit) {
|
||||
emit(UpdateState(wallSensorModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _changeIndicator(ChangeIndicatorEvent event, Emitter<WallSensorState> emit) async {
|
||||
void _changeIndicator(
|
||||
ChangeIndicatorEvent event, Emitter<WallSensorState> emit) async {
|
||||
emit(LoadingNewSate(wallSensorModel: deviceStatus));
|
||||
try {
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: deviceId, code: 'indicator', value: !event.value), deviceId);
|
||||
DeviceControlModel(
|
||||
deviceId: deviceId, code: 'indicator', value: !event.value),
|
||||
deviceId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
deviceStatus.indicator = !event.value;
|
||||
@ -74,11 +98,14 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
|
||||
emit(UpdateState(wallSensorModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _changeValue(ChangeValueEvent event, Emitter<WallSensorState> emit) async {
|
||||
void _changeValue(
|
||||
ChangeValueEvent event, Emitter<WallSensorState> emit) async {
|
||||
emit(LoadingNewSate(wallSensorModel: deviceStatus));
|
||||
try {
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: deviceId, code: event.code, value: event.value), deviceId);
|
||||
DeviceControlModel(
|
||||
deviceId: deviceId, code: event.code, value: event.value),
|
||||
deviceId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
if (event.code == 'far_detection') {
|
||||
@ -93,7 +120,8 @@ class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
|
||||
emit(UpdateState(wallSensorModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _getDeviceReports(GetDeviceReportsEvent event, Emitter<WallSensorState> emit) async {
|
||||
void _getDeviceReports(
|
||||
GetDeviceReportsEvent event, Emitter<WallSensorState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
|
||||
try {
|
||||
|
@ -35,7 +35,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
List<ScheduleModel> listSchedule = [];
|
||||
DateTime? selectedTime = DateTime.now();
|
||||
|
||||
WaterHeaterBloc({required this.whId, required this.switchCode}) : super(WHInitialState()) {
|
||||
WaterHeaterBloc({required this.whId, required this.switchCode})
|
||||
: super(WHInitialState()) {
|
||||
on<WaterHeaterInitial>(_fetchWaterHeaterStatus);
|
||||
on<WaterHeaterSwitch>(_changeFirstSwitch);
|
||||
on<SetCounterValue>(_setCounterValue);
|
||||
@ -60,7 +61,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
on<WaterHeaterUpdated>(_waterHeaterUpdated);
|
||||
}
|
||||
|
||||
void _fetchWaterHeaterStatus(WaterHeaterInitial event, Emitter<WaterHeaterState> emit) async {
|
||||
void _fetchWaterHeaterStatus(
|
||||
WaterHeaterInitial event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(WHLoadingState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(whId);
|
||||
@ -72,29 +74,33 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
statusModelList,
|
||||
);
|
||||
emit(UpdateState(whModel: deviceStatus));
|
||||
// _listenToChanges();
|
||||
_listenToChanges();
|
||||
} catch (e) {
|
||||
emit(WHFailedState(errorMessage: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_listenToChanges() {
|
||||
//real-time db
|
||||
StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
void _listenToChanges() {
|
||||
try {
|
||||
DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$whId');
|
||||
_streamSubscription?.cancel();
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$whId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) async {
|
||||
_streamSubscription = 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 = WHModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
add(WaterHeaterUpdated());
|
||||
@ -103,12 +109,21 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
_waterHeaterUpdated(WaterHeaterUpdated event, Emitter<WaterHeaterState> emit) async {
|
||||
@override
|
||||
Future<void> close() async {
|
||||
_streamSubscription?.cancel();
|
||||
_streamSubscription = null;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
_waterHeaterUpdated(
|
||||
WaterHeaterUpdated event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(WHLoadingState());
|
||||
emit(UpdateState(whModel: deviceStatus));
|
||||
}
|
||||
|
||||
void _changeFirstSwitch(WaterHeaterSwitch event, Emitter<WaterHeaterState> emit) async {
|
||||
void _changeFirstSwitch(
|
||||
WaterHeaterSwitch event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(LoadingNewSate(whModel: deviceStatus));
|
||||
try {
|
||||
deviceStatus.firstSwitch = !event.whSwitch;
|
||||
@ -118,7 +133,10 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
}
|
||||
_timer = Timer(const Duration(milliseconds: 500), () async {
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: whId, code: 'switch_1', value: deviceStatus.firstSwitch),
|
||||
DeviceControlModel(
|
||||
deviceId: whId,
|
||||
code: 'switch_1',
|
||||
value: deviceStatus.firstSwitch),
|
||||
whId);
|
||||
|
||||
if (!response['success']) {
|
||||
@ -132,13 +150,16 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
|
||||
//=====================---------- timer ----------------------------------------
|
||||
|
||||
void _setCounterValue(SetCounterValue event, Emitter<WaterHeaterState> emit) async {
|
||||
void _setCounterValue(
|
||||
SetCounterValue event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(LoadingNewSate(whModel: deviceStatus));
|
||||
int seconds = 0;
|
||||
try {
|
||||
seconds = event.duration.inSeconds;
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: whId, code: event.deviceCode, value: seconds), whId);
|
||||
DeviceControlModel(
|
||||
deviceId: whId, code: event.deviceCode, value: seconds),
|
||||
whId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
if (event.deviceCode == 'countdown_1') {
|
||||
@ -160,7 +181,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _getCounterValue(GetCounterEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
void _getCounterValue(
|
||||
GetCounterEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(WHLoadingState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(whId);
|
||||
@ -250,7 +272,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
deviceId: whId,
|
||||
);
|
||||
List<dynamic> jsonData = response;
|
||||
listSchedule = jsonData.map((item) => ScheduleModel.fromJson(item)).toList();
|
||||
listSchedule =
|
||||
jsonData.map((item) => ScheduleModel.fromJson(item)).toList();
|
||||
emit(WHInitialState());
|
||||
} on DioException catch (e) {
|
||||
final errorData = e.response!.data;
|
||||
@ -261,12 +284,13 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
|
||||
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<WaterHeaterState> emit) async {
|
||||
Future toggleChange(
|
||||
ToggleScheduleEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
try {
|
||||
emit(WHLoadingState());
|
||||
final response = await DevicesAPI.changeSchedule(
|
||||
@ -284,7 +308,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future deleteSchedule(DeleteScheduleEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
Future deleteSchedule(
|
||||
DeleteScheduleEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
try {
|
||||
emit(WHLoadingState());
|
||||
final response = await DevicesAPI.deleteSchedule(
|
||||
@ -303,7 +328,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _toggleCreateCirculate(ToggleCreateCirculate event, Emitter<WaterHeaterState> emit) {
|
||||
void _toggleCreateCirculate(
|
||||
ToggleCreateCirculate event, Emitter<WaterHeaterState> emit) {
|
||||
emit(WHLoadingState());
|
||||
createCirculate = !createCirculate;
|
||||
selectedDays.clear();
|
||||
@ -311,13 +337,15 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
emit(UpdateCreateScheduleState(createCirculate));
|
||||
}
|
||||
|
||||
void toggleSelectedIndex(ToggleSelectedEvent event, Emitter<WaterHeaterState> emit) {
|
||||
void toggleSelectedIndex(
|
||||
ToggleSelectedEvent event, Emitter<WaterHeaterState> emit) {
|
||||
emit(WHLoadingState());
|
||||
selectedTabIndex = event.index;
|
||||
emit(ChangeSlidingSegmentState(value: selectedTabIndex));
|
||||
}
|
||||
|
||||
void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter<WaterHeaterState> emit) {
|
||||
void toggleCreateSchedule(
|
||||
ToggleCreateScheduleEvent event, Emitter<WaterHeaterState> emit) {
|
||||
emit(WHLoadingState());
|
||||
createSchedule = !createSchedule;
|
||||
selectedDays.clear();
|
||||
@ -366,8 +394,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
List<GroupWHModel> groupWaterHeaterList = [];
|
||||
bool allSwitchesOn = true;
|
||||
|
||||
void _changeFirstWizardSwitch(
|
||||
ChangeFirstWizardSwitchStatusEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event,
|
||||
Emitter<WaterHeaterState> emit) async {
|
||||
emit(LoadingNewSate(whModel: deviceStatus));
|
||||
try {
|
||||
bool allSwitchesValue = true;
|
||||
@ -379,7 +407,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
allSwitchesValue = false;
|
||||
}
|
||||
});
|
||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: allSwitchesValue));
|
||||
emit(UpdateGroupState(
|
||||
twoGangList: groupWaterHeaterList, allSwitches: allSwitchesValue));
|
||||
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
@ -394,7 +423,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _fetchWHWizardStatus(InitialWizardEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
void _fetchWHWizardStatus(
|
||||
InitialWizardEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(WHLoadingState());
|
||||
try {
|
||||
devicesList = [];
|
||||
@ -404,7 +434,8 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
HomeCubit.getInstance().selectedSpace?.id ?? '', 'WH');
|
||||
|
||||
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));
|
||||
@ -426,23 +457,27 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
return true;
|
||||
});
|
||||
}
|
||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: allSwitchesOn));
|
||||
emit(UpdateGroupState(
|
||||
twoGangList: groupWaterHeaterList, allSwitches: allSwitchesOn));
|
||||
} catch (e) {
|
||||
// emit(FailedState(error: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _groupAllOn(GroupAllOnEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
void _groupAllOn(
|
||||
GroupAllOnEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(LoadingNewSate(whModel: deviceStatus));
|
||||
try {
|
||||
for (int i = 0; i < groupWaterHeaterList.length; i++) {
|
||||
groupWaterHeaterList[i].firstSwitch = true;
|
||||
}
|
||||
|
||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: true));
|
||||
emit(UpdateGroupState(
|
||||
twoGangList: groupWaterHeaterList, allSwitches: true));
|
||||
|
||||
List<String> allDeviceIds = groupWaterHeaterList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupWaterHeaterList.map((device) => device.deviceId).toList();
|
||||
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
@ -460,15 +495,18 @@ class WaterHeaterBloc extends Bloc<WaterHeaterEvent, WaterHeaterState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _groupAllOff(GroupAllOffEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
void _groupAllOff(
|
||||
GroupAllOffEvent event, Emitter<WaterHeaterState> emit) async {
|
||||
emit(LoadingNewSate(whModel: deviceStatus));
|
||||
try {
|
||||
for (int i = 0; i < groupWaterHeaterList.length; i++) {
|
||||
groupWaterHeaterList[i].firstSwitch = false;
|
||||
}
|
||||
emit(UpdateGroupState(twoGangList: groupWaterHeaterList, allSwitches: false));
|
||||
emit(UpdateGroupState(
|
||||
twoGangList: groupWaterHeaterList, allSwitches: false));
|
||||
|
||||
List<String> allDeviceIds = groupWaterHeaterList.map((device) => device.deviceId).toList();
|
||||
List<String> allDeviceIds =
|
||||
groupWaterHeaterList.map((device) => device.deviceId).toList();
|
||||
|
||||
final response = await DevicesAPI.deviceBatchController(
|
||||
code: 'switch_1',
|
||||
|
@ -1,6 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:firebase_database/firebase_database.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/water_leak_bloc/water_leak_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/water_leak_bloc/water_leak_state.dart';
|
||||
@ -21,7 +20,6 @@ class WaterLeakBloc extends Bloc<WaterLeakEvent, WaterLeakState> {
|
||||
on<ToggleClosingReminderEvent>(_toggleClosingReminder);
|
||||
on<ToggleWaterLeakAlarmEvent>(_toggleWaterLeakAlarm);
|
||||
}
|
||||
Timer? _timer;
|
||||
bool lowBattery = false;
|
||||
bool closingReminder = false;
|
||||
bool waterAlarm = false;
|
||||
@ -134,32 +132,42 @@ class WaterLeakBloc extends Bloc<WaterLeakEvent, WaterLeakState> {
|
||||
emit(WaterLeakFailedState(errorMessage: errorMessage));
|
||||
}
|
||||
}
|
||||
// Timer? _timer;
|
||||
// StreamSubscription<DatabaseEvent>? _streamSubscription;
|
||||
// void _listenToChanges() {
|
||||
// try {
|
||||
// _streamSubscription?.cancel();
|
||||
// DatabaseReference ref =
|
||||
// FirebaseDatabase.instance.ref('device-status/$WLId');
|
||||
// Stream<DatabaseEvent> stream = ref.onValue;
|
||||
|
||||
_listenToChanges() {
|
||||
try {
|
||||
DatabaseReference ref =
|
||||
FirebaseDatabase.instance.ref('device-status/$WLId');
|
||||
Stream<DatabaseEvent> stream = ref.onValue;
|
||||
// _streamSubscription = 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>;
|
||||
// List<StatusModel> statusList = [];
|
||||
// usersMap['status'].forEach((element) {
|
||||
// statusList
|
||||
// .add(StatusModel(code: element['code'], value: element['value']));
|
||||
// });
|
||||
// deviceStatus = WaterLeakModel.fromJson(statusList);
|
||||
// if (!isClosed) {
|
||||
// add(
|
||||
// WaterLeakSwitch(switchD: deviceStatus.waterContactState),
|
||||
// );
|
||||
// }
|
||||
// });
|
||||
// } catch (_) {}
|
||||
// }
|
||||
|
||||
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>;
|
||||
List<StatusModel> statusList = [];
|
||||
// @override
|
||||
// Future<void> close() async {
|
||||
// _streamSubscription?.cancel();
|
||||
// _streamSubscription = null;
|
||||
// return super.close();
|
||||
// }
|
||||
|
||||
usersMap['status'].forEach((element) {
|
||||
statusList.add(StatusModel(code: element['code'], value: true));
|
||||
});
|
||||
|
||||
deviceStatus = WaterLeakModel.fromJson(statusList);
|
||||
if (!isClosed) {
|
||||
add(
|
||||
WaterLeakSwitch(switchD: deviceStatus.waterContactState),
|
||||
);
|
||||
}
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
@ -27,24 +27,24 @@ class AcStatusModel {
|
||||
}
|
||||
|
||||
factory AcStatusModel.fromJson(String id, List<StatusModel> jsonList) {
|
||||
late bool _acSwitch;
|
||||
late String _mode;
|
||||
late int _tempSet;
|
||||
late int _currentTemp;
|
||||
late String _fanSpeeds;
|
||||
late int _countdown1;
|
||||
late bool _childLock;
|
||||
bool _acSwitch = false;
|
||||
String _mode = '';
|
||||
int _tempSet = 210;
|
||||
int _currentTemp = 210;
|
||||
String _fanSpeeds = '';
|
||||
int _countdown1 = 0;
|
||||
bool _childLock = false;
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'switch') {
|
||||
_acSwitch = jsonList[i].value ?? false;
|
||||
} else if (jsonList[i].code == 'mode') {
|
||||
_mode = jsonList[i].value ?? TempModes.cold;
|
||||
_mode = jsonList[i].value ?? '';
|
||||
} else if (jsonList[i].code == 'temp_set') {
|
||||
_tempSet = jsonList[i].value ?? 210;
|
||||
} else if (jsonList[i].code == 'temp_current') {
|
||||
_currentTemp = jsonList[i].value ?? 210;
|
||||
} else if (jsonList[i].code == 'level') {
|
||||
_fanSpeeds = jsonList[i].value ?? 210;
|
||||
_fanSpeeds = jsonList[i].value ?? '';
|
||||
} else if (jsonList[i].code == 'child_lock') {
|
||||
_childLock = jsonList[i].value ?? false;
|
||||
} else if (jsonList[i].code == 'countdown_time') {
|
||||
|
@ -23,9 +23,9 @@ class CeilingSensorModel {
|
||||
required this.bodyMovement});
|
||||
|
||||
factory CeilingSensorModel.fromJson(List<StatusModel> jsonList) {
|
||||
late String _presenceState;
|
||||
late int _sensitivity;
|
||||
late String _checkingResult;
|
||||
String _presenceState = 'none';
|
||||
int _sensitivity = 1;
|
||||
String _checkingResult = '';
|
||||
int _presenceRange = 1;
|
||||
int _sportsPara = 1;
|
||||
int _moving_max_dis = 0;
|
||||
|
@ -10,11 +10,11 @@ class CurtainModel {
|
||||
});
|
||||
|
||||
factory CurtainModel.fromJson(List<StatusModel> jsonList) {
|
||||
late String _control;
|
||||
late int _percent;
|
||||
String _control = '';
|
||||
int _percent = 0;
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'control') {
|
||||
_control = jsonList[i].value ?? false;
|
||||
_control = jsonList[i].value ?? '';
|
||||
}
|
||||
if (jsonList[i].code == 'percent_control') {
|
||||
_percent = jsonList[i].value ?? 0;
|
||||
|
@ -1,5 +1,3 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class DeviceInfoModel {
|
||||
final int activeTime;
|
||||
final String category;
|
||||
|
@ -1,27 +1,22 @@
|
||||
|
||||
|
||||
|
||||
|
||||
import 'package:syncrow_app/features/devices/model/status_model.dart';
|
||||
|
||||
class DoorSensorModel {
|
||||
bool doorContactState;
|
||||
int batteryPercentage;
|
||||
|
||||
DoorSensorModel(
|
||||
{required this.doorContactState,
|
||||
required this.batteryPercentage,
|
||||
});
|
||||
DoorSensorModel({
|
||||
required this.doorContactState,
|
||||
required this.batteryPercentage,
|
||||
});
|
||||
|
||||
factory DoorSensorModel.fromJson(List<StatusModel> jsonList) {
|
||||
late bool _doorContactState;
|
||||
late int _batteryPercentage;
|
||||
|
||||
bool _doorContactState = false;
|
||||
int _batteryPercentage = 0;
|
||||
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'doorcontact_state') {
|
||||
_doorContactState = jsonList[i].value ?? false;
|
||||
} else if (jsonList[i].code == 'battery_percentage') {
|
||||
} else if (jsonList[i].code == 'battery_percentage') {
|
||||
_batteryPercentage = jsonList[i].value ?? 0;
|
||||
}
|
||||
}
|
||||
|
@ -24,15 +24,15 @@ class GarageDoorModel {
|
||||
});
|
||||
|
||||
factory GarageDoorModel.fromJson(List<StatusModel> jsonList) {
|
||||
late bool _switch1 = false;
|
||||
late bool _doorContactState = false;
|
||||
late int _countdown1 = 0;
|
||||
late int _countdownAlarm = 0;
|
||||
late String _doorControl1 = "closed";
|
||||
late bool _voiceControl1 = false;
|
||||
late String _doorState1 = "closed";
|
||||
late int _batteryPercentage = 0;
|
||||
late int _tr_timecon = 0;
|
||||
bool _switch1 = false;
|
||||
bool _doorContactState = false;
|
||||
int _countdown1 = 0;
|
||||
int _countdownAlarm = 0;
|
||||
String _doorControl1 = "closed";
|
||||
bool _voiceControl1 = false;
|
||||
String _doorState1 = "closed";
|
||||
int _batteryPercentage = 0;
|
||||
int _tr_timecon = 0;
|
||||
|
||||
for (var status in jsonList) {
|
||||
switch (status.code) {
|
||||
|
@ -1,29 +1,27 @@
|
||||
|
||||
import 'package:syncrow_app/features/devices/model/status_model.dart';
|
||||
|
||||
class OneGangModel {
|
||||
bool firstSwitch;
|
||||
int firstCountDown;
|
||||
|
||||
OneGangModel(
|
||||
{required this.firstSwitch,
|
||||
required this.firstCountDown,
|
||||
});
|
||||
OneGangModel({
|
||||
required this.firstSwitch,
|
||||
required this.firstCountDown,
|
||||
});
|
||||
|
||||
factory OneGangModel.fromJson(List<StatusModel> jsonList) {
|
||||
late bool _switch;
|
||||
late int _count;
|
||||
|
||||
bool _switch = false;
|
||||
int _count = 0;
|
||||
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'switch_1') {
|
||||
_switch = jsonList[i].value ?? false;
|
||||
} else if (jsonList[i].code == 'countdown_1') {
|
||||
} else if (jsonList[i].code == 'countdown_1') {
|
||||
_count = jsonList[i].value ?? 0;
|
||||
}
|
||||
}
|
||||
return OneGangModel(
|
||||
firstSwitch: _switch,
|
||||
firstSwitch: _switch,
|
||||
firstCountDown: _count,
|
||||
);
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
import 'package:syncrow_app/features/devices/model/status_model.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
||||
|
||||
@ -15,15 +13,14 @@ class OneTouchModel {
|
||||
required this.firstCountDown,
|
||||
required this.light_mode,
|
||||
required this.relay,
|
||||
required this.relay_status_1
|
||||
});
|
||||
required this.relay_status_1});
|
||||
|
||||
factory OneTouchModel.fromJson(List<StatusModel> jsonList) {
|
||||
late bool _switch;
|
||||
late int _count;
|
||||
late String _relay;
|
||||
late String _light_mode;
|
||||
late String relay_status_1;
|
||||
bool _switch = false;
|
||||
int _count = 0;
|
||||
String _relay = '';
|
||||
String _light_mode = '';
|
||||
String relay_status_1 = '';
|
||||
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'switch_1') {
|
||||
@ -41,10 +38,8 @@ class OneTouchModel {
|
||||
return OneTouchModel(
|
||||
firstSwitch: _switch,
|
||||
firstCountDown: _count,
|
||||
light_mode: lightStatusExtension.fromString(_light_mode) ,
|
||||
relay: StatusExtension.fromString(_relay ) ,
|
||||
relay_status_1: StatusExtension.fromString(relay_status_1 )
|
||||
|
||||
);
|
||||
light_mode: lightStatusExtension.fromString(_light_mode),
|
||||
relay: StatusExtension.fromString(_relay),
|
||||
relay_status_1: StatusExtension.fromString(relay_status_1));
|
||||
}
|
||||
}
|
||||
|
@ -22,14 +22,14 @@ class SixSceneModel {
|
||||
});
|
||||
|
||||
factory SixSceneModel.fromJson(List<StatusModel> jsonList) {
|
||||
late dynamic _scene_1;
|
||||
late dynamic _scene_2;
|
||||
late dynamic _scene_3;
|
||||
late dynamic _scene_4;
|
||||
late dynamic _scene_5;
|
||||
late dynamic _scene_6;
|
||||
late dynamic _scene_id_group_id;
|
||||
late dynamic _switch_backlight;
|
||||
dynamic _scene_1 = '';
|
||||
dynamic _scene_2 = '';
|
||||
dynamic _scene_3 = '';
|
||||
dynamic _scene_4 = '';
|
||||
dynamic _scene_5 = '';
|
||||
dynamic _scene_6 = '';
|
||||
dynamic _scene_id_group_id = 0;
|
||||
dynamic _switch_backlight = false;
|
||||
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'scene_1') {
|
||||
|
@ -39,23 +39,23 @@ class SmartDoorModel {
|
||||
required this.normalOpenSwitch});
|
||||
|
||||
factory SmartDoorModel.fromJson(List<StatusModel> jsonList) {
|
||||
late int _unlockFingerprint;
|
||||
late int _unlockPassword;
|
||||
late int _unlockTemporary;
|
||||
late int _unlockCard;
|
||||
late String _unlockAlarm;
|
||||
late int _unlockRequest;
|
||||
late int _residualElectricity;
|
||||
late bool _reverseLock;
|
||||
late int _unlockApp;
|
||||
late bool _hijack;
|
||||
late bool _doorbell;
|
||||
late String _unlockOfflinePd;
|
||||
late String _unlockOfflineClear;
|
||||
late String _unlockDoubleKit;
|
||||
late String _remoteNoPdSetkey;
|
||||
late String _remoteNoDpKey;
|
||||
late bool _normalOpenSwitch;
|
||||
int _unlockFingerprint = 0;
|
||||
int _unlockPassword = 0;
|
||||
int _unlockTemporary = 0;
|
||||
int _unlockCard = 0;
|
||||
String _unlockAlarm = '';
|
||||
int _unlockRequest = 0;
|
||||
int _residualElectricity = 0;
|
||||
bool _reverseLock = false;
|
||||
int _unlockApp = 0;
|
||||
bool _hijack = false;
|
||||
bool _doorbell = false;
|
||||
String _unlockOfflinePd = '';
|
||||
String _unlockOfflineClear = '';
|
||||
String _unlockDoubleKit = '';
|
||||
String _remoteNoPdSetkey = '';
|
||||
String _remoteNoDpKey = '';
|
||||
bool _normalOpenSwitch = false;
|
||||
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'unlock_fingerprint') {
|
||||
|
@ -10,8 +10,8 @@ class SosModel {
|
||||
});
|
||||
|
||||
factory SosModel.fromJson(List<StatusModel> jsonList) {
|
||||
late String _sosContactState;
|
||||
late int _batteryPercentage;
|
||||
String _sosContactState = '';
|
||||
int _batteryPercentage = 0;
|
||||
|
||||
for (int i = 0; i < jsonList.length; i++) {
|
||||
if (jsonList[i].code == 'sos') {
|
||||
|
@ -31,7 +31,7 @@ class ACsList extends StatelessWidget {
|
||||
List<DeviceModel> devicesList = [];
|
||||
bool allOn = false;
|
||||
bool allTempSame = false;
|
||||
int temperature = 20;
|
||||
int temperature = 250;
|
||||
if (state is GetAllAcsStatusState) {
|
||||
devicesStatuesList = state.allAcsStatues;
|
||||
devicesList = state.allAcs;
|
||||
|
@ -23,6 +23,7 @@ class ACsView extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
|
||||
create: (context) => ACsBloc(acId: deviceModel?.uuid ?? '')
|
||||
..add(AcsInitial(allAcs: deviceModel != null ? false : true)),
|
||||
child: BlocBuilder<ACsBloc, AcsState>(
|
||||
|
@ -332,7 +332,7 @@ class CeilingSensorInterface extends StatelessWidget {
|
||||
title: title.toString(),
|
||||
sensor: ceilingSensor,
|
||||
value: model.sensitivity,
|
||||
min: 0,
|
||||
min: 1,
|
||||
max: 10,
|
||||
));
|
||||
if (result != null) {
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -20,8 +20,9 @@ class OneGangScreen extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) => OneGangBloc(switchCode: 'switch_1', oneGangId: device?.uuid ?? '')
|
||||
..add(const InitialEvent(groupScreen: false)),
|
||||
create: (context) =>
|
||||
OneGangBloc(switchCode: 'switch_1', oneGangId: device?.uuid ?? '')
|
||||
..add(const InitialEvent(groupScreen: false)),
|
||||
child: BlocBuilder<OneGangBloc, OneGangState>(
|
||||
builder: (context, state) {
|
||||
OneGangModel oneGangModel = OneGangModel(
|
||||
@ -36,13 +37,15 @@ class OneGangScreen extends StatelessWidget {
|
||||
}
|
||||
return state is LoadingInitialState
|
||||
? const Center(
|
||||
child:
|
||||
DefaultContainer(width: 50, height: 50, child: CircularProgressIndicator()),
|
||||
child: DefaultContainer(
|
||||
width: 50,
|
||||
height: 50,
|
||||
child: CircularProgressIndicator()),
|
||||
)
|
||||
: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
BlocProvider.of<OneGangBloc>(context)
|
||||
.add(InitialEvent(groupScreen: device != null ? false : true));
|
||||
BlocProvider.of<OneGangBloc>(context).add(InitialEvent(
|
||||
groupScreen: device != null ? false : true));
|
||||
},
|
||||
child: ListView(
|
||||
children: [
|
||||
@ -55,7 +58,8 @@ class OneGangScreen extends StatelessWidget {
|
||||
const Expanded(child: SizedBox.shrink()),
|
||||
Expanded(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
mainAxisAlignment:
|
||||
MainAxisAlignment.spaceAround,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Column(
|
||||
@ -64,9 +68,10 @@ class OneGangScreen extends StatelessWidget {
|
||||
threeGangSwitch: device!,
|
||||
value: oneGangModel.firstSwitch,
|
||||
action: () {
|
||||
BlocProvider.of<OneGangBloc>(context).add(
|
||||
ChangeFirstSwitchStatusEvent(
|
||||
value: oneGangModel.firstSwitch));
|
||||
BlocProvider.of<OneGangBloc>(context)
|
||||
.add(ChangeFirstSwitchStatusEvent(
|
||||
value: oneGangModel
|
||||
.firstSwitch));
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
@ -74,7 +79,8 @@ class OneGangScreen extends StatelessWidget {
|
||||
width: 70,
|
||||
child: BodySmall(
|
||||
text: " Entrance Light",
|
||||
fontColor: ColorsManager.textPrimaryColor,
|
||||
fontColor:
|
||||
ColorsManager.textPrimaryColor,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
@ -94,20 +100,24 @@ class OneGangScreen extends StatelessWidget {
|
||||
Card(
|
||||
elevation: 3,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(100),
|
||||
borderRadius:
|
||||
BorderRadius.circular(100),
|
||||
),
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
PageRouteBuilder(
|
||||
pageBuilder:
|
||||
(context, animation1, animation2) =>
|
||||
TimerScheduleScreen(
|
||||
switchCode: 'switch_1',
|
||||
device: device!,
|
||||
deviceCode: 'countdown_1',
|
||||
)));
|
||||
pageBuilder: (context,
|
||||
animation1,
|
||||
animation2) =>
|
||||
TimerScheduleScreen(
|
||||
switchCode:
|
||||
'switch_1',
|
||||
device: device!,
|
||||
deviceCode:
|
||||
'countdown_1',
|
||||
)));
|
||||
},
|
||||
child: Stack(
|
||||
alignment: Alignment.center,
|
||||
@ -117,7 +127,9 @@ class OneGangScreen extends StatelessWidget {
|
||||
height: 60,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey[300],
|
||||
borderRadius: BorderRadius.circular(100),
|
||||
borderRadius:
|
||||
BorderRadius.circular(
|
||||
100),
|
||||
),
|
||||
),
|
||||
Container(
|
||||
@ -125,12 +137,15 @@ class OneGangScreen extends StatelessWidget {
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(100),
|
||||
borderRadius:
|
||||
BorderRadius.circular(
|
||||
100),
|
||||
),
|
||||
child: Center(
|
||||
child: Icon(
|
||||
Icons.access_time,
|
||||
color: ColorsManager.primaryColorWithOpacity,
|
||||
color: ColorsManager
|
||||
.primaryColorWithOpacity,
|
||||
size: 25,
|
||||
),
|
||||
),
|
||||
|
@ -119,6 +119,8 @@ Future<void> showDeviceInterface(
|
||||
required BuildContext context,
|
||||
required isAllDevices,
|
||||
List<DeviceModel>? allDevices}) async {
|
||||
print('object-----${device.uuid} ${device.name}');
|
||||
|
||||
final devicesCubit = context.read<DevicesCubit>();
|
||||
|
||||
switch (device.productType) {
|
||||
|
@ -1,10 +1,12 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_event.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_state.dart';
|
||||
import 'package:syncrow_app/services/api/home_creation_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
|
||||
class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
|
||||
@ -239,8 +241,13 @@ Future<String> _createNewRoom(
|
||||
required String communityId}) async {
|
||||
try {
|
||||
Map<String, String> body = {'subspaceName': roomName};
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final response = await HomeCreation.createRoom(
|
||||
communityId: communityId, spaceId: unitId, body: body);
|
||||
communityId: communityId,
|
||||
spaceId: unitId,
|
||||
body: body,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
// if (response['data']['uuid'] != '') {
|
||||
// final result = await _assignToRoom(roomId: response['data']['uuid'], userId: userId);
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_event.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_state.dart';
|
||||
@ -7,6 +8,7 @@ import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_creation_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
|
||||
class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
List<DeviceModel> allDevices = [];
|
||||
@ -24,8 +26,12 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
FetchRoomsEvent event, Emitter<ManageUnitState> emit) async {
|
||||
try {
|
||||
emit(LoadingState());
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final roomsList = await SpacesAPI.getSubSpaceBySpaceId(
|
||||
event.unit.community.uuid, event.unit.id);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
} catch (e) {
|
||||
emit(const ErrorState(message: 'Something went wrong'));
|
||||
@ -37,12 +43,16 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
FetchDevicesByRoomIdEvent event, Emitter<ManageUnitState> emit) async {
|
||||
try {
|
||||
Map<String, bool> roomDevicesId = {};
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
emit(LoadingState());
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
allDevices = await HomeManagementAPI.fetchDevices(
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
|
||||
List<String> allDevicesIds = [];
|
||||
|
||||
@ -72,14 +82,21 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
AssignRoomEvent event, Emitter<ManageUnitState> emit) async {
|
||||
try {
|
||||
Map<String, bool> roomDevicesId = {};
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
emit(LoadingState());
|
||||
|
||||
|
||||
await HomeManagementAPI.assignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, event.deviceId);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
event.roomId,
|
||||
event.deviceId,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
|
||||
List<String> allDevicesIds = [];
|
||||
|
||||
@ -105,19 +122,25 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void _unassignDevice(
|
||||
UnassignRoomEvent event, Emitter<ManageUnitState> emit) async {
|
||||
try {
|
||||
Map<String, bool> roomDevicesId = {};
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
emit(LoadingState());
|
||||
|
||||
|
||||
await HomeManagementAPI.unAssignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, event.deviceId);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
event.roomId,
|
||||
event.deviceId,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
roomId: event.roomId,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
|
||||
List<String> allDevicesIds = [];
|
||||
|
||||
@ -143,18 +166,22 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_addNewRoom(AddNewRoom event, Emitter<ManageUnitState> emit) async {
|
||||
Map<String, String> body = {'subspaceName': event.roomName};
|
||||
try {
|
||||
emit(LoadingState());
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final response = await HomeCreation.createRoom(
|
||||
communityId: event.unit.community.uuid,
|
||||
spaceId: event.unit.id,
|
||||
body: body);
|
||||
body: body,
|
||||
projectId: project?.uuid ?? TempConst.projectIdDev);
|
||||
if (response['data']['uuid'] != '') {
|
||||
final roomsList = await SpacesAPI.getSubSpaceBySpaceId(
|
||||
event.unit.community.uuid, event.unit.id);
|
||||
event.unit.community.uuid,
|
||||
event.unit.id,
|
||||
project?.uuid ?? TempConst.projectIdDev);
|
||||
allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
await HomeCubit.getInstance().fetchUnitsByUserId();
|
||||
|
@ -154,11 +154,11 @@ class MenuCubit extends Cubit<MenuState> {
|
||||
'title': 'Legal Information',
|
||||
'color': const Color(0xFF001B72),
|
||||
'buttons': [
|
||||
{
|
||||
'title': 'About',
|
||||
'Icon': Assets.assetsIconsMenuIconsLeagalInfoIconsAbout,
|
||||
'page': null
|
||||
},
|
||||
// {
|
||||
// 'title': 'About',
|
||||
// 'Icon': Assets.assetsIconsMenuIconsLeagalInfoIconsAbout,
|
||||
// 'page': null
|
||||
// },
|
||||
{
|
||||
'title': 'Privacy Policy',
|
||||
'Icon': Assets.assetsIconsMenuIconsLeagalInfoIconsPrivacyPolicy,
|
||||
@ -176,7 +176,7 @@ class MenuCubit extends Cubit<MenuState> {
|
||||
Future<void> fetchMenuSections() async {
|
||||
emit(MenuLoading());
|
||||
try {
|
||||
emit(MenuItemsLoaded(menuSections));
|
||||
emit(MenuItemsLoaded(menuSections));
|
||||
} catch (e) {
|
||||
emit(MenuError(e.toString()));
|
||||
}
|
||||
|
@ -23,8 +23,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
String timeZoneSelected = '';
|
||||
String regionSelected = '';
|
||||
final TextEditingController searchController = TextEditingController();
|
||||
final TextEditingController nameController =
|
||||
TextEditingController(text: '${HomeCubit.user!.firstName} ${HomeCubit.user!.lastName}');
|
||||
final TextEditingController nameController = TextEditingController(
|
||||
text: '${HomeCubit.user!.firstName} ${HomeCubit.user!.lastName}');
|
||||
|
||||
List<RegionModel> allRegions = [];
|
||||
List<TimeZone> allTimeZone = [];
|
||||
@ -77,10 +77,13 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
emit(NameEditingState(editName: editName));
|
||||
}
|
||||
|
||||
void _fetchUserInfo(InitialProfileEvent event, Emitter<ProfileState> emit) async {
|
||||
void _fetchUserInfo(
|
||||
InitialProfileEvent event, Emitter<ProfileState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
HomeCubit.user = await ProfileApi().fetchUserInfo(HomeCubit.user!.uuid);
|
||||
HomeCubit.getInstance().project = HomeCubit.user?.project;
|
||||
|
||||
emit(SaveState());
|
||||
} catch (e) {
|
||||
emit(FailedState(errorMessage: e.toString()));
|
||||
@ -88,7 +91,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future _fetchTimeZone(TimeZoneInitialEvent event, Emitter<ProfileState> emit) async {
|
||||
Future _fetchTimeZone(
|
||||
TimeZoneInitialEvent event, Emitter<ProfileState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
allTimeZone = await ProfileApi.fetchTimeZone();
|
||||
@ -100,7 +104,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future selectTimeZone(SelectTimeZoneEvent event, Emitter<ProfileState> emit) async {
|
||||
Future selectTimeZone(
|
||||
SelectTimeZoneEvent event, Emitter<ProfileState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
timeZoneSelected = event.val;
|
||||
@ -112,7 +117,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future selectRegion(SelectRegionEvent event, Emitter<ProfileState> emit) async {
|
||||
Future selectRegion(
|
||||
SelectRegionEvent event, Emitter<ProfileState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
await ProfileApi.saveRegion(regionUuid: event.val);
|
||||
@ -124,7 +130,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> searchRegion(SearchRegionEvent event, Emitter<ProfileState> emit) async {
|
||||
Future<void> searchRegion(
|
||||
SearchRegionEvent event, Emitter<ProfileState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
final query = event.query.toLowerCase();
|
||||
if (allRegions.isEmpty) {
|
||||
@ -140,7 +147,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> searchTimeZone(SearchTimeZoneEvent event, Emitter<ProfileState> emit) async {
|
||||
Future<void> searchTimeZone(
|
||||
SearchTimeZoneEvent event, Emitter<ProfileState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
final query = event.query.toLowerCase();
|
||||
if (allTimeZone.isEmpty) {
|
||||
@ -156,7 +164,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _fetchRegion(RegionInitialEvent event, Emitter<ProfileState> emit) async {
|
||||
void _fetchRegion(
|
||||
RegionInitialEvent event, Emitter<ProfileState> emit) async {
|
||||
try {
|
||||
emit(LoadingInitialState());
|
||||
allRegions = await ProfileApi.fetchRegion();
|
||||
@ -166,7 +175,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _selectImage(SelectImageEvent event, Emitter<ProfileState> emit) async {
|
||||
Future<void> _selectImage(
|
||||
SelectImageEvent event, Emitter<ProfileState> emit) async {
|
||||
try {
|
||||
if (await _requestPermission()) {
|
||||
emit(ChangeImageState());
|
||||
@ -283,7 +293,8 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
|
||||
SharedPreferences sharedPreferences =
|
||||
await SharedPreferences.getInstance();
|
||||
bool firstClick = sharedPreferences.getBool('firstPermission') ?? true;
|
||||
await sharedPreferences.setBool('firstPermission', false);
|
||||
if (firstClick == false) {
|
||||
|
@ -28,8 +28,7 @@ class AssignDeviceView extends StatelessWidget {
|
||||
listener: (context, state) {
|
||||
if (state is FetchDeviceByRoomIdState) {
|
||||
if (state.allDevices.isEmpty) {
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'You do not have the permission to assign devices');
|
||||
CustomSnackBar.displaySnackBar('You do not have the devices');
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
}
|
||||
|
@ -16,85 +16,74 @@ class ManageHomeView extends StatelessWidget {
|
||||
var spaces = HomeCubit.getInstance().spaces;
|
||||
return DefaultScaffold(
|
||||
title: 'Manage Your Home',
|
||||
child: spaces == null
|
||||
height: MediaQuery.sizeOf(context).height,
|
||||
child: spaces.isEmpty
|
||||
? const Center(
|
||||
child: BodyMedium(text: 'No spaces found'),
|
||||
)
|
||||
: Column(
|
||||
children: [
|
||||
DefaultContainer(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 25,
|
||||
vertical: 20,
|
||||
),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
children: List.generate(
|
||||
spaces.length,
|
||||
(index) {
|
||||
if (index == spaces.length - 1) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(CustomPageRoute(
|
||||
builder: (context) => HomeSettingsView(
|
||||
space: spaces[index],
|
||||
)));
|
||||
},
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
BodyMedium(text: StringHelpers.toTitleCase(spaces[index].name)),
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
color: ColorsManager.greyColor,
|
||||
size: 15,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
//TODO refactor the routing to use named routes
|
||||
// Navigator.of(context).pushNamed(
|
||||
// '/home_settings',
|
||||
// arguments: spaces[index],
|
||||
// );
|
||||
: DefaultContainer(
|
||||
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 25),
|
||||
child: ListView.builder(
|
||||
itemCount: spaces.length,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == spaces.length - 1) {
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
Navigator.of(context).push(CustomPageRoute(
|
||||
builder: (context) => HomeSettingsView(
|
||||
space: spaces[index],
|
||||
)));
|
||||
},
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
BodyMedium(text: StringHelpers.toTitleCase(spaces[index].name)),
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
color: ColorsManager.greyColor,
|
||||
size: 15,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
//TODO refactor the routing to use named routes
|
||||
// Navigator.of(context).pushNamed(
|
||||
// '/home_settings',
|
||||
// arguments: spaces[index],
|
||||
// );
|
||||
|
||||
Navigator.of(context).push(CustomPageRoute(
|
||||
builder: (context) => HomeSettingsView(
|
||||
space: spaces[index],
|
||||
)));
|
||||
},
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
Navigator.of(context).push(CustomPageRoute(
|
||||
builder: (context) => HomeSettingsView(
|
||||
space: spaces[index],
|
||||
)));
|
||||
},
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
BodyMedium(text: HomeCubit.getInstance().spaces![index].name),
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
color: ColorsManager.greyColor,
|
||||
size: 15,
|
||||
)
|
||||
],
|
||||
),
|
||||
Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 15),
|
||||
height: 1,
|
||||
BodyMedium(text: HomeCubit.getInstance().spaces[index].name),
|
||||
const Icon(
|
||||
Icons.arrow_forward_ios,
|
||||
color: ColorsManager.greyColor,
|
||||
),
|
||||
size: 15,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 15),
|
||||
height: 1,
|
||||
color: ColorsManager.greyColor,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
import 'dart:async';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||
import 'package:syncrow_app/features/scene/bloc/effective_period/effect_period_bloc.dart';
|
||||
import 'package:syncrow_app/features/scene/bloc/effective_period/effect_period_event.dart';
|
||||
@ -348,15 +350,18 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
|
||||
emit(CreateSceneLoading());
|
||||
try {
|
||||
dynamic response;
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
if (event.createSceneModel != null) {
|
||||
response = event.updateScene
|
||||
? await SceneApi.updateScene(event.createSceneModel!, event.sceneId)
|
||||
: await SceneApi.createScene(event.createSceneModel!);
|
||||
} else if (event.createAutomationModel != null) {
|
||||
response = event.updateScene
|
||||
? await SceneApi.updateAutomation(
|
||||
event.createAutomationModel!, event.sceneId)
|
||||
: await SceneApi.createAutomation(event.createAutomationModel!);
|
||||
? await SceneApi.updateAutomation(event.createAutomationModel!,
|
||||
event.sceneId, project?.uuid ?? '')
|
||||
: await SceneApi.createAutomation(
|
||||
event.createAutomationModel!, project?.uuid ?? '');
|
||||
}
|
||||
|
||||
if (response['success'] == true) {
|
||||
@ -421,6 +426,8 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
|
||||
emit(CreateSceneLoading());
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
tasksList.clear();
|
||||
tempTasksList.clear();
|
||||
selectedValues.clear();
|
||||
@ -436,7 +443,8 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
|
||||
conditionRule = 'or';
|
||||
|
||||
final response = event.isAutomation
|
||||
? await SceneApi.getAutomationDetails(event.sceneId)
|
||||
? await SceneApi.getAutomationDetails(
|
||||
event.sceneId, project?.uuid ?? '')
|
||||
: await SceneApi.getSceneDetails(event.sceneId);
|
||||
if (response.id.isNotEmpty) {
|
||||
if (event.isAutomation) {
|
||||
@ -605,10 +613,14 @@ class CreateSceneBloc extends Bloc<CreateSceneEvent, CreateSceneState>
|
||||
emit(DeleteSceneLoading());
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final response =
|
||||
sceneType.name == CreateSceneEnum.deviceStatusChanges.name
|
||||
? await SceneApi.deleteAutomation(
|
||||
automationId: event.sceneId, unitUuid: event.unitUuid)
|
||||
automationId: event.sceneId,
|
||||
unitUuid: event.unitUuid,
|
||||
projectId: project?.uuid ?? '')
|
||||
: await SceneApi.deleteScene(
|
||||
sceneId: event.sceneId,
|
||||
);
|
||||
|
@ -2,9 +2,12 @@ import 'dart:async';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/project_model.dart';
|
||||
import 'package:syncrow_app/features/scene/bloc/scene_bloc/scene_event.dart';
|
||||
import 'package:syncrow_app/features/scene/model/scenes_model.dart';
|
||||
import 'package:syncrow_app/services/api/scene_api.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
|
||||
part 'scene_state.dart';
|
||||
|
||||
@ -23,9 +26,11 @@ class SceneBloc extends Bloc<SceneEvent, SceneState> {
|
||||
emit(SceneLoading());
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
if (event.unitId.isNotEmpty) {
|
||||
scenes = await SceneApi.getScenesByUnitId(
|
||||
event.unitId, event.unit.community.uuid,
|
||||
scenes = await SceneApi.getScenesByUnitId(event.unitId,
|
||||
event.unit.community.uuid, project?.uuid ?? TempConst.projectIdDev,
|
||||
showInDevice: event.showInDevice);
|
||||
emit(SceneLoaded(scenes, automationList));
|
||||
} else {
|
||||
@ -41,8 +46,11 @@ class SceneBloc extends Bloc<SceneEvent, SceneState> {
|
||||
emit(SceneLoading());
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
if (event.unitId.isNotEmpty) {
|
||||
automationList = await SceneApi.getAutomationByUnitId(event.unitId);
|
||||
automationList = await SceneApi.getAutomationByUnitId(
|
||||
event.unitId, event.communityId, project?.uuid ?? '');
|
||||
emit(SceneLoaded(scenes, automationList));
|
||||
} else {
|
||||
emit(const SceneError(message: 'Unit ID is empty'));
|
||||
@ -91,11 +99,17 @@ class SceneBloc extends Bloc<SceneEvent, SceneState> {
|
||||
));
|
||||
|
||||
try {
|
||||
Project? project = HomeCubit.getInstance().project;
|
||||
|
||||
final success = await SceneApi.updateAutomationStatus(
|
||||
event.automationId, event.automationStatusUpdate);
|
||||
event.automationId,
|
||||
event.automationStatusUpdate,
|
||||
project?.uuid ?? '');
|
||||
if (success) {
|
||||
automationList = await SceneApi.getAutomationByUnitId(
|
||||
event.automationStatusUpdate.spaceUuid);
|
||||
event.automationStatusUpdate.spaceUuid,
|
||||
event.communityId,
|
||||
project?.uuid ?? '');
|
||||
newLoadingStates[event.automationId] = false;
|
||||
emit(SceneLoaded(
|
||||
currentState.scenes,
|
||||
|
@ -22,11 +22,13 @@ class LoadScenes extends SceneEvent {
|
||||
|
||||
class LoadAutomation extends SceneEvent {
|
||||
final String unitId;
|
||||
final String communityId;
|
||||
|
||||
const LoadAutomation(this.unitId);
|
||||
|
||||
const LoadAutomation(this.unitId, this.communityId);
|
||||
|
||||
@override
|
||||
List<Object> get props => [unitId];
|
||||
List<Object> get props => [unitId, communityId];
|
||||
}
|
||||
|
||||
class SceneTrigger extends SceneEvent {
|
||||
@ -43,8 +45,9 @@ class SceneTrigger extends SceneEvent {
|
||||
class UpdateAutomationStatus extends SceneEvent {
|
||||
final String automationId;
|
||||
final AutomationStatusUpdate automationStatusUpdate;
|
||||
final String communityId;
|
||||
|
||||
const UpdateAutomationStatus({required this.automationStatusUpdate, required this.automationId});
|
||||
const UpdateAutomationStatus({required this.automationStatusUpdate, required this.automationId, required this.communityId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [automationStatusUpdate];
|
||||
|
@ -29,25 +29,38 @@ class _SceneRoomsTabBarDevicesViewState
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
selectedSpace = HomeCubit.getInstance().selectedSpace!;
|
||||
rooms = List.from(HomeCubit.getInstance().selectedSpace?.subspaces ?? []);
|
||||
if (rooms != null) {
|
||||
if (rooms![0].id != '-1') {
|
||||
rooms?.insert(
|
||||
0,
|
||||
super.initState();
|
||||
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
selectedSpace = HomeCubit.getInstance().selectedSpace!;
|
||||
|
||||
rooms = List.from(selectedSpace.subspaces ?? []);
|
||||
|
||||
if (rooms != null && rooms!.isNotEmpty) {
|
||||
if (rooms![0].id != '-1') {
|
||||
rooms?.insert(
|
||||
0,
|
||||
SubSpaceModel(
|
||||
name: 'All Devices',
|
||||
devices: context.read<DevicesCubit>().allDevices,
|
||||
id: '-1',
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
rooms = [
|
||||
SubSpaceModel(
|
||||
name: 'All Devices',
|
||||
devices: context.read<DevicesCubit>().allDevices,
|
||||
id: '-1',
|
||||
),
|
||||
);
|
||||
)
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
_tabController =
|
||||
TabController(length: rooms!.length, vsync: this, initialIndex: 0);
|
||||
_tabController.addListener(_handleTabSwitched);
|
||||
super.initState();
|
||||
_tabController = TabController(length: rooms!.length, vsync: this);
|
||||
_tabController.addListener(_handleTabSwitched);
|
||||
setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
void _handleTabSwitched() {
|
||||
|
@ -49,7 +49,9 @@ class SceneView extends StatelessWidget {
|
||||
name: '',
|
||||
)),
|
||||
showInDevice: pageType))
|
||||
..add(LoadAutomation(HomeCubit.getInstance().selectedSpace?.id ?? ''));
|
||||
..add(LoadAutomation(
|
||||
HomeCubit.getInstance().selectedSpace?.id ?? '',
|
||||
HomeCubit.getInstance().selectedSpace?.community.uuid ?? ''));
|
||||
}
|
||||
},
|
||||
child: BlocBuilder<CreateSceneBloc, CreateSceneState>(
|
||||
@ -57,30 +59,37 @@ class SceneView extends StatelessWidget {
|
||||
if (state is DeleteSceneSuccess) {
|
||||
if (state.success) {
|
||||
BlocProvider.of<SceneBloc>(context).add(LoadScenes(
|
||||
HomeCubit.getInstance().selectedSpace!.id, HomeCubit.getInstance().selectedSpace!,
|
||||
HomeCubit.getInstance().selectedSpace!.id,
|
||||
HomeCubit.getInstance().selectedSpace!,
|
||||
showInDevice: pageType));
|
||||
BlocProvider.of<SceneBloc>(context)
|
||||
.add(LoadAutomation(HomeCubit.getInstance().selectedSpace!.id));
|
||||
BlocProvider.of<SceneBloc>(context).add(LoadAutomation(
|
||||
HomeCubit.getInstance().selectedSpace!.id,
|
||||
HomeCubit.getInstance().selectedSpace!.community.uuid));
|
||||
}
|
||||
}
|
||||
if (state is CreateSceneWithTasks) {
|
||||
if (state.success == true) {
|
||||
BlocProvider.of<SceneBloc>(context).add(LoadScenes(
|
||||
HomeCubit.getInstance().selectedSpace!.id, HomeCubit.getInstance().selectedSpace!,
|
||||
HomeCubit.getInstance().selectedSpace!.id,
|
||||
HomeCubit.getInstance().selectedSpace!,
|
||||
showInDevice: pageType));
|
||||
BlocProvider.of<SceneBloc>(context)
|
||||
.add(LoadAutomation(HomeCubit.getInstance().selectedSpace!.id));
|
||||
context.read<SmartSceneSelectBloc>().add(const SmartSceneClearEvent());
|
||||
BlocProvider.of<SceneBloc>(context).add(LoadAutomation(
|
||||
HomeCubit.getInstance().selectedSpace!.id,
|
||||
HomeCubit.getInstance().selectedSpace!.community.uuid));
|
||||
context
|
||||
.read<SmartSceneSelectBloc>()
|
||||
.add(const SmartSceneClearEvent());
|
||||
}
|
||||
}
|
||||
return BlocListener<SceneBloc, SceneState>(
|
||||
listener: (context, state) {
|
||||
if (state is SceneTriggerSuccess) {
|
||||
context.showCustomSnackbar(
|
||||
message: 'Scene ${state.sceneName} triggered successfully!');
|
||||
message:
|
||||
'Scene ${state.sceneName} triggered successfully!');
|
||||
}
|
||||
},
|
||||
child: HomeCubit.getInstance().spaces?.isEmpty ?? true
|
||||
child: HomeCubit.getInstance().spaces.isEmpty
|
||||
? const CreateUnitWidget()
|
||||
: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
@ -114,25 +123,30 @@ class SceneView extends StatelessWidget {
|
||||
child: ListView(
|
||||
children: [
|
||||
Theme(
|
||||
data: ThemeData()
|
||||
.copyWith(dividerColor: Colors.transparent),
|
||||
data: ThemeData().copyWith(
|
||||
dividerColor: Colors.transparent),
|
||||
child: ExpansionTile(
|
||||
tilePadding: const EdgeInsets.symmetric(horizontal: 6),
|
||||
tilePadding:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 6),
|
||||
initiallyExpanded: true,
|
||||
iconColor: ColorsManager.grayColor,
|
||||
title: const BodyMedium(text: 'Tap to run routines'),
|
||||
title: const BodyMedium(
|
||||
text: 'Tap to run routines'),
|
||||
children: [
|
||||
scenes.isNotEmpty
|
||||
? SceneGrid(
|
||||
scenes: scenes,
|
||||
loadingSceneId: state.loadingSceneId,
|
||||
loadingSceneId:
|
||||
state.loadingSceneId,
|
||||
disablePlayButton: false,
|
||||
loadingStates:
|
||||
state.loadingStates, // Add this line
|
||||
loadingStates: state
|
||||
.loadingStates, // Add this line
|
||||
)
|
||||
: const Center(
|
||||
child: BodyMedium(
|
||||
text: 'No scenes have been added yet',
|
||||
text:
|
||||
'No scenes have been added yet',
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
@ -142,25 +156,30 @@ class SceneView extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
Theme(
|
||||
data: ThemeData()
|
||||
.copyWith(dividerColor: Colors.transparent),
|
||||
data: ThemeData().copyWith(
|
||||
dividerColor: Colors.transparent),
|
||||
child: ExpansionTile(
|
||||
initiallyExpanded: true,
|
||||
iconColor: ColorsManager.grayColor,
|
||||
tilePadding: const EdgeInsets.symmetric(horizontal: 6),
|
||||
title: const BodyMedium(text: 'Automation'),
|
||||
tilePadding:
|
||||
const EdgeInsets.symmetric(
|
||||
horizontal: 6),
|
||||
title: const BodyMedium(
|
||||
text: 'Automation'),
|
||||
children: [
|
||||
automationList.isNotEmpty
|
||||
? SceneGrid(
|
||||
scenes: automationList,
|
||||
loadingSceneId: state.loadingSceneId,
|
||||
loadingSceneId:
|
||||
state.loadingSceneId,
|
||||
disablePlayButton: true,
|
||||
loadingStates:
|
||||
state.loadingStates, // Add this line
|
||||
loadingStates: state
|
||||
.loadingStates, // Add this line
|
||||
)
|
||||
: const Center(
|
||||
child: BodyMedium(
|
||||
text: 'No automations have been added yet',
|
||||
text:
|
||||
'No automations have been added yet',
|
||||
),
|
||||
),
|
||||
const SizedBox(
|
||||
|
@ -25,7 +25,7 @@ class DeleteRoutineButton extends StatelessWidget {
|
||||
BlocProvider.of<SceneBloc>(context).add(LoadScenes(
|
||||
HomeCubit.getInstance().selectedSpace!.id, HomeCubit.getInstance().selectedSpace!));
|
||||
BlocProvider.of<SceneBloc>(context)
|
||||
.add(LoadAutomation(HomeCubit.getInstance().selectedSpace!.id));
|
||||
.add(LoadAutomation(HomeCubit.getInstance().selectedSpace!.id,HomeCubit.getInstance().selectedSpace!.community.uuid));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -132,6 +132,10 @@ class SceneItem extends StatelessWidget {
|
||||
spaceUuid: HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.id),
|
||||
communityId: HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.community
|
||||
.uuid,
|
||||
automationId: scene.id));
|
||||
},
|
||||
),
|
||||
|
@ -22,7 +22,7 @@ class SmartEnableAutomation extends StatelessWidget {
|
||||
child: BlocBuilder<SceneBloc, SceneState>(
|
||||
bloc: context.read<SceneBloc>()
|
||||
..add(
|
||||
LoadAutomation(HomeCubit.getInstance().selectedSpace?.id ?? '')),
|
||||
LoadAutomation(HomeCubit.getInstance().selectedSpace?.id ?? '',HomeCubit.getInstance().selectedSpace?.community.uuid ?? '')),
|
||||
builder: (context, state) {
|
||||
if (state is SceneLoading) {
|
||||
return const Align(
|
||||
|
@ -88,6 +88,8 @@ abstract class ApiEndpoints {
|
||||
//SPACE Module
|
||||
//GET
|
||||
static const String userSpaces = '/user/{userUuid}/spaces';
|
||||
static const String devices = '/projects/{projectUuid}/devices';
|
||||
|
||||
static const String spaceDevices =
|
||||
'/projects/{projectUuid}/communities/{communityUuid}/spaces/{spaceUuid}/devices';
|
||||
|
||||
@ -142,7 +144,7 @@ abstract class ApiEndpoints {
|
||||
/// POST
|
||||
static const String createScene = '/scene/tap-to-run';
|
||||
static const String triggerScene = '/scene/tap-to-run/{sceneId}/trigger';
|
||||
static const String createAutomation = '/automation';
|
||||
static const String createAutomation = '/projects/{projectId}/automations';
|
||||
|
||||
/// GET
|
||||
static const String getUnitScenes =
|
||||
@ -151,23 +153,23 @@ abstract class ApiEndpoints {
|
||||
static const String getScene = '/scene/tap-to-run/{sceneId}';
|
||||
static const String getIconScene = '/scene/icon';
|
||||
|
||||
static const String getUnitAutomation = '/automation/{unitUuid}';
|
||||
static const String getUnitAutomation = '/projects/{projectId}/communities/{communityId}/spaces/{unitUuid}/automations';
|
||||
|
||||
static const String getAutomationDetails =
|
||||
'/automation/details/{automationId}';
|
||||
'/projects/{projectId}/automations/{automationId}';
|
||||
|
||||
/// PUT
|
||||
static const String updateScene = '/scene/tap-to-run/{sceneId}';
|
||||
|
||||
static const String updateAutomation = '/automation/{automationId}';
|
||||
static const String updateAutomation = '/projects/{projectId}/automations/{automationId}';
|
||||
|
||||
static const String updateAutomationStatus =
|
||||
'/automation/status/{automationId}';
|
||||
'/projects/{projectId}/automations/{automationId}';
|
||||
|
||||
/// DELETE
|
||||
static const String deleteScene = '/scene/tap-to-run/{sceneId}';
|
||||
|
||||
static const String deleteAutomation = '/automation/{automationId}';
|
||||
static const String deleteAutomation = '/projects/{projectId}/automations/{automationId}';
|
||||
|
||||
//////////////////////Door Lock //////////////////////
|
||||
//online
|
||||
|
@ -219,13 +219,14 @@ class DevicesAPI {
|
||||
required String communityUuid,
|
||||
required String spaceUuid,
|
||||
required String roomId,
|
||||
required String projectId,
|
||||
}) async {
|
||||
try {
|
||||
final String path = ApiEndpoints.deviceByRoom
|
||||
.replaceAll('{communityUuid}', communityUuid)
|
||||
.replaceAll('{spaceUuid}', spaceUuid)
|
||||
.replaceAll('{subSpaceUuid}', roomId)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev);
|
||||
.replaceAll('{projectUuid}', projectId);
|
||||
|
||||
final response = await _httpService.get(
|
||||
path: path,
|
||||
@ -564,12 +565,13 @@ class DevicesAPI {
|
||||
static Future<List<DeviceModel>> getAllDevices({
|
||||
required String communityUuid,
|
||||
required String spaceUuid,
|
||||
required String projectId,
|
||||
}) async {
|
||||
try {
|
||||
final String path = ApiEndpoints.getAllDevices
|
||||
.replaceAll('{communityUuid}', communityUuid)
|
||||
.replaceAll('{spaceUuid}', spaceUuid)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev);
|
||||
.replaceAll('{projectUuid}', projectId);
|
||||
|
||||
final response = await _httpService.get(
|
||||
path: path,
|
||||
|
@ -145,12 +145,13 @@ class HomeCreation {
|
||||
required String communityId,
|
||||
required String spaceId,
|
||||
required Map<String, String> body,
|
||||
required String projectId
|
||||
}) async {
|
||||
try {
|
||||
final fullPath = ApiEndpoints.addSubSpace
|
||||
.replaceAll('{communityUuid}', communityId)
|
||||
.replaceAll('{spaceUuid}', spaceId)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev);
|
||||
.replaceAll('{projectUuid}', projectId);
|
||||
final response = await _httpService.post(
|
||||
path: fullPath,
|
||||
body: body,
|
||||
|
@ -27,7 +27,23 @@ class HomeManagementAPI {
|
||||
return list;
|
||||
}
|
||||
|
||||
static Future<List<DeviceModel>> fetchDevicesByUnitId() async {
|
||||
static Future<List<DeviceModel>> fetchDevices(projectUuid) async {
|
||||
List<DeviceModel> list = [];
|
||||
|
||||
await _httpService.get(
|
||||
path: ApiEndpoints.devices.replaceAll("{projectUuid}", projectUuid),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
json.forEach((value) {
|
||||
list.add(DeviceModel.fromJson(value));
|
||||
});
|
||||
});
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
static Future<List<DeviceModel>> fetchDevicesByUnitId(
|
||||
String projectUuid) async {
|
||||
List<DeviceModel> list = [];
|
||||
|
||||
try {
|
||||
@ -40,7 +56,7 @@ class HomeManagementAPI {
|
||||
final path = ApiEndpoints.spaceDevices
|
||||
.replaceAll('{communityUuid}', communityUuid)
|
||||
.replaceAll('{spaceUuid}', spaceUuid)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev);
|
||||
.replaceAll('{projectUuid}', projectUuid);
|
||||
|
||||
await _httpService.get(
|
||||
path: path,
|
||||
@ -62,12 +78,16 @@ class HomeManagementAPI {
|
||||
return list;
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> assignDeviceToRoom(String communityId,
|
||||
String spaceId, String subSpaceId, String deviceId) async {
|
||||
static Future<Map<String, dynamic>> assignDeviceToRoom(
|
||||
String communityId,
|
||||
String spaceId,
|
||||
String subSpaceId,
|
||||
String deviceId,
|
||||
String projectId) async {
|
||||
try {
|
||||
final response = await _httpService.post(
|
||||
path: ApiEndpoints.assignDeviceToRoom
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev)
|
||||
.replaceAll('{projectUuid}', projectId)
|
||||
.replaceAll('{communityUuid}', communityId)
|
||||
.replaceAll('{spaceUuid}', spaceId)
|
||||
.replaceAll('{subSpaceUuid}', subSpaceId)
|
||||
@ -82,8 +102,12 @@ class HomeManagementAPI {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> unAssignDeviceToRoom(String communityId,
|
||||
String spaceId, String subSpaceId, String deviceId) async {
|
||||
static Future<Map<String, dynamic>> unAssignDeviceToRoom(
|
||||
String communityId,
|
||||
String spaceId,
|
||||
String subSpaceId,
|
||||
String deviceId,
|
||||
String projectId) async {
|
||||
try {
|
||||
final response = await _httpService.delete(
|
||||
path: ApiEndpoints.assignDeviceToRoom
|
||||
@ -91,7 +115,7 @@ class HomeManagementAPI {
|
||||
.replaceAll('{spaceUuid}', spaceId)
|
||||
.replaceAll('{subSpaceUuid}', subSpaceId)
|
||||
.replaceAll('{deviceUuid}', deviceId)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev),
|
||||
.replaceAll('{projectUuid}', projectId),
|
||||
expectedResponseModel: (json) {
|
||||
return json;
|
||||
},
|
||||
|
@ -6,7 +6,6 @@ import 'package:syncrow_app/features/scene/model/scenes_model.dart';
|
||||
import 'package:syncrow_app/features/scene/model/update_automation.dart';
|
||||
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
|
||||
import 'package:syncrow_app/services/api/http_service.dart';
|
||||
import 'package:syncrow_app/utils/constants/temp_const.dart';
|
||||
|
||||
class SceneApi {
|
||||
static final HTTPService _httpService = HTTPService();
|
||||
@ -31,10 +30,11 @@ class SceneApi {
|
||||
|
||||
// create automation
|
||||
static Future<Map<String, dynamic>> createAutomation(
|
||||
CreateAutomationModel createAutomationModel) async {
|
||||
CreateAutomationModel createAutomationModel, String projectId) async {
|
||||
try {
|
||||
final response = await _httpService.post(
|
||||
path: ApiEndpoints.createAutomation,
|
||||
path:
|
||||
ApiEndpoints.createAutomation.replaceAll('{projectId}', projectId),
|
||||
body: createAutomationModel.toMap(),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
@ -50,14 +50,14 @@ class SceneApi {
|
||||
//get scene by unit id
|
||||
|
||||
static Future<List<ScenesModel>> getScenesByUnitId(
|
||||
String unitId, String communityId,
|
||||
String unitId, String communityId, String projectId,
|
||||
{showInDevice = false}) async {
|
||||
try {
|
||||
final response = await _httpService.get(
|
||||
path: ApiEndpoints.getUnitScenes
|
||||
.replaceAll('{spaceUuid}', unitId)
|
||||
.replaceAll('{communityUuid}', communityId)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev),
|
||||
.replaceAll('{projectUuid}', projectId),
|
||||
queryParameters: {'showInHomePage': showInDevice},
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
@ -78,10 +78,17 @@ class SceneApi {
|
||||
|
||||
//getAutomation
|
||||
|
||||
static Future<List<ScenesModel>> getAutomationByUnitId(String unitId) async {
|
||||
static Future<List<ScenesModel>> getAutomationByUnitId(
|
||||
String unitId,
|
||||
String communityId,
|
||||
String projectId,
|
||||
) async {
|
||||
try {
|
||||
final response = await _httpService.get(
|
||||
path: ApiEndpoints.getUnitAutomation.replaceAll('{unitUuid}', unitId),
|
||||
path: ApiEndpoints.getUnitAutomation
|
||||
.replaceAll('{unitUuid}', unitId)
|
||||
.replaceAll('{communityId}', communityId)
|
||||
.replaceAll('{projectId}', projectId),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
List<ScenesModel> scenes = [];
|
||||
@ -112,11 +119,12 @@ class SceneApi {
|
||||
|
||||
//automation details
|
||||
static Future<SceneDetailsModel> getAutomationDetails(
|
||||
String automationId) async {
|
||||
String automationId, String projectId) async {
|
||||
try {
|
||||
final response = await _httpService.get(
|
||||
path: ApiEndpoints.getAutomationDetails
|
||||
.replaceAll('{automationId}', automationId),
|
||||
.replaceAll('{automationId}', automationId)
|
||||
.replaceAll('{projectId}', projectId),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) => SceneDetailsModel.fromJson(json),
|
||||
);
|
||||
@ -128,11 +136,12 @@ class SceneApi {
|
||||
|
||||
//updateAutomationStatus
|
||||
static Future<bool> updateAutomationStatus(String automationId,
|
||||
AutomationStatusUpdate createAutomationEnable) async {
|
||||
AutomationStatusUpdate createAutomationEnable, String projectId) async {
|
||||
try {
|
||||
final response = await _httpService.put(
|
||||
final response = await _httpService.patch(
|
||||
path: ApiEndpoints.updateAutomationStatus
|
||||
.replaceAll('{automationId}', automationId),
|
||||
.replaceAll('{automationId}', automationId)
|
||||
.replaceAll('{projectId}', projectId),
|
||||
body: createAutomationEnable.toMap(),
|
||||
expectedResponseModel: (json) => json['success'],
|
||||
);
|
||||
@ -194,12 +203,13 @@ class SceneApi {
|
||||
}
|
||||
|
||||
//update automation
|
||||
static updateAutomation(
|
||||
CreateAutomationModel createAutomationModel, String automationId) async {
|
||||
static updateAutomation(CreateAutomationModel createAutomationModel,
|
||||
String automationId, String projectId) async {
|
||||
try {
|
||||
final response = await _httpService.put(
|
||||
path: ApiEndpoints.updateAutomation
|
||||
.replaceAll('{automationId}', automationId),
|
||||
.replaceAll('{automationId}', automationId)
|
||||
.replaceAll('{projectId}', projectId),
|
||||
body: createAutomationModel
|
||||
.toJson(automationId.isNotEmpty == true ? automationId : null),
|
||||
expectedResponseModel: (json) {
|
||||
@ -229,11 +239,14 @@ class SceneApi {
|
||||
|
||||
// delete automation
|
||||
static Future<bool> deleteAutomation(
|
||||
{required String unitUuid, required String automationId}) async {
|
||||
{required String unitUuid,
|
||||
required String automationId,
|
||||
required String projectId}) async {
|
||||
try {
|
||||
final response = await _httpService.delete(
|
||||
path: ApiEndpoints.deleteAutomation
|
||||
.replaceAll('{automationId}', automationId),
|
||||
.replaceAll('{automationId}', automationId)
|
||||
.replaceAll('{projectId}', projectId),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) => json['statusCode'] == 200,
|
||||
);
|
||||
|
@ -32,13 +32,13 @@ class SpacesAPI {
|
||||
}
|
||||
|
||||
static Future<List<SubSpaceModel>> getSubSpaceBySpaceId(
|
||||
String communityId, String spaceId) async {
|
||||
String communityId, String spaceId, String projectId) async {
|
||||
try {
|
||||
// Construct the API path
|
||||
final path = ApiEndpoints.listSubspace
|
||||
.replaceFirst('{communityUuid}', communityId)
|
||||
.replaceFirst('{spaceUuid}', spaceId)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev);
|
||||
.replaceAll('{projectUuid}', projectId);
|
||||
|
||||
final response = await _httpService.get(
|
||||
path: path,
|
||||
@ -66,12 +66,12 @@ class SpacesAPI {
|
||||
//factory/reset/{deviceUuid}
|
||||
|
||||
static Future<String> generateInvitationCode(
|
||||
String unitId, String communityId) async {
|
||||
String unitId, String communityId, String projectId) async {
|
||||
final response = await _httpService.post(
|
||||
path: ApiEndpoints.invitationCode
|
||||
.replaceAll('{unitUuid}', unitId)
|
||||
.replaceAll('{communityUuid}', communityId)
|
||||
.replaceAll('{projectUuid}', TempConst.projectIdDev),
|
||||
.replaceAll('{projectUuid}', projectId),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
if (json != null && json['data'] != null) {
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
class TempConst {
|
||||
static const projectIdDev = '0e62577c-06fa-41b9-8a92-99a21fbaf51c';
|
||||
static const projectIdStag = 'bcda711e-9fc2-4168-a05e-171b4026d1ff';
|
||||
static String projectIdDev = dotenv.env['PROJECT_ID'] ?? '';
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ description: This is the mobile application project, developed with Flutter for
|
||||
# pub.dev using `flutter pub publish`. This is preferred for private packages.
|
||||
publish_to: "none" # Remove this line if you wish to publish to pub.dev
|
||||
|
||||
version: 1.0.18+55
|
||||
version: 1.0.30+1
|
||||
|
||||
environment:
|
||||
sdk: ">=3.0.6 <4.0.0"
|
||||
|
Reference in New Issue
Block a user