From 0a97a4867dc5326a29b8771aed53cea0ce1ec3f8 Mon Sep 17 00:00:00 2001 From: mohammad Date: Tue, 25 Feb 2025 12:41:49 +0300 Subject: [PATCH] disconnect real-time to some devices --- .../devices/bloc/acs_bloc/acs_bloc.dart | 6 +- .../ceiling_bloc/ceiling_sensor_bloc.dart | 22 ---- .../bloc/curtain_bloc/curtain_bloc.dart | 59 +++------ lib/features/devices/bloc/devices_cubit.dart | 7 +- .../door_sensor_bloc/door_sensor_bloc.dart | 66 +++------- .../bloc/four_scene_bloc/four_scene_bloc.dart | 54 ++++---- .../garage_door_bloc/garage_door_bloc.dart | 64 +++------ .../bloc/one_gang_bloc/one_gang_bloc.dart | 63 +++------ .../bloc/one_touch_bloc/one_touch_bloc.dart | 121 +++++++++++------- .../bloc/smart_door_bloc/smart_door_bloc.dart | 19 +-- .../devices/bloc/sos_bloc/sos_bloc.dart | 56 +++----- .../bloc/three_gang_bloc/three_gang_bloc.dart | 28 ---- .../three_touch_bloc/three_touch_bloc.dart | 42 +++--- .../bloc/two_gang_bloc/two_gang_bloc.dart | 43 +++---- .../bloc/two_touch_bloc/two_touch_bloc.dart | 54 +++----- .../wall_sensor_bloc/wall_sensor_bloc.dart | 21 --- .../water_heater_bloc/water_heater_bloc.dart | 64 +++------ .../bloc/water_leak_bloc/water_leak_bloc.dart | 63 +++------ 18 files changed, 293 insertions(+), 559 deletions(-) diff --git a/lib/features/devices/bloc/acs_bloc/acs_bloc.dart b/lib/features/devices/bloc/acs_bloc/acs_bloc.dart index 7b47fce..b83da0b 100644 --- a/lib/features/devices/bloc/acs_bloc/acs_bloc.dart +++ b/lib/features/devices/bloc/acs_bloc/acs_bloc.dart @@ -73,7 +73,7 @@ class ACsBloc extends Bloc { AcStatusModel.fromJson(response['productUuid'], statusModelList); emit(GetAcStatusState(acStatusModel: deviceStatus)); Future.delayed(const Duration(milliseconds: 500)); - _listenToChanges(); + _listenToChanges(acId); } } catch (e) { emit(AcsFailedState(errorMessage: e.toString())); @@ -83,7 +83,7 @@ class ACsBloc extends Bloc { StreamSubscription? _streamSubscription; - void _listenToChanges() { + void _listenToChanges(acId) { try { _streamSubscription?.cancel(); DatabaseReference ref = @@ -125,6 +125,7 @@ class ACsBloc extends Bloc { HomeCubit.getInstance().selectedSpace?.id ?? '', 'AC'); for (int i = 0; i < devicesList.length; i++) { + _listenToChanges(devicesList[i].uuid); var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); List statusModelList = []; @@ -134,6 +135,7 @@ class ACsBloc extends Bloc { deviceStatusList.add( AcStatusModel.fromJson(response['productUuid'], statusModelList)); } + _setAllAcsTempsAndSwitches(); } diff --git a/lib/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart b/lib/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart index 895fb67..6140267 100644 --- a/lib/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart +++ b/lib/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart @@ -79,28 +79,6 @@ class CeilingSensorBloc extends Bloc { return super.close(); } - // _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 = CeilingSensorModel.fromJson(statusList); - // add(CeilingSensorUpdated()); - // }); - // } catch (_) {} - // } - _onCeilingSensorUpdated( CeilingSensorUpdated event, Emitter emit) { emit(UpdateState(ceilingSensorModel: deviceStatus)); diff --git a/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart b/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart index dc56b4e..bc6620e 100644 --- a/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart +++ b/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart @@ -1,6 +1,4 @@ import 'dart:async'; - -import 'package:firebase_database/firebase_database.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/curtain_bloc/curtain_event.dart'; @@ -172,7 +170,7 @@ class CurtainBloc extends Bloc { openPercentage = double.tryParse(statusModelList[1].value.toString())!; curtainWidth = 270 - (openPercentage / 100) * curtainOpeningSpace; blindHeight = 310 - (openPercentage / 100) * blindOpeningSpace; - _listenToChanges(); + // _listenToChanges(); emit(CurtainsOpening( curtainWidth: curtainWidth, blindHeight: blindHeight, @@ -183,61 +181,38 @@ class CurtainBloc extends Bloc { return; } } - - StreamSubscription? _streamSubscription; - Timer? _timer; - - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$curtainId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - deviceStatus = CurtainModel.fromJson(statusList); - if (!isClosed) {} - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } - - // _listenToChanges() { + // Real-time Database + // StreamSubscription? _streamSubscription; + // Timer? _timer; + // void _listenToChanges() { // try { + // _streamSubscription?.cancel(); // DatabaseReference ref = // FirebaseDatabase.instance.ref('device-status/$curtainId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) { + // _streamSubscription = stream.listen((DatabaseEvent event) async { + // if (_timer != null) { + // await Future.delayed(const Duration(seconds: 2)); + // } // Map usersMap = // event.snapshot.value as Map; // List statusList = []; - // usersMap['status'].forEach((element) { // statusList // .add(StatusModel(code: element['code'], value: element['value'])); // }); - // deviceStatus = CurtainModel.fromJson(statusList); + // if (!isClosed) {} // }); // } catch (_) {} // } + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } List groupList = []; bool allSwitchesOn = true; diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index f8726ab..57c4306 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -84,7 +84,7 @@ class DevicesCubit extends Cubit { } Map usersMap = event.snapshot.value as Map; - print('object-----${usersMap['status']}'); + //print('object-----${usersMap['status']}'); List statusList = []; usersMap['status'].forEach((element) { statusList @@ -456,8 +456,9 @@ class DevicesCubit extends Cubit { ); allDevices = devices; for (var deviceId in allDevices) { - - _listenToChanges(deviceId.uuid); + if (deviceId.type == "3G" || deviceId.type == "AC") { + _listenToChanges(deviceId.uuid); + } } emitSafe(GetDevicesSuccess(allDevices)); } catch (e) { diff --git a/lib/features/devices/bloc/door_sensor_bloc/door_sensor_bloc.dart b/lib/features/devices/bloc/door_sensor_bloc/door_sensor_bloc.dart index f0ad998..20f2758 100644 --- a/lib/features/devices/bloc/door_sensor_bloc/door_sensor_bloc.dart +++ b/lib/features/devices/bloc/door_sensor_bloc/door_sensor_bloc.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:dio/dio.dart'; -import 'package:firebase_database/firebase_database.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/devices/bloc/door_sensor_bloc/door_sensor_event.dart'; import 'package:syncrow_app/features/devices/bloc/door_sensor_bloc/door_sensor_state.dart'; @@ -21,7 +20,6 @@ class DoorSensorBloc extends Bloc { on(_toggleClosingReminder); on(_toggleDoorAlarm); } - Timer? _timer; bool lowBattery = false; bool closingReminder = false; bool doorAlarm = false; @@ -42,7 +40,7 @@ class DoorSensorBloc extends Bloc { ); emit(UpdateState(doorSensor: deviceStatus)); Future.delayed(const Duration(milliseconds: 500)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(DoorSensorFailedState(errorMessage: e.toString())); return; @@ -138,23 +136,27 @@ class DoorSensorBloc extends Bloc { String errorMessage = errorData['message']; } } - - // _listenToChanges() { + // real-time database + // Timer? _timer; + // StreamSubscription? _streamSubscription; + // void _listenToChanges() { // try { - // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$DSId'); + // _streamSubscription?.cancel(); + // DatabaseReference ref = + // FirebaseDatabase.instance.ref('device-status/$DSId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) async { + // _streamSubscription = stream.listen((DatabaseEvent event) async { // if (_timer != null) { // await Future.delayed(const Duration(seconds: 2)); // } - // Map usersMap = event.snapshot.value as Map; + // Map usersMap = + // event.snapshot.value as Map; // List statusList = []; - // usersMap['status'].forEach((element) { - // statusList.add(StatusModel(code: element['code'], value: true)); + // statusList + // .add(StatusModel(code: element['code'], value: element['value'])); // }); - // deviceStatus = DoorSensorModel.fromJson(statusList); // if (!isClosed) { // add( @@ -165,40 +167,10 @@ class DoorSensorBloc extends Bloc { // } catch (_) {} // } - StreamSubscription? _streamSubscription; - - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$DSId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - deviceStatus = DoorSensorModel.fromJson(statusList); - if (!isClosed) { - add( - DoorSensorSwitch(switchD: deviceStatus.doorContactState), - ); - } - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } } diff --git a/lib/features/devices/bloc/four_scene_bloc/four_scene_bloc.dart b/lib/features/devices/bloc/four_scene_bloc/four_scene_bloc.dart index 069fd64..d60983d 100644 --- a/lib/features/devices/bloc/four_scene_bloc/four_scene_bloc.dart +++ b/lib/features/devices/bloc/four_scene_bloc/four_scene_bloc.dart @@ -231,7 +231,7 @@ class FourSceneBloc extends Bloc { deviceStatus = FourSceneModelState.fromJson( statusModelList, ); - _listenToChanges(); + // _listenToChanges(); add(const FourSceneSwitchInitial()); } catch (e) { emit(FourSceneFailedState(errorMessage: e.toString())); @@ -329,33 +329,33 @@ class FourSceneBloc extends Bloc { }).toList(); emit(SearchResultsState()); } + // Real-time database + // Timer? _timer; + // _listenToChanges() { + // try { + // DatabaseReference ref = + // FirebaseDatabase.instance.ref('device-status/$fourSceneId'); + // Stream stream = ref.onValue; - Timer? _timer; - _listenToChanges() { - try { - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$fourSceneId'); - Stream stream = ref.onValue; + // stream.listen((DatabaseEvent event) async { + // if (_timer != null) { + // await Future.delayed(const Duration(seconds: 2)); + // } + // Map usersMap = + // event.snapshot.value as Map; + // List statusList = []; - stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; + // usersMap['status'].forEach((element) { + // statusList.add(StatusModel(code: element['code'], value: true)); + // }); - usersMap['status'].forEach((element) { - statusList.add(StatusModel(code: element['code'], value: true)); - }); - - deviceStatus = FourSceneModelState.fromJson(statusList); - // if (!isClosed) { - // add( - // DoorSensorSwitch(switchD: deviceStatus.doorContactState), - // ); - // } - }); - } catch (_) {} - } + // deviceStatus = FourSceneModelState.fromJson(statusList); + // // if (!isClosed) { + // // add( + // // DoorSensorSwitch(switchD: deviceStatus.doorContactState), + // // ); + // // } + // }); + // } catch (_) {} + // } } diff --git a/lib/features/devices/bloc/garage_door_bloc/garage_door_bloc.dart b/lib/features/devices/bloc/garage_door_bloc/garage_door_bloc.dart index f6f6332..b36e634 100644 --- a/lib/features/devices/bloc/garage_door_bloc/garage_door_bloc.dart +++ b/lib/features/devices/bloc/garage_door_bloc/garage_door_bloc.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:dio/dio.dart'; -import 'package:firebase_database/firebase_database.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/garage_door_bloc/garage_door_event.dart'; @@ -42,7 +41,6 @@ class GarageDoorBloc extends Bloc { on(_changeFirstWizardSwitch); on(_toggleAlarmEvent); on(deleteSchedule); - //_toggleAlarmEvent } void _onClose(OnClose event, Emitter emit) { _timer?.cancel(); @@ -81,7 +79,7 @@ class GarageDoorBloc extends Bloc { toggleDoor = deviceStatus.switch1; emit(UpdateState(garageSensor: deviceStatus)); Future.delayed(const Duration(milliseconds: 500)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(GarageDoorFailedState(errorMessage: e.toString())); return; @@ -180,47 +178,16 @@ class GarageDoorBloc extends Bloc { } } - StreamSubscription? _streamSubscription; - - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$GDId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - deviceStatus = GarageDoorModel.fromJson(statusList); - // if (!isClosed) { - // add(ToggleSelectedEvent()); - // } - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } - // _listenToChanges() { + // Real-time db + // StreamSubscription? _streamSubscription; + // void _listenToChanges() { // try { + // _streamSubscription?.cancel(); // DatabaseReference ref = // FirebaseDatabase.instance.ref('device-status/$GDId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) async { + // _streamSubscription = stream.listen((DatabaseEvent event) async { // if (_timer != null) { // await Future.delayed(const Duration(seconds: 2)); // } @@ -228,19 +195,24 @@ class GarageDoorBloc extends Bloc { // event.snapshot.value as Map; // List statusList = []; // usersMap['status'].forEach((element) { - // statusList.add(StatusModel(code: element['code'], value: true)); + // statusList + // .add(StatusModel(code: element['code'], value: element['value'])); // }); - // deviceStatus = GarageDoorModel.fromJson(statusList); - // if (!isClosed) { - // // add( - // // DoorSensorSwitch(switchD: deviceStatus.doorContactState), - // // ); - // } + // // if (!isClosed) { + // // add(ToggleSelectedEvent()); + // // } // }); // } catch (_) {} // } + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } + List> days = [ {"day": "Sun", "key": "Sun"}, {"day": "Mon", "key": "Mon"}, diff --git a/lib/features/devices/bloc/one_gang_bloc/one_gang_bloc.dart b/lib/features/devices/bloc/one_gang_bloc/one_gang_bloc.dart index 50745cd..c5ff33d 100644 --- a/lib/features/devices/bloc/one_gang_bloc/one_gang_bloc.dart +++ b/lib/features/devices/bloc/one_gang_bloc/one_gang_bloc.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:dio/dio.dart'; -import 'package:firebase_database/firebase_database.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/one_gang_bloc/one_gang_state.dart'; @@ -61,29 +60,32 @@ class OneGangBloc extends Bloc { } deviceStatus = OneGangModel.fromJson(statusModelList); emit(UpdateState(oneGangModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; } } - - // _listenToChanges() { + // Real-time db + // StreamSubscription? _streamSubscription; + // void _listenToChanges() { // try { - // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$oneGangId'); + // _streamSubscription?.cancel(); + // DatabaseReference ref = + // FirebaseDatabase.instance.ref('device-status/$oneGangId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) async { + // _streamSubscription = stream.listen((DatabaseEvent event) async { // if (_timer != null) { // await Future.delayed(const Duration(seconds: 2)); // } - // Map usersMap = event.snapshot.value as Map; + // Map usersMap = + // event.snapshot.value as Map; // List statusList = []; - // usersMap['status'].forEach((element) { - // statusList.add(StatusModel(code: element['code'], value: element['value'])); + // statusList + // .add(StatusModel(code: element['code'], value: element['value'])); // }); - // deviceStatus = OneGangModel.fromJson(statusList); // if (!isClosed) { // add(OneGangUpdated()); @@ -92,41 +94,12 @@ class OneGangBloc extends Bloc { // } catch (_) {} // } - StreamSubscription? _streamSubscription; - - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$oneGangId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - print('=========${usersMap['status']}'); - deviceStatus = OneGangModel.fromJson(statusList); - if (!isClosed) { - add(OneGangUpdated()); - } - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } _oneGangUpdated(OneGangUpdated event, Emitter emit) { emit(UpdateState(oneGangModel: deviceStatus)); diff --git a/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart b/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart index 6843cc6..d18a248 100644 --- a/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart +++ b/lib/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:dio/dio.dart'; -import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; @@ -30,7 +29,8 @@ class OneTouchBloc extends Bloc { bool oneTouchGroup = false; List devicesList = []; - OneTouchBloc({required this.oneTouchId, required this.switchCode}) : super(InitialState()) { + OneTouchBloc({required this.oneTouchId, required this.switchCode}) + : super(InitialState()) { on(_fetchOneTouchStatus); on(_oneTouchUpdated); on(_changeFirstSwitch); @@ -53,7 +53,8 @@ class OneTouchBloc extends Bloc { on(_changeStatus); } - void _fetchOneTouchStatus(InitialEvent event, Emitter emit) async { + void _fetchOneTouchStatus( + InitialEvent event, Emitter emit) async { emit(LoadingInitialState()); try { var response = await DevicesAPI.getDeviceStatus(oneTouchId); @@ -63,42 +64,43 @@ class OneTouchBloc extends Bloc { } deviceStatus = OneTouchModel.fromJson(statusModelList); emit(UpdateState(oneTouchModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; } } + // Real-time db + // _listenToChanges() { + // try { + // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$oneTouchId'); + // Stream stream = ref.onValue; - _listenToChanges() { - try { - DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$oneTouchId'); - Stream stream = ref.onValue; + // stream.listen((DatabaseEvent event) async { + // if (_timer != null) { + // await Future.delayed(const Duration(seconds: 2)); + // } + // Map usersMap = event.snapshot.value as Map; + // List statusList = []; - stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = event.snapshot.value as Map; - List statusList = []; + // usersMap['status'].forEach((element) { + // statusList.add(StatusModel(code: element['code'], value: element['value'])); + // }); - usersMap['status'].forEach((element) { - statusList.add(StatusModel(code: element['code'], value: element['value'])); - }); - - deviceStatus = OneTouchModel.fromJson(statusList); - if (!isClosed) { - add(OneTouchUpdated()); - } - }); - } catch (_) {} - } + // deviceStatus = OneTouchModel.fromJson(statusList); + // if (!isClosed) { + // add(OneTouchUpdated()); + // } + // }); + // } catch (_) {} + // } _oneTouchUpdated(OneTouchUpdated event, Emitter emit) { emit(UpdateState(oneTouchModel: deviceStatus)); } - void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter emit) async { + void _changeFirstSwitch( + ChangeFirstSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(oneTouchModel: deviceStatus)); try { deviceStatus.firstSwitch = !event.value; @@ -123,17 +125,20 @@ class OneTouchBloc extends Bloc { } } - void _changeSliding(ChangeSlidingSegment event, Emitter emit) async { + void _changeSliding( + ChangeSlidingSegment event, Emitter emit) async { emit(ChangeSlidingSegmentState(value: event.value)); } - void _setCounterValue(SetCounterValue event, Emitter emit) async { + void _setCounterValue( + SetCounterValue event, Emitter emit) async { emit(LoadingNewSate(oneTouchModel: deviceStatus)); int seconds = 0; try { seconds = event.duration.inSeconds; final response = await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: oneTouchId, code: event.deviceCode, value: seconds), + DeviceControlModel( + deviceId: oneTouchId, code: event.deviceCode, value: seconds), oneTouchId); if (response['success'] ?? false) { @@ -156,7 +161,8 @@ class OneTouchBloc extends Bloc { } } - void _getCounterValue(GetCounterEvent event, Emitter emit) async { + void _getCounterValue( + GetCounterEvent event, Emitter emit) async { emit(LoadingInitialState()); try { var response = await DevicesAPI.getDeviceStatus(oneTouchId); @@ -245,7 +251,8 @@ class OneTouchBloc extends Bloc { deviceId: oneTouchId, ); List jsonData = response; - listSchedule = jsonData.map((item) => ScheduleModel.fromJson(item)).toList(); + listSchedule = + jsonData.map((item) => ScheduleModel.fromJson(item)).toList(); emit(InitialState()); } on DioException catch (e) { final errorData = e.response!.data; @@ -256,12 +263,13 @@ class OneTouchBloc extends Bloc { int? getTimeStampWithoutSeconds(DateTime? dateTime) { if (dateTime == null) return null; - DateTime dateTimeWithoutSeconds = - DateTime(dateTime.year, dateTime.month, dateTime.day, dateTime.hour, dateTime.minute); + DateTime dateTimeWithoutSeconds = DateTime(dateTime.year, dateTime.month, + dateTime.day, dateTime.hour, dateTime.minute); return dateTimeWithoutSeconds.millisecondsSinceEpoch ~/ 1000; } - Future toggleChange(ToggleScheduleEvent event, Emitter emit) async { + Future toggleChange( + ToggleScheduleEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final response = await DevicesAPI.changeSchedule( @@ -280,7 +288,8 @@ class OneTouchBloc extends Bloc { } } - Future deleteSchedule(DeleteScheduleEvent event, Emitter emit) async { + Future deleteSchedule( + DeleteScheduleEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final response = await DevicesAPI.deleteSchedule( @@ -300,7 +309,8 @@ class OneTouchBloc extends Bloc { } } - void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter emit) { + void toggleCreateSchedule( + ToggleCreateScheduleEvent event, Emitter emit) { emit(LoadingInitialState()); createSchedule = !createSchedule; selectedDays.clear(); @@ -329,7 +339,8 @@ class OneTouchBloc extends Bloc { int selectedTabIndex = 0; - void toggleSelectedIndex(ToggleSelectedEvent event, Emitter emit) { + void toggleSelectedIndex( + ToggleSelectedEvent event, Emitter emit) { emit(LoadingInitialState()); selectedTabIndex = event.index; emit(ChangeSlidingSegmentState(value: selectedTabIndex)); @@ -338,7 +349,8 @@ class OneTouchBloc extends Bloc { List groupOneTouchList = []; bool allSwitchesOn = true; - void _fetchOneTouchWizardStatus(InitialWizardEvent event, Emitter emit) async { + void _fetchOneTouchWizardStatus( + InitialWizardEvent event, Emitter emit) async { emit(LoadingInitialState()); try { devicesList = []; @@ -348,7 +360,8 @@ class OneTouchBloc extends Bloc { HomeCubit.getInstance().selectedSpace?.id ?? '', '1GT'); for (int i = 0; i < devicesList.length; i++) { - var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); + var response = + await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); List statusModelList = []; for (var status in response['status']) { statusModelList.add(StatusModel.fromJson(status)); @@ -369,15 +382,16 @@ class OneTouchBloc extends Bloc { return true; }); } - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: allSwitchesOn)); + emit(UpdateGroupState( + oneTouchList: groupOneTouchList, allSwitches: allSwitchesOn)); } catch (e) { emit(FailedState(error: e.toString())); return; } } - void _changeFirstWizardSwitch( - ChangeFirstWizardSwitchStatusEvent event, Emitter emit) async { + void _changeFirstWizardSwitch(ChangeFirstWizardSwitchStatusEvent event, + Emitter emit) async { emit(LoadingNewSate(oneTouchModel: deviceStatus)); try { bool allSwitchesValue = true; @@ -395,7 +409,8 @@ class OneTouchBloc extends Bloc { value: !event.value, ); - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: allSwitchesValue)); + emit(UpdateGroupState( + oneTouchList: groupOneTouchList, allSwitches: allSwitchesValue)); if (response['success']) { add(InitialEvent(groupScreen: oneTouchGroup)); } @@ -413,10 +428,12 @@ class OneTouchBloc extends Bloc { } // Emit the state with updated values - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: true)); + emit( + UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: true)); // Get a list of all device IDs - List allDeviceIds = groupOneTouchList.map((device) => device.deviceId).toList(); + List allDeviceIds = + groupOneTouchList.map((device) => device.deviceId).toList(); // First call for switch_1 final response1 = await DevicesAPI.deviceBatchController( @@ -445,10 +462,12 @@ class OneTouchBloc extends Bloc { } // Emit the state with updated values - emit(UpdateGroupState(oneTouchList: groupOneTouchList, allSwitches: false)); + emit(UpdateGroupState( + oneTouchList: groupOneTouchList, allSwitches: false)); // Get a list of all device IDs - List allDeviceIds = groupOneTouchList.map((device) => device.deviceId).toList(); + List allDeviceIds = + groupOneTouchList.map((device) => device.deviceId).toList(); // First call for switch_1 final response1 = await DevicesAPI.deviceBatchController( @@ -472,7 +491,8 @@ class OneTouchBloc extends Bloc { String statusSelected = ''; String optionSelected = ''; - Future _changeStatus(ChangeStatusEvent event, Emitter emit) async { + Future _changeStatus( + ChangeStatusEvent event, Emitter emit) async { try { emit(LoadingInitialState()); @@ -497,7 +517,10 @@ class OneTouchBloc extends Bloc { final selectedControl = controlMap[optionSelected]?[statusSelected]; if (selectedControl != null) { await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: oneTouchId, code: optionSelected, value: selectedControl), + DeviceControlModel( + deviceId: oneTouchId, + code: optionSelected, + value: selectedControl), oneTouchId, ); } else { diff --git a/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart b/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart index cc4a1d9..d749fb3 100644 --- a/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart +++ b/lib/features/devices/bloc/smart_door_bloc/smart_door_bloc.dart @@ -149,24 +149,7 @@ class SmartDoorBloc extends Bloc { _streamSubscription = null; return super.close(); } - // _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 = SmartDoorModel.fromJson(statusList); - // add(DoorLockUpdated()); - // }); - // } catch (_) {} - // } + _doorLockUpdated(DoorLockUpdated event, Emitter emit) { unlockRequest = deviceStatus.unlockRequest; diff --git a/lib/features/devices/bloc/sos_bloc/sos_bloc.dart b/lib/features/devices/bloc/sos_bloc/sos_bloc.dart index e4d73e0..06546ea 100644 --- a/lib/features/devices/bloc/sos_bloc/sos_bloc.dart +++ b/lib/features/devices/bloc/sos_bloc/sos_bloc.dart @@ -172,7 +172,7 @@ class SosBloc extends Bloc { ); emit(UpdateState(sensor: deviceStatus)); Future.delayed(const Duration(milliseconds: 500)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(SosFailedState(errorMessage: e.toString())); return; @@ -422,58 +422,38 @@ class SosBloc extends Bloc { } } - // _listenToChanges() { + + //real-time db + // StreamSubscription? _streamSubscription; + // Timer? _timer; + + // void _listenToChanges() { // try { + // _streamSubscription?.cancel(); // DatabaseReference ref = // FirebaseDatabase.instance.ref('device-status/$sosId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) { + // _streamSubscription = stream.listen((DatabaseEvent event) async { + // if (_timer != null) { + // await Future.delayed(const Duration(seconds: 2)); + // } // Map usersMap = // event.snapshot.value as Map; // List statusList = []; - // usersMap['status'].forEach((element) { // statusList // .add(StatusModel(code: element['code'], value: element['value'])); // }); - // deviceStatus = SosModel.fromJson(statusList); - // // add(DoorLockUpdated()); // }); // } catch (_) {} // } - StreamSubscription? _streamSubscription; - Timer? _timer; - - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$sosId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - deviceStatus = SosModel.fromJson(statusList); - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } } diff --git a/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart b/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart index 0eb7757..a9dfd97 100644 --- a/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart +++ b/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart @@ -115,32 +115,6 @@ class ThreeGangBloc extends Bloc { } } - // _listenToChanges() { - // try { - // DatabaseReference ref = - // FirebaseDatabase.instance.ref('device-status/$threeGangId'); - // Stream stream = ref.onValue; - - // stream.listen((DatabaseEvent event) async { - // if (_timer != null) { - // await Future.delayed(const Duration(seconds: 2)); - // } - // Map usersMap = - // event.snapshot.value as Map; - // List statusList = []; - // usersMap['status'].forEach((element) { - // statusList - // .add(StatusModel(code: element['code'], value: element['value'])); - // }); - // print('=========${usersMap['status']}'); - - // deviceStatus = ThreeGangModel.fromJson(statusList); - // if (!isClosed) { - // add(ThreeGangUpdated()); - // } - // }); - // } catch (_) {} - // } StreamSubscription? _streamSubscription; @@ -162,8 +136,6 @@ class ThreeGangBloc extends Bloc { statusList .add(StatusModel(code: element['code'], value: element['value'])); }); - print('=========${usersMap['status']}'); - deviceStatus = ThreeGangModel.fromJson(statusList); if (!isClosed) { add(ThreeGangUpdated()); diff --git a/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart b/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart index 8caf234..2fc9967 100644 --- a/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart +++ b/lib/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart @@ -107,7 +107,7 @@ class ThreeTouchBloc extends Bloc { } deviceStatus = ThreeTouchModel.fromJson(statusModelList); emit(UpdateState(threeTouchModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } } catch (e) { emit(FailedState(error: e.toString())); @@ -115,29 +115,29 @@ class ThreeTouchBloc extends Bloc { } } - _listenToChanges() { - try { - DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeTouchId'); - Stream stream = ref.onValue; + // _listenToChanges() { + // try { + // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeTouchId'); + // Stream stream = ref.onValue; - stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = event.snapshot.value as Map; - List statusList = []; + // stream.listen((DatabaseEvent event) async { + // if (_timer != null) { + // await Future.delayed(const Duration(seconds: 2)); + // } + // Map usersMap = event.snapshot.value as Map; + // List statusList = []; - usersMap['status'].forEach((element) { - statusList.add(StatusModel(code: element['code'], value: element['value'])); - }); + // usersMap['status'].forEach((element) { + // statusList.add(StatusModel(code: element['code'], value: element['value'])); + // }); - deviceStatus = ThreeTouchModel.fromJson(statusList); - if (!isClosed) { - add(ThreeTouchUpdated()); - } - }); - } catch (_) {} - } + // deviceStatus = ThreeTouchModel.fromJson(statusList); + // if (!isClosed) { + // add(ThreeTouchUpdated()); + // } + // }); + // } catch (_) {} + // } _threeTouchUpdated(ThreeTouchUpdated event, Emitter emit) { emit(UpdateState(threeTouchModel: deviceStatus)); diff --git a/lib/features/devices/bloc/two_gang_bloc/two_gang_bloc.dart b/lib/features/devices/bloc/two_gang_bloc/two_gang_bloc.dart index bc14479..5435148 100644 --- a/lib/features/devices/bloc/two_gang_bloc/two_gang_bloc.dart +++ b/lib/features/devices/bloc/two_gang_bloc/two_gang_bloc.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:dio/dio.dart'; -import 'package:firebase_database/firebase_database.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; import 'package:syncrow_app/features/devices/bloc/two_gang_bloc/two_gang_event.dart'; @@ -89,36 +88,36 @@ class TwoGangBloc extends Bloc { } deviceStatus = TwoGangModel.fromJson(statusModelList); emit(UpdateState(twoGangModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; } } - _listenToChanges() { - try { - DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$twoGangId'); - Stream stream = ref.onValue; + // _listenToChanges() { + // try { + // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$twoGangId'); + // Stream stream = ref.onValue; - stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = event.snapshot.value as Map; - List statusList = []; + // stream.listen((DatabaseEvent event) async { + // if (_timer != null) { + // await Future.delayed(const Duration(seconds: 2)); + // } + // Map usersMap = event.snapshot.value as Map; + // List statusList = []; - usersMap['status'].forEach((element) { - statusList.add(StatusModel(code: element['code'], value: element['value'])); - }); + // usersMap['status'].forEach((element) { + // statusList.add(StatusModel(code: element['code'], value: element['value'])); + // }); - deviceStatus = TwoGangModel.fromJson(statusList); - if (!isClosed) { - add(TwoGangUpdated()); - } - }); - } catch (_) {} - } + // deviceStatus = TwoGangModel.fromJson(statusList); + // if (!isClosed) { + // add(TwoGangUpdated()); + // } + // }); + // } catch (_) {} + // } _twoGangUpdated(TwoGangUpdated event, Emitter emit) { emit(UpdateState(twoGangModel: deviceStatus)); diff --git a/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart b/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart index 96c882d..3b14c19 100644 --- a/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart +++ b/lib/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart @@ -99,56 +99,23 @@ class TwoTouchBloc extends Bloc { } deviceStatus = TwoTouchModel.fromJson(statusModelList); emit(UpdateState(twoTouchModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; } } - StreamSubscription? _streamSubscription; + // StreamSubscription? _streamSubscription; - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$twoTouchId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - print('=========${usersMap['status']}'); - deviceStatus = TwoTouchModel.fromJson(statusList); - if (!isClosed) { - add(TwoTouchUpdated()); - } - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } - - // _listenToChanges() { + // void _listenToChanges() { // try { + // _streamSubscription?.cancel(); // DatabaseReference ref = // FirebaseDatabase.instance.ref('device-status/$twoTouchId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) async { + // _streamSubscription = stream.listen((DatabaseEvent event) async { // if (_timer != null) { // await Future.delayed(const Duration(seconds: 2)); // } @@ -159,7 +126,7 @@ class TwoTouchBloc extends Bloc { // statusList // .add(StatusModel(code: element['code'], value: element['value'])); // }); - + // print('=========${usersMap['status']}'); // deviceStatus = TwoTouchModel.fromJson(statusList); // if (!isClosed) { // add(TwoTouchUpdated()); @@ -168,6 +135,15 @@ class TwoTouchBloc extends Bloc { // } catch (_) {} // } + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } + + + _twoTouchUpdated(TwoTouchUpdated event, Emitter emit) { emit(UpdateState(twoTouchModel: deviceStatus)); } diff --git a/lib/features/devices/bloc/wall_sensor_bloc/wall_sensor_bloc.dart b/lib/features/devices/bloc/wall_sensor_bloc/wall_sensor_bloc.dart index 2aff727..96a5f03 100644 --- a/lib/features/devices/bloc/wall_sensor_bloc/wall_sensor_bloc.dart +++ b/lib/features/devices/bloc/wall_sensor_bloc/wall_sensor_bloc.dart @@ -41,27 +41,6 @@ class WallSensorBloc extends Bloc { } } - // _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 (_) {} - // } Timer? _timer; StreamSubscription? _streamSubscription; diff --git a/lib/features/devices/bloc/water_heater_bloc/water_heater_bloc.dart b/lib/features/devices/bloc/water_heater_bloc/water_heater_bloc.dart index 86b6bcd..8305927 100644 --- a/lib/features/devices/bloc/water_heater_bloc/water_heater_bloc.dart +++ b/lib/features/devices/bloc/water_heater_bloc/water_heater_bloc.dart @@ -74,64 +74,33 @@ class WaterHeaterBloc extends Bloc { statusModelList, ); emit(UpdateState(whModel: deviceStatus)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(WHFailedState(errorMessage: e.toString())); return; } } - StreamSubscription? _streamSubscription; - - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$whId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - deviceStatus = WHModel.fromJson(statusList); - if (!isClosed) { - add(WaterHeaterUpdated()); - } - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } - - // _listenToChanges() { + //real-time db + // StreamSubscription? _streamSubscription; + // void _listenToChanges() { // try { - // DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$whId'); + // _streamSubscription?.cancel(); + // DatabaseReference ref = + // FirebaseDatabase.instance.ref('device-status/$whId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) async { + // _streamSubscription = stream.listen((DatabaseEvent event) async { // if (_timer != null) { // await Future.delayed(const Duration(seconds: 2)); // } - // Map usersMap = event.snapshot.value as Map; + // Map usersMap = + // event.snapshot.value as Map; // List statusList = []; - // usersMap['status'].forEach((element) { - // statusList.add(StatusModel(code: element['code'], value: element['value'])); + // statusList + // .add(StatusModel(code: element['code'], value: element['value'])); // }); - // deviceStatus = WHModel.fromJson(statusList); // if (!isClosed) { // add(WaterHeaterUpdated()); @@ -140,6 +109,15 @@ class WaterHeaterBloc extends Bloc { // } catch (_) {} // } + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } + + + _waterHeaterUpdated( WaterHeaterUpdated event, Emitter emit) async { emit(WHLoadingState()); diff --git a/lib/features/devices/bloc/water_leak_bloc/water_leak_bloc.dart b/lib/features/devices/bloc/water_leak_bloc/water_leak_bloc.dart index 58b1445..c4f70a1 100644 --- a/lib/features/devices/bloc/water_leak_bloc/water_leak_bloc.dart +++ b/lib/features/devices/bloc/water_leak_bloc/water_leak_bloc.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'package:dio/dio.dart'; -import 'package:firebase_database/firebase_database.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/devices/bloc/water_leak_bloc/water_leak_event.dart'; import 'package:syncrow_app/features/devices/bloc/water_leak_bloc/water_leak_state.dart'; @@ -21,7 +20,6 @@ class WaterLeakBloc extends Bloc { on(_toggleClosingReminder); on(_toggleWaterLeakAlarm); } - Timer? _timer; bool lowBattery = false; bool closingReminder = false; bool waterAlarm = false; @@ -43,7 +41,7 @@ class WaterLeakBloc extends Bloc { emit(UpdateState(waterSensor: deviceStatus)); Future.delayed(const Duration(milliseconds: 500)); - _listenToChanges(); + // _listenToChanges(); } catch (e) { emit(WaterLeakFailedState(errorMessage: e.toString())); return; @@ -134,62 +132,26 @@ class WaterLeakBloc extends Bloc { emit(WaterLeakFailedState(errorMessage: errorMessage)); } } - - StreamSubscription? _streamSubscription; - - void _listenToChanges() { - try { - _streamSubscription?.cancel(); - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$WLId'); - Stream stream = ref.onValue; - - _streamSubscription = stream.listen((DatabaseEvent event) async { - if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); - } - Map usersMap = - event.snapshot.value as Map; - List statusList = []; - usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); - }); - deviceStatus = WaterLeakModel.fromJson(statusList); - if (!isClosed) { - add( - WaterLeakSwitch(switchD: deviceStatus.waterContactState), - ); - } - }); - } catch (_) {} - } - - @override - Future close() async { - _streamSubscription?.cancel(); - _streamSubscription = null; - return super.close(); - } - - // _listenToChanges() { + // Timer? _timer; + // StreamSubscription? _streamSubscription; + // void _listenToChanges() { // try { + // _streamSubscription?.cancel(); // DatabaseReference ref = // FirebaseDatabase.instance.ref('device-status/$WLId'); // Stream stream = ref.onValue; - // stream.listen((DatabaseEvent event) async { + // _streamSubscription = stream.listen((DatabaseEvent event) async { // if (_timer != null) { // await Future.delayed(const Duration(seconds: 2)); // } // Map usersMap = // event.snapshot.value as Map; // List statusList = []; - // usersMap['status'].forEach((element) { - // statusList.add(StatusModel(code: element['code'], value: true)); + // statusList + // .add(StatusModel(code: element['code'], value: element['value'])); // }); - // deviceStatus = WaterLeakModel.fromJson(statusList); // if (!isClosed) { // add( @@ -199,4 +161,13 @@ class WaterLeakBloc extends Bloc { // }); // } catch (_) {} // } + + // @override + // Future close() async { + // _streamSubscription?.cancel(); + // _streamSubscription = null; + // return super.close(); + // } + + }