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