36 lines
1.1 KiB
Dart
36 lines
1.1 KiB
Dart
|
import 'dart:io' show File, FileMode;
|
||
|
import 'package:intl/intl.dart' show DateFormat;
|
||
|
import 'package:gps/models/gps_point.dart';
|
||
|
|
||
|
abstract class GpsPersister {
|
||
|
void save(Iterable<GpsPoint> data);
|
||
|
}
|
||
|
|
||
|
class CsvGpsPersister implements GpsPersister {
|
||
|
static const String _csvHeader =
|
||
|
"#Time, Latitude, Longitude, Accuracy, Speed, Heading";
|
||
|
|
||
|
/// Format of DateTime part of file name
|
||
|
static final DateFormat _formatter = DateFormat('yyyyMMdd_HHmmss');
|
||
|
|
||
|
/// Path where the CSV will be stored
|
||
|
final String _rootDir;
|
||
|
|
||
|
const CsvGpsPersister(this._rootDir);
|
||
|
|
||
|
@override
|
||
|
void save(Iterable<GpsPoint> points) {
|
||
|
File file = File("$_rootDir/geo${_formatter.format(DateTime.now())}.csv");
|
||
|
|
||
|
file.writeAsStringSync(_csvHeader);
|
||
|
for (var point in points) {
|
||
|
file.writeAsStringSync("\n${_toCsv(point)}",
|
||
|
mode: FileMode.writeOnlyAppend);
|
||
|
}
|
||
|
file.writeAsString("\n", mode: FileMode.writeOnlyAppend, flush: true);
|
||
|
}
|
||
|
|
||
|
String _toCsv(GpsPoint p) =>
|
||
|
"${p.time}, ${p.latitude}, ${p.longitude}, ${p.accuracy}, ${p.speed}, ${p.heading}";
|
||
|
}
|