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

72 lines
1.8 KiB
C#
Raw 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 CsvWriter output;
public CSVFlightLogger(string path)
{
var filename = Path.Combine(path, DateTime.Now.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
});
}
public void Dispose()
{
output?.Dispose();
}
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
};
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; }
}
}
}