mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
Merge branch 'dev' into space_managment_refactoring
This commit is contained in:
10
lib/services/api/api_exception.dart
Normal file
10
lib/services/api/api_exception.dart
Normal file
@ -0,0 +1,10 @@
|
||||
class APIException implements Exception {
|
||||
final String message;
|
||||
|
||||
APIException(this.message);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return message;
|
||||
}
|
||||
}
|
@ -1,18 +1,26 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_web/pages/auth/model/region_model.dart';
|
||||
import 'package:syncrow_web/pages/auth/model/token.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
import 'package:syncrow_web/utils/constants/api_const.dart';
|
||||
|
||||
class AuthenticationAPI {
|
||||
static Future<Token> loginWithEmail({required var model}) async {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.login,
|
||||
body: model.toJson(),
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {
|
||||
return Token.fromJson(json['data']);
|
||||
});
|
||||
return response;
|
||||
try {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.login,
|
||||
body: model.toJson(),
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {
|
||||
return Token.fromJson(json['data']);
|
||||
});
|
||||
return response;
|
||||
} on DioException catch (e) {
|
||||
final message = e.response?.data['error']['message'] ??
|
||||
'An error occurred while logging in';
|
||||
throw APIException(message);
|
||||
}
|
||||
}
|
||||
|
||||
static Future forgetPassword({
|
||||
@ -20,12 +28,18 @@ class AuthenticationAPI {
|
||||
required var password,
|
||||
required var otpCode,
|
||||
}) async {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.forgetPassword,
|
||||
body: {"email": email, "password": password, "otpCode": otpCode},
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {});
|
||||
return response;
|
||||
try {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.forgetPassword,
|
||||
body: {"email": email, "password": password, "otpCode": otpCode},
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {});
|
||||
return response;
|
||||
} on DioException catch (e) {
|
||||
final message = e.response?.data['error']['message'] ??
|
||||
'An error occurred while resetting the password';
|
||||
throw APIException(message);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<int?> sendOtp({required String email}) async {
|
||||
@ -39,19 +53,26 @@ class AuthenticationAPI {
|
||||
return response;
|
||||
}
|
||||
|
||||
static Future verifyOtp({required String email, required String otpCode}) async {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.verifyOtp,
|
||||
body: {"email": email, "type": "PASSWORD", "otpCode": otpCode},
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {
|
||||
if (json['message'] == 'Otp Verified Successfully') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return response;
|
||||
static Future verifyOtp(
|
||||
{required String email, required String otpCode}) async {
|
||||
try {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.verifyOtp,
|
||||
body: {"email": email, "type": "PASSWORD", "otpCode": otpCode},
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {
|
||||
if (json['message'] == 'Otp Verified Successfully') {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return response;
|
||||
} on APIException catch (e) {
|
||||
throw APIException(e.message);
|
||||
} catch (e) {
|
||||
throw APIException('An error occurred while verifying the OTP');
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<RegionModel>> fetchRegion() async {
|
||||
@ -59,7 +80,9 @@ class AuthenticationAPI {
|
||||
path: ApiEndpoints.getRegion,
|
||||
showServerMessage: true,
|
||||
expectedResponseModel: (json) {
|
||||
return (json as List).map((zone) => RegionModel.fromJson(zone)).toList();
|
||||
return (json as List)
|
||||
.map((zone) => RegionModel.fromJson(zone))
|
||||
.toList();
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
@ -91,7 +91,8 @@ class DevicesManagementApi {
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> deviceBatchControl(List<String> uuids, String code, dynamic value) async {
|
||||
Future<bool> deviceBatchControl(
|
||||
List<String> uuids, String code, dynamic value) async {
|
||||
try {
|
||||
final body = {
|
||||
'devicesUuid': uuids,
|
||||
@ -116,7 +117,8 @@ class DevicesManagementApi {
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<DeviceModel>> getDevicesByGatewayId(String gatewayId) async {
|
||||
static Future<List<DeviceModel>> getDevicesByGatewayId(
|
||||
String gatewayId) async {
|
||||
final response = await HTTPService().get(
|
||||
path: ApiEndpoints.gatewayApi.replaceAll('{gatewayUuid}', gatewayId),
|
||||
showServerMessage: false,
|
||||
@ -150,7 +152,9 @@ class DevicesManagementApi {
|
||||
String code,
|
||||
) async {
|
||||
final response = await HTTPService().get(
|
||||
path: ApiEndpoints.getDeviceLogs.replaceAll('{uuid}', uuid).replaceAll('{code}', code),
|
||||
path: ApiEndpoints.getDeviceLogs
|
||||
.replaceAll('{uuid}', uuid)
|
||||
.replaceAll('{code}', code),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
return DeviceReport.fromJson(json['data']);
|
||||
@ -223,7 +227,8 @@ class DevicesManagementApi {
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> addScheduleRecord(ScheduleEntry sendSchedule, String uuid) async {
|
||||
Future<bool> addScheduleRecord(
|
||||
ScheduleEntry sendSchedule, String uuid) async {
|
||||
try {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.scheduleByDeviceId.replaceAll('{deviceUuid}', uuid),
|
||||
@ -240,7 +245,8 @@ class DevicesManagementApi {
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<ScheduleModel>> getDeviceSchedules(String uuid, String category) async {
|
||||
Future<List<ScheduleModel>> getDeviceSchedules(
|
||||
String uuid, String category) async {
|
||||
try {
|
||||
final response = await HTTPService().get(
|
||||
path: ApiEndpoints.getScheduleByDeviceId
|
||||
@ -263,7 +269,9 @@ class DevicesManagementApi {
|
||||
}
|
||||
|
||||
Future<bool> updateScheduleRecord(
|
||||
{required bool enable, required String uuid, required String scheduleId}) async {
|
||||
{required bool enable,
|
||||
required String uuid,
|
||||
required String scheduleId}) async {
|
||||
try {
|
||||
final response = await HTTPService().put(
|
||||
path: ApiEndpoints.updateScheduleByDeviceId
|
||||
@ -284,7 +292,8 @@ class DevicesManagementApi {
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> editScheduleRecord(String uuid, ScheduleEntry newSchedule) async {
|
||||
Future<bool> editScheduleRecord(
|
||||
String uuid, ScheduleEntry newSchedule) async {
|
||||
try {
|
||||
final response = await HTTPService().put(
|
||||
path: ApiEndpoints.scheduleByDeviceId.replaceAll('{deviceUuid}', uuid),
|
||||
@ -335,4 +344,46 @@ class DevicesManagementApi {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> putDeviceName(
|
||||
{required String deviceId, required String deviceName}) async {
|
||||
try {
|
||||
final response = await HTTPService().put(
|
||||
path: ApiEndpoints.deviceByUuid.replaceAll('{deviceUuid}', deviceId),
|
||||
body: {"deviceName": deviceName},
|
||||
expectedResponseModel: (json) {
|
||||
return json['data'];
|
||||
},
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
static Future getDeviceInfo(String deviceId) async {
|
||||
final response = await HTTPService().get(
|
||||
path: ApiEndpoints.deviceByUuid.replaceAll('{deviceUuid}', deviceId),
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
return json['data'] as Map<String, dynamic>;
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
static Future resetDevice({
|
||||
String? devicesUuid,
|
||||
}) async {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.resetDevice.replaceAll('{deviceUuid}', devicesUuid!),
|
||||
showServerMessage: false,
|
||||
body: {
|
||||
"devicesUuid": [devicesUuid]
|
||||
},
|
||||
expectedResponseModel: (json) {
|
||||
return json;
|
||||
},
|
||||
);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_web/pages/routines/bloc/automation_scene_trigger_bloc/automation_status_update.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/create_scene_and_autoamtion/create_automation_model.dart';
|
||||
@ -5,6 +6,7 @@ import 'package:syncrow_web/pages/routines/models/create_scene_and_autoamtion/cr
|
||||
import 'package:syncrow_web/pages/routines/models/icon_model.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/routine_details_model.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/routine_model.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
import 'package:syncrow_web/utils/constants/api_const.dart';
|
||||
|
||||
@ -26,9 +28,10 @@ class SceneApi {
|
||||
);
|
||||
debugPrint('create scene response: $response');
|
||||
return response;
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
rethrow;
|
||||
} on DioException catch (e) {
|
||||
String errorMessage =
|
||||
e.response?.data['error']['message'][0] ?? 'something went wrong';
|
||||
throw APIException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,9 +51,10 @@ class SceneApi {
|
||||
);
|
||||
debugPrint('create automation response: $response');
|
||||
return response;
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
rethrow;
|
||||
} on DioException catch (e) {
|
||||
String errorMessage =
|
||||
e.response?.data['error']['message'][0] ?? 'something went wrong';
|
||||
throw APIException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@ -165,8 +169,10 @@ class SceneApi {
|
||||
},
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
} on DioException catch (e) {
|
||||
String errorMessage =
|
||||
e.response?.data['error']['message'][0] ?? 'something went wrong';
|
||||
throw APIException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,8 +191,10 @@ class SceneApi {
|
||||
},
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
} on DioException catch (e) {
|
||||
String errorMessage =
|
||||
e.response?.data['error']['message'][0] ?? 'something went wrong';
|
||||
throw APIException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@ -217,8 +225,10 @@ class SceneApi {
|
||||
expectedResponseModel: (json) => json['statusCode'] == 200,
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
} on DioException catch (e) {
|
||||
String errorMessage =
|
||||
e.response?.data['error']['message'][0] ?? 'something went wrong';
|
||||
throw APIException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@ -236,8 +246,10 @@ class SceneApi {
|
||||
expectedResponseModel: (json) => json['statusCode'] == 200,
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
} on DioException catch (e) {
|
||||
String errorMessage =
|
||||
e.response?.data['error']['message'][0] ?? 'something went wrong';
|
||||
throw APIException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:syncrow_web/pages/device_managment/device_setting/settings_model/sub_space_model.dart';
|
||||
import 'package:syncrow_web/pages/space_tree/model/pagination_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/create_subspace_model.dart';
|
||||
@ -365,4 +366,59 @@ class CommunitySpaceManagementApi {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
static Future<List<SubSpaceModel>> getSubSpaceBySpaceId(
|
||||
{required String communityId,
|
||||
required String spaceId,
|
||||
required String projectId}) async {
|
||||
try {
|
||||
final path = ApiEndpoints.listSubspace
|
||||
.replaceFirst('{communityUuid}', communityId)
|
||||
.replaceFirst('{spaceUuid}', spaceId)
|
||||
.replaceAll('{projectUuid}', projectId);
|
||||
|
||||
final response = await HTTPService().get(
|
||||
path: path,
|
||||
queryParameters: {"page": 1, "pageSize": 10},
|
||||
showServerMessage: false,
|
||||
expectedResponseModel: (json) {
|
||||
List<SubSpaceModel> rooms = [];
|
||||
if (json['data'] != null) {
|
||||
for (var subspace in json['data']) {
|
||||
rooms.add(SubSpaceModel.fromJson(subspace));
|
||||
}
|
||||
}
|
||||
return rooms;
|
||||
},
|
||||
);
|
||||
|
||||
return response;
|
||||
} catch (error, stackTrace) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Map<String, dynamic>> assignDeviceToRoom(
|
||||
{required String communityId,
|
||||
required String spaceId,
|
||||
required String subSpaceId,
|
||||
required String deviceId,
|
||||
required String projectId}) async {
|
||||
try {
|
||||
final response = await HTTPService().post(
|
||||
path: ApiEndpoints.assignDeviceToRoom
|
||||
.replaceAll('{projectUuid}', projectId)
|
||||
.replaceAll('{communityUuid}', communityId)
|
||||
.replaceAll('{spaceUuid}', spaceId)
|
||||
.replaceAll('{subSpaceUuid}', subSpaceId)
|
||||
.replaceAll('{deviceUuid}', deviceId),
|
||||
expectedResponseModel: (json) {
|
||||
return json;
|
||||
},
|
||||
);
|
||||
return response;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user