mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
179 lines
5.1 KiB
Dart
179 lines
5.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_reports.dart';
|
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
|
|
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
|
|
import 'package:syncrow_web/pages/visitor_password/model/device_model.dart';
|
|
import 'package:syncrow_web/services/api/http_service.dart';
|
|
import 'package:syncrow_web/utils/constants/api_const.dart';
|
|
|
|
class DevicesManagementApi {
|
|
Future<List<AllDevicesModel>> fetchDevices() async {
|
|
try {
|
|
final response = await HTTPService().get(
|
|
path: ApiEndpoints.getAllDevices,
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
List<dynamic> jsonData = json;
|
|
List<AllDevicesModel> devicesList = jsonData.map((jsonItem) {
|
|
return AllDevicesModel.fromJson(jsonItem);
|
|
}).toList();
|
|
return devicesList;
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<DeviceStatus> getDeviceStatus(String uuid) async {
|
|
try {
|
|
final response = await HTTPService().get(
|
|
path: ApiEndpoints.getDeviceStatus.replaceAll('{uuid}', uuid),
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
return DeviceStatus.fromJson(json);
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return DeviceStatus(
|
|
productUuid: '',
|
|
productType: '',
|
|
status: [],
|
|
);
|
|
}
|
|
}
|
|
|
|
//deviceControl
|
|
Future<bool> deviceControl(String uuid, Status status) async {
|
|
try {
|
|
final response = await HTTPService().post(
|
|
path: ApiEndpoints.deviceControl.replaceAll('{uuid}', uuid),
|
|
body: status.toMap(),
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
return json['success'] ?? false;
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> deviceBatchControl(
|
|
List<String> uuids, String code, dynamic value) async {
|
|
try {
|
|
final body = {
|
|
'devicesUuid': uuids,
|
|
'code': code,
|
|
'value': value,
|
|
};
|
|
|
|
final response = await HTTPService().post(
|
|
path: ApiEndpoints.deviceBatchControl,
|
|
body: body,
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
return json['success'] ?? false;
|
|
},
|
|
);
|
|
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
static Future<bool> openDoorLock(String deviceId) async {
|
|
final response = await HTTPService().post(
|
|
path: ApiEndpoints.openDoorLock.replaceAll('{doorLockUuid}', deviceId),
|
|
showServerMessage: false,
|
|
expectedResponseModel: (json) {
|
|
return json['success'] ?? false;
|
|
},
|
|
);
|
|
return response;
|
|
}
|
|
|
|
static Future<DeviceReport> getDeviceReports(
|
|
String uuid,
|
|
String code,
|
|
) async {
|
|
final response = await HTTPService().get(
|
|
path: ApiEndpoints.getDeviceLogs
|
|
.replaceAll('{uuid}', uuid)
|
|
.replaceAll('{code}', code),
|
|
showServerMessage: false,
|
|
expectedResponseModel: (json) {
|
|
return DeviceReport.fromJson(json);
|
|
},
|
|
);
|
|
return response;
|
|
}
|
|
|
|
static Future<DeviceReport> getDeviceReportsByDate(String uuid, String code,
|
|
[String? from, String? to]) async {
|
|
final response = await HTTPService().get(
|
|
path: ApiEndpoints.getDeviceLogsByDate
|
|
.replaceAll('{uuid}', uuid)
|
|
.replaceAll('{code}', code)
|
|
.replaceAll('{startTime}', from ?? '')
|
|
.replaceAll('{endTime}', to ?? ''),
|
|
showServerMessage: false,
|
|
expectedResponseModel: (json) {
|
|
return DeviceReport.fromJson(json);
|
|
},
|
|
);
|
|
return response;
|
|
}
|
|
|
|
Future<DeviceStatus> getBatchStatus(List<String> uuids) async {
|
|
try {
|
|
final queryParameters = {
|
|
'devicesUuid': uuids.join(','),
|
|
};
|
|
final response = await HTTPService().get(
|
|
path: ApiEndpoints.getBatchStatus,
|
|
queryParameters: queryParameters,
|
|
showServerMessage: true,
|
|
expectedResponseModel: (json) {
|
|
return DeviceStatus.fromJson(json['status']);
|
|
},
|
|
);
|
|
return response;
|
|
} catch (e) {
|
|
debugPrint('Error fetching $e');
|
|
return DeviceStatus(
|
|
productUuid: '',
|
|
productType: '',
|
|
status: [],
|
|
);
|
|
}
|
|
}
|
|
}
|