mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 14:47:23 +00:00
193 lines
5.5 KiB
Dart
193 lines
5.5 KiB
Dart
import 'dart:async';
|
|
import 'package:firebase_database/firebase_database.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
|
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_event.dart';
|
|
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_state.dart';
|
|
import 'package:syncrow_web/services/devices_mang_api.dart';
|
|
|
|
class CurtainBloc extends Bloc<CurtainEvent, CurtainState> {
|
|
late bool deviceStatus;
|
|
final String deviceId;
|
|
Timer? _timer;
|
|
|
|
CurtainBloc({required this.deviceId}) : super(CurtainInitial()) {
|
|
on<CurtainFetchDeviceStatus>(_onFetchDeviceStatus);
|
|
on<CurtainFetchBatchStatus>(_onFetchBatchStatus);
|
|
on<CurtainControl>(_onCurtainControl);
|
|
on<CurtainBatchControl>(_onCurtainBatchControl);
|
|
on<CurtainFactoryReset>(_onFactoryReset);
|
|
}
|
|
|
|
FutureOr<void> _onFetchDeviceStatus(
|
|
CurtainFetchDeviceStatus event, Emitter<CurtainState> emit) async {
|
|
emit(CurtainStatusLoading());
|
|
try {
|
|
final status =
|
|
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
|
|
|
deviceStatus = _checkStatus(status.status[0].value);
|
|
|
|
emit(CurtainStatusLoaded(deviceStatus));
|
|
} catch (e) {
|
|
emit(CurtainError(e.toString()));
|
|
}
|
|
}
|
|
// _listenToChanges(deviceId) {
|
|
// try {
|
|
// 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>;
|
|
|
|
// List<Status> statusList = [];
|
|
// usersMap['status'].forEach((element) {
|
|
// statusList
|
|
// .add(Status(code: element['code'], value: element['value']));
|
|
// });
|
|
|
|
// deviceStatus =
|
|
// GarageDoorStatusModel.fromJson(usersMap['productUuid'], statusList);
|
|
// if (!isClosed) {
|
|
// add(StatusUpdated(deviceStatus));
|
|
// }
|
|
// });
|
|
// } catch (_) {}
|
|
// }
|
|
|
|
// void _onStatusUpdated(StatusUpdated event, Emitter<GarageDoorState> emit) {
|
|
// deviceStatus = event.deviceStatus;
|
|
// emit(GarageDoorLoadedState(status: deviceStatus));
|
|
// }
|
|
|
|
|
|
FutureOr<void> _onCurtainControl(
|
|
CurtainControl event, Emitter<CurtainState> emit) async {
|
|
final oldValue = deviceStatus;
|
|
|
|
_updateLocalValue(event.value, emit);
|
|
|
|
emit(CurtainStatusLoaded(deviceStatus));
|
|
|
|
await _runDebounce(
|
|
deviceId: event.deviceId,
|
|
code: event.code,
|
|
value: event.value,
|
|
oldValue: oldValue,
|
|
emit: emit,
|
|
isBatch: false,
|
|
);
|
|
}
|
|
|
|
Future<void> _runDebounce({
|
|
required dynamic deviceId,
|
|
required String code,
|
|
required bool value,
|
|
required bool oldValue,
|
|
required Emitter<CurtainState> emit,
|
|
required bool isBatch,
|
|
}) async {
|
|
late String id;
|
|
|
|
if (deviceId is List) {
|
|
id = deviceId.first;
|
|
} else {
|
|
id = deviceId;
|
|
}
|
|
|
|
if (_timer != null) {
|
|
_timer!.cancel();
|
|
}
|
|
_timer = Timer(const Duration(seconds: 1), () async {
|
|
try {
|
|
final controlValue = value ? 'open' : 'close';
|
|
|
|
late bool response;
|
|
if (isBatch) {
|
|
response = await DevicesManagementApi()
|
|
.deviceBatchControl(deviceId, code, controlValue);
|
|
} else {
|
|
response = await DevicesManagementApi()
|
|
.deviceControl(deviceId, Status(code: code, value: controlValue));
|
|
}
|
|
|
|
if (!response) {
|
|
_revertValueAndEmit(id, oldValue, emit);
|
|
}
|
|
} catch (e) {
|
|
_revertValueAndEmit(id, oldValue, emit);
|
|
}
|
|
});
|
|
}
|
|
|
|
void _revertValueAndEmit(
|
|
String deviceId, bool oldValue, Emitter<CurtainState> emit) {
|
|
_updateLocalValue(oldValue, emit);
|
|
emit(CurtainStatusLoaded(deviceStatus));
|
|
emit(const CurtainControlError('Failed to control the device.'));
|
|
}
|
|
|
|
void _updateLocalValue(bool value, Emitter<CurtainState> emit) {
|
|
deviceStatus = value;
|
|
emit(CurtainStatusLoaded(deviceStatus));
|
|
}
|
|
|
|
bool _checkStatus(String command) {
|
|
return command.toLowerCase() == 'open';
|
|
}
|
|
|
|
FutureOr<void> _onFetchBatchStatus(
|
|
CurtainFetchBatchStatus event, Emitter<CurtainState> emit) async {
|
|
emit(CurtainStatusLoading());
|
|
try {
|
|
final status =
|
|
await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
|
|
|
deviceStatus = _checkStatus(status.status[0].value);
|
|
|
|
emit(CurtainStatusLoaded(deviceStatus));
|
|
} catch (e) {
|
|
emit(CurtainError(e.toString()));
|
|
}
|
|
}
|
|
|
|
FutureOr<void> _onCurtainBatchControl(
|
|
CurtainBatchControl event, Emitter<CurtainState> emit) async {
|
|
final oldValue = deviceStatus;
|
|
|
|
_updateLocalValue(event.value, emit);
|
|
|
|
emit(CurtainStatusLoaded(deviceStatus));
|
|
|
|
await _runDebounce(
|
|
deviceId: event.devicesIds,
|
|
code: event.code,
|
|
value: event.value,
|
|
oldValue: oldValue,
|
|
emit: emit,
|
|
isBatch: true,
|
|
);
|
|
}
|
|
|
|
FutureOr<void> _onFactoryReset(
|
|
CurtainFactoryReset event, Emitter<CurtainState> emit) async {
|
|
emit(CurtainStatusLoading());
|
|
try {
|
|
final response = await DevicesManagementApi().factoryReset(
|
|
event.factoryReset,
|
|
event.deviceId,
|
|
);
|
|
if (!response) {
|
|
emit(const CurtainControlError('Failed'));
|
|
} else {
|
|
add(CurtainFetchDeviceStatus(event.deviceId));
|
|
}
|
|
} catch (e) {
|
|
emit(CurtainControlError(e.toString()));
|
|
}
|
|
}
|
|
}
|