public static BestLapViewModel AsViewModel(this BestLap bestLap, bool isMetricUnits)
        {
            var lapViewModel = new BestLapViewModel
            {
                Id              = bestLap.id,
                IsPublic        = bestLap.IsPublic,
                TrackId         = bestLap.TrackId,
                UserName        = bestLap.UserName,
                UserDisplayName = bestLap.UserDisplayName,
                VehicleId       = bestLap.VehicleId,
                Vehicle         = bestLap.Vehicle != null
                            ? bestLap.Vehicle.AsViewModel()
                            : null,
                VerificationCode   = bestLap.VerificationCode,
                StartElapsedTime   = new TimeSpan(0),
                EndElapsedTime     = new TimeSpan(bestLap.LapTimeTicks),
                DataPoints         = new ObservableCollection <LapDataPointViewModel>(),
                Timestamp          = bestLap.Timestamp.ToLocalTime(),
                IsUnofficial       = bestLap.IsUnofficial,
                GpsDeviceName      = bestLap.GpsDeviceName,
                WeatherCondition   = bestLap.WeatherCondition,
                AmbientTemperature = bestLap.AmbientTemperature
            };

            double maximumSpeed = 0;

            foreach (var dataPoint in bestLap.DataPoints)
            {
                double?speedInUserUnits = isMetricUnits
                                            ? dataPoint.Speed * Constants.KMH_TO_METRES_PER_SECOND
                                            : dataPoint.Speed * Constants.KMH_TO_METRES_PER_SECOND / Constants.MILES_TO_KILOMETRES;

                lapViewModel.DataPoints.Add(new LapDataPointViewModel
                {
                    SectorNumber  = dataPoint.SectorNumber,
                    ElapsedTime   = new TimeSpan(dataPoint.ElapsedTimeTicks),
                    AccelerationX = dataPoint.AccelerationX,
                    AccelerationY = dataPoint.AccelerationY,
                    AccelerationZ = dataPoint.AccelerationZ,
                    Altitude      = dataPoint.Altitude,
                    Heading       = dataPoint.Heading,
                    Latitude      = dataPoint.Latitude,
                    Longitude     = dataPoint.Longitude,
                    Speed         = speedInUserUnits
                });
                if (speedInUserUnits.HasValue && speedInUserUnits.Value > maximumSpeed)
                {
                    maximumSpeed = speedInUserUnits.Value;
                }
            }
            lapViewModel.MaximumSpeed = maximumSpeed;

            return(lapViewModel);
        }
Example #2
0
        public static BestLap AsModel(this BestLapViewModel lapViewModel, VehicleViewModel vehicle, bool isMetricUnits, bool isPublic, string userName, long trackId, IEnumerable <TrackSectorViewModel> sectors)
        {
            DateTimeOffset utcLapTimestamp  = lapViewModel.Timestamp.ToUniversalTime();
            var            hashAlgorithm    = new HMACSHA256(Convert.FromBase64String(Constants.APP_LAPVERIFICATION_HASHKEY));
            string         verificationText = string.Format("{0}{1}{2}{3}{4}{5}", Constants.APP_LAPVERIFICATION_PREFIX, trackId, vehicle.Key.ToString().ToUpperInvariant(), lapViewModel.LapTime.Ticks, utcLapTimestamp.ToString("yyyy-MM-ddTHH:mm:ss.fffK"), userName);
            string         verificationCode = Convert.ToBase64String(hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(verificationText)));

            var model = new BestLap
            {
                LapTimeTicks       = lapViewModel.LapTime.Ticks,
                Timestamp          = utcLapTimestamp,
                VehicleId          = vehicle.Id,
                VehicleClass       = (int)vehicle.Class,
                Vehicle            = vehicle.AsModel(),
                VerificationCode   = verificationCode,
                TrackId            = trackId,
                IsPublic           = isPublic,
                UserName           = userName,
                IsUnofficial       = lapViewModel.IsUnofficial,
                GpsDeviceName      = lapViewModel.GpsDeviceName,
                WeatherCondition   = lapViewModel.WeatherCondition,
                AmbientTemperature = lapViewModel.AmbientTemperature
            };

            var dataPoints           = new List <LapDataPoint>();
            var geographicDataPoints = lapViewModel.DataPoints.Where(dp => dp != null && dp.Latitude.HasValue && dp.Longitude.HasValue);

            foreach (var sector in sectors)
            {
                LapDataPointViewModel lastDataPoint     = null;
                LapDataPointViewModel previousDataPoint = null;
                foreach (var dataPoint in geographicDataPoints.SkipWhile(dp => dp.SectorNumber != sector.SectorNumber))
                {
                    if (dataPoint.SectorNumber != sector.SectorNumber)
                    {
                        break;
                    }
                    lastDataPoint = dataPoint;
                    if (previousDataPoint != null && (dataPoint.ElapsedTime - previousDataPoint.ElapsedTime < TimeSpan.FromMilliseconds(900d)))
                    {
                        continue;
                    }
                    previousDataPoint = dataPoint;
                    dataPoints.Add(dataPoint.AsBestLapDataPoint(isMetricUnits));
                }
                // Last data point used for split times
                dataPoints.Add(lastDataPoint.AsBestLapDataPoint(isMetricUnits));
            }
            model.DataPoints = dataPoints;
            return(model);
        }
        public static BestLapViewModel AsViewModel(this LapViewModel lap, bool isMetricUnits, bool lapIsUnofficial = false)
        {
            var bestLapViewModel = new BestLapViewModel
            {
                DataPoints       = new ObservableCollection <LapDataPointViewModel>(),
                StartElapsedTime = TimeSpan.Zero,
                EndElapsedTime   = lap.LapTime,
                MaximumSpeed     = lap.MaximumSpeed,
                SectorNumber     = lap.SectorNumber,
                Timestamp        = lap.Timestamp.ToLocalTime(),
                TotalLaps        = lap.TotalLaps,
                IsUnofficial     = lapIsUnofficial,
                IsComplete       = lap.IsComplete
            };

            foreach (var dataPoint in lap.DataPoints)
            {
                bestLapViewModel.DataPoints.Add(new LapDataPointViewModel
                {
                    SectorNumber  = dataPoint.SectorNumber,
                    ElapsedTime   = dataPoint.ElapsedTime - lap.StartElapsedTime,
                    AccelerationX = dataPoint.AccelerationX,
                    AccelerationY = dataPoint.AccelerationY,
                    AccelerationZ = dataPoint.AccelerationZ,
                    Altitude      = dataPoint.Altitude,
                    Heading       = dataPoint.Heading,
                    Latitude      = dataPoint.Latitude,
                    Longitude     = dataPoint.Longitude,
                    IsEndOfLap    = dataPoint.IsEndOfLap,
                    Speed         = dataPoint.Speed,
                    Timestamp     = dataPoint.Timestamp.ToLocalTime()
                });
            }

            return(bestLapViewModel);
        }
 public static void SetFromModel(this TrackViewModel trackViewModel, Track trackModel, bool isMetricUnits, BestLapViewModel bestLap)
 {
     trackViewModel.Id                  = trackModel.id;
     trackViewModel.Name                = trackModel.Name;
     trackViewModel.Latitude            = trackModel.Latitude;
     trackViewModel.Longitude           = trackModel.Longitude;
     trackViewModel.Description         = trackModel.Description;
     trackViewModel.Length              = trackModel.Length;
     trackViewModel.IsMetricUnits       = isMetricUnits;
     trackViewModel.BackgroundImagePath = trackModel.BackgroundImagePath;
     trackViewModel.Sectors             = trackModel.Sectors != null
                                 ? new ObservableCollection <TrackSectorViewModel>(trackModel.Sectors.Select(AsViewModel))
                                 : null;
     trackViewModel.BestLap      = bestLap;
     trackViewModel.MaximumSpeed = bestLap != null ? bestLap.MaximumSpeed : 0;
     trackViewModel.TotalLaps    = trackModel.TotalLaps;
     trackViewModel.Country      = trackModel.Country;
 }