From 623634cb6e6be53ec2e2ebb6109b69a3b8eba5c9 Mon Sep 17 00:00:00 2001 From: estevez-dev Date: Sat, 31 Aug 2019 22:37:55 +0300 Subject: [PATCH] Send location on app resume --- lib/main.dart | 1 + lib/managers/location_manager.class.dart | 55 ++++++++++++++++++++++-- lib/panels/config_panel_widget.dart | 2 +- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 82b13f9..8bb6eb9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -300,6 +300,7 @@ class _MainPageState extends State with WidgetsBindingObserver, Ticker _hideBottomBar(); _showInfoBottomBar(progress: true,); ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){ + LocationManager().updateDeviceLocation(); _fetchData(); }, onError: (e) { _setErrorState(e); diff --git a/lib/managers/location_manager.class.dart b/lib/managers/location_manager.class.dart index 4635e6d..e902dca 100644 --- a/lib/managers/location_manager.class.dart +++ b/lib/managers/location_manager.class.dart @@ -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 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"); + } + } + } \ No newline at end of file diff --git a/lib/panels/config_panel_widget.dart b/lib/panels/config_panel_widget.dart index 9cf0ae9..ad492cf 100644 --- a/lib/panels/config_panel_widget.dart +++ b/lib/panels/config_panel_widget.dart @@ -23,7 +23,7 @@ class _ConfigPanelWidgetState extends State { await prefs.reload(); SharedPreferences.getInstance().then((prefs) { setState(() { - locationInterval = prefs.getInt("location-interval") ?? 10; + locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes; }); }); }