import 'dart:async'; import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:fires_flutter/models/yourLocation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:latlong2/latlong.dart'; import '../globals.dart' as globals; import '../objectIdUtils.dart'; import '../redux/actions.dart'; import 'appState.dart'; import 'falsePositiveTypes.dart'; class FiresApi { late final Dio _dio; FiresApi() { _dio = Dio(); } Future createUser( AppState state, String mobileToken, String lang) async { final params = { "token": state.firesApiKey, "mobileToken": mobileToken, "lang": lang }; final String url = '${state.firesApiUrl}mobile/users'; try { final response = await _dio.post(url, data: params); if (response.statusCode == 200) { return response.data['data']['userId']; } else { throw "Unexpected error on create user"; } } catch (e) { throw "Error creating user: $e"; } } Future> fetchYourLocations(AppState state) async { final apiKey = state.firesApiKey; final mobileToken = state.user.token; final String url = '${state.firesApiUrl}mobile/subscriptions/all/$apiKey/$mobileToken'; try { final response = await _dio.get(url); if (response.statusCode == 200) { final dataSubscriptions = response.data['data']['subscriptions']; List subscribed = []; for (int i = 0; i < dataSubscriptions.length; i++) { var el = dataSubscriptions[i]; var lat = el['location']['lat']; var lon = el['location']['lon']; subscribed.add(YourLocation( id: objectIdFromJson(el['_id']['_str']), lat: lat, lon: lon, subscribed: true, distance: el['distance'])); } return subscribed; } else { throw "Unexpected error fetching your locations"; } } catch (e) { throw "Error fetching locations: $e"; } } Future subscribe(AppState state, YourLocation loc) async { final params = { "token": state.firesApiKey, "mobileToken": state.user.token, "id": loc.id.hexString, "lat": loc.lat, "lon": loc.lon, "distance": loc.distance }; final String url = '${state.firesApiUrl}mobile/subscriptions'; try { final response = await _dio.post(url, data: params); if (response.statusCode == 200) { return response.data['data']['subsId']; } else { print(response.data); throw "Unexpected error on subscribe"; } } catch (e) { throw "Error subscribing: $e"; } } Future unsubscribe(AppState state, String subsId) async { final apiKey = state.firesApiKey; final mobileToken = state.user.token; final String url = '${state.firesApiUrl}mobile/subscriptions/$apiKey/$mobileToken/$subsId'; try { final response = await _dio.delete(url); if (response.statusCode == 200) { return true; } else { throw "Unexpected error on unsubscribe"; } } catch (e) { throw "Error unsubscribing: $e"; } } Future getFiresInLocation( {required AppState state, required double lat, required double lon, required int distance}) async { var url = '${state.firesApiUrl}fires-in-full/${state.firesApiKey}/$lat/$lon/$distance'; if (globals.isDevelopment) print(url); try { final response = await _dio.get(url); if (response.statusCode == 200) { var resultDecoded = response.data; int numFires = resultDecoded['real']; List fires = resultDecoded['fires']; List falsePos = resultDecoded['falsePos']; List industries = resultDecoded['industries']; if (globals.isDevelopment) { var firesCount = fires.length; var industriesCount = industries.length; var falsePosCount = falsePos.length; print( '(Pos: $lat, $lon) real: $numFires, fire: $firesCount falsePos: $falsePosCount industries: $industriesCount'); } return UpdateFireMapStatsAction( numFires: numFires, fires: fires, falsePos: falsePos, industries: industries); } else throw Exception('Wrong response trying to get fire data'); } catch (e) { throw Exception('Error getting fires: $e'); } } Future> getMonitoredAreas({required AppState state}) async { var url = '${state.firesApiUrl}status/subs-public-union/${state.firesApiKey}'; var color = const Color(0xFF145A32); try { final response = await _dio.get(url); if (response.statusCode == 200) { var resultDecoded = response.data; List union = []; final multipolygon = json.decode(resultDecoded['data']['union']['value'])['geometry'] ['coordinates']; for (List polygon in multipolygon) { for (List hole in polygon) { List points = []; for (List point in hole) { points.add(LatLng(point[1].toDouble(), point[0].toDouble())); } union.add(Polyline(points: points, color: color, strokeWidth: 3.0)); } } return union; } else throw Exception('Wrong response trying to get fire data'); } catch (e) { throw Exception('Error getting monitored areas: $e'); } } Future markFalsePositive(AppState state, String mobileToken, String sealed, FalsePositiveType type) async { final params = { "token": state.firesApiKey, "mobileToken": mobileToken, "sealed": sealed, "type": type.toString().split('.')[1] }; final String url = '${state.firesApiUrl}mobile/falsepositive'; try { final response = await _dio.post(url, data: params); if (response.statusCode == 200) { if (globals.isDevelopment) print(response.data['data']['upsert']); return true; } else { debugPrint(response.data.toString()); return false; } } catch (e) { debugPrint("Error marking false positive: $e"); return false; } } }