1
0
mirror of synced 2024-11-15 01:23:49 +00:00
FSFlightLogger/FlightLoggerLib/Concrete/CSVFlightLogger.cs

89 lines
2.2 KiB
C#
Raw Permalink Normal View History

2020-09-02 10:49:09 +00:00
using CsvHelper;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CsvHelper.Configuration;
using CsvHelper.Configuration.Attributes;
namespace FlightLoggerLib.Concrete
{
public class CSVFlightLogger : IFlightLogger
{
private readonly string path;
private CsvWriter output;
2020-09-02 10:49:09 +00:00
public CSVFlightLogger(string path)
{
this.path = path;
}
2020-09-02 10:49:09 +00:00
public async ValueTask DisposeAsync()
{
if (output != null)
await output.DisposeAsync();
2020-09-02 10:49:09 +00:00
}
public async Task NewLog()
2020-09-02 10:49:09 +00:00
{
if (output != null)
{
await output.DisposeAsync();
output = null;
}
2020-09-02 10:49:09 +00:00
}
public async Task LogPosition(DateTime eventTime, FlightPosition position)
{
var record = new OutputRecord
{
Time = eventTime,
Latitude = position.Latitude,
Longitude = position.Longitude,
Altitude = position.Altitude,
Airspeed = position.Airspeed
};
if (output == null)
{
var filename = Path.Combine(path, eventTime.ToString("yyyy-MM-dd HH.mm.ss") + ".csv");
var header = !File.Exists(filename);
output = new CsvWriter(new StreamWriter(filename, true), new CsvConfiguration(CultureInfo.CurrentCulture)
{
SanitizeForInjection = false,
HasHeaderRecord = header
});
}
2020-09-02 10:49:09 +00:00
await output.WriteRecordsAsync(Enumerable.Repeat(record, 1));
await output.FlushAsync();
}
protected class OutputRecord
{
[Index(0)]
public DateTime Time { get; set; }
[Index(1)]
public float Latitude { get; set; }
[Index(2)]
public float Longitude { get; set; }
[Index(3)]
public float Altitude { get; set; }
[Index(4)]
public float Airspeed { get; set; }
}
}
}