diff --git a/lib/pages/device_managment/all_devices/models/factory_reset_model.dart b/lib/pages/device_managment/all_devices/models/factory_reset_model.dart index aec14d16..56c6c90b 100644 --- a/lib/pages/device_managment/all_devices/models/factory_reset_model.dart +++ b/lib/pages/device_managment/all_devices/models/factory_reset_model.dart @@ -2,14 +2,17 @@ import 'package:flutter/foundation.dart'; class FactoryResetModel { final List devicesUuid; + final String operationType; FactoryResetModel({ required this.devicesUuid, + this.operationType = "RESET", }); factory FactoryResetModel.fromJson(Map json) { return FactoryResetModel( devicesUuid: List.from(json['devicesUuid']), + operationType: "RESET", ); } diff --git a/lib/pages/visitor_password/bloc/visitor_password_bloc.dart b/lib/pages/visitor_password/bloc/visitor_password_bloc.dart index 45fb03fe..438b1abf 100644 --- a/lib/pages/visitor_password/bloc/visitor_password_bloc.dart +++ b/lib/pages/visitor_password/bloc/visitor_password_bloc.dart @@ -194,9 +194,10 @@ class VisitorPasswordBloc emit(DeviceLoaded()); final projectUuid = await ProjectManager.getProjectUUID() ?? ''; - data = await AccessMangApi().fetchDevices(projectUuid); + data = await AccessMangApi().fetchDoorLockDeviceList(projectUuid); emit(TableLoaded(data)); } catch (e) { + print("error: $e"); emit(FailedState(e.toString())); } } diff --git a/lib/services/access_mang_api.dart b/lib/services/access_mang_api.dart index 6acbc395..a780a12b 100644 --- a/lib/services/access_mang_api.dart +++ b/lib/services/access_mang_api.dart @@ -6,13 +6,23 @@ import 'package:syncrow_web/services/api/http_service.dart'; import 'package:syncrow_web/utils/constants/api_const.dart'; class AccessMangApi { + AccessMangApi() { + _validateEndpoints(); + } + + void _validateEndpoints() { + if (!ApiEndpoints.getDevices.contains('{projectId}')) { + throw Exception("Endpoint 'getDevices' must contain '{projectId}' placeholder."); + } + } + Future> fetchVisitorPassword(String projectId) async { try { final response = await HTTPService().get( - path: ApiEndpoints.visitorPassword.replaceAll('{projectId}', projectId), + path: ApiEndpoints.visitorPassword, showServerMessage: true, expectedResponseModel: (json) { - List jsonData = json; + List jsonData = json['data'] ?? []; List passwordList = jsonData.map((jsonItem) { return PasswordModel.fromJson(jsonItem); }).toList(); @@ -25,17 +35,22 @@ class AccessMangApi { } } - Future fetchDevices(String projectId) async { + Future fetchDoorLockDeviceList(String projectId) async { try { + // The endpoint structure is already validated during initialization. + final response = await HTTPService().get( path: ApiEndpoints.getDevices.replaceAll('{projectId}', projectId), + queryParameters: { + 'deviceType': 'DOOR_LOCK', + }, showServerMessage: true, expectedResponseModel: (json) { - List jsonData = json; - List passwordList = jsonData.map((jsonItem) { + List jsonData = json['data'] ?? []; + List deviceList = jsonData.map((jsonItem) { return DeviceModel.fromJson(jsonItem); }).toList(); - return passwordList; + return deviceList; }, ); return response; @@ -52,14 +67,15 @@ class AccessMangApi { String? invalidTime, List? devicesUuid}) async { final response = await HTTPService().post( - path: ApiEndpoints.sendOnlineOneTime, + path: ApiEndpoints.visitorPassword, body: jsonEncode({ "email": email, "passwordName": passwordName, "password": password, "devicesUuid": devicesUuid, "effectiveTime": effectiveTime, - "invalidTime": invalidTime + "invalidTime": invalidTime, + "operationType": "ONLINE_ONE_TIME", }), showServerMessage: true, expectedResponseModel: (json) { @@ -84,13 +100,13 @@ class AccessMangApi { "password": password, "effectiveTime": effectiveTime, "invalidTime": invalidTime, + "operationType": "ONLINE_MULTIPLE_TIME", }; if (scheduleList != null) { - body["scheduleList"] = - scheduleList.map((schedule) => schedule.toJson()).toList(); + body["scheduleList"] = scheduleList.map((schedule) => schedule.toJson()).toList(); } final response = await HTTPService().post( - path: ApiEndpoints.sendOnlineMultipleTime, + path: ApiEndpoints.visitorPassword, body: jsonEncode(body), showServerMessage: true, expectedResponseModel: (json) { @@ -105,8 +121,9 @@ class AccessMangApi { Future postOffLineOneTime( {String? email, String? passwordName, List? devicesUuid}) async { final response = await HTTPService().post( - path: ApiEndpoints.sendOffLineOneTime, + path: ApiEndpoints.visitorPassword, body: jsonEncode({ + "operationType": "OFFLINE_ONE_TIME", "email": email, "passwordName": passwordName, "devicesUuid": devicesUuid @@ -126,13 +143,14 @@ class AccessMangApi { String? invalidTime, List? devicesUuid}) async { final response = await HTTPService().post( - path: ApiEndpoints.sendOffLineMultipleTime, + path: ApiEndpoints.visitorPassword, body: jsonEncode({ "email": email, "devicesUuid": devicesUuid, "passwordName": passwordName, "effectiveTime": effectiveTime, "invalidTime": invalidTime, + "operationType": "OFFLINE_MULTIPLE_TIME", }), showServerMessage: true, expectedResponseModel: (json) { diff --git a/lib/services/devices_mang_api.dart b/lib/services/devices_mang_api.dart index 604abaf4..4bd6f884 100644 --- a/lib/services/devices_mang_api.dart +++ b/lib/services/devices_mang_api.dart @@ -23,9 +23,8 @@ class DevicesManagementApi { : ApiEndpoints.getAllDevices.replaceAll('{projectId}', projectId), showServerMessage: true, expectedResponseModel: (json) { - List jsonData = communityId.isNotEmpty && spaceId.isNotEmpty - ? json['data'] - : json; + List jsonData = + communityId.isNotEmpty && spaceId.isNotEmpty ? json['data'] : json['data']; List devicesList = jsonData.map((jsonItem) { return AllDevicesModel.fromJson(jsonItem); }).toList(); @@ -34,7 +33,7 @@ class DevicesManagementApi { ); return response; } catch (e) { - debugPrint('fetchDevices Error fetching $e'); + debugPrint('Error fetching device $e'); return []; } } @@ -45,7 +44,7 @@ class DevicesManagementApi { path: ApiEndpoints.getDeviceStatus.replaceAll('{uuid}', uuid), showServerMessage: true, expectedResponseModel: (json) { - return DeviceStatus.fromJson(json); + return DeviceStatus.fromJson(json['data']); }, ); return response; @@ -62,7 +61,7 @@ class DevicesManagementApi { Future getPowerClampInfo(String deviceId) async { try { final response = await HTTPService().get( - path: ApiEndpoints.powerClamp.replaceAll('{powerClampUuid}', deviceId), + path: ApiEndpoints.getDeviceStatus.replaceAll('{uuid}', deviceId), showServerMessage: true, expectedResponseModel: (json) { return json; @@ -100,6 +99,7 @@ class DevicesManagementApi { 'devicesUuid': uuids, 'code': code, 'value': value, + 'operationType': 'COMMAND', }; final response = await HTTPService().post( @@ -107,7 +107,7 @@ class DevicesManagementApi { body: body, showServerMessage: true, expectedResponseModel: (json) { - return (json['successResults'] as List).isNotEmpty; + return json['success'] ?? false; }, ); @@ -128,7 +128,7 @@ class DevicesManagementApi { if (json == null || json.isEmpty || json == []) { return devices; } - for (var device in json['devices']) { + for (var device in json['data']['devices']) { devices.add(DeviceModel.fromJson(device)); } return devices; @@ -158,7 +158,7 @@ class DevicesManagementApi { .replaceAll('{code}', code), showServerMessage: false, expectedResponseModel: (json) { - return DeviceReport.fromJson(json); + return DeviceReport.fromJson(json['data']); }, ); return response; @@ -174,7 +174,7 @@ class DevicesManagementApi { .replaceAll('{endTime}', to ?? ''), showServerMessage: false, expectedResponseModel: (json) { - return DeviceReport.fromJson(json); + return DeviceReport.fromJson(json['data']); }, ); return response; @@ -190,7 +190,7 @@ class DevicesManagementApi { queryParameters: queryParameters, showServerMessage: true, expectedResponseModel: (json) { - return DeviceStatus.fromJson(json['status']); + return DeviceStatus.fromJson(json['data']['status']); }, ); return response; diff --git a/lib/utils/constants/api_const.dart b/lib/utils/constants/api_const.dart index be972392..454ec46d 100644 --- a/lib/utils/constants/api_const.dart +++ b/lib/utils/constants/api_const.dart @@ -9,21 +9,8 @@ abstract class ApiEndpoints { static const String sendOtp = '/authentication/user/send-otp'; static const String verifyOtp = '/authentication/user/verify-otp'; static const String getRegion = '/region'; - static const String visitorPassword = - '/projects/{projectId}/visitor-password'; - static const String getDevices = - '/projects/{projectId}/visitor-password/devices'; - - static const String sendOnlineOneTime = - '/visitor-password/temporary-password/online/one-time'; - static const String sendOnlineMultipleTime = - '/visitor-password/temporary-password/online/multiple-time'; - -//offline Password - static const String sendOffLineOneTime = - '/visitor-password/temporary-password/offline/one-time'; - static const String sendOffLineMultipleTime = - '/visitor-password/temporary-password/offline/multiple-time'; + static const String visitorPassword = '/visitor-passwords'; + static const String getDevices = '/projects/{projectId}/devices'; static const String getUser = '/user/{userUuid}'; @@ -32,15 +19,15 @@ abstract class ApiEndpoints { static const String getAllDevices = '/projects/{projectId}/devices'; static const String getSpaceDevices = '/projects/{projectId}/communities/{communityUuid}/spaces/{spaceUuid}/devices'; - static const String getDeviceStatus = '/device/{uuid}/functions/status'; - static const String getBatchStatus = '/device/status/batch'; + static const String getDeviceStatus = '/devices/{uuid}/functions/status'; + static const String getBatchStatus = '/devices/batch'; - static const String deviceControl = '/device/{uuid}/control'; - static const String deviceBatchControl = '/device/control/batch'; - static const String gatewayApi = '/device/gateway/{gatewayUuid}/devices'; + static const String deviceControl = '/devices/{uuid}/command'; + static const String deviceBatchControl = '/devices/batch'; + static const String gatewayApi = '/devices/gateway/{gatewayUuid}/devices'; static const String openDoorLock = '/door-lock/open/{doorLockUuid}'; - static const String getDeviceLogs = '/device/report-logs/{uuid}?code={code}'; + static const String getDeviceLogs = '/devices/{uuid}/report-logs?code={code}'; // Space Module static const String createSpace = @@ -70,18 +57,13 @@ abstract class ApiEndpoints { static const String createUserCommunity = '/projects/{projectId}/communities/user'; static const String getDeviceLogsByDate = - '/device/report-logs/{uuid}?code={code}&startTime={startTime}&endTime={endTime}'; + '/devices/{uuid}/report-logs?code={code}&startTime={startTime}&endTime={endTime}'; static const String scheduleByDeviceId = '/schedule/{deviceUuid}'; - static const String getScheduleByDeviceId = - '/schedule/{deviceUuid}?category={category}'; - static const String deleteScheduleByDeviceId = - '/schedule/{deviceUuid}/{scheduleUuid}'; - static const String updateScheduleByDeviceId = - '/schedule/enable/{deviceUuid}'; - static const String factoryReset = '/device/factory/reset/{deviceUuid}'; - static const String powerClamp = - '/device/{powerClampUuid}/power-clamp/status'; + static const String getScheduleByDeviceId = '/schedule/{deviceUuid}?category={category}'; + static const String deleteScheduleByDeviceId = '/schedule/{deviceUuid}/{scheduleUuid}'; + static const String updateScheduleByDeviceId = '/schedule/enable/{deviceUuid}'; + static const String factoryReset = '/devices/batch'; //product static const String listProducts = '/products';