mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
fix touch gangs realtime. (#322)
<!-- Thanks for contributing! Provide a description of your changes below and a general summary in the title Please look at the following checklist to ensure that your PR can be accepted quickly: --> ## Jira Ticket [SP-0000](https://syncrow.atlassian.net/browse/SP-0000) ## Description <!--- Describe your changes in detail --> ## Type of Change <!--- Put an `x` in all the boxes that apply: --> - [ ] ✨ New feature (non-breaking change which adds functionality) - [ ] 🛠️ Bug fix (non-breaking change which fixes an issue) - [ ] ❌ Breaking change (fix or feature that would cause existing functionality to change) - [ ] 🧹 Code refactor - [ ] ✅ Build configuration change - [ ] 📝 Documentation - [ ] 🗑️ Chore
This commit is contained in:
@ -40,7 +40,7 @@ class OneGangGlassSwitchBloc
|
|||||||
emit(OneGangGlassSwitchLoading());
|
emit(OneGangGlassSwitchLoading());
|
||||||
try {
|
try {
|
||||||
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||||
_listenToChanges(event.deviceId, emit);
|
_listenToChanges(event.deviceId);
|
||||||
deviceStatus = OneGangGlassStatusModel.fromJson(event.deviceId, status.status);
|
deviceStatus = OneGangGlassStatusModel.fromJson(event.deviceId, status.status);
|
||||||
emit(OneGangGlassSwitchStatusLoaded(deviceStatus));
|
emit(OneGangGlassSwitchStatusLoaded(deviceStatus));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -48,42 +48,28 @@ class OneGangGlassSwitchBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _listenToChanges(
|
StreamSubscription<DatabaseEvent>? _deviceStatusSubscription;
|
||||||
String deviceId,
|
|
||||||
Emitter<OneGangGlassSwitchState> emit,
|
void _listenToChanges(String deviceId) {
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||||
final stream = ref.onValue;
|
_deviceStatusSubscription = ref.onValue.listen((DatabaseEvent event) async {
|
||||||
|
if (event.snapshot.value == null) return;
|
||||||
|
|
||||||
stream.listen((DatabaseEvent event) {
|
final usersMap = event.snapshot.value! as Map<dynamic, dynamic>;
|
||||||
final data = event.snapshot.value as Map<dynamic, dynamic>?;
|
|
||||||
if (data == null) return;
|
|
||||||
|
|
||||||
final statusList = <Status>[];
|
final statusList = <Status>[];
|
||||||
if (data['status'] != null) {
|
|
||||||
for (var element in data['status']) {
|
usersMap['status'].forEach((element) {
|
||||||
statusList.add(
|
statusList.add(Status(code: element['code'], value: element['value']));
|
||||||
Status(
|
|
||||||
code: element['code'].toString(),
|
|
||||||
value: element['value'].toString(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (statusList.isNotEmpty) {
|
|
||||||
final newStatus = OneGangGlassStatusModel.fromJson(deviceId, statusList);
|
|
||||||
if (newStatus != deviceStatus) {
|
|
||||||
deviceStatus = newStatus;
|
|
||||||
if (!isClosed) {
|
|
||||||
add(StatusUpdated(deviceStatus));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch (e) {
|
|
||||||
emit(OneGangGlassSwitchError('Failed to listen to changes: $e'));
|
deviceStatus =
|
||||||
}
|
OneGangGlassStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||||
|
|
||||||
|
add(StatusUpdated(deviceStatus));
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onStatusUpdated(
|
void _onStatusUpdated(
|
||||||
@ -174,4 +160,10 @@ class OneGangGlassSwitchBloc
|
|||||||
deviceStatus = deviceStatus.copyWith(switch1: value);
|
deviceStatus = deviceStatus.copyWith(switch1: value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
_deviceStatusSubscription?.cancel();
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,7 +41,7 @@ class ThreeGangGlassSwitchBloc
|
|||||||
emit(ThreeGangGlassSwitchLoading());
|
emit(ThreeGangGlassSwitchLoading());
|
||||||
try {
|
try {
|
||||||
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||||
_listenToChanges(event.deviceId, emit);
|
_listenToChanges(event.deviceId);
|
||||||
deviceStatus =
|
deviceStatus =
|
||||||
ThreeGangGlassStatusModel.fromJson(event.deviceId, status.status);
|
ThreeGangGlassStatusModel.fromJson(event.deviceId, status.status);
|
||||||
emit(ThreeGangGlassSwitchStatusLoaded(deviceStatus));
|
emit(ThreeGangGlassSwitchStatusLoaded(deviceStatus));
|
||||||
@ -50,42 +50,28 @@ class ThreeGangGlassSwitchBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _listenToChanges(
|
StreamSubscription<DatabaseEvent>? _deviceStatusSubscription;
|
||||||
String deviceId,
|
|
||||||
Emitter<ThreeGangGlassSwitchState> emit,
|
void _listenToChanges(String deviceId) {
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||||
final stream = ref.onValue;
|
_deviceStatusSubscription = ref.onValue.listen((DatabaseEvent event) async {
|
||||||
|
if (event.snapshot.value == null) return;
|
||||||
|
|
||||||
stream.listen((DatabaseEvent event) {
|
final usersMap = event.snapshot.value! as Map<dynamic, dynamic>;
|
||||||
final data = event.snapshot.value as Map<dynamic, dynamic>?;
|
|
||||||
if (data == null) return;
|
|
||||||
|
|
||||||
final statusList = <Status>[];
|
final statusList = <Status>[];
|
||||||
if (data['status'] != null) {
|
|
||||||
for (var element in data['status']) {
|
usersMap['status'].forEach((element) {
|
||||||
statusList.add(
|
statusList.add(Status(code: element['code'], value: element['value']));
|
||||||
Status(
|
|
||||||
code: element['code'].toString(),
|
|
||||||
value: element['value'].toString(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (statusList.isNotEmpty) {
|
|
||||||
final newStatus = ThreeGangGlassStatusModel.fromJson(deviceId, statusList);
|
|
||||||
if (newStatus != deviceStatus) {
|
|
||||||
deviceStatus = newStatus;
|
|
||||||
if (!isClosed) {
|
|
||||||
add(StatusUpdated(deviceStatus));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch (e) {
|
|
||||||
emit(ThreeGangGlassSwitchError('Failed to listen to changes: $e'));
|
deviceStatus =
|
||||||
}
|
ThreeGangGlassStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||||
|
|
||||||
|
add(StatusUpdated(deviceStatus));
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onStatusUpdated(
|
void _onStatusUpdated(
|
||||||
@ -184,4 +170,10 @@ class ThreeGangGlassSwitchBloc
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
_deviceStatusSubscription?.cancel();
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:developer';
|
|
||||||
|
|
||||||
import 'package:bloc/bloc.dart';
|
import 'package:bloc/bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
@ -51,29 +50,28 @@ class TwoGangGlassSwitchBloc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StreamSubscription<DatabaseEvent>? _deviceStatusSubscription;
|
||||||
|
|
||||||
void _listenToChanges(String deviceId) {
|
void _listenToChanges(String deviceId) {
|
||||||
try {
|
try {
|
||||||
final ref = FirebaseDatabase.instance.ref(
|
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||||
'device-status/$deviceId',
|
_deviceStatusSubscription = ref.onValue.listen((DatabaseEvent event) async {
|
||||||
);
|
if (event.snapshot.value == null) return;
|
||||||
|
|
||||||
ref.onValue.listen((event) {
|
final usersMap = event.snapshot.value! as Map<dynamic, dynamic>;
|
||||||
final eventsMap = event.snapshot.value as Map<dynamic, dynamic>;
|
|
||||||
|
|
||||||
List<Status> statusList = [];
|
final statusList = <Status>[];
|
||||||
eventsMap['status'].forEach((element) {
|
|
||||||
|
usersMap['status'].forEach((element) {
|
||||||
statusList.add(Status(code: element['code'], value: element['value']));
|
statusList.add(Status(code: element['code'], value: element['value']));
|
||||||
});
|
});
|
||||||
|
|
||||||
deviceStatus = TwoGangGlassStatusModel.fromJson(deviceId, statusList);
|
deviceStatus =
|
||||||
|
TwoGangGlassStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||||
|
|
||||||
add(StatusUpdated(deviceStatus));
|
add(StatusUpdated(deviceStatus));
|
||||||
});
|
});
|
||||||
} catch (_) {
|
} catch (_) {}
|
||||||
log(
|
|
||||||
'Error listening to changes',
|
|
||||||
name: 'TwoGangGlassSwitchBloc._listenToChanges',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onControl(
|
Future<void> _onControl(
|
||||||
@ -170,4 +168,10 @@ class TwoGangGlassSwitchBloc
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
_deviceStatusSubscription?.cancel();
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user