Files
syncrow-app/lib/services/api/devices_api.dart
2024-06-25 10:38:39 +03:00

92 lines
2.8 KiB
Dart

import 'dart:async';
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/functions.dart';
import 'package:syncrow_app/services/api/api_links_endpoints.dart';
import 'package:syncrow_app/services/api/http_service.dart';
class DevicesAPI {
static final HTTPService _httpService = HTTPService();
static Future<Map<String, dynamic>> controlDevice(
DeviceControlModel controlModel, String deviceId) async {
try {
final response = await _httpService.post(
path: ApiEndpoints.controlDevice.replaceAll('{deviceUuid}', deviceId),
body: controlModel.toJson(),
showServerMessage: false,
expectedResponseModel: (json) {
return json;
},
);
return response;
} catch (e) {
rethrow;
}
}
static Future<List<DevicesCategoryModel>> fetchGroups(String spaceId) async {
Map<String, dynamic> params = {
"homeId": spaceId,
"pageSize": 100,
"pageNo": 1
};
final response = await _httpService.get(
path: ApiEndpoints.groupBySpace.replaceAll("{spaceUuid}", spaceId),
queryParameters: params,
showServerMessage: false,
expectedResponseModel: (json) =>
DevicesCategoryModel.fromJsonList(json['groups']),
);
return response;
}
static Future<Map<String, dynamic>> getDeviceStatus(String deviceId) async {
final response = await _httpService.get(
path: ApiEndpoints.deviceFunctionsStatus
.replaceAll('{deviceUuid}', deviceId),
showServerMessage: false,
expectedResponseModel: (json) {
return json;
},
);
return response;
}
/// Get Device Functions
static Future<FunctionsEntity> deviceFunctions(String deviceId) async {
final response = await _httpService.get(
path: ApiEndpoints.deviceFunctions.replaceAll('{deviceUuid}', deviceId),
showServerMessage: false,
expectedResponseModel: (json) {
final functions = FunctionsEntity.fromJson(json);
return functions;
},
);
return response;
}
static Future<List<DeviceModel>> getDevicesByRoomId(String roomId) async {
// print("Room ID: $roomId");
final response = await _httpService.get(
path: ApiEndpoints.deviceByRoom,
queryParameters: {"roomUuid": roomId},
showServerMessage: false,
expectedResponseModel: (json) {
if (json == null || json.isEmpty || json == []) {
return <DeviceModel>[];
}
List<DeviceModel> devices = [];
for (var device in json) {
devices.add(DeviceModel.fromJson(device));
}
return devices;
},
);
return response;
}
}