WIP #49 - geolocator plugin testing
This commit is contained in:
@ -23,8 +23,8 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:device_info/device_info.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:in_app_purchase/in_app_purchase.dart';
|
||||
import 'package:location/location.dart';
|
||||
import 'package:android_alarm_manager/android_alarm_manager.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
|
||||
part 'const.dart';
|
||||
part 'entities/entity.class.dart';
|
||||
|
@ -2,47 +2,7 @@ part of '../main.dart';
|
||||
|
||||
class LocationManager {
|
||||
|
||||
static void updateDeviceLocation(List<LocationData> locations) {
|
||||
print("[GPS isolate #${Isolate.current.hashCode}] Got device location update");
|
||||
SharedPreferences.getInstance().then((prefs){
|
||||
print("[GPS isolate #${Isolate.current.hashCode}] loading settings");
|
||||
String webhookId = prefs.getString('app-webhook-id');
|
||||
String domain = prefs.getString('hassio-domain');
|
||||
String port = prefs.getString('hassio-port');
|
||||
String httpWebHost =
|
||||
"${prefs.getString('hassio-res-protocol')}://$domain:$port";
|
||||
if (webhookId != null && webhookId.isNotEmpty) {
|
||||
int battery = DateTime.now().hour;
|
||||
try {
|
||||
print("[GPS isolate #${Isolate.current.hashCode}] Sending data home...");
|
||||
String url = "$httpWebHost/api/webhook/$webhookId";
|
||||
Map<String, String> headers = {};
|
||||
headers["Content-Type"] = "application/json";
|
||||
var data = {
|
||||
"type": "update_location",
|
||||
"data": {
|
||||
"gps": [locations[0].latitude, locations[0].longitude],
|
||||
"gps_accuracy": locations[0].accuracy,
|
||||
"battery": battery
|
||||
}
|
||||
};
|
||||
http.post(
|
||||
url,
|
||||
headers: headers,
|
||||
body: json.encode(data)
|
||||
);
|
||||
} on PlatformException catch (e) {
|
||||
if (e.code == 'PERMISSION_DENIED') {
|
||||
print("[GPS isolate #${Isolate.current.hashCode}] No location permission. Aborting");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
print("[GPS isolate #${Isolate.current.hashCode}] No webhook id. Aborting");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static void updateTestEntity() {
|
||||
static void updateDeviceLocation() {
|
||||
print("[Test isolate #${Isolate.current.hashCode}] alarm service callback");
|
||||
SharedPreferences.getInstance().then((prefs){
|
||||
print("[Test isolate #${Isolate.current.hashCode}] loading settings");
|
||||
@ -55,7 +15,7 @@ class LocationManager {
|
||||
DateTime currentTime = DateTime.now();
|
||||
String timeData = "${currentTime.year}-${currentTime.month}-${currentTime.day} ${currentTime.hour}:${currentTime.minute}";
|
||||
try {
|
||||
print("[Test isolate #${Isolate.current.hashCode}] Sending data home...");
|
||||
print("[Test isolate #${Isolate.current.hashCode}] Sending test time data home...");
|
||||
String url = "$httpWebHost/api/webhook/$webhookId";
|
||||
Map<String, String> headers = {};
|
||||
headers["Content-Type"] = "application/json";
|
||||
@ -78,6 +38,32 @@ class LocationManager {
|
||||
} catch (e) {
|
||||
print("[Test isolate #${Isolate.current.hashCode}] Error: ${e.toString()}");
|
||||
}
|
||||
Logger.d("[Test isolate #${Isolate.current.hashCode}] Getting device location...");
|
||||
Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) {
|
||||
Logger.d("[Test isolate #${Isolate.current.hashCode}] Got location: ${location.latitude} ${location.longitude}. Sending home...");
|
||||
int battery = DateTime.now().hour;
|
||||
try {
|
||||
String url = "$httpWebHost/api/webhook/$webhookId";
|
||||
Map<String, String> headers = {};
|
||||
headers["Content-Type"] = "application/json";
|
||||
var data = {
|
||||
"type": "update_location",
|
||||
"data": {
|
||||
"gps": [location.latitude, location.longitude],
|
||||
"gps_accuracy": location.accuracy,
|
||||
"battery": battery
|
||||
}
|
||||
};
|
||||
http.post(
|
||||
url,
|
||||
headers: headers,
|
||||
body: json.encode(data)
|
||||
);
|
||||
} catch (e) {
|
||||
print("[Test isolate #${Isolate.current.hashCode}] Error sending location: ${e.toString()}");
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
print("[Test isolate #${Isolate.current.hashCode}] No webhook id. Aborting");
|
||||
}
|
||||
@ -95,29 +81,17 @@ class LocationManager {
|
||||
_registerLocationListener();
|
||||
}
|
||||
|
||||
final int alarmId = 349011;
|
||||
final Duration testAlarmUpdateInterval = Duration(minutes: 1);
|
||||
final Duration locationUpdateInterval = Duration(minutes: 1);
|
||||
final int alarmId = 34901199;
|
||||
final Duration locationUpdateInterval = Duration(minutes: 5);
|
||||
|
||||
void _registerLocationListener() async {
|
||||
var _locationService = Location();
|
||||
bool _permission = await _locationService.requestPermission();
|
||||
if (_permission) {
|
||||
Logger.d("Activating device location tracking");
|
||||
_locationService.changeSettings(interval: locationUpdateInterval.inMilliseconds, accuracy: LocationAccuracy.BALANCED);
|
||||
bool statusBackgroundLocation = await _locationService.registerBackgroundLocation(LocationManager.updateDeviceLocation);
|
||||
Logger.d("Location listener status: $statusBackgroundLocation");
|
||||
} else {
|
||||
Logger.e("Location permission not granted");
|
||||
}
|
||||
//await AndroidAlarmManager.cancel(alarmId);
|
||||
Logger.d("Activating alarm service test");
|
||||
await AndroidAlarmManager.periodic(
|
||||
testAlarmUpdateInterval,
|
||||
locationUpdateInterval,
|
||||
alarmId,
|
||||
LocationManager.updateTestEntity,
|
||||
LocationManager.updateDeviceLocation,
|
||||
wakeup: true,
|
||||
rescheduleOnReboot: false
|
||||
rescheduleOnReboot: true
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user