import 'dart:async'; import 'dart:convert'; import 'dart:io'; import '../consts.dart'; import 'marker_data.dart'; // fetchMarkerTemplate requests all marker data from the server Future<List<Measurement>> fetchMarkerData() async { try { // Custom HTTP client HttpClient client = HttpClient() ..badCertificateCallback = // NB: temporary disable SSL certificate validation (X509Certificate cert, String host, int port) => true; // Request markers from server var request = await client.getUrl(Uri.parse(serverURI + mapEndpoint)); var response = await request.close(); // Close response body at end of function // Parse body to JSON if request is ok if (response.statusCode == 200) { var responseBody = await response.transform(utf8.decoder).join(); if (responseBody.isNotEmpty) { var jsonData = json.decode(responseBody); // Attempt to parse response to Measurement object only if the body // contains correctly formatted data if (jsonData != null && jsonData is List) { print(jsonData.map((data) => Measurement.fromJson(data)).toList()); return jsonData.map((data) => Measurement.fromJson(data)).toList(); } else { throw Exception('Failed to parse marker data: Unexpected response format'); } } else { throw Exception('Failed to parse marker data: Empty response body'); } } else { throw Exception('Failed to fetch marker data: Status code ${response.statusCode}'); } } catch (e) { throw Exception('Failed to fetch marker data: ${e.toString()}'); } } // fetchRelation requests lake relation data from the server Future<String> fetchRelation() async { try { // Custom HTTP client HttpClient client = HttpClient() ..badCertificateCallback = // NB: temporary disable SSL certificate validation (X509Certificate cert, String host, int port) => true; // Request markers from server var request = await client.getUrl(Uri.parse(serverURI + 'get_relation')); var response = await request.close(); // Close response body at end of function // Parse body to JSON if request is ok if (response.statusCode == 200) { var responseBody = await response.transform(utf8.decoder).join(); if (responseBody.isNotEmpty) { return responseBody; } else { throw Exception('Failed to fetch relation, body is empty'); } } else { throw Exception('Failed to fetch relation: Status code ${response.statusCode}'); } } catch (e) { throw Exception('Failed to fetch relation: ${e.toString()}'); } }