2019-08-30 15:04:51 +03:00
|
|
|
part of '../main.dart';
|
|
|
|
|
|
|
|
class LocationManager {
|
|
|
|
|
2019-08-31 22:37:55 +03:00
|
|
|
static void updateDeviceLocationIsolate() {
|
2019-08-30 21:45:34 +03:00
|
|
|
print("[Location isolate #${Isolate.current.hashCode}] started");
|
2019-08-30 15:04:51 +03:00
|
|
|
SharedPreferences.getInstance().then((prefs){
|
2019-08-30 21:45:34 +03:00
|
|
|
print("[Location isolate #${Isolate.current.hashCode}] loading settings");
|
2019-08-30 15:04:51 +03:00
|
|
|
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) {
|
|
|
|
DateTime currentTime = DateTime.now();
|
|
|
|
String timeData = "${currentTime.year}-${currentTime.month}-${currentTime.day} ${currentTime.hour}:${currentTime.minute}";
|
|
|
|
try {
|
2019-08-30 21:45:34 +03:00
|
|
|
print("[Location isolate #${Isolate.current.hashCode}] Sending test time data home...");
|
2019-08-30 15:04:51 +03:00
|
|
|
String url = "$httpWebHost/api/webhook/$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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
http.post(
|
|
|
|
url,
|
|
|
|
headers: headers,
|
|
|
|
body: json.encode(data)
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2019-08-30 21:45:34 +03:00
|
|
|
print("[Location isolate #${Isolate.current.hashCode}] Error: ${e.toString()}");
|
2019-08-30 15:04:51 +03:00
|
|
|
}
|
2019-08-30 21:45:34 +03:00
|
|
|
Logger.d("[Location isolate #${Isolate.current.hashCode}] Getting device location...");
|
2019-08-30 16:12:03 +03:00
|
|
|
Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.medium).then((location) {
|
2019-08-30 21:45:34 +03:00
|
|
|
Logger.d("[Location isolate #${Isolate.current.hashCode}] Got location: ${location.latitude} ${location.longitude}. Sending home...");
|
2019-08-30 16:12:03 +03:00
|
|
|
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) {
|
2019-08-30 21:45:34 +03:00
|
|
|
print("[Location isolate #${Isolate.current.hashCode}] Error sending location: ${e.toString()}");
|
2019-08-30 16:12:03 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-30 15:04:51 +03:00
|
|
|
} else {
|
2019-08-30 21:45:34 +03:00
|
|
|
print("[Location isolate #${Isolate.current.hashCode}] No webhook id. Aborting");
|
2019-08-30 15:04:51 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static final LocationManager _instance = LocationManager
|
|
|
|
._internal();
|
|
|
|
|
|
|
|
factory LocationManager() {
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocationManager._internal() {
|
2019-08-30 21:45:34 +03:00
|
|
|
_startLocationService();
|
2019-08-30 15:04:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-31 22:37:55 +03:00
|
|
|
final int defaultUpdateIntervalMinutes = 15;
|
2019-08-30 16:12:03 +03:00
|
|
|
final int alarmId = 34901199;
|
2019-08-30 15:04:51 +03:00
|
|
|
|
2019-08-30 21:45:34 +03:00
|
|
|
void _startLocationService() async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.reload();
|
2019-08-31 22:37:55 +03:00
|
|
|
Duration locationUpdateInterval = Duration(minutes: prefs.getInt("location-interval") ?? defaultUpdateIntervalMinutes);
|
2019-08-30 21:45:34 +03:00
|
|
|
Logger.d("Canceling previous schedule if any...");
|
|
|
|
await AndroidAlarmManager.cancel(alarmId);
|
|
|
|
Logger.d("Scheduling location update for every ${locationUpdateInterval.inMinutes} minutes...");
|
2019-08-30 15:51:39 +03:00
|
|
|
await AndroidAlarmManager.periodic(
|
2019-08-30 16:12:03 +03:00
|
|
|
locationUpdateInterval,
|
2019-08-30 15:51:39 +03:00
|
|
|
alarmId,
|
2019-08-31 22:37:55 +03:00
|
|
|
LocationManager.updateDeviceLocationIsolate,
|
2019-08-30 15:51:39 +03:00
|
|
|
wakeup: true,
|
2019-08-30 16:12:03 +03:00
|
|
|
rescheduleOnReboot: true
|
2019-08-30 15:51:39 +03:00
|
|
|
);
|
2019-08-30 15:04:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-31 22:37:55 +03:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 15:04:51 +03:00
|
|
|
}
|