Files
syncrow-app/lib/services/api/devices_api.dart
2024-06-26 22:46:29 +03:00

127 lines
4.0 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/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>> 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<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('{unitUuid}', spaceId),
// queryParameters: params,
showServerMessage: false,
expectedResponseModel: (json) => DevicesCategoryModel.fromJsonList(json),
);
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;
}
static Future<List<DeviceModel>> 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 <DeviceModel>[];
}
List<DeviceModel> devices = [];
for (var device in json) {
devices.add(DeviceModel.fromJson(device));
}
return devices;
},
);
return response;
}
static Future<List<DeviceModel>> 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 <DeviceModel>[];
}
List<DeviceModel> devices = [];
for (var device in json) {
devices.add(DeviceModel.fromJson(device));
}
return devices;
},
);
return response;
}
static Future<List<DeviceModel>> getDevicesByGatewayId(String gatewayId) async {
final response = await _httpService.get(
path: ApiEndpoints.gatewayApi.replaceAll('{gatewayUuid}', gatewayId),
showServerMessage: false,
expectedResponseModel: (json) {
List<DeviceModel> devices = [];
if (json == null || json.isEmpty || json == []) {
return devices;
}
for (var device in json['devices']) {
devices.add(DeviceModel.fromJson(device));
}
return devices;
},
);
return response;
}
}