mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
88 lines
3.3 KiB
Dart
88 lines
3.3 KiB
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/wall_sensor/bloc/event.dart';
|
|
import 'package:syncrow_web/pages/device_managment/wall_sensor/bloc/state.dart';
|
|
import 'package:syncrow_web/pages/device_managment/wall_sensor/model/wall_sensor_model.dart';
|
|
import 'package:syncrow_web/services/devices_mang_api.dart';
|
|
|
|
class WallSensorBloc extends Bloc<WallSensorEvent, WallSensorState> {
|
|
final String deviceId;
|
|
late WallSensorModel deviceStatus;
|
|
|
|
WallSensorBloc({required this.deviceId}) : super(InitialState()) {
|
|
on<WallSensorInitialEvent>(_fetchCeilingSensorStatus);
|
|
on<ChangeIndicatorEvent>(_changeIndicator);
|
|
on<ChangeValueEvent>(_changeValue);
|
|
on<WallSensorUpdatedEvent>(_wallSensorUpdated);
|
|
}
|
|
|
|
void _fetchCeilingSensorStatus(
|
|
WallSensorInitialEvent event, Emitter<WallSensorState> emit) async {
|
|
emit(LoadingInitialState());
|
|
try {
|
|
var response = await DevicesManagementApi().getDeviceStatus(deviceId);
|
|
deviceStatus = WallSensorModel.fromJson(response.status);
|
|
emit(UpdateState(wallSensorModel: deviceStatus));
|
|
// _listenToChanges();
|
|
} catch (e) {
|
|
emit(FailedState(error: e.toString()));
|
|
return;
|
|
}
|
|
}
|
|
|
|
// _listenToChanges() {
|
|
// 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<StatusModel> statusList = [];
|
|
|
|
// usersMap['status'].forEach((element) {
|
|
// statusList.add(StatusModel(code: element['code'], value: element['value']));
|
|
// });
|
|
|
|
// deviceStatus = WallSensorModel.fromJson(statusList);
|
|
// add(WallSensorUpdatedEvent());
|
|
// });
|
|
// } catch (_) {}
|
|
// }
|
|
|
|
_wallSensorUpdated(WallSensorUpdatedEvent event, Emitter<WallSensorState> emit) {
|
|
emit(UpdateState(wallSensorModel: deviceStatus));
|
|
}
|
|
|
|
void _changeIndicator(ChangeIndicatorEvent event, Emitter<WallSensorState> emit) async {
|
|
emit(LoadingNewSate(wallSensorModel: deviceStatus));
|
|
try {
|
|
final response = await DevicesManagementApi()
|
|
.deviceControl(deviceId, Status(code: 'indicator', value: !event.value));
|
|
|
|
if (response) {
|
|
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 DevicesManagementApi()
|
|
.deviceControl(deviceId, Status(code: event.code, value: event.value));
|
|
|
|
if (response) {
|
|
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));
|
|
}
|
|
}
|