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(),
|
deviceStatus =
|
||||||
),
|
OneGangGlassStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||||
);
|
|
||||||
}
|
add(StatusUpdated(deviceStatus));
|
||||||
}
|
|
||||||
if (statusList.isNotEmpty) {
|
|
||||||
final newStatus = OneGangGlassStatusModel.fromJson(deviceId, statusList);
|
|
||||||
if (newStatus != deviceStatus) {
|
|
||||||
deviceStatus = newStatus;
|
|
||||||
if (!isClosed) {
|
|
||||||
add(StatusUpdated(deviceStatus));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (_) {}
|
||||||
emit(OneGangGlassSwitchError('Failed to listen to changes: $e'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(),
|
deviceStatus =
|
||||||
),
|
ThreeGangGlassStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||||
);
|
|
||||||
}
|
add(StatusUpdated(deviceStatus));
|
||||||
}
|
|
||||||
if (statusList.isNotEmpty) {
|
|
||||||
final newStatus = ThreeGangGlassStatusModel.fromJson(deviceId, statusList);
|
|
||||||
if (newStatus != deviceStatus) {
|
|
||||||
deviceStatus = newStatus;
|
|
||||||
if (!isClosed) {
|
|
||||||
add(StatusUpdated(deviceStatus));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (_) {}
|
||||||
emit(ThreeGangGlassSwitchError('Failed to listen to changes: $e'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onStatusUpdated(
|
void _onStatusUpdated(
|
||||||
@ -184,4 +170,10 @@ class ThreeGangGlassSwitchBloc
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
_deviceStatusSubscription?.cancel();
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,173 +1,177 @@
|
|||||||
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';
|
||||||
import 'package:firebase_database/firebase_database.dart';
|
import 'package:firebase_database/firebase_database.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
|
||||||
import 'package:syncrow_web/pages/device_managment/two_g_glass_switch/models/two_gang_glass_status_model.dart';
|
import 'package:syncrow_web/pages/device_managment/two_g_glass_switch/models/two_gang_glass_status_model.dart';
|
||||||
import 'package:syncrow_web/services/batch_control_devices_service.dart';
|
import 'package:syncrow_web/services/batch_control_devices_service.dart';
|
||||||
import 'package:syncrow_web/services/control_device_service.dart';
|
import 'package:syncrow_web/services/control_device_service.dart';
|
||||||
import 'package:syncrow_web/services/devices_mang_api.dart';
|
import 'package:syncrow_web/services/devices_mang_api.dart';
|
||||||
|
|
||||||
part 'two_gang_glass_switch_event.dart';
|
part 'two_gang_glass_switch_event.dart';
|
||||||
part 'two_gang_glass_switch_state.dart';
|
part 'two_gang_glass_switch_state.dart';
|
||||||
|
|
||||||
class TwoGangGlassSwitchBloc
|
class TwoGangGlassSwitchBloc
|
||||||
extends Bloc<TwoGangGlassSwitchEvent, TwoGangGlassSwitchState> {
|
extends Bloc<TwoGangGlassSwitchEvent, TwoGangGlassSwitchState> {
|
||||||
final String deviceId;
|
final String deviceId;
|
||||||
final ControlDeviceService controlDeviceService;
|
final ControlDeviceService controlDeviceService;
|
||||||
final BatchControlDevicesService batchControlDevicesService;
|
final BatchControlDevicesService batchControlDevicesService;
|
||||||
|
|
||||||
late TwoGangGlassStatusModel deviceStatus;
|
late TwoGangGlassStatusModel deviceStatus;
|
||||||
|
|
||||||
TwoGangGlassSwitchBloc({
|
TwoGangGlassSwitchBloc({
|
||||||
required this.deviceId,
|
required this.deviceId,
|
||||||
required this.controlDeviceService,
|
required this.controlDeviceService,
|
||||||
required this.batchControlDevicesService,
|
required this.batchControlDevicesService,
|
||||||
}) : super(TwoGangGlassSwitchInitial()) {
|
}) : super(TwoGangGlassSwitchInitial()) {
|
||||||
on<TwoGangGlassSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
|
on<TwoGangGlassSwitchFetchDeviceEvent>(_onFetchDeviceStatus);
|
||||||
on<TwoGangGlassSwitchControl>(_onControl);
|
on<TwoGangGlassSwitchControl>(_onControl);
|
||||||
on<TwoGangGlassSwitchBatchControl>(_onBatchControl);
|
on<TwoGangGlassSwitchBatchControl>(_onBatchControl);
|
||||||
on<TwoGangGlassSwitchFetchBatchStatusEvent>(_onFetchBatchStatus);
|
on<TwoGangGlassSwitchFetchBatchStatusEvent>(_onFetchBatchStatus);
|
||||||
on<TwoGangGlassFactoryReset>(_onFactoryReset);
|
on<TwoGangGlassFactoryReset>(_onFactoryReset);
|
||||||
on<StatusUpdated>(_onStatusUpdated);
|
on<StatusUpdated>(_onStatusUpdated);
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _onFetchDeviceStatus(
|
|
||||||
TwoGangGlassSwitchFetchDeviceEvent event,
|
|
||||||
Emitter<TwoGangGlassSwitchState> emit,
|
|
||||||
) async {
|
|
||||||
emit(TwoGangGlassSwitchLoading());
|
|
||||||
try {
|
|
||||||
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
|
||||||
deviceStatus = TwoGangGlassStatusModel.fromJson(event.deviceId, status.status);
|
|
||||||
_listenToChanges(event.deviceId);
|
|
||||||
emit(TwoGangGlassSwitchStatusLoaded(deviceStatus));
|
|
||||||
} catch (e) {
|
|
||||||
emit(TwoGangGlassSwitchError(e.toString()));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void _listenToChanges(String deviceId) {
|
Future<void> _onFetchDeviceStatus(
|
||||||
try {
|
TwoGangGlassSwitchFetchDeviceEvent event,
|
||||||
final ref = FirebaseDatabase.instance.ref(
|
Emitter<TwoGangGlassSwitchState> emit,
|
||||||
'device-status/$deviceId',
|
) async {
|
||||||
);
|
emit(TwoGangGlassSwitchLoading());
|
||||||
|
try {
|
||||||
ref.onValue.listen((event) {
|
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
||||||
final eventsMap = event.snapshot.value as Map<dynamic, dynamic>;
|
deviceStatus = TwoGangGlassStatusModel.fromJson(event.deviceId, status.status);
|
||||||
|
_listenToChanges(event.deviceId);
|
||||||
List<Status> statusList = [];
|
emit(TwoGangGlassSwitchStatusLoaded(deviceStatus));
|
||||||
eventsMap['status'].forEach((element) {
|
} catch (e) {
|
||||||
statusList.add(Status(code: element['code'], value: element['value']));
|
emit(TwoGangGlassSwitchError(e.toString()));
|
||||||
});
|
|
||||||
|
|
||||||
deviceStatus = TwoGangGlassStatusModel.fromJson(deviceId, statusList);
|
|
||||||
add(StatusUpdated(deviceStatus));
|
|
||||||
});
|
|
||||||
} catch (_) {
|
|
||||||
log(
|
|
||||||
'Error listening to changes',
|
|
||||||
name: 'TwoGangGlassSwitchBloc._listenToChanges',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _onControl(
|
|
||||||
TwoGangGlassSwitchControl event,
|
|
||||||
Emitter<TwoGangGlassSwitchState> emit,
|
|
||||||
) async {
|
|
||||||
emit(TwoGangGlassSwitchLoading());
|
|
||||||
_updateLocalValue(event.code, event.value);
|
|
||||||
emit(TwoGangGlassSwitchStatusLoaded(deviceStatus));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await controlDeviceService.controlDevice(
|
|
||||||
deviceUuid: event.deviceId,
|
|
||||||
status: Status(code: event.code, value: event.value),
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
_updateLocalValue(event.code, !event.value);
|
|
||||||
emit(TwoGangGlassSwitchError(e.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _onBatchControl(
|
|
||||||
TwoGangGlassSwitchBatchControl event,
|
|
||||||
Emitter<TwoGangGlassSwitchState> emit,
|
|
||||||
) async {
|
|
||||||
emit(TwoGangGlassSwitchLoading());
|
|
||||||
_updateLocalValue(event.code, event.value);
|
|
||||||
emit(TwoGangGlassSwitchBatchStatusLoaded(deviceStatus));
|
|
||||||
|
|
||||||
try {
|
|
||||||
await batchControlDevicesService.batchControlDevices(
|
|
||||||
uuids: event.deviceIds,
|
|
||||||
code: event.code,
|
|
||||||
value: event.value,
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
_updateLocalValue(event.code, !event.value);
|
|
||||||
emit(TwoGangGlassSwitchError(e.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _onFetchBatchStatus(
|
|
||||||
TwoGangGlassSwitchFetchBatchStatusEvent event,
|
|
||||||
Emitter<TwoGangGlassSwitchState> emit,
|
|
||||||
) async {
|
|
||||||
emit(TwoGangGlassSwitchLoading());
|
|
||||||
try {
|
|
||||||
final status = await DevicesManagementApi().getBatchStatus(event.deviceIds);
|
|
||||||
deviceStatus = TwoGangGlassStatusModel.fromJson(
|
|
||||||
event.deviceIds.first,
|
|
||||||
status.status,
|
|
||||||
);
|
|
||||||
emit(TwoGangGlassSwitchBatchStatusLoaded(deviceStatus));
|
|
||||||
} catch (e) {
|
|
||||||
emit(TwoGangGlassSwitchError(e.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _onFactoryReset(
|
|
||||||
TwoGangGlassFactoryReset event,
|
|
||||||
Emitter<TwoGangGlassSwitchState> emit,
|
|
||||||
) async {
|
|
||||||
emit(TwoGangGlassSwitchLoading());
|
|
||||||
try {
|
|
||||||
final response = await DevicesManagementApi().factoryReset(
|
|
||||||
event.factoryReset,
|
|
||||||
event.deviceId,
|
|
||||||
);
|
|
||||||
if (!response) {
|
|
||||||
emit(TwoGangGlassSwitchError('Failed to reset device'));
|
|
||||||
} else {
|
|
||||||
add(TwoGangGlassSwitchFetchDeviceEvent(event.deviceId));
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
emit(TwoGangGlassSwitchError(e.toString()));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void _onStatusUpdated(
|
StreamSubscription<DatabaseEvent>? _deviceStatusSubscription;
|
||||||
StatusUpdated event,
|
|
||||||
Emitter<TwoGangGlassSwitchState> emit,
|
|
||||||
) {
|
|
||||||
deviceStatus = event.deviceStatus;
|
|
||||||
emit(TwoGangGlassSwitchStatusLoaded(deviceStatus));
|
|
||||||
}
|
|
||||||
|
|
||||||
void _updateLocalValue(String code, bool value) {
|
void _listenToChanges(String deviceId) {
|
||||||
switch (code) {
|
try {
|
||||||
case 'switch_1':
|
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
||||||
deviceStatus = deviceStatus.copyWith(switch1: value);
|
_deviceStatusSubscription = ref.onValue.listen((DatabaseEvent event) async {
|
||||||
break;
|
if (event.snapshot.value == null) return;
|
||||||
case 'switch_2':
|
|
||||||
deviceStatus = deviceStatus.copyWith(switch2: value);
|
final usersMap = event.snapshot.value! as Map<dynamic, dynamic>;
|
||||||
break;
|
|
||||||
|
final statusList = <Status>[];
|
||||||
|
|
||||||
|
usersMap['status'].forEach((element) {
|
||||||
|
statusList.add(Status(code: element['code'], value: element['value']));
|
||||||
|
});
|
||||||
|
|
||||||
|
deviceStatus =
|
||||||
|
TwoGangGlassStatusModel.fromJson(usersMap['productUuid'], statusList);
|
||||||
|
|
||||||
|
add(StatusUpdated(deviceStatus));
|
||||||
|
});
|
||||||
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onControl(
|
||||||
|
TwoGangGlassSwitchControl event,
|
||||||
|
Emitter<TwoGangGlassSwitchState> emit,
|
||||||
|
) async {
|
||||||
|
emit(TwoGangGlassSwitchLoading());
|
||||||
|
_updateLocalValue(event.code, event.value);
|
||||||
|
emit(TwoGangGlassSwitchStatusLoaded(deviceStatus));
|
||||||
|
|
||||||
|
try {
|
||||||
|
await controlDeviceService.controlDevice(
|
||||||
|
deviceUuid: event.deviceId,
|
||||||
|
status: Status(code: event.code, value: event.value),
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
_updateLocalValue(event.code, !event.value);
|
||||||
|
emit(TwoGangGlassSwitchError(e.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onBatchControl(
|
||||||
|
TwoGangGlassSwitchBatchControl event,
|
||||||
|
Emitter<TwoGangGlassSwitchState> emit,
|
||||||
|
) async {
|
||||||
|
emit(TwoGangGlassSwitchLoading());
|
||||||
|
_updateLocalValue(event.code, event.value);
|
||||||
|
emit(TwoGangGlassSwitchBatchStatusLoaded(deviceStatus));
|
||||||
|
|
||||||
|
try {
|
||||||
|
await batchControlDevicesService.batchControlDevices(
|
||||||
|
uuids: event.deviceIds,
|
||||||
|
code: event.code,
|
||||||
|
value: event.value,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
_updateLocalValue(event.code, !event.value);
|
||||||
|
emit(TwoGangGlassSwitchError(e.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onFetchBatchStatus(
|
||||||
|
TwoGangGlassSwitchFetchBatchStatusEvent event,
|
||||||
|
Emitter<TwoGangGlassSwitchState> emit,
|
||||||
|
) async {
|
||||||
|
emit(TwoGangGlassSwitchLoading());
|
||||||
|
try {
|
||||||
|
final status = await DevicesManagementApi().getBatchStatus(event.deviceIds);
|
||||||
|
deviceStatus = TwoGangGlassStatusModel.fromJson(
|
||||||
|
event.deviceIds.first,
|
||||||
|
status.status,
|
||||||
|
);
|
||||||
|
emit(TwoGangGlassSwitchBatchStatusLoaded(deviceStatus));
|
||||||
|
} catch (e) {
|
||||||
|
emit(TwoGangGlassSwitchError(e.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onFactoryReset(
|
||||||
|
TwoGangGlassFactoryReset event,
|
||||||
|
Emitter<TwoGangGlassSwitchState> emit,
|
||||||
|
) async {
|
||||||
|
emit(TwoGangGlassSwitchLoading());
|
||||||
|
try {
|
||||||
|
final response = await DevicesManagementApi().factoryReset(
|
||||||
|
event.factoryReset,
|
||||||
|
event.deviceId,
|
||||||
|
);
|
||||||
|
if (!response) {
|
||||||
|
emit(TwoGangGlassSwitchError('Failed to reset device'));
|
||||||
|
} else {
|
||||||
|
add(TwoGangGlassSwitchFetchDeviceEvent(event.deviceId));
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
emit(TwoGangGlassSwitchError(e.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onStatusUpdated(
|
||||||
|
StatusUpdated event,
|
||||||
|
Emitter<TwoGangGlassSwitchState> emit,
|
||||||
|
) {
|
||||||
|
deviceStatus = event.deviceStatus;
|
||||||
|
emit(TwoGangGlassSwitchStatusLoaded(deviceStatus));
|
||||||
|
}
|
||||||
|
|
||||||
|
void _updateLocalValue(String code, bool value) {
|
||||||
|
switch (code) {
|
||||||
|
case 'switch_1':
|
||||||
|
deviceStatus = deviceStatus.copyWith(switch1: value);
|
||||||
|
break;
|
||||||
|
case 'switch_2':
|
||||||
|
deviceStatus = deviceStatus.copyWith(switch2: value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
_deviceStatusSubscription?.cancel();
|
||||||
|
return super.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user