Location tracking improvements 2

This commit is contained in:
Yegor Vialov 2019-10-22 18:42:30 +00:00
parent 4494da1f4f
commit 89c91b4b01

View File

@ -66,13 +66,10 @@ class LocationManager {
"httpWebHost": httpWebHost "httpWebHost": httpWebHost
}, },
frequency: _updateInterval, frequency: _updateInterval,
existingWorkPolicy: workManager.ExistingWorkPolicy.replace, existingWorkPolicy: workManager.ExistingWorkPolicy.keep,
backoffPolicy: workManager.BackoffPolicy.linear, backoffPolicy: workManager.BackoffPolicy.linear,
backoffPolicyDelay: _updateInterval,
constraints: workManager.Constraints( constraints: workManager.Constraints(
requiresBatteryNotLow: false,
requiresCharging: false,
requiresDeviceIdle: false,
requiresStorageNotLow: false,
networkType: workManager.NetworkType.connected networkType: workManager.NetworkType.connected
) )
); );
@ -120,19 +117,20 @@ class LocationManager {
void updateDeviceLocationIsolate() { void updateDeviceLocationIsolate() {
workManager.Workmanager.executeTask((backgroundTask, data) { workManager.Workmanager.executeTask((backgroundTask, data) {
print("[Location isolate] Started: $backgroundTask"); print("[Background $backgroundTask] Started");
print("[Location isolate] loading settings");
String webhookId = data["webhookId"]; String webhookId = data["webhookId"];
String httpWebHost = data["httpWebHost"]; String httpWebHost = data["httpWebHost"];
if (webhookId != null && webhookId.isNotEmpty) { if (webhookId != null && webhookId.isNotEmpty) {
Logger.d("[Location isolate] Getting device location..."); int battery = DateTime.now().hour;
print("[Background $backgroundTask] hour=$battery");
String url = "$httpWebHost/api/webhook/$webhookId";
Map<String, String> headers = {};
headers["Content-Type"] = "application/json";
print("[Background $backgroundTask] Getting device location...");
Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) { Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) {
Logger.d("[Location isolate] Got location: ${location.latitude} ${location.longitude}. Sending home..."); print("[Background $backgroundTask] Got location: ${location.latitude} ${location.longitude}");
int battery = DateTime.now().hour; print("[Background $backgroundTask] sending data home...");
String url = "$httpWebHost/api/webhook/$webhookId";
Map<String, String> headers = {};
headers["Content-Type"] = "application/json";
var data = { var data = {
"type": "update_location", "type": "update_location",
"data": { "data": {
@ -146,15 +144,56 @@ void updateDeviceLocationIsolate() {
headers: headers, headers: headers,
body: json.encode(data) body: json.encode(data)
).catchError((e) { ).catchError((e) {
print("[Location isolate] Error sending data: ${e.toString()}"); print("[Background $backgroundTask] Error sending data: ${e.toString()}");
}).then((_) { }).then((_) {
print("[Location isolate] done!"); print("[Background $backgroundTask] Success!");
}); });
}).catchError((e) { }).catchError((e) {
print("[Location isolate] Error getting location: ${e.toString()}"); print("[Background $backgroundTask] Error getting current location: ${e.toString()}. Trying last known...");
Geolocator().getLastKnownPosition(desiredAccuracy: LocationAccuracy.medium).then((location){
print("[Background $backgroundTask] Got last known location: ${location.latitude} ${location.longitude}");
print("[Background $backgroundTask] sending data home...");
var data = {
"type": "update_location",
"data": {
"gps": [location.latitude, location.longitude],
"gps_accuracy": location.accuracy,
"battery": battery
}
};
http.post(
url,
headers: headers,
body: json.encode(data)
).catchError((e) {
print("[Background $backgroundTask] Error sending data: ${e.toString()}");
}).then((_) {
print("[Background $backgroundTask] Success!");
});
}).catchError((e){
print("[Background $backgroundTask] Error getting last known location: ${e.toString()}. Sending fake...");
print("[Background $backgroundTask] sending data home...");
var data = {
"type": "update_location",
"data": {
"gps": [40.34, 30.34],
"gps_accuracy": 300,
"battery": battery
}
};
http.post(
url,
headers: headers,
body: json.encode(data)
).catchError((e) {
print("[Background $backgroundTask] Error sending data: ${e.toString()}");
}).then((_) {
print("[Background $backgroundTask] Success!");
});
});
}); });
} else { } else {
print("[Location isolate] No webhook id. Aborting"); print("[Background $backgroundTask] No webhook id. Aborting");
} }
return Future.value(true); return Future.value(true);
}); });