private static DeviceFile FromTcxV2_Base(XmlDocument Doc) { var ns = new XmlNamespaceManager(Doc.NameTable); ns.AddNamespace("tcd", "http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"); ns.AddNamespace("ae", "http://www.garmin.com/xmlschemas/ActivityExtension/v2"); XMLParserHelper.Manager = ns; //var Activities = Doc.SelectNodes("/tcd:TrainingCenterDatabase/tcd:Activities/tcd:Activity", ns); var Activities = Doc.GetElementsByTagName("Activity"); DeviceActivity CurrentActivity; DeviceLap CurrentLap; DevicePoint CurrentPoint; DeviceCreator CurrentCreator; DeviceAuthor CurrentAuthor; XmlNode WorkNode; XmlAttribute WorkAttr; DateTime dtTryParse; DateTimeOffset dtoTryParse; DeviceFile File = new DeviceFile(); CurrentAuthor = new DeviceAuthor(); File.Author = CurrentAuthor; string VMajor, VMinor, BMajor, BMinor; CurrentAuthor.Name = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Name"); CurrentAuthor.Language = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:LangID"); CurrentAuthor.PartNumber = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:PartNumber"); VMajor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:VersionMajor", "0"); VMinor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:VersionMinor", "0"); BMajor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:BuildMajor", "0"); BMinor = XMLParserHelper.SelectSingleTextString(Doc, "/tcd:TrainingCenterDatabase/tcd:Author/tcd:Build/tcd:Version/tcd:BuildMinor", "0"); CurrentAuthor.Version = VMajor + "." + VMinor + "." + BMajor + "." + BMinor; foreach (XmlNode Activity in Activities) { CurrentActivity = new DeviceActivity(); File.Activities.Add(CurrentActivity); CurrentActivity.Id = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Id"); if (CurrentActivity.Id != null && DateTimeOffset.TryParse(CurrentActivity.Id, out dtoTryParse)) { //dtoTryParse = FSSecurity.Current.ToUserTime(dtoTryParse); dtTryParse = new DateTime(dtoTryParse.Year, dtoTryParse.Month, dtoTryParse.Day, dtoTryParse.Hour, dtoTryParse.Minute, dtoTryParse.Second); CurrentActivity.ActivityTime = dtTryParse; } CurrentActivity.Sport = XMLParserHelper.SelectSingleAttributeString(Activity, "Sport", "Unknown"); CurrentCreator = new DeviceCreator(); CurrentActivity.Creator = CurrentCreator; CurrentCreator.Name = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Name"); CurrentCreator.UnitID = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:UnitId"); CurrentCreator.ProductID = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:ProductID"); VMajor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:VersionMajor", "0"); VMinor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:VersionMinor", "0"); BMajor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:BuildMajor", "0"); BMinor = XMLParserHelper.SelectSingleTextString(Activity, "tcd:Creator/tcd:Version/tcd:BuildMinor", "0"); CurrentCreator.Version = VMajor + "." + VMinor + "." + BMajor + "." + BMinor; var CreatorName = CurrentCreator.Name ?? ""; var Laps = Activity.SelectNodes("tcd:Lap", ns); var IsFirst = true; foreach (XmlNode Lap in Laps) { var MyInt = XMLParserHelper.SelectSingleTextString(Lap, "tcd:Intensity", ""); // This is for trailing resting tomtom laps that have zero distance and time if (MyInt.ToLower() == "resting" && CreatorName.ToLower().IndexOf("tomtom") >= 0) { continue; } if (IsFirst) { IsFirst = false; var TestSeconds = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:TotalTimeSeconds", null); var TestDist = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:DistanceMeters", null); if (TestSeconds == 0 && TestDist == 0) { continue; } } CurrentLap = new DeviceLap(); CurrentActivity.Laps.Add(CurrentLap); WorkAttr = Lap.Attributes["StartTime"]; if (null != WorkAttr && null != CurrentActivity.ActivityTime && DateTimeOffset.TryParse(WorkAttr.Value, out dtoTryParse)) { //dtoTryParse = FSSecurity.Current.ToUserTime(dtoTryParse); dtTryParse = new DateTime(dtoTryParse.Year, dtoTryParse.Month, dtoTryParse.Day, dtoTryParse.Hour, dtoTryParse.Minute, dtoTryParse.Second); CurrentLap.StartSeconds = (dtTryParse - (CurrentActivity.ActivityTime ?? DateTime.Now)).TotalSeconds; } CurrentLap.Time = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:TotalTimeSeconds", null); CurrentLap.Distance = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:DistanceMeters", null); CurrentLap.SpeedAvg = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:Extensions/ae:LX/ae:AvgSpeed", null); CurrentLap.SpeedMax = XMLParserHelper.SelectSingleTextDecimal(Lap, "tcd:MaximumSpeed", null); CurrentLap.Calories = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Calories", null); CurrentLap.RPMAvg = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Cadence", null); CurrentLap.RPMMax = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Extensions/ae:LX/ae:MaxBikeCadence", null); CurrentLap.HeartRateAvg = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:AverageHeartRateBpm/tcd:Value", null); CurrentLap.HeartRateMax = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:MaximumHeartRateBpm/tcd:Value", null); CurrentLap.WattsAvg = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Extensions/ae:LX/ae:AvgWatts", null); CurrentLap.WattsMax = XMLParserHelper.SelectSingleTextInt(Lap, "tcd:Extensions/ae:LX/ae:MaxWatts", null); var TrackPoints = Lap.SelectNodes("tcd:Track/tcd:Trackpoint", ns); DevicePoint LastTrackPoint = null; foreach (XmlNode Point in TrackPoints) { CurrentPoint = new DevicePoint(); CurrentLap.Track.Add(CurrentPoint); WorkNode = Point.SelectSingleNode("tcd:Time", ns); if (null != WorkNode && null != CurrentActivity.ActivityTime && DateTimeOffset.TryParse(WorkNode.InnerText, out dtoTryParse)) { //dtoTryParse = FSSecurity.Current.ToUserTime(dtoTryParse); dtTryParse = new DateTime(dtoTryParse.Year, dtoTryParse.Month, dtoTryParse.Day, dtoTryParse.Hour, dtoTryParse.Minute, dtoTryParse.Second); CurrentPoint.StartSeconds = (dtTryParse - (CurrentActivity.ActivityTime ?? DateTime.Now)).TotalSeconds; } CurrentPoint.Latitude = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:Position/tcd:LatitudeDegrees", null); CurrentPoint.Longitude = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:Position/tcd:LongitudeDegrees", null); CurrentPoint.Altitude = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:AltitudeMeters", null); CurrentPoint.Distance = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:DistanceMeters", null); CurrentPoint.HR = XMLParserHelper.SelectSingleTextInt(Point, "tcd:HeartRateBpm/tcd:Value", null); CurrentPoint.RPM = XMLParserHelper.SelectSingleTextInt(Point, "tcd:Cadence", null); CurrentPoint.CAD = XMLParserHelper.SelectSingleTextInt(Point, "tcd:Extensions/ae:TPX/ae:RunCadence", null); if (CurrentPoint.CAD != null) { CurrentPoint.CAD = CurrentPoint.CAD.Value * 2; } CurrentPoint.Speed = XMLParserHelper.SelectSingleTextDecimal(Point, "tcd:Extensions/ae:TPX/ae:Speed", null); CurrentPoint.Watts = XMLParserHelper.SelectSingleTextInt(Point, "tcd:Extensions/ae:TPX/ae:Watts", null); if (LastTrackPoint != null && CurrentPoint.Speed == null && (CurrentPoint.Distance != null && CurrentPoint.StartSeconds != null) && (LastTrackPoint.Distance != null && LastTrackPoint.StartSeconds != null)) { // Last Distance and Time vs Current Distance and Time -> var SecondDelta = (decimal)(CurrentPoint.StartSeconds - LastTrackPoint.StartSeconds); var DistanceDelta = CurrentPoint.Distance - LastTrackPoint.Distance; if (SecondDelta == 0 || DistanceDelta == 0) { CurrentPoint.Speed = 0; } else { CurrentPoint.Speed = DistanceDelta / SecondDelta; } } LastTrackPoint = CurrentPoint; } var CadencePoints = from x in CurrentLap.Track where x.CAD != null select x; if (CadencePoints.Any()) { var m = (from x in CadencePoints select x.CAD).Max(); var a = (from x in CadencePoints select x.CAD).Average(); CurrentLap.CADAvg = (int?)a; CurrentLap.CADMax = (int?)m; } } } return(File); }
private static DeviceFile FromGPXV1_Base(XmlDocument document, decimal lapIntervalMeters) { var nav = document.CreateNavigator(); nav.MoveToFollowing(XPathNodeType.Element); if (nav == null) { throw new Exception("File Type Not Supported"); } var namespaces = nav.GetNamespacesInScope(XmlNamespaceScope.Local); if (namespaces == null || !namespaces.Keys.Any()) { throw new Exception("File Type Not Supported"); } var namespacePrefixes = namespaces.Join( ValidNamespaces, o => o.Value.ToLower(), i => i.Value.ToLower(), (i, o) => new { Type = o.Key, Prefix = string.IsNullOrWhiteSpace(i.Key) ? "gpx" : i.Key, Url = o.Value }) .ToList(); var prefixGpx = namespacePrefixes.Single(x => x.Type == NamespaceType.GPX).Prefix; var prefixTrackPointExt = namespacePrefixes.SingleOrDefault(x => x.Type == NamespaceType.TrackPointExtensions)?.Prefix; var prefixCluetrustExt = namespacePrefixes.SingleOrDefault(x => x.Type == NamespaceType.CluetrustExtensions)?.Prefix; var namespaceManager = new XmlNamespaceManager(document.NameTable); namespacePrefixes.ForEach(x => namespaceManager.AddNamespace(x.Prefix, x.Url)); XMLParserHelper.Manager = namespaceManager; var creator = new DeviceCreator { Name = XMLParserHelper.SelectSingleAttributeString( document.DocumentElement, "creator"), Version = XMLParserHelper.SelectSingleAttributeString( document.DocumentElement, "version") }; var author = new DeviceAuthor { Name = creator.Name, Version = creator.Version }; var file = new DeviceFile(); file.Author = author; DateTimeOffset dtoFileTime; DateTime? fileTime = null; var fileTimeString = XMLParserHelper.SelectSingleTextString(document, $"/{prefixGpx}:gpx/{prefixGpx}:metadata/{prefixGpx}:time"); if (!string.IsNullOrWhiteSpace(fileTimeString) && DateTimeOffset.TryParse(fileTimeString, out dtoFileTime)) { // TODO: dtoFileTime = FSSecurity.Current.ToUserTime(dtoFileTime); fileTime = dtoFileTime.DateTime; } // Walk through all tracks and assign to Activities; usually just 1... var activityNodes = document.GetElementsByTagName("trk"); foreach (XmlNode activityNode in activityNodes) { var activity = new DeviceActivity(); file.Activities.Add(activity); activity.Id = XMLParserHelper.SelectSingleTextString(activityNode, $"{prefixGpx}:name"); activity.Creator = creator; activity.Sport = XMLParserHelper.SelectSingleTextString(activityNode, $"{prefixGpx}:type", "Unknown"); activity.ActivityTime = fileTime; // Parse entire list of track points... var allTrackPoints = new List <DevicePoint>(); var trackPointNodes = activityNode.SelectNodes($"{prefixGpx}:trkseg/{prefixGpx}:trkpt", namespaceManager); if (trackPointNodes != null) { DateTime?activityStartTimeUtc = null; foreach (XmlNode trackPointNode in trackPointNodes) { var point = new DevicePoint(); var pointTimeUtc = XMLParserHelper.SelectSingleTextDateTime(trackPointNode, $"{prefixGpx}:time"); if (pointTimeUtc != null) { if (activityStartTimeUtc == null) { activityStartTimeUtc = pointTimeUtc; point.StartSeconds = 0; point.Distance = 0; } else { point.StartSeconds = (pointTimeUtc.Value - activityStartTimeUtc.Value.AddSeconds(1)).TotalSeconds; } } // Parse latitude / longitude... point.Latitude = XMLParserHelper.SelectSingleAttributeDecimal(trackPointNode, "lat"); point.Longitude = XMLParserHelper.SelectSingleAttributeDecimal(trackPointNode, "lon"); // Parse altitude... point.Altitude = XMLParserHelper.SelectSingleTextDecimal( trackPointNode, $"{prefixGpx}:ele"); // Parse TrackPoint extension data... if (prefixTrackPointExt != null) { // Parse heart rate... point.HR = XMLParserHelper.SelectSingleTextInt( trackPointNode, $"{prefixGpx}:extensions/{prefixTrackPointExt}:TrackPointExtension/{prefixTrackPointExt}:hr"); // Parse ambient temp... point.Temp = XMLParserHelper.SelectSingleTextInt( trackPointNode, $"{prefixGpx}:extensions/{prefixTrackPointExt}:TrackPointExtension/{prefixTrackPointExt}:atemp"); // Parse cadence... point.CAD = XMLParserHelper.SelectSingleTextInt( trackPointNode, $"{prefixGpx}:extensions/{prefixTrackPointExt}:TrackPointExtension/{prefixTrackPointExt}:cad"); } // Parse Cluetrust extension data... if (prefixCluetrustExt != null) { // Parse heart rate... if (point.HR == null) { point.HR = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixCluetrustExt}:hr"); } // Parse ambient temp... if (point.Temp == null) { point.Temp = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixCluetrustExt}:temp"); } // Parse cadence... if (point.CAD == null) { point.CAD = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixCluetrustExt}:cadence"); } } // Parse any generic extensions data... // Parse heart rate... if (point.HR == null) { point.HR = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:heartrate"); } // Parse cadence... if (point.CAD == null) { point.CAD = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:cadence"); } // Parse distance... if (point.Distance == null) { point.Distance = XMLParserHelper.SelectSingleTextDecimal(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:distance"); } // Parse power... if (point.Watts == null) { point.Watts = XMLParserHelper.SelectSingleTextInt(trackPointNode, $"{prefixGpx}:extensions/{prefixGpx}:power"); } // Multiply CAD result x 2 like in TCX? // TODO: Bike no...Run yes? if (point.CAD != null) { point.CAD = point.CAD * 2; } allTrackPoints.Add(point); } } // Re-run through point list to calculate distances and break up into laps... var pointsHaveTimeInfo = allTrackPoints.All(p => p.StartSeconds.HasValue); var lapList = new List <DeviceLap>(); var currentLap = new DeviceLap { StartSeconds = pointsHaveTimeInfo ? (double?)0 : null }; for (var i = 1; i < allTrackPoints.Count; i++) { var currentPoint = allTrackPoints[i]; var previousPoint = allTrackPoints[i - 1]; if (currentLap.Track.Count == 0) { var previousLap = activity.Laps.LastOrDefault(); if (previousLap != null && pointsHaveTimeInfo) { previousLap.Time = (decimal)(currentPoint.StartSeconds.Value - previousLap.StartSeconds.Value); } activity.Laps.Add(currentLap); } decimal?currentPointDistanceDelta = null; if (currentPoint.Distance == null) { if (currentPoint.Latitude == null || currentPoint.Longitude == null || previousPoint.Latitude == null || previousPoint.Longitude == null) { continue; } var distanceKm = HaversineInKM( (double)previousPoint.Latitude.Value, (double)previousPoint.Longitude.Value, (double)currentPoint.Latitude.Value, (double)currentPoint.Longitude.Value); currentPointDistanceDelta = (decimal)(distanceKm * 1000.0); currentPoint.Distance = currentPointDistanceDelta + (previousPoint.Distance ?? 0); } if (i == 1) { previousPoint.Distance = previousPoint.Distance ?? 0; currentLap.Track.Add(previousPoint); } currentLap.Track.Add(currentPoint); if (pointsHaveTimeInfo) { var startSecondsDelta = (decimal)(currentPoint.StartSeconds.Value - previousPoint.StartSeconds.Value); startSecondsDelta = startSecondsDelta == 0 ? 1 : startSecondsDelta; currentPointDistanceDelta = currentPointDistanceDelta ?? currentPoint.Distance - previousPoint.Distance; currentPoint.Speed = currentPointDistanceDelta / startSecondsDelta; if (currentLap.StartSeconds == null) { currentLap.StartSeconds = currentPoint.StartSeconds; } } currentLap.Distance = currentLap.Track.Last().Distance.Value - currentLap.Track.First().Distance.Value; if (currentLap.Distance.Value < lapIntervalMeters) { continue; } // Reached the end of the lap, start a new one... currentLap = new DeviceLap { StartSeconds = currentLap.Track.Last().StartSeconds }; } // Calculate Time for last lap... var lastLap = activity.Laps.Last(); if (pointsHaveTimeInfo) { lastLap.Time = (decimal)(lastLap.Track.Last().StartSeconds.Value - lastLap.Track.First().StartSeconds.Value); } // Loop through resulting laps to calculate Time and remaining aggregates... activity.Laps.ForEach(lap => { // Calculate HRMax and HRAvg from lap's collection of HR points... var hrPoints = lap.Track.Where(t => t.HR != null).Select(x => x.HR.Value).ToList(); lap.HeartRateMax = hrPoints.Any() ? (int?)hrPoints.Max() : null; lap.HeartRateAvg = hrPoints.Any() ? (int?)hrPoints.Average() : null; // Calculate CADMax and CADAvg from lap's collection of CAD points... var cadPoints = lap.Track.Where(t => t.CAD != null).Select(x => x.CAD.Value).ToList(); lap.CADMax = cadPoints.Any() ? (int?)cadPoints.Max() : null; lap.CADAvg = cadPoints.Any() ? (int?)cadPoints.Average() : null; // Calculate TempMax and TempAvg from lap's collection of Temp points... var tempPoints = lap.Track.Where(t => t.Temp != null).Select(x => x.Temp.Value).ToList(); lap.TempMax = tempPoints.Any() ? (int?)tempPoints.Max() : null; lap.TempAvg = tempPoints.Any() ? (int?)tempPoints.Average() : null; // Calculage SpeedMax and SpeedAvg from lap's collection of points... var speedPoints = lap.Track.Where(t => t.Speed != null).Select(x => x.Speed.Value).ToList(); lap.SpeedMax = speedPoints.Any() ? (decimal?)speedPoints.Max() : null; lap.SpeedAvg = speedPoints.Any() ? (decimal?)speedPoints.Average() : null; }); } return(file); }