mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 21:17:55 +00:00
Added smart door bloc
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
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/bloc/smart_door_bloc/smart_door_event.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/smart_door_bloc/smart_door_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/smart_door_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 SmartDoorBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
|
||||
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);
|
||||
on<InitialEvent>(_fetchSmartDoorStatus);
|
||||
on<UpdateLockEvent>(_updateLock);
|
||||
}
|
||||
|
||||
void _fetchThreeGangStatus(InitialEvent event, Emitter<ThreeGangState> emit) async {
|
||||
void _fetchSmartDoorStatus(InitialEvent event, Emitter<SmartDoorState> emit) async {
|
||||
emit(LoadingInitialState());
|
||||
try {
|
||||
var response = await DevicesAPI.getDeviceStatus(deviceId);
|
||||
@@ -29,94 +24,24 @@ class SmartDoorBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
|
||||
statusModelList.add(StatusModel.fromJson(status));
|
||||
}
|
||||
deviceStatus = SmartDoorModel.fromJson(statusModelList);
|
||||
emit(UpdateState(threeGangModel: deviceStatus));
|
||||
emit(UpdateState(smartDoorModel: deviceStatus));
|
||||
} catch (e) {
|
||||
emit(FailedState(error: e.toString()));
|
||||
emit(FailedState(errorMessage: e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
|
||||
emit(LoadingNewSate(threeGangModel: deviceStatus));
|
||||
void _updateLock(UpdateLockEvent event, Emitter<SmartDoorState> emit) async {
|
||||
emit(LoadingNewSate(smartDoorModel: deviceStatus));
|
||||
try {
|
||||
final response = await DevicesAPI.controlDevice(
|
||||
DeviceControlModel(deviceId: deviceId, code: 'switch_1', value: !event.value), deviceId);
|
||||
DeviceControlModel(deviceId: deviceId, code: 'normal_open_switch', value: !event.value),
|
||||
deviceId);
|
||||
|
||||
if (response['success'] ?? false) {
|
||||
deviceStatus.firstSwitch = !event.value;
|
||||
deviceStatus.normalOpenSwitch = !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));
|
||||
emit(UpdateState(smartDoorModel: deviceStatus));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
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';
|
||||
import 'package:syncrow_app/features/devices/model/smart_door_model.dart';
|
||||
|
||||
class SmartDoorState extends Equatable {
|
||||
const SmartDoorState();
|
||||
@@ -30,10 +29,10 @@ class LoadingNewSate extends SmartDoorState {
|
||||
}
|
||||
|
||||
class FailedState extends SmartDoorState {
|
||||
final String error;
|
||||
final String errorMessage;
|
||||
|
||||
const FailedState({required this.error});
|
||||
const FailedState({required this.errorMessage});
|
||||
|
||||
@override
|
||||
List<Object> get props => [error];
|
||||
List<Object> get props => [errorMessage];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user