mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Add bloc closure handling and improve device status updates in AcBloc (#298)
<!-- Thanks for contributing! Provide a description of your changes below and a general summary in the title Please look at the following checklist to ensure that your PR can be accepted quickly: --> ## Description <!--- Describe your changes in detail --> Add bloc closure handling and improve device status updates in AcBloc ## Type of Change <!--- Put an `x` in all the boxes that apply: --> - [ ] ✨ New feature (non-breaking change which adds functionality) - [x] 🛠️ Bug fix (non-breaking change which fixes an issue) - [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change) - [ ] 🧹 Code refactor - [ ] ✅ Build configuration change - [ ] 📝 Documentation - [ ] 🗑️ Chore
This commit is contained in:
@ -45,7 +45,8 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
) async {
|
||||
emit(AcsLoadingState());
|
||||
try {
|
||||
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||
final status =
|
||||
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||
deviceStatus = AcStatusModel.fromJson(event.deviceId, status.status);
|
||||
if (deviceStatus.countdown1 != 0) {
|
||||
final totalMinutes = deviceStatus.countdown1 * 6;
|
||||
@ -68,12 +69,13 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _listenToChanges(deviceId) {
|
||||
StreamSubscription<DatabaseEvent>? _deviceStatusSubscription;
|
||||
|
||||
void _listenToChanges(String deviceId) {
|
||||
try {
|
||||
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||
final stream = ref.onValue;
|
||||
|
||||
stream.listen((DatabaseEvent event) async {
|
||||
_deviceStatusSubscription =
|
||||
ref.onValue.listen((DatabaseEvent event) async {
|
||||
if (event.snapshot.value == null) return;
|
||||
|
||||
Map<dynamic, dynamic> usersMap =
|
||||
@ -82,10 +84,14 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
List<Status> statusList = [];
|
||||
|
||||
usersMap['status'].forEach((element) {
|
||||
statusList.add(Status(code: element['code'], value: element['value']));
|
||||
statusList
|
||||
.add(Status(code: element['code'], value: element['value']));
|
||||
});
|
||||
|
||||
deviceStatus = AcStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||
deviceStatus =
|
||||
AcStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||
print('Device status updated: ${deviceStatus.acSwitch}');
|
||||
|
||||
if (!isClosed) {
|
||||
add(AcStatusUpdated(deviceStatus));
|
||||
}
|
||||
@ -106,15 +112,14 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
Emitter<AcsState> emit,
|
||||
) async {
|
||||
emit(AcsLoadingState());
|
||||
_updateDeviceFunctionFromCode(event.code, event.value);
|
||||
emit(ACStatusLoaded(status: deviceStatus));
|
||||
|
||||
try {
|
||||
final success = await controlDeviceService.controlDevice(
|
||||
deviceUuid: event.deviceId,
|
||||
status: Status(code: event.code, value: event.value),
|
||||
);
|
||||
|
||||
_updateDeviceFunctionFromCode(event.code, event.value);
|
||||
emit(ACStatusLoaded(status: deviceStatus));
|
||||
if (!success) {
|
||||
emit(const AcsFailedState(error: 'Failed to control device'));
|
||||
}
|
||||
@ -129,8 +134,10 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
) async {
|
||||
emit(AcsLoadingState());
|
||||
try {
|
||||
final status = await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
||||
deviceStatus = AcStatusModel.fromJson(event.devicesIds.first, status.status);
|
||||
final status =
|
||||
await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
||||
deviceStatus =
|
||||
AcStatusModel.fromJson(event.devicesIds.first, status.status);
|
||||
emit(ACStatusLoaded(status: deviceStatus));
|
||||
} catch (e) {
|
||||
emit(AcsFailedState(error: e.toString()));
|
||||
@ -293,13 +300,17 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
totalSeconds--;
|
||||
scheduledHours = totalSeconds ~/ 3600;
|
||||
scheduledMinutes = (totalSeconds % 3600) ~/ 60;
|
||||
add(UpdateTimerEvent());
|
||||
if (!isClosed) {
|
||||
add(UpdateTimerEvent());
|
||||
}
|
||||
} else {
|
||||
_countdownTimer?.cancel();
|
||||
timerActive = false;
|
||||
scheduledHours = 0;
|
||||
scheduledMinutes = 0;
|
||||
add(TimerCompletedEvent());
|
||||
if (!isClosed) {
|
||||
add(TimerCompletedEvent());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -326,7 +337,9 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
_startCountdownTimer(
|
||||
emit,
|
||||
);
|
||||
add(UpdateTimerEvent());
|
||||
if (!isClosed) {
|
||||
add(UpdateTimerEvent());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -370,6 +383,8 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
@override
|
||||
Future<void> close() {
|
||||
add(OnClose());
|
||||
_countdownTimer?.cancel();
|
||||
_deviceStatusSubscription?.cancel();
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user