Implemented ceiling, wall sensors functionality

This commit is contained in:
Abdullah Alassaf
2024-05-22 23:55:35 +03:00
parent 008ba8be32
commit 44b08c482d
26 changed files with 1296 additions and 493 deletions
@@ -0,0 +1,48 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/ceiling_bloc/ceiling_sensor_event.dart';
import 'package:syncrow_app/features/devices/bloc/ceiling_bloc/ceiling_sensor_state.dart';
import 'package:syncrow_app/features/devices/model/ceiling_sensor_model.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
class CeilingSensorBloc extends Bloc<CeilingSensorEvent, CeilingSensorState> {
final String deviceId;
late DeviceModel deviceModel;
late CeilingSensorModel deviceStatus;
CeilingSensorBloc({required this.deviceId}) : super(InitialState()) {
on<InitialEvent>(_fetchCeilingSensorStatus);
on<ChangeValueEvent>(_changeValue);
}
void _fetchCeilingSensorStatus(InitialEvent event, Emitter<CeilingSensorState> emit) async {
emit(LoadingInitialState());
try {
var response = await DevicesAPI.getDeviceStatus(deviceId);
List<StatusModel> statusModelList = [];
for (var status in response['status']) {
statusModelList.add(StatusModel.fromJson(status));
}
deviceStatus = CeilingSensorModel.fromJson(statusModelList);
emit(UpdateState(ceilingSensorModel: deviceStatus));
} catch (e) {
emit(FailedState(error: e.toString()));
return;
}
}
void _changeValue(ChangeValueEvent event, Emitter<CeilingSensorState> emit) async {
emit(LoadingNewSate(ceilingSensorModel: deviceStatus));
try {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: event.code, value: event.value), deviceId);
if (response['success'] ?? false) {
deviceStatus.sensitivity = event.value;
}
} catch (_) {}
emit(UpdateState(ceilingSensorModel: deviceStatus));
}
}
@@ -0,0 +1,21 @@
import 'package:equatable/equatable.dart';
abstract class CeilingSensorEvent extends Equatable {
const CeilingSensorEvent();
@override
List<Object> get props => [];
}
class LoadingEvent extends CeilingSensorEvent {}
class InitialEvent extends CeilingSensorEvent {}
class ChangeValueEvent extends CeilingSensorEvent {
final int value;
final String code;
const ChangeValueEvent({required this.value, required this.code});
@override
List<Object> get props => [value, code];
}
@@ -0,0 +1,38 @@
import 'package:equatable/equatable.dart';
import 'package:syncrow_app/features/devices/model/ceiling_sensor_model.dart';
class CeilingSensorState extends Equatable {
const CeilingSensorState();
@override
List<Object> get props => [];
}
class InitialState extends CeilingSensorState {}
class LoadingInitialState extends CeilingSensorState {}
class UpdateState extends CeilingSensorState {
final CeilingSensorModel ceilingSensorModel;
const UpdateState({required this.ceilingSensorModel});
@override
List<Object> get props => [ceilingSensorModel];
}
class LoadingNewSate extends CeilingSensorState {
final CeilingSensorModel ceilingSensorModel;
const LoadingNewSate({required this.ceilingSensorModel});
@override
List<Object> get props => [ceilingSensorModel];
}
class FailedState extends CeilingSensorState {
final String error;
const FailedState({required this.error});
@override
List<Object> get props => [error];
}
@@ -0,0 +1,122 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_event.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_state.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/devices/model/smart_door.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/features/devices/model/three_gang_model.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
class SmartDoorBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
final String deviceId;
late SmartDoorModel deviceStatus;
SmartDoorBloc({required this.deviceId}) : super(InitialState()) {
on<InitialEvent>(_fetchThreeGangStatus);
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
on<ChangeSecondSwitchStatusEvent>(_changeSecondSwitch);
on<ChangeThirdSwitchStatusEvent>(_changeThirdSwitch);
on<AllOffEvent>(_allOff);
on<AllOnEvent>(_allOn);
}
void _fetchThreeGangStatus(InitialEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingInitialState());
try {
var response = await DevicesAPI.getDeviceStatus(deviceId);
List<StatusModel> statusModelList = [];
for (var status in response['status']) {
statusModelList.add(StatusModel.fromJson(status));
}
deviceStatus = SmartDoorModel.fromJson(statusModelList);
emit(UpdateState(threeGangModel: deviceStatus));
} catch (e) {
emit(FailedState(error: e.toString()));
return;
}
}
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_1', value: !event.value), deviceId);
if (response['success'] ?? false) {
deviceStatus.firstSwitch = !event.value;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _changeSecondSwitch(
ChangeSecondSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_2', value: !event.value), deviceId);
if (response['success'] ?? false) {
deviceStatus.secondSwitch = !event.value;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_3', value: !event.value), deviceId);
if (response['success'] ?? false) {
deviceStatus.thirdSwitch = !event.value;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _allOff(AllOffEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_1', value: false), deviceId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_2', value: false), deviceId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_3', value: false), deviceId),
]);
if (response.every((element) => element['success'] ?? false)) {
deviceStatus.firstSwitch = false;
deviceStatus.secondSwitch = false;
deviceStatus.thirdSwitch = false;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _allOn(AllOnEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_1', value: true), deviceId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_2', value: true), deviceId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: deviceId, code: 'switch_3', value: true), deviceId),
]);
if (response.every((element) => element['success'] ?? false)) {
deviceStatus.firstSwitch = true;
deviceStatus.secondSwitch = true;
deviceStatus.thirdSwitch = true;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
}
@@ -0,0 +1,17 @@
import 'package:equatable/equatable.dart';
abstract class SmartDoorEvent extends Equatable {
const SmartDoorEvent();
@override
List<Object> get props => [];
}
class InitialEvent extends SmartDoorEvent {}
class UpdateLockEvent extends SmartDoorEvent {
final bool value;
const UpdateLockEvent({required this.value});
@override
List<Object> get props => [value];
}
@@ -0,0 +1,39 @@
import 'package:equatable/equatable.dart';
import 'package:syncrow_app/features/devices/model/smart_door.dart';
import 'package:syncrow_app/features/devices/model/three_gang_model.dart';
class SmartDoorState extends Equatable {
const SmartDoorState();
@override
List<Object> get props => [];
}
class InitialState extends SmartDoorState {}
class LoadingInitialState extends SmartDoorState {}
class UpdateState extends SmartDoorState {
final SmartDoorModel smartDoorModel;
const UpdateState({required this.smartDoorModel});
@override
List<Object> get props => [smartDoorModel];
}
class LoadingNewSate extends SmartDoorState {
final SmartDoorModel smartDoorModel;
const LoadingNewSate({required this.smartDoorModel});
@override
List<Object> get props => [smartDoorModel];
}
class FailedState extends SmartDoorState {
final String error;
const FailedState({required this.error});
@override
List<Object> get props => [error];
}
@@ -1,22 +1,26 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_event.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/features/devices/model/three_gang_model.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
final String threeGangId;
late DeviceModel deviceModel;
late ThreeGangModel deviceStatus;
ThreeGangBloc({required this.threeGangId}) : super(InitialState()) {
on<InitialEvent>(_fetchThreeGangStatus);
on<ChangeFirstSwitchStatusEvent>(_changeFirstSwitch);
on<ChangeSecondSwitchStatusEvent>(_changeSecondSwitch);
on<ChangeThirdSwitchStatusEvent>(_changeThirdSwitch);
on<AllOffEvent>(_allOff);
on<AllOnEvent>(_allOn);
}
void _fetchThreeGangStatus(InitialEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingState());
emit(LoadingInitialState());
try {
var response = await DevicesAPI.getDeviceStatus(threeGangId);
List<StatusModel> statusModelList = [];
@@ -30,4 +34,91 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
return;
}
}
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: !event.value),
threeGangId);
if (response['success'] ?? false) {
deviceStatus.firstSwitch = !event.value;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _changeSecondSwitch(
ChangeSecondSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: !event.value),
threeGangId);
if (response['success'] ?? false) {
deviceStatus.secondSwitch = !event.value;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_3', value: !event.value),
threeGangId);
if (response['success'] ?? false) {
deviceStatus.thirdSwitch = !event.value;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _allOff(AllOffEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: false), threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: false), threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_3', value: false), threeGangId),
]);
if (response.every((element) => element['success'] ?? false)) {
deviceStatus.firstSwitch = false;
deviceStatus.secondSwitch = false;
deviceStatus.thirdSwitch = false;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
void _allOn(AllOnEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: true), threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: true), threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_3', value: true), threeGangId),
]);
if (response.every((element) => element['success'] ?? false)) {
deviceStatus.firstSwitch = true;
deviceStatus.secondSwitch = true;
deviceStatus.thirdSwitch = true;
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
}
}
@@ -11,4 +11,27 @@ class LoadingEvent extends ThreeGangEvent {}
class InitialEvent extends ThreeGangEvent {}
class ChangeStatusEvent extends ThreeGangEvent {}
class ChangeFirstSwitchStatusEvent extends ThreeGangEvent {
final bool value;
const ChangeFirstSwitchStatusEvent({required this.value});
@override
List<Object> get props => [value];
}
class ChangeSecondSwitchStatusEvent extends ThreeGangEvent {
final bool value;
const ChangeSecondSwitchStatusEvent({required this.value});
@override
List<Object> get props => [value];
}
class ChangeThirdSwitchStatusEvent extends ThreeGangEvent {
final bool value;
const ChangeThirdSwitchStatusEvent({required this.value});
@override
List<Object> get props => [value];
}
class AllOffEvent extends ThreeGangEvent {}
class AllOnEvent extends ThreeGangEvent {}
@@ -10,7 +10,7 @@ class ThreeGangState extends Equatable {
class InitialState extends ThreeGangState {}
class LoadingState extends ThreeGangState {}
class LoadingInitialState extends ThreeGangState {}
class UpdateState extends ThreeGangState {
final ThreeGangModel threeGangModel;
@@ -20,6 +20,14 @@ class UpdateState extends ThreeGangState {
List<Object> get props => [threeGangModel];
}
class LoadingNewSate extends ThreeGangState {
final ThreeGangModel threeGangModel;
const LoadingNewSate({required this.threeGangModel});
@override
List<Object> get props => [threeGangModel];
}
class FailedState extends ThreeGangState {
final String error;
@@ -0,0 +1,68 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/wall_sensor_bloc/wall_sensor_state.dart';
import 'package:syncrow_app/features/devices/bloc/wall_sensor_bloc/wall_sensor_event.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/wall_sensor_model.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
final String deviceId;
late DeviceModel deviceModel;
late WallSensorModel deviceStatus;
WallSensorBloc({required this.deviceId}) : super(InitialState()) {
on<InitialEvent>(_fetchCeilingSensorStatus);
on<ChangeIndicatorEvent>(_changeIndicator);
on<ChangeValueEvent>(_changeValue);
}
void _fetchCeilingSensorStatus(InitialEvent event, Emitter<WallSensorState> emit) async {
emit(LoadingInitialState());
try {
var response = await DevicesAPI.getDeviceStatus(deviceId);
List<StatusModel> statusModelList = [];
for (var status in response['status']) {
statusModelList.add(StatusModel.fromJson(status));
}
deviceStatus = WallSensorModel.fromJson(statusModelList);
emit(UpdateState(wallSensorModel: deviceStatus));
} catch (e) {
emit(FailedState(error: e.toString()));
return;
}
}
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);
if (response['success'] ?? false) {
deviceStatus.indicator = !event.value;
}
} catch (_) {}
emit(UpdateState(wallSensorModel: deviceStatus));
}
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);
if (response['success'] ?? false) {
if (event.code == 'far_detection') {
deviceStatus.farDetection = event.value;
} else if (event.code == 'motionless_sensitivity') {
deviceStatus.motionlessSensitivity = event.value;
} else if (event.code == 'motion_sensitivity_value') {
deviceStatus.motionSensitivity = event.value;
}
}
} catch (_) {}
emit(UpdateState(wallSensorModel: deviceStatus));
}
}
@@ -0,0 +1,29 @@
import 'package:equatable/equatable.dart';
abstract class WallSensorEvent extends Equatable {
const WallSensorEvent();
@override
List<Object> get props => [];
}
class LoadingEvent extends WallSensorEvent {}
class InitialEvent extends WallSensorEvent {}
class ChangeIndicatorEvent extends WallSensorEvent {
final bool value;
const ChangeIndicatorEvent({required this.value});
@override
List<Object> get props => [value];
}
class ChangeValueEvent extends WallSensorEvent {
final int value;
final String code;
const ChangeValueEvent({required this.value, required this.code});
@override
List<Object> get props => [value, code];
}
@@ -0,0 +1,38 @@
import 'package:equatable/equatable.dart';
import 'package:syncrow_app/features/devices/model/wall_sensor_model.dart';
class WallSensorState extends Equatable {
const WallSensorState();
@override
List<Object> get props => [];
}
class InitialState extends WallSensorState {}
class LoadingInitialState extends WallSensorState {}
class UpdateState extends WallSensorState {
final WallSensorModel wallSensorModel;
const UpdateState({required this.wallSensorModel});
@override
List<Object> get props => [wallSensorModel];
}
class LoadingNewSate extends WallSensorState {
final WallSensorModel wallSensorModel;
const LoadingNewSate({required this.wallSensorModel});
@override
List<Object> get props => [wallSensorModel];
}
class FailedState extends WallSensorState {
final String error;
const FailedState({required this.error});
@override
List<Object> get props => [error];
}