|
|
|
|
@ -45,8 +45,7 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
) async {
|
|
|
|
|
emit(AcsLoadingState());
|
|
|
|
|
try {
|
|
|
|
|
final status =
|
|
|
|
|
await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
|
|
|
|
final status = await DevicesManagementApi().getDeviceStatus(event.deviceId);
|
|
|
|
|
deviceStatus = AcStatusModel.fromJson(event.deviceId, status.status);
|
|
|
|
|
if (deviceStatus.countdown1 != 0) {
|
|
|
|
|
final totalMinutes = deviceStatus.countdown1 * 6;
|
|
|
|
|
@ -74,22 +73,18 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
void _listenToChanges(String deviceId) {
|
|
|
|
|
try {
|
|
|
|
|
final ref = FirebaseDatabase.instance.ref('device-status/$deviceId');
|
|
|
|
|
_deviceStatusSubscription =
|
|
|
|
|
ref.onValue.listen((DatabaseEvent event) async {
|
|
|
|
|
_deviceStatusSubscription = ref.onValue.listen((DatabaseEvent event) async {
|
|
|
|
|
if (event.snapshot.value == null) return;
|
|
|
|
|
|
|
|
|
|
Map<dynamic, dynamic> usersMap =
|
|
|
|
|
event.snapshot.value as Map<dynamic, dynamic>;
|
|
|
|
|
final usersMap = event.snapshot.value! as Map<dynamic, dynamic>;
|
|
|
|
|
|
|
|
|
|
List<Status> statusList = [];
|
|
|
|
|
final statusList = <Status>[];
|
|
|
|
|
|
|
|
|
|
usersMap['status'].forEach((element) {
|
|
|
|
|
statusList
|
|
|
|
|
.add(Status(code: element['code'], value: element['value']));
|
|
|
|
|
statusList.add(Status(code: element['code'], value: element['value']));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
deviceStatus =
|
|
|
|
|
AcStatusModel.fromJson(usersMap['productUuid'], statusList);
|
|
|
|
|
deviceStatus = AcStatusModel.fromJson(usersMap['productUuid'], statusList);
|
|
|
|
|
print('Device status updated: ${deviceStatus.acSwitch}');
|
|
|
|
|
|
|
|
|
|
if (!isClosed) {
|
|
|
|
|
@ -111,21 +106,14 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
AcControlEvent event,
|
|
|
|
|
Emitter<AcsState> emit,
|
|
|
|
|
) async {
|
|
|
|
|
emit(AcsLoadingState());
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final success = await controlDeviceService.controlDevice(
|
|
|
|
|
_updateDeviceFunctionFromCode(event.code, event.value);
|
|
|
|
|
emit(ACStatusLoaded(status: deviceStatus));
|
|
|
|
|
await controlDeviceService.controlDevice(
|
|
|
|
|
deviceUuid: event.deviceId,
|
|
|
|
|
status: Status(code: event.code, value: event.value),
|
|
|
|
|
);
|
|
|
|
|
_updateDeviceFunctionFromCode(event.code, event.value);
|
|
|
|
|
emit(ACStatusLoaded(status: deviceStatus));
|
|
|
|
|
if (!success) {
|
|
|
|
|
emit(const AcsFailedState(error: 'Failed to control device'));
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
emit(AcsFailedState(error: e.toString()));
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FutureOr<void> _onFetchAcBatchStatus(
|
|
|
|
|
@ -134,10 +122,8 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
) async {
|
|
|
|
|
emit(AcsLoadingState());
|
|
|
|
|
try {
|
|
|
|
|
final status =
|
|
|
|
|
await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
|
|
|
|
deviceStatus =
|
|
|
|
|
AcStatusModel.fromJson(event.devicesIds.first, status.status);
|
|
|
|
|
final status = await DevicesManagementApi().getBatchStatus(event.devicesIds);
|
|
|
|
|
deviceStatus = AcStatusModel.fromJson(event.devicesIds.first, status.status);
|
|
|
|
|
emit(ACStatusLoaded(status: deviceStatus));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
emit(AcsFailedState(error: e.toString()));
|
|
|
|
|
@ -148,23 +134,16 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
AcBatchControlEvent event,
|
|
|
|
|
Emitter<AcsState> emit,
|
|
|
|
|
) async {
|
|
|
|
|
emit(AcsLoadingState());
|
|
|
|
|
_updateDeviceFunctionFromCode(event.code, event.value);
|
|
|
|
|
emit(ACStatusLoaded(status: deviceStatus));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final success = await batchControlDevicesService.batchControlDevices(
|
|
|
|
|
await batchControlDevicesService.batchControlDevices(
|
|
|
|
|
uuids: event.devicesIds,
|
|
|
|
|
code: event.code,
|
|
|
|
|
value: event.value,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
|
emit(const AcsFailedState(error: 'Failed to control devices'));
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
emit(AcsFailedState(error: e.toString()));
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _onFactoryReset(
|
|
|
|
|
@ -197,8 +176,8 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
void _handleIncreaseTime(IncreaseTimeEvent event, Emitter<AcsState> emit) {
|
|
|
|
|
if (state is! ACStatusLoaded) return;
|
|
|
|
|
final currentState = state as ACStatusLoaded;
|
|
|
|
|
int newHours = scheduledHours;
|
|
|
|
|
int newMinutes = scheduledMinutes + 30;
|
|
|
|
|
var newHours = scheduledHours;
|
|
|
|
|
var newMinutes = scheduledMinutes + 30;
|
|
|
|
|
newHours += newMinutes ~/ 60;
|
|
|
|
|
newMinutes = newMinutes % 60;
|
|
|
|
|
if (newHours > 23) {
|
|
|
|
|
@ -220,7 +199,7 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
) {
|
|
|
|
|
if (state is! ACStatusLoaded) return;
|
|
|
|
|
final currentState = state as ACStatusLoaded;
|
|
|
|
|
int totalMinutes = (scheduledHours * 60) + scheduledMinutes;
|
|
|
|
|
var totalMinutes = (scheduledHours * 60) + scheduledMinutes;
|
|
|
|
|
totalMinutes = (totalMinutes - 30).clamp(0, 1440);
|
|
|
|
|
scheduledHours = totalMinutes ~/ 60;
|
|
|
|
|
scheduledMinutes = totalMinutes % 60;
|
|
|
|
|
@ -293,7 +272,7 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
|
|
|
|
|
|
|
|
|
void _startCountdownTimer(Emitter<AcsState> emit) {
|
|
|
|
|
_countdownTimer?.cancel();
|
|
|
|
|
int totalSeconds = (scheduledHours * 3600) + (scheduledMinutes * 60);
|
|
|
|
|
var totalSeconds = (scheduledHours * 3600) + (scheduledMinutes * 60);
|
|
|
|
|
|
|
|
|
|
_countdownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
|
|
|
if (totalSeconds > 0) {
|
|
|
|
|
|