/// <summary> /// See interface docs. /// </summary> public void ResetCoordinates() { lock (this) { LatestCoordinateTime = default(DateTime); FullCoordinates.Clear(); ShortCoordinates.Clear(); FirstCoordinateChanged = 0; LastCoordinateChanged = 0; } }
/// <summary> /// See interface docs. /// </summary> /// <param name="utcNow"></param> /// <param name="shortCoordinateSeconds"></param> public void UpdateCoordinates(DateTime utcNow, int shortCoordinateSeconds) { if (Latitude != null && Longitude != null) { var nowTick = utcNow.Ticks; var lastFullCoordinate = FullCoordinates.Count == 0 ? null : FullCoordinates[FullCoordinates.Count - 1]; var secondLastFullCoordinate = FullCoordinates.Count < 2 ? null : FullCoordinates[FullCoordinates.Count - 2]; if (lastFullCoordinate == null || Latitude != lastFullCoordinate.Latitude || Longitude != lastFullCoordinate.Longitude) { PositionTime = utcNow; // Check to see whether the aircraft appears to be moving impossibly fast and, if it is, reset its trail. Do this even if // the gap between this message and the last is below the threshold for adding to the trails. if (lastFullCoordinate != null) { var distance = GreatCircleMaths.Distance(lastFullCoordinate.Latitude, lastFullCoordinate.Longitude, Latitude, Longitude); if (distance > _ResetCoordinatesDistance) { var fastestTime = _ResetCoordinatesTime * (distance / _ResetCoordinatesDistance); if (nowTick - lastFullCoordinate.Tick < fastestTime) { ResetCoordinates(); } } } // Only update the trails if more than one second has elapsed since the last position update long lastUpdateTick = lastFullCoordinate == null ? 0 : lastFullCoordinate.Tick; if (nowTick - lastUpdateTick >= TicksPerSecond) { var coordinate = new Coordinate(DataVersion, nowTick, (float)Latitude, (float)Longitude, Track); if (FullCoordinates.Count > 1 && (int)(lastFullCoordinate.Heading.GetValueOrDefault() + 0.5f) == (int)(Track.GetValueOrDefault() + 0.5f) && (int)(secondLastFullCoordinate.Heading.GetValueOrDefault() + 0.5f) == (int)(Track.GetValueOrDefault() + 0.5f)) { FullCoordinates[FullCoordinates.Count - 1] = coordinate; } else { FullCoordinates.Add(coordinate); } long earliestAllowable = nowTick - (TicksPerSecond * shortCoordinateSeconds); var firstAllowableIndex = ShortCoordinates.FindIndex(c => c.Tick >= earliestAllowable); if (firstAllowableIndex == -1) { ShortCoordinates.Clear(); } else if (firstAllowableIndex > 0) { ShortCoordinates.RemoveRange(0, firstAllowableIndex); } ShortCoordinates.Add(coordinate); if (FirstCoordinateChanged == 0) { FirstCoordinateChanged = DataVersion; } LastCoordinateChanged = DataVersion; LatestCoordinateTime = utcNow; } } } }