import 'dart:async'; 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 { final String deviceId; late WallSensorModel deviceStatus; Timer? _timer; WallSensorBloc({required this.deviceId}) : super(WallSensorInitialState()) { on(_fetchWallSensorStatus); on(_fetchWallSensorBatchControl); on(_changeValue); on(_onBatchControl); on(_getDeviceReports); on(_showDescription); on(_backToGridView); } void _fetchWallSensorStatus( WallSensorFetchStatusEvent event, Emitter emit) async { emit(WallSensorLoadingInitialState()); try { var response = await DevicesManagementApi().getDeviceStatus(deviceId); deviceStatus = WallSensorModel.fromJson(response.status); emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); // _listenToChanges(); } catch (e) { emit(WallSensorFailedState(error: e.toString())); return; } } // Fetch batch status FutureOr _fetchWallSensorBatchControl( WallSensorFetchBatchStatusEvent event, Emitter emit) async { emit(WallSensorLoadingInitialState()); try { var response = await DevicesManagementApi().getBatchStatus(event.devicesIds); deviceStatus = WallSensorModel.fromJson(response.status); emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); } catch (e) { emit(WallSensorFailedState(error: e.toString())); } } // _listenToChanges() { // try { // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId'); // Stream stream = ref.onValue; // stream.listen((DatabaseEvent event) { // Map usersMap = event.snapshot.value as Map; // List statusList = []; // usersMap['status'].forEach((element) { // statusList.add(StatusModel(code: element['code'], value: element['value'])); // }); // deviceStatus = WallSensorModel.fromJson(statusList); // add(WallSensorUpdatedEvent()); // }); // } catch (_) {} // } void _changeValue( WallSensorChangeValueEvent event, Emitter emit) async { emit(WallSensorLoadingNewSate(wallSensorModel: deviceStatus)); 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; } else if (event.code == 'no_one_time') { deviceStatus.noBodyTime = event.value; } emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); await _runDeBouncer( deviceId: deviceId, code: event.code, value: event.value, isBatch: false, emit: emit, ); } Future _onBatchControl( WallSensorBatchControlEvent event, Emitter emit) async { emit(WallSensorLoadingNewSate(wallSensorModel: deviceStatus)); 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; } else if (event.code == 'no_one_time') { deviceStatus.noBodyTime = event.value; } emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); await _runDeBouncer( deviceId: event.deviceIds, code: event.code, value: event.value, emit: emit, isBatch: true, ); } _runDeBouncer({ required dynamic deviceId, required String code, required dynamic value, required Emitter emit, required bool isBatch, }) { if (_timer != null) { _timer!.cancel(); } _timer = Timer(const Duration(seconds: 1), () async { try { late bool response; if (isBatch) { response = await DevicesManagementApi() .deviceBatchControl(deviceId, code, value); } else { response = await DevicesManagementApi() .deviceControl(deviceId, Status(code: code, value: value)); } if (!response) { add(WallSensorFetchStatusEvent()); } } catch (_) { await Future.delayed(const Duration(milliseconds: 500)); add(WallSensorFetchStatusEvent()); } }); } FutureOr _getDeviceReports( GetDeviceReportsEvent event, Emitter emit) async { emit(DeviceReportsLoadingState()); try { await DevicesManagementApi.getDeviceReports(deviceId, event.code) .then((value) { emit(DeviceReportsState(deviceReport: value, code: event.code)); }); } catch (e) { emit(DeviceReportsFailedState(error: e.toString())); return; } } void _showDescription( ShowDescriptionEvent event, Emitter emit) { emit(WallSensorShowDescriptionState(description: event.description)); } void _backToGridView( BackToGridViewEvent event, Emitter emit) { emit(WallSensorUpdateState(wallSensorModel: deviceStatus)); } }