WIP #49 - geolocator plugin testing

This commit is contained in:
estevez-dev 2019-08-30 16:12:03 +03:00
parent 61b459ed8a
commit eea59cf11b
6 changed files with 56 additions and 83 deletions

View File

@ -50,13 +50,6 @@
</intent-filter>
</activity>
<receiver android:name="com.lyokone.location.BackgroundLocationBroadcastReceiver"
android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.lyokone.location.BackgroundLocationBroadcastReceiver.ACTION_PROCESS_UPDATES" />
</intent-filter>
</receiver>
<service
android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
android:permission="android.permission.BIND_JOB_SERVICE"

View File

@ -5,13 +5,11 @@ import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.androidalarmmanager.AlarmService;
import com.lyokone.location.LocationPlugin;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
LocationPlugin.setPluginRegistrant(this);
AlarmService.setPluginRegistrant(this);
}

View File

@ -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';

View File

@ -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
);
}

View File

@ -172,6 +172,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.7"
geolocator:
dependency: "direct main"
description:
name: geolocator
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.3"
google_api_availability:
dependency: transitive
description:
name: google_api_availability
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
http:
dependency: transitive
description:
@ -214,15 +228,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.4.0"
location:
dependency: "direct main"
location_permissions:
dependency: transitive
description:
path: "."
ref: background_location
resolved-ref: "7c089fad38acbb7be7c46f207f601ac8e6b21caf"
url: "git://github.com/Lyokone/flutterlocation.git"
source: git
version: "2.3.5"
name: location_permissions
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
logging:
dependency: transitive
description:

View File

@ -18,7 +18,7 @@ dependencies:
date_format: any
charts_flutter: any
flutter_markdown: any
in_app_purchase: ^0.2.1
in_app_purchase: ^0.2.1+2
# flutter_svg: ^0.10.3
flutter_custom_tabs: ^0.6.0
firebase_messaging: ^5.1.4
@ -27,11 +27,7 @@ dependencies:
device_info: ^0.4.0+2
flutter_local_notifications: ^0.8.2
android_alarm_manager: ^0.4.4
location:
git:
url: git://github.com/Lyokone/flutterlocation.git
ref: background_location
geolocator: ^5.1.3
dev_dependencies:
flutter_test: