feat: added datepicker, better refresh, and lots of fixes
This commit is contained in:
@ -8,9 +8,7 @@ import 'package:rust_core/option.dart';
|
||||
import 'package:ws/ws.dart';
|
||||
|
||||
String _responseNiceErrorString(http.Response response) {
|
||||
return 'Request failed: ${response.statusCode}: ${response.body.length > 500
|
||||
? response.body.substring(0, 500)
|
||||
: response.body}';
|
||||
return 'Request failed: ${response.statusCode}: ${response.body.length > 500 ? response.body.substring(0, 500) : response.body}';
|
||||
}
|
||||
|
||||
const API_PREFIX = '/api/0';
|
||||
@ -26,8 +24,7 @@ class OwntracksApi {
|
||||
final String username;
|
||||
final String pass;
|
||||
|
||||
static Map<String, String> _createBasicAuthHeader(String username,
|
||||
String password,
|
||||
static Map<String, String> _createBasicAuthHeader(String username, String password,
|
||||
[Map<String, String>? additionalHeaders]) {
|
||||
final credentials = base64Encode(utf8.encode('$username:$password'));
|
||||
final headers = {
|
||||
@ -42,6 +39,36 @@ class OwntracksApi {
|
||||
return headers;
|
||||
}
|
||||
|
||||
Future<Result<Point>> fetchLastPoint({
|
||||
required String user,
|
||||
required String device,
|
||||
}) async {
|
||||
final queryParams = {
|
||||
'user': user,
|
||||
'device': device,
|
||||
'fields': 'lat,lon,isotst',
|
||||
};
|
||||
final uri = Uri.parse('$baseUrl$API_PREFIX/last').replace(queryParameters: queryParams);
|
||||
|
||||
var auth_header = _createBasicAuthHeader(username, pass);
|
||||
|
||||
final response = await http.get(
|
||||
uri,
|
||||
headers: auth_header,
|
||||
);
|
||||
|
||||
// print('${response.statusCode}: ${response.body.length > 500 ? response.body.substring(0, 500) : response.body}');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final point = Point.fromJson((json.decode(response.body) as List<dynamic>)[0]);
|
||||
// print(point);
|
||||
return Ok(point);
|
||||
} else {
|
||||
return bail("couldn't get point data for device '$user:$device': ${_responseNiceErrorString(response)}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Method to fetch points from a device within a specified date range
|
||||
Future<Result<List<Point>>> fetchPointsForDevice({
|
||||
required String user,
|
||||
@ -57,8 +84,7 @@ class OwntracksApi {
|
||||
'to': (to ?? DateTime.now()).toIso8601String(),
|
||||
'fields': 'lat,lon,isotst',
|
||||
};
|
||||
final uri = Uri.parse('$baseUrl$API_PREFIX/locations')
|
||||
.replace(queryParameters: queryParams);
|
||||
final uri = Uri.parse('$baseUrl$API_PREFIX/locations').replace(queryParameters: queryParams);
|
||||
|
||||
var auth_header = _createBasicAuthHeader(username, pass);
|
||||
|
||||
@ -67,18 +93,13 @@ class OwntracksApi {
|
||||
headers: auth_header,
|
||||
);
|
||||
|
||||
print(
|
||||
'${response.statusCode}: ${response.body.length > 500 ? response.body
|
||||
.substring(0, 500) : response.body}');
|
||||
print('${response.statusCode}: ${response.body.length > 500 ? response.body.substring(0, 500) : response.body}');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final jsonData =
|
||||
(json.decode(response.body) as Map<String, dynamic>)['data'] as List;
|
||||
final jsonData = (json.decode(response.body) as Map<String, dynamic>)['data'] as List;
|
||||
return Ok(jsonData.map((point) => Point.fromJson(point)).toList());
|
||||
} else {
|
||||
return bail(
|
||||
"couldn't get point data for device '$user:$device': ${_responseNiceErrorString(
|
||||
response)}");
|
||||
return bail("couldn't get point data for device '$user:$device': ${_responseNiceErrorString(response)}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,9 +112,7 @@ class OwntracksApi {
|
||||
final response = await http.get(uri, headers: authHeader);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
return bail(
|
||||
"couldn't get user list from server: ${_responseNiceErrorString(
|
||||
response)}");
|
||||
return bail("couldn't get user list from server: ${_responseNiceErrorString(response)}");
|
||||
}
|
||||
|
||||
final users = List<String>.from(json.decode(response.body)['results']);
|
||||
@ -101,14 +120,10 @@ class OwntracksApi {
|
||||
Map<String, List<String>> map = {};
|
||||
|
||||
for (String user in users) {
|
||||
var response = await http.get(
|
||||
uri.replace(queryParameters: {'user': user}),
|
||||
headers: authHeader);
|
||||
var response = await http.get(uri.replace(queryParameters: {'user': user}), headers: authHeader);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
return bail(
|
||||
"couldn't get devices list for user '$user': ${_responseNiceErrorString(
|
||||
response)}");
|
||||
return bail("couldn't get devices list for user '$user': ${_responseNiceErrorString(response)}");
|
||||
}
|
||||
|
||||
map[user] = List<String>.from(jsonDecode(response.body)['results']);
|
||||
@ -125,22 +140,21 @@ class OwntracksApi {
|
||||
Option<(String, String)> onlyDeviceId = None,
|
||||
}) async {
|
||||
const retryInterval = (
|
||||
min: Duration(milliseconds: 500),
|
||||
max: Duration(seconds: 15),
|
||||
min: Duration(milliseconds: 500),
|
||||
max: Duration(seconds: 15),
|
||||
);
|
||||
|
||||
Map<String, String> headers = {};
|
||||
|
||||
if (onlyDeviceId case Some(:final v)){
|
||||
headers.putIfAbsent('X-Limit-User', () => v.$1);
|
||||
headers.putIfAbsent('X-Limit-Device', () => v.$2);
|
||||
if (onlyDeviceId case Some(:final v)) {
|
||||
headers.putIfAbsent('X-Limit-User', () => v.$1);
|
||||
headers.putIfAbsent('X-Limit-Device', () => v.$2);
|
||||
}
|
||||
|
||||
final client = WebSocketClient(kIsWeb
|
||||
? WebSocketOptions.common(connectionRetryInterval: retryInterval)
|
||||
? WebSocketOptions.common(connectionRetryInterval: retryInterval)
|
||||
: WebSocketOptions.vm(
|
||||
connectionRetryInterval: retryInterval,
|
||||
headers: _createBasicAuthHeader(username, pass)..addAll(headers)));
|
||||
connectionRetryInterval: retryInterval, headers: _createBasicAuthHeader(username, pass)..addAll(headers)));
|
||||
|
||||
// Listen to messages
|
||||
client.stream.listen(onMessage);
|
||||
|
Reference in New Issue
Block a user