Report foreground location errors

This commit is contained in:
Yegor Vialov 2020-05-06 16:19:42 +00:00
parent e7cce01ca9
commit 17ec73b176

View File

@ -108,40 +108,47 @@ class LocationManager {
} }
updateDeviceLocation() async { updateDeviceLocation() async {
Logger.d("[Foreground location] Started"); try {
Geolocator geolocator = Geolocator(); Logger.d("[Foreground location] Started");
var battery = Battery(); Geolocator geolocator = Geolocator();
String webhookId = ConnectionManager().webhookId; var battery = Battery();
String httpWebHost = ConnectionManager().httpWebHost; String webhookId = ConnectionManager().webhookId;
if (webhookId != null && webhookId.isNotEmpty) { String httpWebHost = ConnectionManager().httpWebHost;
Logger.d("[Foreground location] Getting battery level..."); if (webhookId != null && webhookId.isNotEmpty) {
int batteryLevel = await battery.batteryLevel; Logger.d("[Foreground location] Getting battery level...");
Logger.d("[Foreground location] Getting device location..."); int batteryLevel = await battery.batteryLevel;
Position position = await geolocator.getCurrentPosition( Logger.d("[Foreground location] Getting device location...");
desiredAccuracy: LocationAccuracy.high, Position position = await geolocator.getCurrentPosition(
locationPermissionLevel: GeolocationPermission.locationAlways desiredAccuracy: LocationAccuracy.high,
); locationPermissionLevel: GeolocationPermission.locationAlways
if (position != null) { );
Logger.d("[Foreground location] Location: ${position.latitude} ${position.longitude}. Accuracy: ${position.accuracy}. (${position.timestamp})"); if (position != null) {
String url = "$httpWebHost/api/webhook/$webhookId"; Logger.d("[Foreground location] Location: ${position.latitude} ${position.longitude}. Accuracy: ${position.accuracy}. (${position.timestamp})");
Map data = { String url = "$httpWebHost/api/webhook/$webhookId";
"type": "update_location", Map data = {
"data": { "type": "update_location",
"gps": [position.latitude, position.longitude], "data": {
"gps_accuracy": position.accuracy, "gps": [position.latitude, position.longitude],
"battery": batteryLevel ?? 100 "gps_accuracy": position.accuracy,
"battery": batteryLevel ?? 100
}
};
Logger.d("[Foreground location] Sending data home...");
http.Response response = await http.post(
url,
headers: {"Content-Type": "application/json"},
body: json.encode(data)
);
if (response.statusCode >= 300) {
Logger.e('Foreground location update error: ${response.body}');
} }
}; Logger.d("[Foreground location] Got HTTP ${response.statusCode}");
Logger.d("[Foreground location] Sending data home..."); } else {
var response = await http.post( Logger.d("[Foreground location] No location. Aborting.");
url, }
headers: {"Content-Type": "application/json"}, }
body: json.encode(data) } catch (e, stack) {
); Logger.e('Foreground location error: ${e.toSTring()}', stacktrace: stack);
Logger.d("[Foreground location] Got HTTP ${response.statusCode}");
} else {
Logger.d("[Foreground location] No location. Aborting.");
}
} }
} }