public async Task Transform(string canBusLogFile, string targetCsvFile) { DateTime lastTimestamp = default; CsvRow lastRow = null; var row = new CsvRow(); using var reader = File.OpenText(canBusLogFile); var timeLine = await _canBusLogFileTimelineReader.ReadFromCanBusLog(reader, true); await using var writer = File.CreateText(targetCsvFile); await _csvRowWriter.WriteHeader(writer); var lines = 0; foreach (var timedMessage in timeLine.Where(m => !(m.Value is UnknownMessage))) { var timestamp = timedMessage.Timestamp ?? default; var message = timedMessage.Value; ParseMessage(message, row); if (timestamp == default || timestamp == lastTimestamp) { continue; } if (lastTimestamp == default) { lastTimestamp = timestamp; row = new CsvRow { Timestamp = timestamp }; continue; } EnrichMemoizedValues(row, lastRow); await _csvRowWriter.WriteLine(writer, row); if (lines++ % 100 == 0) { Console.Write('.'); } lastTimestamp = timestamp; lastRow = row; row = new CsvRow { Timestamp = timestamp }; } Console.WriteLine(); }
public async IAsyncEnumerable <MessageTimeline> LoadTimelines(string path, bool includeSubDirs) { var directoryInfo = new DirectoryInfo(path); if (path.Length <= 3 && path.Contains(':')) { directoryInfo = directoryInfo.Root; } var searchOption = includeSubDirs ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; foreach (var file in directoryInfo.EnumerateFiles("*.*", searchOption)) { if (!AllowedFileExtensions.Contains(file.Extension)) { continue; } using var reader = new StreamReader(file.FullName); yield return(await _canBusLogFileTimelineReader.ReadFromCanBusLog(reader, true)); } }