Send location on app resume
This commit is contained in:
		| @@ -300,6 +300,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker | |||||||
|     _hideBottomBar(); |     _hideBottomBar(); | ||||||
|     _showInfoBottomBar(progress: true,); |     _showInfoBottomBar(progress: true,); | ||||||
|     ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){ |     ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){ | ||||||
|  |       LocationManager().updateDeviceLocation(); | ||||||
|       _fetchData(); |       _fetchData(); | ||||||
|     }, onError: (e) { |     }, onError: (e) { | ||||||
|       _setErrorState(e); |       _setErrorState(e); | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ part of '../main.dart'; | |||||||
|  |  | ||||||
| class LocationManager { | class LocationManager { | ||||||
|  |  | ||||||
|   static void updateDeviceLocation() { |   static void updateDeviceLocationIsolate() { | ||||||
|     print("[Location isolate #${Isolate.current.hashCode}] started"); |     print("[Location isolate #${Isolate.current.hashCode}] started"); | ||||||
|     SharedPreferences.getInstance().then((prefs){ |     SharedPreferences.getInstance().then((prefs){ | ||||||
|       print("[Location isolate #${Isolate.current.hashCode}] loading settings"); |       print("[Location isolate #${Isolate.current.hashCode}] loading settings"); | ||||||
| @@ -81,22 +81,71 @@ class LocationManager { | |||||||
|     _startLocationService(); |     _startLocationService(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   final int defaultUpdateIntervalMinutes = 15; | ||||||
|   final int alarmId = 34901199; |   final int alarmId = 34901199; | ||||||
|  |  | ||||||
|   void _startLocationService() async { |   void _startLocationService() async { | ||||||
|     SharedPreferences prefs = await SharedPreferences.getInstance(); |     SharedPreferences prefs = await SharedPreferences.getInstance(); | ||||||
|     await prefs.reload(); |     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..."); |     Logger.d("Canceling previous schedule if any..."); | ||||||
|     await AndroidAlarmManager.cancel(alarmId); |     await AndroidAlarmManager.cancel(alarmId); | ||||||
|     Logger.d("Scheduling location update for every ${locationUpdateInterval.inMinutes} minutes..."); |     Logger.d("Scheduling location update for every ${locationUpdateInterval.inMinutes} minutes..."); | ||||||
|     await AndroidAlarmManager.periodic( |     await AndroidAlarmManager.periodic( | ||||||
|         locationUpdateInterval, |         locationUpdateInterval, | ||||||
|       alarmId, |       alarmId, | ||||||
|       LocationManager.updateDeviceLocation, |       LocationManager.updateDeviceLocationIsolate, | ||||||
|       wakeup: true, |       wakeup: true, | ||||||
|       rescheduleOnReboot: 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"); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
| } | } | ||||||
| @@ -23,7 +23,7 @@ class _ConfigPanelWidgetState extends State<ConfigPanelWidget> { | |||||||
|     await prefs.reload(); |     await prefs.reload(); | ||||||
|     SharedPreferences.getInstance().then((prefs) { |     SharedPreferences.getInstance().then((prefs) { | ||||||
|       setState(() { |       setState(() { | ||||||
|         locationInterval = prefs.getInt("location-interval") ?? 10; |         locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes; | ||||||
|       }); |       }); | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user