90 lines
2.6 KiB
Dart
90 lines
2.6 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:csv/csv.dart';
|
|
import 'package:flutter_email_sender/flutter_email_sender.dart';
|
|
import 'package:eagletracker/class/geoPosition.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
/*
|
|
* Function to generate a CSV file with the positions array
|
|
*/
|
|
Future<File> generateCsv(List<GeoPosition> positions) async {
|
|
List<List<dynamic>> csvData = [
|
|
['latitude', 'longitude', 'altitude', 'spped'] // Header
|
|
];
|
|
|
|
for (var position in positions) {
|
|
csvData.add([position.latitude, position.longitude, position.altitude]);
|
|
}
|
|
|
|
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
|
|
String appDocumentsPath = appDocumentsDirectory.path;
|
|
|
|
String csvFilePath = '$appDocumentsPath/positions.csv';
|
|
File csvFile = File(csvFilePath);
|
|
|
|
String csvContent = const ListToCsvConverter().convert(csvData);
|
|
await csvFile.writeAsString(csvContent);
|
|
|
|
return csvFile;
|
|
}
|
|
|
|
/*
|
|
* Function to send an email with the CSV file as an attachment
|
|
*/
|
|
void sendEmailWithAttachment(File csvFile) async {
|
|
final Email email = Email(
|
|
subject: 'Eagle Tr@cker - Positions',
|
|
body: 'Veuillez trouver ci-joint le fichier CSV contenant les positions.',
|
|
recipients: [],
|
|
attachmentPaths: [csvFile.path],
|
|
);
|
|
|
|
try {
|
|
await FlutterEmailSender.send(email);
|
|
} catch (error) {
|
|
throw 'Failed to send email: $error';
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Function to get the values from the manufacturer data according to the data structure
|
|
*/
|
|
List<double> getValueFromAdvData(List<int> data) {
|
|
double latitude = 0;
|
|
double longitude = 0;
|
|
double altitude = 0;
|
|
double speed = 0;
|
|
|
|
List<double> values = [];
|
|
//Latitude
|
|
List<int> dataLatitude = data.sublist(0, 4);
|
|
ByteData byteDataLatitude =
|
|
ByteData.sublistView(Uint8List.fromList(dataLatitude));
|
|
latitude = byteDataLatitude.getFloat32(0, Endian.little);
|
|
values.add(latitude);
|
|
|
|
//Longitude
|
|
List<int> dataLongitude = data.sublist(4, 8);
|
|
ByteData byteDataLongitude =
|
|
ByteData.sublistView(Uint8List.fromList(dataLongitude));
|
|
longitude = byteDataLongitude.getFloat32(0, Endian.little);
|
|
values.add(longitude);
|
|
|
|
//Altitude
|
|
List<int> dataAltitude = data.sublist(8, 12);
|
|
ByteData byteDataAltitude =
|
|
ByteData.sublistView(Uint8List.fromList(dataAltitude));
|
|
altitude = byteDataAltitude.getFloat32(0, Endian.little);
|
|
values.add(altitude);
|
|
|
|
//Speed
|
|
List<int> dataSpeed = data.sublist(12, 16);
|
|
ByteData byteDataSpeed = ByteData.sublistView(Uint8List.fromList(dataSpeed));
|
|
speed = byteDataSpeed.getFloat32(0, Endian.little);
|
|
values.add(speed);
|
|
|
|
return values;
|
|
}
|