Location tracking improvements
This commit is contained in:
@ -13,8 +13,9 @@ class LocationManager {
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
final int defaultUpdateIntervalMinutes = 15;
|
final int defaultUpdateIntervalMinutes = 20;
|
||||||
final String alarmId = "ha_location_background";
|
final String backgroundTaskId = "haclocationtask4352";
|
||||||
|
final String backgroundTaskTag = "haclocation";
|
||||||
Duration _updateInterval;
|
Duration _updateInterval;
|
||||||
bool _isEnabled;
|
bool _isEnabled;
|
||||||
|
|
||||||
@ -41,7 +42,6 @@ class LocationManager {
|
|||||||
prefs.setBool("location-enabled", enabled);
|
prefs.setBool("location-enabled", enabled);
|
||||||
_isEnabled = true;
|
_isEnabled = true;
|
||||||
_startLocationService();
|
_startLocationService();
|
||||||
updateDeviceLocation();
|
|
||||||
} else if (!enabled && _isEnabled) {
|
} else if (!enabled && _isEnabled) {
|
||||||
Logger.d("Disabling location service");
|
Logger.d("Disabling location service");
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
@ -54,24 +54,37 @@ class LocationManager {
|
|||||||
void _startLocationService() async {
|
void _startLocationService() async {
|
||||||
Logger.d("Scheduling location update for every ${_updateInterval
|
Logger.d("Scheduling location update for every ${_updateInterval
|
||||||
.inMinutes} minutes...");
|
.inMinutes} minutes...");
|
||||||
|
String webhookId = ConnectionManager().webhookId;
|
||||||
|
String httpWebHost = ConnectionManager().httpWebHost;
|
||||||
|
if (webhookId != null && webhookId.isNotEmpty) {
|
||||||
await workManager.Workmanager.registerPeriodicTask(
|
await workManager.Workmanager.registerPeriodicTask(
|
||||||
alarmId,
|
backgroundTaskId,
|
||||||
"simplePeriodicTask",
|
"haClientLocationTracking",
|
||||||
|
tag: backgroundTaskTag,
|
||||||
|
inputData: {
|
||||||
|
"webhookId": webhookId,
|
||||||
|
"httpWebHost": httpWebHost
|
||||||
|
},
|
||||||
frequency: _updateInterval,
|
frequency: _updateInterval,
|
||||||
existingWorkPolicy: workManager.ExistingWorkPolicy.replace,
|
existingWorkPolicy: workManager.ExistingWorkPolicy.replace,
|
||||||
|
backoffPolicy: workManager.BackoffPolicy.linear,
|
||||||
constraints: workManager.Constraints(
|
constraints: workManager.Constraints(
|
||||||
|
requiresBatteryNotLow: false,
|
||||||
|
requiresCharging: false,
|
||||||
|
requiresDeviceIdle: false,
|
||||||
|
requiresStorageNotLow: false,
|
||||||
networkType: workManager.NetworkType.connected
|
networkType: workManager.NetworkType.connected
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _stopLocationService() async {
|
void _stopLocationService() async {
|
||||||
Logger.d("Canceling previous schedule if any...");
|
Logger.d("Canceling previous schedule if any...");
|
||||||
await workManager.Workmanager.cancelByUniqueName(alarmId);
|
await workManager.Workmanager.cancelByTag(backgroundTaskTag);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateDeviceLocation() async {
|
void updateDeviceLocation() async {
|
||||||
if (_isEnabled) {
|
|
||||||
if (ConnectionManager().webhookId != null &&
|
if (ConnectionManager().webhookId != null &&
|
||||||
ConnectionManager().webhookId.isNotEmpty) {
|
ConnectionManager().webhookId.isNotEmpty) {
|
||||||
String url = "${ConnectionManager()
|
String url = "${ConnectionManager()
|
||||||
@ -100,28 +113,18 @@ class LocationManager {
|
|||||||
body: json.encode(data)
|
body: json.encode(data)
|
||||||
);
|
);
|
||||||
Logger.d("[Location] ...done.");
|
Logger.d("[Location] ...done.");
|
||||||
} else {
|
|
||||||
Logger.d("[Location] No webhook id. Aborting");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Logger.d("[Location] Location tracking is disabled");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateDeviceLocationIsolate() {
|
void updateDeviceLocationIsolate() {
|
||||||
workManager.Workmanager.executeTask((backgroundTask, _) {
|
workManager.Workmanager.executeTask((backgroundTask, data) {
|
||||||
print("[Location isolate] Started: $backgroundTask");
|
print("[Location isolate] Started: $backgroundTask");
|
||||||
//Completer completer = Completer();
|
|
||||||
|
|
||||||
SharedPreferences.getInstance().then((prefs){
|
|
||||||
print("[Location isolate] loading settings");
|
print("[Location isolate] loading settings");
|
||||||
String webhookId = prefs.getString('app-webhook-id');
|
String webhookId = data["webhookId"];
|
||||||
String domain = prefs.getString('hassio-domain');
|
String httpWebHost = data["httpWebHost"];
|
||||||
String port = prefs.getString('hassio-port');
|
|
||||||
String httpWebHost =
|
|
||||||
"${prefs.getString('hassio-res-protocol')}://$domain:$port";
|
|
||||||
if (webhookId != null && webhookId.isNotEmpty) {
|
if (webhookId != null && webhookId.isNotEmpty) {
|
||||||
Logger.d("[Location isolate] Getting device location...");
|
Logger.d("[Location isolate] Getting device location...");
|
||||||
Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) {
|
Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) {
|
||||||
@ -147,12 +150,12 @@ void updateDeviceLocationIsolate() {
|
|||||||
}).then((_) {
|
}).then((_) {
|
||||||
print("[Location isolate] done!");
|
print("[Location isolate] done!");
|
||||||
});
|
});
|
||||||
|
}).catchError((e) {
|
||||||
|
print("[Location isolate] Error getting location: ${e.toString()}");
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
print("[Location isolate] No webhook id. Aborting");
|
print("[Location isolate] No webhook id. Aborting");
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
return Future.value(true);
|
return Future.value(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -133,6 +133,9 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
|
|||||||
value: _locationTrackingEnabled,
|
value: _locationTrackingEnabled,
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value));
|
SharedPreferences.getInstance().then((prefs) => prefs.setBool("location-enabled", value));
|
||||||
|
if (value) {
|
||||||
|
LocationManager().updateDeviceLocation();
|
||||||
|
}
|
||||||
setState(() {
|
setState(() {
|
||||||
_locationTrackingEnabled = value;
|
_locationTrackingEnabled = value;
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user