2026-02-27 12:28:16 -05:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
2026-03-02 13:59:50 -05:00
|
|
|
import 'package:encrypter/encrypter/xor.dart';
|
2026-05-27 13:57:03 -04:00
|
|
|
import 'package:flutter/foundation.dart';
|
2026-03-02 13:59:50 -05:00
|
|
|
import 'package:frontend/utils/pref_utils.dart';
|
2026-05-28 18:16:04 -04:00
|
|
|
import '../model/arp_scanner_status.dart';
|
2026-05-29 10:19:50 -04:00
|
|
|
import '../model/mdns_scanner_status.dart';
|
2026-05-27 14:58:11 -04:00
|
|
|
import '../model/device.dart';
|
2026-05-28 10:05:49 -04:00
|
|
|
import '../model/device_event.dart';
|
2026-05-28 19:10:08 -04:00
|
|
|
import '../model/device_summary.dart';
|
2026-05-27 19:46:19 -04:00
|
|
|
import '../model/device_type.dart';
|
2026-03-02 13:59:50 -05:00
|
|
|
import '../model/notification.dart';
|
2026-02-27 12:28:16 -05:00
|
|
|
|
|
|
|
|
class BackendAPI {
|
2026-03-02 13:59:50 -05:00
|
|
|
static final BackendAPI _instance = BackendAPI._internal();
|
2026-03-04 11:05:24 -05:00
|
|
|
static const _pageSize = 5;
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
static BackendAPI get instance => _instance;
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
BackendAPI._internal() {
|
2026-03-03 12:20:57 -05:00
|
|
|
_baseUrl =
|
|
|
|
|
PrefUtil.getValue("base_url", "http://localhost:3000/api") as String;
|
|
|
|
|
_apiKey = XOR().xorDecode(PrefUtil.getValue("api_key", "") as String);
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('Base URL: $_baseUrl');
|
|
|
|
|
debugPrint('API KEY: $_apiKey');
|
2026-03-02 13:59:50 -05:00
|
|
|
|
|
|
|
|
_dio = Dio(
|
|
|
|
|
BaseOptions(
|
|
|
|
|
baseUrl: _baseUrl,
|
|
|
|
|
headers: {
|
|
|
|
|
HttpHeaders.contentTypeHeader: 'application/json',
|
|
|
|
|
HttpHeaders.authorizationHeader: 'Bearer $_apiKey',
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 12:20:57 -05:00
|
|
|
// Returns null if the test was successful, and a String with a message about the issue if not
|
|
|
|
|
static Future<String?> test(String baseUrl, String apiKey) async {
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('About to test API with baseUrl=$baseUrl and apiKey=$apiKey');
|
2026-03-03 12:20:57 -05:00
|
|
|
Dio dio = Dio(
|
|
|
|
|
BaseOptions(
|
|
|
|
|
baseUrl: baseUrl,
|
|
|
|
|
headers: {
|
|
|
|
|
HttpHeaders.contentTypeHeader: 'application/json',
|
|
|
|
|
HttpHeaders.authorizationHeader: 'Bearer $apiKey',
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = await dio.get('/test');
|
|
|
|
|
return response.toString().contains('OOTT_API_OK')
|
|
|
|
|
? null
|
|
|
|
|
: "URL successfully called but didn't return the expected value. Check your URL and make sure it points to your OOTT backend base URL.";
|
|
|
|
|
} on DioException catch (e) {
|
|
|
|
|
if (e.response != null) {
|
|
|
|
|
if (e.response!.statusCode == 401) {
|
|
|
|
|
return "Authorization failed, check your API Key.";
|
|
|
|
|
} else {
|
|
|
|
|
return "Error querying the given URL (${e.response!.statusCode} - ${e.message ?? 'N/A'})";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Response was null, something happened while sending the message
|
|
|
|
|
return "Error sending message to provided URL (${e.message ?? 'no message'})";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
late String _baseUrl;
|
|
|
|
|
late String _apiKey;
|
|
|
|
|
late Dio _dio;
|
|
|
|
|
|
2026-05-27 09:58:39 -04:00
|
|
|
Future<void> markNotificationAsRead(int id) async {
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('About to call /notifications/$id');
|
2026-05-27 09:58:39 -04:00
|
|
|
await _dio.get('/notifications/$id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 10:43:10 -04:00
|
|
|
Future<void> markNotificationAsNew(int id) async {
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('About to call /notifications/$id/mark_as_new');
|
2026-05-27 10:43:10 -04:00
|
|
|
await _dio.post('/notifications/$id/mark_as_new');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 12:10:02 -04:00
|
|
|
Future<void> markAllNotificationsAsRead() async {
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('About to call /notifications/mark_all_as_old');
|
2026-05-27 12:10:02 -04:00
|
|
|
await _dio.post('/notifications/mark_all_as_old');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 18:24:35 -04:00
|
|
|
Future<Device> getDevice(String macAddress) async {
|
|
|
|
|
debugPrint('About to call GET /devices/$macAddress');
|
|
|
|
|
final response = await _dio.get('/devices/$macAddress');
|
|
|
|
|
debugPrint('Received: ${response.data}');
|
|
|
|
|
return Device.fromJson(response.data as Map<String, dynamic>);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 19:46:19 -04:00
|
|
|
Future<List<Device>> listDevices({
|
|
|
|
|
bool? isRegistered,
|
|
|
|
|
String? owner,
|
|
|
|
|
DeviceType? deviceType,
|
2026-05-30 11:09:55 -04:00
|
|
|
String? sortBy,
|
|
|
|
|
bool? sortAscending,
|
2026-05-29 13:07:27 -04:00
|
|
|
int? offset,
|
|
|
|
|
int? limit,
|
2026-05-27 19:46:19 -04:00
|
|
|
}) async {
|
2026-05-27 14:58:11 -04:00
|
|
|
debugPrint('About to call /devices');
|
|
|
|
|
|
|
|
|
|
final params = <String, dynamic>{};
|
|
|
|
|
if (isRegistered != null) params['is_registered'] = isRegistered;
|
2026-05-27 19:46:19 -04:00
|
|
|
if (owner != null && owner.isNotEmpty) params['owner'] = owner;
|
|
|
|
|
if (deviceType != null) {
|
2026-05-29 10:19:50 -04:00
|
|
|
params['device_type'] = deviceType == DeviceType.unknown
|
|
|
|
|
? ''
|
|
|
|
|
: deviceType.apiName;
|
2026-05-27 19:46:19 -04:00
|
|
|
}
|
2026-05-30 11:09:55 -04:00
|
|
|
if (sortBy != null) params['sort_by'] = sortBy;
|
|
|
|
|
if (sortAscending != null) {
|
|
|
|
|
params['sort_order'] = sortAscending ? 'asc' : 'desc';
|
|
|
|
|
}
|
2026-05-29 13:07:27 -04:00
|
|
|
if (offset != null) {
|
|
|
|
|
params['page_offset'] = offset;
|
|
|
|
|
params['page_limit'] = limit ?? _pageSize;
|
|
|
|
|
}
|
2026-05-27 14:58:11 -04:00
|
|
|
|
|
|
|
|
final response = await _dio.get('/devices', queryParameters: params);
|
|
|
|
|
|
|
|
|
|
debugPrint('Received: ${response.data}');
|
|
|
|
|
|
|
|
|
|
return (response.data as List)
|
|
|
|
|
.map((item) => Device.fromJson(item as Map<String, dynamic>))
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 15:33:24 -04:00
|
|
|
Future<void> registerDevice(
|
|
|
|
|
String macAddress,
|
|
|
|
|
String owner,
|
2026-05-30 11:09:55 -04:00
|
|
|
String deviceType, {
|
|
|
|
|
String? name,
|
|
|
|
|
}) async {
|
2026-05-27 15:33:24 -04:00
|
|
|
debugPrint('About to call PUT /devices');
|
2026-05-27 18:24:35 -04:00
|
|
|
await _dio.put(
|
|
|
|
|
'/devices',
|
|
|
|
|
data: {
|
|
|
|
|
'mac_address': macAddress,
|
|
|
|
|
'owner': owner,
|
|
|
|
|
'device_type': deviceType,
|
2026-05-30 11:09:55 -04:00
|
|
|
'name': ?name,
|
2026-05-27 18:24:35 -04:00
|
|
|
},
|
|
|
|
|
);
|
2026-05-27 15:33:24 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 11:27:52 -04:00
|
|
|
Future<void> updateDevice(
|
|
|
|
|
String macAddress,
|
|
|
|
|
String owner,
|
|
|
|
|
String deviceType,
|
|
|
|
|
String vendor, {
|
|
|
|
|
String? name,
|
|
|
|
|
}) async {
|
|
|
|
|
debugPrint('About to call PUT /devices/$macAddress');
|
|
|
|
|
await _dio.put(
|
|
|
|
|
'/devices/$macAddress',
|
|
|
|
|
data: {
|
|
|
|
|
'owner': owner,
|
|
|
|
|
'device_type': deviceType,
|
|
|
|
|
'vendor': vendor,
|
|
|
|
|
'name': name,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-27 15:33:24 -04:00
|
|
|
Future<void> forgetDevice(String macAddress) async {
|
|
|
|
|
debugPrint('About to call DELETE /devices/$macAddress');
|
|
|
|
|
await _dio.delete('/devices/$macAddress');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 10:51:19 -04:00
|
|
|
Future<List<DeviceEvent>> getDeviceEvents(
|
|
|
|
|
String macAddress, {
|
|
|
|
|
DateTime? createdFrom,
|
|
|
|
|
}) async {
|
2026-05-28 10:05:49 -04:00
|
|
|
debugPrint('About to call GET /devices/$macAddress/events');
|
2026-05-28 10:51:19 -04:00
|
|
|
final queryParams = <String, dynamic>{};
|
|
|
|
|
if (createdFrom != null) {
|
|
|
|
|
queryParams['created_from'] = createdFrom.toUtc().toIso8601String();
|
|
|
|
|
}
|
|
|
|
|
final response = await _dio.get(
|
|
|
|
|
'/devices/$macAddress/events',
|
|
|
|
|
queryParameters: queryParams.isEmpty ? null : queryParams,
|
|
|
|
|
);
|
2026-05-28 10:05:49 -04:00
|
|
|
debugPrint('Received: ${response.data}');
|
|
|
|
|
return (response.data as List)
|
|
|
|
|
.map((item) => DeviceEvent.fromJson(item as Map<String, dynamic>))
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 19:10:08 -04:00
|
|
|
Future<DeviceSummary> getDeviceSummary() async {
|
|
|
|
|
debugPrint('About to call GET /devices/summary');
|
|
|
|
|
final response = await _dio.get('/devices/summary');
|
|
|
|
|
debugPrint('Received: ${response.data}');
|
|
|
|
|
return DeviceSummary.fromJson(response.data as Map<String, dynamic>);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 18:16:04 -04:00
|
|
|
Future<ArpScannerStatus> getArpScannerStatus() async {
|
|
|
|
|
debugPrint('About to call GET /arp_scanner/status');
|
|
|
|
|
final response = await _dio.get('/arp_scanner/status');
|
|
|
|
|
debugPrint('Received: ${response.data}');
|
|
|
|
|
return ArpScannerStatus.fromJson(response.data as Map<String, dynamic>);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 10:19:50 -04:00
|
|
|
Future<MdnsScannerStatus> getMdnsScannerStatus() async {
|
|
|
|
|
debugPrint('About to call GET /mdns_scanner/status');
|
|
|
|
|
final response = await _dio.get('/mdns_scanner/status');
|
|
|
|
|
debugPrint('Received: ${response.data}');
|
|
|
|
|
return MdnsScannerStatus.fromJson(response.data as Map<String, dynamic>);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 07:56:03 -04:00
|
|
|
Future<List<Notification>> listNotifications(
|
|
|
|
|
bool? isNew,
|
|
|
|
|
int offset, {
|
|
|
|
|
int? limit,
|
|
|
|
|
}) async {
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('About to call /notifications');
|
2026-02-27 12:28:16 -05:00
|
|
|
|
|
|
|
|
Response response;
|
2026-03-04 11:05:24 -05:00
|
|
|
response = await _dio.get(
|
|
|
|
|
'/notifications',
|
2026-03-04 16:22:00 -05:00
|
|
|
queryParameters: {
|
|
|
|
|
'is_new': isNew ?? '',
|
|
|
|
|
'page_offset': offset,
|
2026-05-29 07:56:03 -04:00
|
|
|
'page_limit': limit ?? _pageSize,
|
2026-03-04 16:22:00 -05:00
|
|
|
},
|
2026-03-04 11:05:24 -05:00
|
|
|
);
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('Received: ${response.data}');
|
2026-02-27 12:28:16 -05:00
|
|
|
|
|
|
|
|
List<dynamic> list = response.data;
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('List contains ${list.length} items');
|
2026-02-27 12:28:16 -05:00
|
|
|
|
2026-03-02 13:59:50 -05:00
|
|
|
List<Notification> events = List<Notification>.from(
|
|
|
|
|
list.map((item) => Notification.fromJson(item)),
|
2026-02-27 12:28:16 -05:00
|
|
|
);
|
|
|
|
|
|
2026-05-27 13:57:03 -04:00
|
|
|
debugPrint('Parsed ${events.length} events');
|
2026-02-27 12:28:16 -05:00
|
|
|
|
|
|
|
|
return events;
|
|
|
|
|
}
|
|
|
|
|
}
|