Send location on app resume

This commit is contained in:
estevez-dev 2019-08-31 22:37:55 +03:00
parent f9c37f5084
commit 623634cb6e
3 changed files with 54 additions and 4 deletions

View File

@ -300,6 +300,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
_hideBottomBar();
_showInfoBottomBar(progress: true,);
ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){
LocationManager().updateDeviceLocation();
_fetchData();
}, onError: (e) {
_setErrorState(e);

View File

@ -2,7 +2,7 @@ part of '../main.dart';
class LocationManager {
static void updateDeviceLocation() {
static void updateDeviceLocationIsolate() {
print("[Location isolate #${Isolate.current.hashCode}] started");
SharedPreferences.getInstance().then((prefs){
print("[Location isolate #${Isolate.current.hashCode}] loading settings");
@ -81,22 +81,71 @@ class LocationManager {
_startLocationService();
}
final int defaultUpdateIntervalMinutes = 15;
final int alarmId = 34901199;
void _startLocationService() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.reload();
Duration locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ?? 10);
Duration locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ?? defaultUpdateIntervalMinutes);
Logger.d("Canceling previous schedule if any...");
await AndroidAlarmManager.cancel(alarmId);
Logger.d("Scheduling location update for every ${locationUpdateInterval.inMinutes} minutes...");
await AndroidAlarmManager.periodic(
locationUpdateInterval,
alarmId,
LocationManager.updateDeviceLocation,
LocationManager.updateDeviceLocationIsolate,
wakeup: true,
rescheduleOnReboot: true
);
}
void updateDeviceLocation() async {
print("[Location] started");
if (ConnectionManager().webhookId != null && ConnectionManager().webhookId.isNotEmpty) {
DateTime currentTime = DateTime.now();
String timeData = "${currentTime.year}-${currentTime.month}-${currentTime.day} ${currentTime.hour}:${currentTime.minute}";
print("[Location] Sending test time data home...");
String url = "${ConnectionManager().httpWebHost}/api/webhook/${ConnectionManager().webhookId}";
Map<String, String> headers = {};
headers["Content-Type"] = "application/json";
var data = {
"type": "call_service",
"data": {
"domain": "input_datetime",
"service": "set_datetime",
"service_data": {
"entity_id": "input_datetime.app_alarm_service_test",
"datetime": timeData
}
}
};
await http.post(
url,
headers: headers,
body: json.encode(data)
);
Logger.d("[Location] Getting device location...");
Position location = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium);
Logger.d("[Location] Got location: ${location.latitude} ${location.longitude}. Sending home...");
int battery = DateTime.now().hour;
data = {
"type": "update_location",
"data": {
"gps": [location.latitude, location.longitude],
"gps_accuracy": location.accuracy,
"battery": battery
}
};
await http.post(
url,
headers: headers,
body: json.encode(data)
);
Logger.d("[Location] ...done.");
} else {
print("[Location] No webhook id. Aborting");
}
}
}

View File

@ -23,7 +23,7 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> {
await prefs.reload();
SharedPreferences.getInstance().then((prefs) {
setState(() {
locationInterval = prefs.getInt("location-interval") ?? 10;
locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes;
});
});
}