From cd417202447c20735a5e23937446f9a2854e22c1 Mon Sep 17 00:00:00 2001 From: mohammad Date: Sun, 16 Feb 2025 10:04:34 +0300 Subject: [PATCH] active the implemented device --- .../devices/bloc/acs_bloc/acs_bloc.dart | 3 +- .../ceiling_bloc/ceiling_sensor_bloc.dart | 2 +- .../bloc/curtain_bloc/curtain_bloc.dart | 25 +++- .../door_sensor_bloc/door_sensor_bloc.dart | 2 +- .../bloc/four_scene_bloc/four_scene_bloc.dart | 31 +++++ .../garage_door_bloc/garage_door_bloc.dart | 2 +- .../bloc/one_gang_bloc/one_gang_bloc.dart | 2 +- .../bloc/one_touch_bloc/one_touch_bloc.dart | 2 +- .../bloc/smart_door_bloc/smart_door_bloc.dart | 2 +- .../devices/bloc/sos_bloc/sos_bloc.dart | 24 ++++ .../bloc/three_gang_bloc/three_gang_bloc.dart | 126 ++++++++++++------ .../three_touch_bloc/three_touch_bloc.dart | 2 +- .../bloc/two_gang_bloc/two_gang_bloc.dart | 2 +- .../bloc/two_touch_bloc/two_touch_bloc.dart | 2 +- .../wall_sensor_bloc/wall_sensor_bloc.dart | 34 +++-- .../water_heater_bloc/water_heater_bloc.dart | 2 +- .../bloc/water_leak_bloc/water_leak_bloc.dart | 2 +- 17 files changed, 200 insertions(+), 65 deletions(-) diff --git a/lib/features/devices/bloc/acs_bloc/acs_bloc.dart b/lib/features/devices/bloc/acs_bloc/acs_bloc.dart index b0b43af..d5c21c0 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(); } } catch (e) { emit(AcsFailedState(errorMessage: e.toString())); @@ -136,7 +136,6 @@ class ACsBloc extends Bloc { ac.acSwitch = acSwitchValue; } } - _setAllAcsTempsAndSwitches(); _emitAcsStatus(emit); 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 d8a463c..39ac044 100644 --- a/lib/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart +++ b/lib/features/devices/bloc/ceiling_bloc/ceiling_sensor_bloc.dart @@ -35,7 +35,7 @@ class CeilingSensorBloc extends Bloc { } deviceStatus = CeilingSensorModel.fromJson(statusModelList); emit(UpdateState(ceilingSensorModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; diff --git a/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart b/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart index 3cff103..a5c5f49 100644 --- a/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart +++ b/lib/features/devices/bloc/curtain_bloc/curtain_bloc.dart @@ -1,3 +1,4 @@ +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'; @@ -169,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(); emit(CurtainsOpening( curtainWidth: curtainWidth, blindHeight: blindHeight, @@ -181,6 +182,28 @@ class CurtainBloc extends Bloc { } } + _listenToChanges() { + try { + DatabaseReference ref = + FirebaseDatabase.instance.ref('device-status/$curtainId'); + 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 = CurtainModel.fromJson(statusList); + // add(CeilingSensorUpdated()); + }); + } catch (_) {} + } + List groupList = []; bool allSwitchesOn = true; List devicesList = []; 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 15c7d76..b88d477 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 @@ -40,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; 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 d3069f7..069fd64 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 @@ -1,4 +1,5 @@ import 'dart:async'; +import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/devices/bloc/four_scene_bloc/four_scene_event.dart'; @@ -230,6 +231,7 @@ class FourSceneBloc extends Bloc { deviceStatus = FourSceneModelState.fromJson( statusModelList, ); + _listenToChanges(); add(const FourSceneSwitchInitial()); } catch (e) { emit(FourSceneFailedState(errorMessage: e.toString())); @@ -327,4 +329,33 @@ class FourSceneBloc extends Bloc { }).toList(); emit(SearchResultsState()); } + + 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 = []; + + usersMap['status'].forEach((element) { + statusList.add(StatusModel(code: element['code'], value: true)); + }); + + 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 43dcda4..3db0b7e 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 @@ -81,7 +81,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; 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 9e7161d..6a90042 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 @@ -59,7 +59,7 @@ class OneGangBloc extends Bloc { } deviceStatus = OneGangModel.fromJson(statusModelList); emit(UpdateState(oneGangModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; 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 37e016f..6843cc6 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 @@ -63,7 +63,7 @@ class OneTouchBloc extends Bloc { } deviceStatus = OneTouchModel.fromJson(statusModelList); emit(UpdateState(oneTouchModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; 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 7f1a606..ffd8760 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 @@ -102,7 +102,7 @@ class SmartDoorBloc extends Bloc { } deviceStatus = SmartDoorModel.fromJson(statusModelList); emit(UpdateState(smartDoorModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(FailedState(errorMessage: e.toString())); return; diff --git a/lib/features/devices/bloc/sos_bloc/sos_bloc.dart b/lib/features/devices/bloc/sos_bloc/sos_bloc.dart index 63082fa..44008bd 100644 --- a/lib/features/devices/bloc/sos_bloc/sos_bloc.dart +++ b/lib/features/devices/bloc/sos_bloc/sos_bloc.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'package:dio/dio.dart'; +import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; @@ -171,6 +172,7 @@ class SosBloc extends Bloc { ); emit(UpdateState(sensor: deviceStatus)); Future.delayed(const Duration(milliseconds: 500)); + _listenToChanges(); } catch (e) { emit(SosFailedState(errorMessage: e.toString())); return; @@ -419,4 +421,26 @@ class SosBloc extends Bloc { emit(SosFailedState(errorMessage: e.toString())); } } + + _listenToChanges() { + try { + DatabaseReference ref = + FirebaseDatabase.instance.ref('device-status/$sosId'); + 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 = SosModel.fromJson(statusList); + // add(DoorLockUpdated()); + }); + } catch (_) {} + } } 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 04f434c..3311ef2 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 @@ -31,7 +31,8 @@ class ThreeGangBloc extends Bloc { List groupThreeGangList = []; bool allSwitchesOn = true; - ThreeGangBloc({required this.threeGangId, required this.switchCode}) : super(InitialState()) { + ThreeGangBloc({required this.threeGangId, required this.switchCode}) + : super(InitialState()) { on(_fetchThreeGangStatus); on(_threeGangUpdated); on(_changeFirstSwitch); @@ -57,7 +58,8 @@ class ThreeGangBloc extends Bloc { on(toggleCreateSchedule); } - void _fetchThreeGangStatus(InitialEvent event, Emitter emit) async { + void _fetchThreeGangStatus( + InitialEvent event, Emitter emit) async { emit(LoadingInitialState()); try { threeGangGroup = event.groupScreen; @@ -69,7 +71,8 @@ class ThreeGangBloc extends Bloc { HomeCubit.getInstance().selectedSpace?.id ?? '', '3G'); 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)); @@ -86,13 +89,16 @@ class ThreeGangBloc extends Bloc { if (groupThreeGangList.isNotEmpty) { groupThreeGangList.firstWhere((element) { - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesOn = false; } return true; }); } - emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesOn)); + emit(UpdateGroupState( + threeGangList: groupThreeGangList, allSwitches: allSwitchesOn)); } else { var response = await DevicesAPI.getDeviceStatus(threeGangId); List statusModelList = []; @@ -101,7 +107,7 @@ class ThreeGangBloc extends Bloc { } deviceStatus = ThreeGangModel.fromJson(statusModelList); emit(UpdateState(threeGangModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } } catch (e) { emit(FailedState(error: e.toString())); @@ -111,19 +117,22 @@ class ThreeGangBloc extends Bloc { _listenToChanges() { try { - DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$threeGangId'); + 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; + 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'])); }); + print('=========${usersMap['status']}'); deviceStatus = ThreeGangModel.fromJson(statusList); if (!isClosed) { @@ -137,7 +146,8 @@ class ThreeGangBloc extends Bloc { emit(UpdateState(threeGangModel: deviceStatus)); } - void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter emit) async { + void _changeFirstSwitch( + ChangeFirstSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { if (threeGangGroup) { @@ -146,11 +156,14 @@ class ThreeGangBloc extends Bloc { if (element.deviceId == event.deviceId) { element.firstSwitch = !event.value; } - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesValue = false; } }); - emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); + emit(UpdateGroupState( + threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); } else { deviceStatus.firstSwitch = !event.value; emit(UpdateState(threeGangModel: deviceStatus)); @@ -187,11 +200,14 @@ class ThreeGangBloc extends Bloc { if (element.deviceId == event.deviceId) { element.secondSwitch = !event.value; } - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesValue = false; } }); - emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); + emit(UpdateGroupState( + threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); } else { deviceStatus.secondSwitch = !event.value; emit(UpdateState(threeGangModel: deviceStatus)); @@ -217,7 +233,8 @@ class ThreeGangBloc extends Bloc { } } - void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter emit) async { + void _changeThirdSwitch( + ChangeThirdSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { if (threeGangGroup) { @@ -226,11 +243,14 @@ class ThreeGangBloc extends Bloc { if (element.deviceId == event.deviceId) { element.thirdSwitch = !event.value; } - if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + if (!element.firstSwitch || + !element.secondSwitch || + !element.thirdSwitch) { allSwitchesValue = false; } }); - emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); + emit(UpdateGroupState( + threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); } else { deviceStatus.thirdSwitch = !event.value; emit(UpdateState(threeGangModel: deviceStatus)); @@ -269,15 +289,21 @@ class ThreeGangBloc extends Bloc { final response = await Future.wait([ DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch), + deviceId: threeGangId, + code: 'switch_1', + value: deviceStatus.firstSwitch), threeGangId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch), + deviceId: threeGangId, + code: 'switch_2', + value: deviceStatus.secondSwitch), threeGangId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch), + deviceId: threeGangId, + code: 'switch_3', + value: deviceStatus.thirdSwitch), threeGangId), ]); @@ -303,15 +329,21 @@ class ThreeGangBloc extends Bloc { final response = await Future.wait([ DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch), + deviceId: threeGangId, + code: 'switch_1', + value: deviceStatus.firstSwitch), threeGangId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch), + deviceId: threeGangId, + code: 'switch_2', + value: deviceStatus.secondSwitch), threeGangId), DevicesAPI.controlDevice( DeviceControlModel( - deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch), + deviceId: threeGangId, + code: 'switch_3', + value: deviceStatus.thirdSwitch), threeGangId), ]); @@ -333,9 +365,11 @@ class ThreeGangBloc extends Bloc { groupThreeGangList[i].secondSwitch = true; groupThreeGangList[i].thirdSwitch = true; } - emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: true)); + emit(UpdateGroupState( + threeGangList: groupThreeGangList, allSwitches: true)); - List allDeviceIds = groupThreeGangList.map((device) => device.deviceId).toList(); + List allDeviceIds = + groupThreeGangList.map((device) => device.deviceId).toList(); final response1 = await DevicesAPI.deviceBatchController( code: 'switch_1', @@ -365,7 +399,8 @@ class ThreeGangBloc extends Bloc { } } - void _groupAllOff(GroupAllOffEvent event, Emitter emit) async { + void _groupAllOff( + GroupAllOffEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { for (int i = 0; i < groupThreeGangList.length; i++) { @@ -373,8 +408,10 @@ class ThreeGangBloc extends Bloc { groupThreeGangList[i].secondSwitch = false; groupThreeGangList[i].thirdSwitch = false; } - emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: false)); - List allDeviceIds = groupThreeGangList.map((device) => device.deviceId).toList(); + emit(UpdateGroupState( + threeGangList: groupThreeGangList, allSwitches: false)); + List allDeviceIds = + groupThreeGangList.map((device) => device.deviceId).toList(); final response1 = await DevicesAPI.deviceBatchController( code: 'switch_1', @@ -404,17 +441,20 @@ class ThreeGangBloc 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(threeGangModel: deviceStatus)); int seconds = 0; try { seconds = event.duration.inSeconds; final response = await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeGangId, code: event.deviceCode, value: seconds), + DeviceControlModel( + deviceId: threeGangId, code: event.deviceCode, value: seconds), threeGangId); if (response['success'] ?? false) { @@ -441,7 +481,8 @@ class ThreeGangBloc extends Bloc { } } - void _getCounterValue(GetCounterEvent event, Emitter emit) async { + void _getCounterValue( + GetCounterEvent event, Emitter emit) async { emit(LoadingInitialState()); try { var response = await DevicesAPI.getDeviceStatus(threeGangId); @@ -551,7 +592,8 @@ class ThreeGangBloc extends Bloc { deviceId: threeGangId, ); 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; @@ -562,12 +604,13 @@ class ThreeGangBloc 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( @@ -586,7 +629,8 @@ class ThreeGangBloc extends Bloc { } } - Future deleteSchedule(DeleteScheduleEvent event, Emitter emit) async { + Future deleteSchedule( + DeleteScheduleEvent event, Emitter emit) async { try { emit(LoadingInitialState()); final response = await DevicesAPI.deleteSchedule( @@ -606,13 +650,15 @@ class ThreeGangBloc extends Bloc { } } - void toggleSelectedIndex(ToggleSelectedEvent event, Emitter emit) { + void toggleSelectedIndex( + ToggleSelectedEvent event, Emitter emit) { emit(LoadingInitialState()); selectedTabIndex = event.index; emit(ChangeSlidingSegmentState(value: selectedTabIndex)); } - void toggleCreateSchedule(ToggleCreateScheduleEvent event, Emitter emit) { + void toggleCreateSchedule( + ToggleCreateScheduleEvent event, Emitter emit) { emit(LoadingInitialState()); createSchedule = !createSchedule; selectedDays.clear(); 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 beec442..8caf234 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())); 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 7906348..bc14479 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 @@ -89,7 +89,7 @@ class TwoGangBloc extends Bloc { } deviceStatus = TwoGangModel.fromJson(statusModelList); emit(UpdateState(twoGangModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; 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 56031c8..a429b7a 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,7 +99,7 @@ class TwoTouchBloc extends Bloc { } deviceStatus = TwoTouchModel.fromJson(statusModelList); emit(UpdateState(twoTouchModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; 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 6994bcc..dfda751 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 @@ -21,7 +21,8 @@ class WallSensorBloc extends Bloc { on(_getDeviceReports); } - void _fetchCeilingSensorStatus(InitialEvent event, Emitter emit) async { + void _fetchCeilingSensorStatus( + InitialEvent event, Emitter emit) async { emit(LoadingInitialState()); try { var response = await DevicesAPI.getDeviceStatus(deviceId); @@ -31,7 +32,7 @@ class WallSensorBloc extends Bloc { } deviceStatus = WallSensorModel.fromJson(statusModelList); emit(UpdateState(wallSensorModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(FailedState(error: e.toString())); return; @@ -40,15 +41,18 @@ class WallSensorBloc extends Bloc { _listenToChanges() { try { - DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$deviceId'); + DatabaseReference ref = + FirebaseDatabase.instance.ref('device-status/$deviceId'); Stream stream = ref.onValue; stream.listen((DatabaseEvent event) { - 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 = WallSensorModel.fromJson(statusList); @@ -57,15 +61,19 @@ class WallSensorBloc extends Bloc { } catch (_) {} } - _wallSensorUpdated(WallSensorUpdatedEvent event, Emitter emit) { + _wallSensorUpdated( + WallSensorUpdatedEvent event, Emitter emit) { emit(UpdateState(wallSensorModel: deviceStatus)); } - void _changeIndicator(ChangeIndicatorEvent event, Emitter emit) async { + void _changeIndicator( + ChangeIndicatorEvent event, Emitter emit) async { emit(LoadingNewSate(wallSensorModel: deviceStatus)); try { final response = await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: deviceId, code: 'indicator', value: !event.value), deviceId); + DeviceControlModel( + deviceId: deviceId, code: 'indicator', value: !event.value), + deviceId); if (response['success'] ?? false) { deviceStatus.indicator = !event.value; @@ -74,11 +82,14 @@ class WallSensorBloc extends Bloc { emit(UpdateState(wallSensorModel: deviceStatus)); } - void _changeValue(ChangeValueEvent event, Emitter emit) async { + void _changeValue( + ChangeValueEvent event, Emitter emit) async { emit(LoadingNewSate(wallSensorModel: deviceStatus)); try { final response = await DevicesAPI.controlDevice( - DeviceControlModel(deviceId: deviceId, code: event.code, value: event.value), deviceId); + DeviceControlModel( + deviceId: deviceId, code: event.code, value: event.value), + deviceId); if (response['success'] ?? false) { if (event.code == 'far_detection') { @@ -93,7 +104,8 @@ class WallSensorBloc extends Bloc { emit(UpdateState(wallSensorModel: deviceStatus)); } - void _getDeviceReports(GetDeviceReportsEvent event, Emitter emit) async { + void _getDeviceReports( + GetDeviceReportsEvent event, Emitter emit) async { emit(LoadingInitialState()); try { 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 40f2bc7..0df2228 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 @@ -72,7 +72,7 @@ class WaterHeaterBloc extends Bloc { statusModelList, ); emit(UpdateState(whModel: deviceStatus)); - // _listenToChanges(); + _listenToChanges(); } catch (e) { emit(WHFailedState(errorMessage: e.toString())); return; 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 300c4f4..265adda 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 @@ -43,7 +43,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;