Add log file for background service
This commit is contained in:
parent
fc4cb80b74
commit
3e6229cf3e
@ -8,6 +8,7 @@
|
|||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||||
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
|
||||||
|
|
||||||
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
|
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
import 'dart:io';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/rendering.dart';
|
import 'package:flutter/rendering.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
@ -9,6 +10,7 @@ import 'package:web_socket_channel/io.dart';
|
|||||||
import 'package:event_bus/event_bus.dart';
|
import 'package:event_bus/event_bus.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:cached_network_image/cached_network_image.dart';
|
import 'package:cached_network_image/cached_network_image.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart' as urlLauncher;
|
import 'package:url_launcher/url_launcher.dart' as urlLauncher;
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:date_format/date_format.dart';
|
import 'package:date_format/date_format.dart';
|
||||||
|
@ -154,6 +154,12 @@ void updateDeviceLocationIsolate() {
|
|||||||
var battery = Battery();
|
var battery = Battery();
|
||||||
String webhookId = data["webhookId"];
|
String webhookId = data["webhookId"];
|
||||||
String httpWebHost = data["httpWebHost"];
|
String httpWebHost = data["httpWebHost"];
|
||||||
|
String logData = '==> ${DateTime.now()} [Background $backgroundTask]:';
|
||||||
|
print("[Background $backgroundTask] Getting path for log file...");
|
||||||
|
final logFileDirectory = await getExternalStorageDirectory();
|
||||||
|
print("[Background $backgroundTask] Opening log file...");
|
||||||
|
File logFile = File('${logFileDirectory.path}/ha-client-background-log.txt');
|
||||||
|
print("[Background $backgroundTask] Log file path: ${logFile.path}");
|
||||||
if (webhookId != null && webhookId.isNotEmpty) {
|
if (webhookId != null && webhookId.isNotEmpty) {
|
||||||
String url = "$httpWebHost/api/webhook/$webhookId";
|
String url = "$httpWebHost/api/webhook/$webhookId";
|
||||||
Map<String, String> headers = {};
|
Map<String, String> headers = {};
|
||||||
@ -166,24 +172,64 @@ void updateDeviceLocationIsolate() {
|
|||||||
"battery": 100
|
"battery": 100
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
print("[Background $backgroundTask] Getting battery level...");
|
//print("[Background $backgroundTask] Getting battery level...");
|
||||||
int batteryLevel = await battery.batteryLevel;
|
int batteryLevel;
|
||||||
|
try {
|
||||||
|
batteryLevel = await battery.batteryLevel;
|
||||||
|
//print("[Background $backgroundTask] Got battery level: $batteryLevel");
|
||||||
|
} catch(e) {
|
||||||
|
//print("[Background $backgroundTask] Error getting battery level: $e. Setting zero");
|
||||||
|
batteryLevel = 0;
|
||||||
|
logData += 'Battery: error, $e';
|
||||||
|
}
|
||||||
if (batteryLevel != null) {
|
if (batteryLevel != null) {
|
||||||
data["data"]["battery"] = batteryLevel;
|
data["data"]["battery"] = batteryLevel;
|
||||||
|
logData += 'Battery: success, $batteryLevel';
|
||||||
|
} else {
|
||||||
|
logData += 'Battery: error, level is null';
|
||||||
}
|
}
|
||||||
Position location = await geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high, locationPermissionLevel: GeolocationPermission.locationAlways);
|
Position location;
|
||||||
|
try {
|
||||||
|
location = await geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high, locationPermissionLevel: GeolocationPermission.locationAlways);
|
||||||
if (location != null && location.latitude != null) {
|
if (location != null && location.latitude != null) {
|
||||||
print("[Background $backgroundTask] Got location: ${location.latitude} ${location.longitude}");
|
//print("[Background $backgroundTask] Got location: ${location.latitude} ${location.longitude} (${location.timestamp})");
|
||||||
|
logData += ' || Location: success, ${location.latitude} ${location.longitude} (${location.timestamp})';
|
||||||
data["data"]["gps"] = [location.latitude, location.longitude];
|
data["data"]["gps"] = [location.latitude, location.longitude];
|
||||||
data["data"]["gps_accuracy"] = location.accuracy;
|
data["data"]["gps_accuracy"] = location.accuracy;
|
||||||
print("[Background $backgroundTask] Sending location data home.");
|
} else {
|
||||||
http.post(
|
logData += ' || Location: error, location is null';
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
//print("[Background $backgroundTask] Error getting location: $e. Setting fake one in the middle of the Black See");
|
||||||
|
data["data"]["gps"] = [43.373750, 34.026441];
|
||||||
|
data["data"]["gps_accuracy"] = 200;
|
||||||
|
logData += ' || Location: error, $e';
|
||||||
|
}
|
||||||
|
//print("[Background $backgroundTask] Sending data home.");
|
||||||
|
try {
|
||||||
|
http.Response response = await http.post(
|
||||||
url,
|
url,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
body: json.encode(data)
|
body: json.encode(data)
|
||||||
);
|
);
|
||||||
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
||||||
|
logData += ' || Post: success, ${response.statusCode}';
|
||||||
|
} else {
|
||||||
|
logData += ' || Post: error, ${response.statusCode}';
|
||||||
}
|
}
|
||||||
|
} catch(e) {
|
||||||
|
logData += ' || Post: error, $e';
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
logData += 'Not configured';
|
||||||
|
}
|
||||||
|
print("[Background $backgroundTask] Writing log data...");
|
||||||
|
try {
|
||||||
|
await logFile.writeAsString('$logData\n', mode: FileMode.append);
|
||||||
|
} catch (e) {
|
||||||
|
print("[Background $backgroundTask] Error writing log: $e");
|
||||||
|
}
|
||||||
|
print("[Background $backgroundTask] Finished.");
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -13,6 +13,7 @@ dependencies:
|
|||||||
web_socket_channel: ^1.1.0
|
web_socket_channel: ^1.1.0
|
||||||
shared_preferences: ^0.5.6+1
|
shared_preferences: ^0.5.6+1
|
||||||
progress_indicators: ^0.1.4
|
progress_indicators: ^0.1.4
|
||||||
|
path_provider: ^1.6.5
|
||||||
event_bus: ^1.1.1
|
event_bus: ^1.1.1
|
||||||
cached_network_image: ^2.0.0
|
cached_network_image: ^2.0.0
|
||||||
url_launcher: ^5.4.1
|
url_launcher: ^5.4.1
|
||||||
|
Reference in New Issue
Block a user