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/function_model.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> 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> fetchGroups(String spaceId) async { Map 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> 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 deviceFunctions(String deviceId) async { final response = await _httpService.get( path: ApiEndpoints.deviceFunctions.replaceAll('{deviceUuid}', deviceId), showServerMessage: false, expectedResponseModel: (json) { final functions = FunctionModel.fromJson(json); return functions; }, ); return response; } static Future> 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 []; } List devices = []; for (var device in json) { devices.add(DeviceModel.fromJson(device)); } return devices; }, ); return response; } }