Add status model and fetch device statuses

Added a new StatusModel class to represent device statuses and implemented
functionality to fetch and update device statuses in the DevicesCubit and
DeviceModel classes. Also updated UI components to display device status
information.
This commit is contained in:
Mohammad Salameh
2024-04-03 11:07:55 +03:00
parent 5ff5f65549
commit e49627d3e1
8 changed files with 134 additions and 32 deletions
@@ -8,6 +8,7 @@ import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/ACs/acs_view.dart';
import 'package:syncrow_app/features/devices/view/widgets/curtains/curtain_view.dart';
import 'package:syncrow_app/features/devices/view/widgets/gateway/gateway_view.dart';
@@ -280,6 +281,13 @@ class DevicesCubit extends Cubit<DevicesState> {
.indexWhere((element) => element.id == roomId);
HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices =
await SpacesAPI.getDevicesByRoomId(roomId);
//get status for each device
for (var device in HomeCubit.getInstance()
.selectedSpace!
.rooms![roomIndex]
.devices!) {
getDevicesStatues(device.id!, roomIndex);
}
emitSafe(GetDevicesSuccess());
} on DioException catch (error) {
@@ -289,6 +297,38 @@ class DevicesCubit extends Cubit<DevicesState> {
}
}
getDevicesStatues(String deviceId, int roomIndex) async {
if (_isClosed) return;
try {
emitSafe(GetDeviceStatusLoading());
int deviceIndex = HomeCubit.getInstance()
.selectedSpace!
.rooms![roomIndex]
.devices!
.indexWhere((element) => element.id == deviceId);
List<StatusModel> statuses = [];
var response = await DevicesAPI.getDeviceStatus(deviceId);
print('response : $response');
for (var status in response['result']['status']) {
statuses.add(StatusModel.fromJson(status));
}
HomeCubit.getInstance()
.selectedSpace!
.rooms![roomIndex]
.devices![deviceIndex]
.status = statuses;
emitSafe(GetDeviceStatusSuccess());
} on DioException catch (error) {
emitSafe(
GetDeviceStatusError(ServerFailure.fromDioError(error).errMessage),
);
}
}
///Lights
onHorizontalDragUpdate(DeviceModel light, double dx, double screenWidth) {
double newBrightness = (dx / (screenWidth - 15) * 100);
@@ -15,6 +15,16 @@ class ChangeIndex extends DevicesState {}
// Devices
class GetDeviceStatusLoading extends DevicesState {}
class GetDeviceStatusSuccess extends DevicesState {}
class GetDeviceStatusError extends DevicesState {
final String errorMsg;
GetDeviceStatusError(this.errorMsg);
}
class GetDevicesLoading extends DevicesState {}
class GetDevicesSuccess extends DevicesState {}