public LogbookEntry GetEntryFromAcmiFile(FileInfo acmiFile) { /* copy to temp */ FileInfo tmpFile = acmiFile.CopyTo(Path.GetTempFileName(), true); /* unzip */ string unzipDir = Path.Combine(tmpFile.DirectoryName, "XPlaneLauncher", Path.GetRandomFileName()); ZipFile.ExtractToDirectory(tmpFile.FullName, unzipDir); FileInfo unzippedFile = new DirectoryInfo(unzipDir).GetFiles().First(); /* parse */ AcmiDto acmiDto = _acmiService.ParseFile(unzippedFile); /* remove tmp file */ tmpFile.Delete(); unzippedFile.Directory?.Delete(true); double trackLength = 0; for (int index = 0; index < acmiDto.Track.Count; index++) { Location location = acmiDto.Track[index]; Location nextLocation = acmiDto.Track.ElementAtOrDefault(index + 1); if (nextLocation != null) { trackLength += location.GreatCircleDistance(nextLocation); } } trackLength = Length.FromMeters(trackLength).NauticalMiles; /* Take year from recording time as year for reference time - currently ignore february 29th problems*/ acmiDto.ReferenceTime = new DateTime( acmiDto.RecordingTime.Year, acmiDto.ReferenceTime.Month, acmiDto.ReferenceTime.Day, acmiDto.ReferenceTime.Hour, acmiDto.ReferenceTime.Minute, acmiDto.ReferenceTime.Second); LogbookEntry autoLogEntry = new LogbookEntry( LogbookEntryType.AcmiZip, acmiDto.RecordingTime, acmiDto.RecordingTime.Add(acmiDto.Duration), acmiDto.Duration, acmiDto.Track, trackLength); autoLogEntry.Update(acmiFile.Name); return(autoLogEntry); }
public void TestParsedReferenceLon() { // Given: FileInfo acmiFile = new FileInfo("../../TestData/Tacview-20190924-225048-XPL.txt.acmi"); IAcmiService acmiService = new AcmiService(); // When: AcmiDto acmiContent = acmiService.ParseFile(acmiFile); // Then: acmiContent.ReferenceLongitude.Should().Be(-82); }
public void TestParsedRecordingTime() { // Given: FileInfo acmiFile = new FileInfo("../../TestData/Tacview-20190924-225048-XPL.txt.acmi"); IAcmiService acmiService = new AcmiService(); // When: AcmiDto acmiContent = acmiService.ParseFile(acmiFile); // Then: acmiContent.RecordingTime.Should().Be(new DateTime(2019, 9, 24, 20, 50, 48, DateTimeKind.Utc).AddMilliseconds(772)); }
public void TestDuration() { // Given: FileInfo acmiFile = new FileInfo("../../TestData/Tacview-20190924-225048-XPL.txt.acmi"); IAcmiService acmiService = new AcmiService(); // When: AcmiDto acmiContent = acmiService.ParseFile(acmiFile); // Then: acmiContent.Duration.Should().Be(new TimeSpan(0, 0, 3, 12, 180)); }
public void TestParsedReferenceTime() { // Given: FileInfo acmiFile = new FileInfo("../../TestData/Tacview-20190924-225048-XPL.txt.acmi"); IAcmiService acmiService = new AcmiService(); // When: AcmiDto acmiContent = acmiService.ParseFile(acmiFile); // Then: acmiContent.ReferenceTime.Should().Be(new DateTime(2017, 7, 24, 22, 02, 04, DateTimeKind.Utc)); }
public void TestParsedLocations() { // Given: FileInfo acmiFile = new FileInfo("../../TestData/Tacview-20190924-225048-XPL.txt.acmi"); IAcmiService acmiService = new AcmiService(); // When: AcmiDto acmiContent = acmiService.ParseFile(acmiFile); // Then: acmiContent.Track.Count.Should().Be(12); acmiContent.Track[0].Latitude.Should().Be(39.184346); acmiContent.Track[0].Longitude.Should().Be(-76.6584197); acmiContent.Track[1].Latitude.Should().Be(39.184346); acmiContent.Track[1].Longitude.Should().Be(-76.6584196); acmiContent.Track[2].Latitude.Should().Be(39.1843459); acmiContent.Track[2].Longitude.Should().Be(-76.6584196); acmiContent.Track[3].Latitude.Should().Be(39.1843459); acmiContent.Track[3].Longitude.Should().Be(-76.6584197); acmiContent.Track[4].Latitude.Should().Be(39.1843458); acmiContent.Track[4].Longitude.Should().Be(-76.6584197); acmiContent.Track[5].Latitude.Should().Be(39.1843457); acmiContent.Track[5].Longitude.Should().Be(-76.6584198); acmiContent.Track[6].Latitude.Should().Be(39.1843457); acmiContent.Track[6].Longitude.Should().Be(-76.6584199); acmiContent.Track[7].Latitude.Should().Be(39.1843456); acmiContent.Track[7].Longitude.Should().Be(-76.6584201); acmiContent.Track[8].Latitude.Should().Be(39.1843455); acmiContent.Track[8].Longitude.Should().Be(-76.6584201); acmiContent.Track[9].Latitude.Should().Be(39.184345300000004); acmiContent.Track[9].Longitude.Should().Be(-76.6584202); acmiContent.Track[10].Latitude.Should().Be(39.184345300000004); acmiContent.Track[10].Longitude.Should().Be(-76.6584203); acmiContent.Track[11].Latitude.Should().Be(39.1843452); acmiContent.Track[11].Longitude.Should().Be(-76.6584203); }
private LogbookLocation GetLocation(AcmiDto dto, string[] transformationParts, DateTime timestamp) { double.TryParse(transformationParts[0], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double lonOffset); double longitude; if (lonOffset == 0 && dto.Track.Any()) { longitude = dto.Track.Last().Longitude; } else if (lonOffset == 0 && !dto.Track.Any()) { longitude = dto.ReferenceLongitude; } else { longitude = dto.ReferenceLongitude + lonOffset; } double.TryParse(transformationParts[1], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out double latOffset); double latitude; if (latOffset == 0 && dto.Track.Any()) { latitude = dto.Track.Last().Latitude; } else if (latOffset == 0 && !dto.Track.Any()) { latitude = dto.ReferenceLatitude; } else { latitude = dto.ReferenceLatitude + latOffset; } return(new LogbookLocation(timestamp, latitude, longitude)); }
public AcmiDto ParseFile(FileInfo acmiFile) { AcmiDto dto = new AcmiDto(); string firstTimeOffset = null; string lastTimeOffset = null; using (StreamReader reader = File.OpenText(acmiFile.FullName)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (line != null && line.StartsWith("0")) { string[] lineParts = line.Replace("0,", "").Split('='); switch (lineParts[0]) { case "ReferenceTime": dto.ReferenceTime = ParseToDateTime(lineParts[1]); break; case "RecordingTime": dto.RecordingTime = ParseToDateTime(lineParts[1]); break; case "ReferenceLongitude": dto.ReferenceLongitude = double.Parse(lineParts[1]); break; case "ReferenceLatitude": dto.ReferenceLatitude = double.Parse(lineParts[1]); break; } } else if (line != null && line.StartsWith("#")) { if (firstTimeOffset == null) { firstTimeOffset = line; } lastTimeOffset = line; } else if (line != null) { string[] lineParts = line.Split(','); string transformation = lineParts.FirstOrDefault(x => x.StartsWith("T=")); if (transformation != null) { string[] transformationParts = transformation.Replace("T=", "").Split('|'); if (HasLocation(transformationParts)) { TimeSpan?durationToThisLocation = GetDurationOrDefault(firstTimeOffset, lastTimeOffset); DateTime timestamp = dto.ReferenceTime; if (durationToThisLocation.HasValue) { timestamp.Add(durationToThisLocation.Value); } dto.Track.Add(GetLocation(dto, transformationParts, timestamp)); } } } } } TimeSpan?duration = GetDurationOrDefault(firstTimeOffset, lastTimeOffset); if (duration.HasValue) { dto.Duration = duration.Value; } return(dto); }