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'; import '../../features/devices/model/create_temporary_password_model.dart'; class DevicesAPI { static final HTTPService _httpService = HTTPService(); static Future> firmwareDevice( {required String deviceId, required String firmwareVersion}) async { try { final response = await _httpService.post( path: ApiEndpoints.firmwareDevice .replaceAll('{deviceUuid}', deviceId) .replaceAll('{firmwareVersion}', firmwareVersion), showServerMessage: false, expectedResponseModel: (json) { return json; }, ); return response; } catch (e) { rethrow; } } 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('{unitUuid}', spaceId), // queryParameters: params, showServerMessage: false, expectedResponseModel: (json) => DevicesCategoryModel.fromJsonList(json), ); 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; } static Future> renamePass({ required String name, required String doorLockUuid, required String passwordId}) async { final response = await _httpService.put( path: ApiEndpoints.renamePassword .replaceAll('{doorLockUuid}', doorLockUuid) .replaceAll('{passwordId}', passwordId), body: { "name": name }, 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> getDeviceByGroupName(String unitId, String groupName) async { final response = await _httpService.get( path: ApiEndpoints.devicesByGroupName .replaceAll('{unitUuid}', unitId) .replaceAll('{groupName}', groupName), 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; } static Future> getDevicesByRoomId(String roomId) async { 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; } static Future> getDevicesByGatewayId(String gatewayId) async { final response = await _httpService.get( path: ApiEndpoints.gatewayApi.replaceAll('{gatewayUuid}', gatewayId), showServerMessage: false, expectedResponseModel: (json) { List devices = []; if (json == null || json.isEmpty || json == []) { return devices; } for (var device in json['devices']) { devices.add(DeviceModel.fromJson(device)); } return devices; }, ); return response; } static Future getTemporaryPasswords(String deviceId, ) async { final response = await _httpService.get( path: ApiEndpoints.getTemporaryPassword.replaceAll('{doorLockUuid}', deviceId), showServerMessage: false, expectedResponseModel: (json) { return json; }, ); return response; } static Future getOneTimePasswords(String deviceId) async { final response = await _httpService.get( path: ApiEndpoints.getOneTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId), showServerMessage: false, expectedResponseModel: (json) { return json; }, ); return response; } static Future getTimeLimitPasswords(String deviceId) async { final response = await _httpService.get( path: ApiEndpoints.getMultipleTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId), showServerMessage: false, expectedResponseModel: (json) { return json; }, ); return response; } //create password static Future createPassword({ required String name, required String password, required String effectiveTime, required String invalidTime, required String deviceId, List? scheduleList,}) async { Map body = { "name": name, "password": password, "effectiveTime": effectiveTime, "invalidTime": invalidTime, }; print('createPassword =$body'); if (scheduleList != null) { body["scheduleList"] = scheduleList.map((schedule) => schedule.toJson()).toList(); } final response = await _httpService.post( path: ApiEndpoints.addTemporaryPassword.replaceAll('{doorLockUuid}', deviceId), body: body, showServerMessage: false, expectedResponseModel: (json) => json, ); return response; } static Future generateOneTimePassword({deviceId}) async { try { final response = await _httpService.post( path: ApiEndpoints.addOneTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId), showServerMessage: false, expectedResponseModel: (json) { return json; }, ); return response; } catch (e) { rethrow; } } static Future generateMultiTimePassword({deviceId,effectiveTime,invalidTime}) async { try { final response = await _httpService.post( path: ApiEndpoints.addMultipleTimeTemporaryPassword.replaceAll('{doorLockUuid}', deviceId), showServerMessage: true, body: { "effectiveTime": effectiveTime, "invalidTime": invalidTime }, expectedResponseModel: (json) { return json; }, ); return response; } catch (e) { rethrow; } } static Future> deletePassword( {required String passwordId, required String deviceId}) async { final response = await _httpService.delete( path: ApiEndpoints.deleteTemporaryPassword .replaceAll('{doorLockUuid}', deviceId) .replaceAll('{passwordId}', passwordId), showServerMessage: false, expectedResponseModel: (json) { return json; }, ); return response; } }