Limit log file size to 5MB. Background location fixes

This commit is contained in:
Yegor Vialov 2020-03-09 14:53:12 +00:00
parent 3e6229cf3e
commit 216276e5f3

View File

@ -91,7 +91,7 @@ class LocationManager {
}, },
frequency: interval, frequency: interval,
initialDelay: Duration(minutes: delay), initialDelay: Duration(minutes: delay),
existingWorkPolicy: workManager.ExistingWorkPolicy.replace, existingWorkPolicy: workManager.ExistingWorkPolicy.keep,
backoffPolicy: workManager.BackoffPolicy.linear, backoffPolicy: workManager.BackoffPolicy.linear,
backoffPolicyDelay: interval, backoffPolicyDelay: interval,
constraints: workManager.Constraints( constraints: workManager.Constraints(
@ -104,7 +104,7 @@ class LocationManager {
_stopLocationService() async { _stopLocationService() async {
Logger.d("Canceling previous schedule if any..."); Logger.d("Canceling previous schedule if any...");
await workManager.Workmanager.cancelByTag(backgroundTaskTag); await workManager.Workmanager.cancelAll();
} }
updateDeviceLocation() async { updateDeviceLocation() async {
@ -196,36 +196,39 @@ void updateDeviceLocationIsolate() {
logData += ' || Location: success, ${location.latitude} ${location.longitude} (${location.timestamp})'; logData += ' || Location: success, ${location.latitude} ${location.longitude} (${location.timestamp})';
data["data"]["gps"] = [location.latitude, location.longitude]; data["data"]["gps"] = [location.latitude, location.longitude];
data["data"]["gps_accuracy"] = location.accuracy; data["data"]["gps_accuracy"] = location.accuracy;
try {
http.Response response = await http.post(
url,
headers: headers,
body: json.encode(data)
);
if (response.statusCode >= 200 && response.statusCode < 300) {
logData += ' || Post: success, ${response.statusCode}';
} else {
logData += ' || Post: error, ${response.statusCode}';
}
} catch(e) {
logData += ' || Post: error, $e';
}
} else { } else {
logData += ' || Location: error, location is null'; logData += ' || Location: error, location is null';
} }
} catch (e) { } catch (e) {
//print("[Background $backgroundTask] Error getting location: $e. Setting fake one in the middle of the Black See");
data["data"]["gps"] = [43.373750, 34.026441];
data["data"]["gps_accuracy"] = 200;
logData += ' || Location: error, $e'; logData += ' || Location: error, $e';
} }
//print("[Background $backgroundTask] Sending data home."); //print("[Background $backgroundTask] Sending data home.");
try {
http.Response response = await http.post(
url,
headers: headers,
body: json.encode(data)
);
if (response.statusCode >= 200 && response.statusCode < 300) {
logData += ' || Post: success, ${response.statusCode}';
} else {
logData += ' || Post: error, ${response.statusCode}';
}
} catch(e) {
logData += ' || Post: error, $e';
}
} else { } else {
logData += 'Not configured'; logData += 'Not configured';
} }
print("[Background $backgroundTask] Writing log data..."); print("[Background $backgroundTask] Writing log data...");
try { try {
await logFile.writeAsString('$logData\n', mode: FileMode.append); var fileMode;
if (logFile.lengthSync() < 5000000) {
fileMode = FileMode.append;
} else {
fileMode = FileMode.write;
}
await logFile.writeAsString('$logData\n', mode: fileMode);
} catch (e) { } catch (e) {
print("[Background $backgroundTask] Error writing log: $e"); print("[Background $backgroundTask] Error writing log: $e");
} }