Example #1
0
        public void TestInitialise()
        {
            _OriginalFactory = Factory.TakeSnapshot();

            _Clock = new ClockMock();
            _Timer = TestUtilities.CreateMockImplementation <ITimer>();

            _Compressor = TestUtilities.CreateMockImplementation <IBaseStationMessageCompressor>();

            _Server = Factory.Resolve <IRebroadcastServer>();

            _Feed     = TestUtilities.CreateMockInstance <IFeed>();
            _Listener = TestUtilities.CreateMockInstance <IListener>();
            _Feed.SetupGet(r => r.Listener).Returns(_Listener.Object);

            _Connector = new MockConnector <INetworkConnector, INetworkConnection>();

            _AircraftListJsonBuilder     = TestUtilities.CreateMockImplementation <IAircraftListJsonBuilder>();
            _AircraftListJson            = new AircraftListJson();
            _AircraftListJsonBuilderArgs = null;
            _IgnoreInvisibleFeeds        = null;
            _FallbackToDefault           = null;
            _AircraftListJsonBuilder.Setup(r => r.Build(It.IsAny <AircraftListJsonBuilderArgs>(), It.IsAny <bool>(), It.IsAny <bool>())).Callback((AircraftListJsonBuilderArgs args, bool ignoreInvisibleFeeds, bool fallbackToDefault) => {
                _AircraftListJsonBuilderArgs = args;
                _IgnoreInvisibleFeeds        = ignoreInvisibleFeeds;
                _FallbackToDefault           = fallbackToDefault;
            }).Returns(_AircraftListJson);

            _AircraftList = TestUtilities.CreateMockImplementation <IBaseStationAircraftList>();
            _Feed.SetupGet(r => r.AircraftList).Returns(_AircraftList.Object);
            _SnapshotAircraft = new List <IAircraft>();
            long of1, of2;

            _AircraftList.Setup(m => m.TakeSnapshot(out of1, out of2)).Returns(_SnapshotAircraft);

            _Server.UniqueId  = 1;
            _Server.Name      = "It's the code word";
            _Server.Format    = RebroadcastFormat.Port30003;
            _Server.Feed      = _Feed.Object;
            _Server.Connector = _Connector.Object;

            _ExceptionCaughtEvent              = new EventRecorder <EventArgs <Exception> >();
            _ExceptionCaughtEvent.EventRaised += DefaultExceptionCaughtHandler;
            _OnlineChangedEvent      = new EventRecorder <EventArgs>();
            _Server.ExceptionCaught += _ExceptionCaughtEvent.Handler;
            _Server.OnlineChanged   += _OnlineChangedEvent.Handler;

            _Port30003Message = new BaseStationMessage()
            {
                Icao24 = "313233"
            };
        }
        public void AircraftListJson_Constructor_Initialises_To_Known_State_And_Properties_Work()
        {
            var aircraftListJson = new AircraftListJson();

            Assert.AreEqual(0, aircraftListJson.Aircraft.Count);
            TestUtilities.TestProperty(aircraftListJson, "AvailableAircraft", 0, 12);
            TestUtilities.TestProperty(aircraftListJson, "FlagHeight", 0, 19);
            TestUtilities.TestProperty(aircraftListJson, "FlagWidth", 0, 132);
            TestUtilities.TestProperty(aircraftListJson, "LastDataVersion", null, "192");
            TestUtilities.TestProperty(aircraftListJson, "ServerTime", 0L, 900L);
            TestUtilities.TestProperty(aircraftListJson, "ShortTrailLengthSeconds", 0, 89);
            TestUtilities.TestProperty(aircraftListJson, "ShowFlags", false);
            TestUtilities.TestProperty(aircraftListJson, "ShowPictures", false);
            TestUtilities.TestProperty(aircraftListJson, "ShowSilhouettes", false);
            TestUtilities.TestProperty(aircraftListJson, "Source", 0, 3);
        }
Example #3
0
        public void AircraftListJson_Constructor_Initialises_To_Known_State_And_Properties_Work()
        {
            var aircraftListJson = new AircraftListJson();

            Assert.AreEqual(0, aircraftListJson.Aircraft.Count);
            Assert.AreEqual(0, aircraftListJson.Feeds.Count);
            TestUtilities.TestProperty(aircraftListJson, r => r.AvailableAircraft, 0, 12);
            TestUtilities.TestProperty(aircraftListJson, r => r.FlagHeight, 0, 19);
            TestUtilities.TestProperty(aircraftListJson, r => r.FlagWidth, 0, 132);
            TestUtilities.TestProperty(aircraftListJson, r => r.LastDataVersion, null, "192");
            TestUtilities.TestProperty(aircraftListJson, r => r.ServerTime, 0L, 900L);
            TestUtilities.TestProperty(aircraftListJson, r => r.ShortTrailLengthSeconds, 0, 89);
            TestUtilities.TestProperty(aircraftListJson, r => r.ShowFlags, false);
            TestUtilities.TestProperty(aircraftListJson, r => r.ShowPictures, false);
            TestUtilities.TestProperty(aircraftListJson, r => r.ShowSilhouettes, false);
            TestUtilities.TestProperty(aircraftListJson, r => r.Source, 0, 3);
            TestUtilities.TestProperty(aircraftListJson, r => r.SourceFeedId, 0, 129);
            TestUtilities.TestProperty(aircraftListJson, r => r.ServerConfigChanged, false);
        }
Example #4
0
        /// <summary>
        /// Returns a fully-formed <see cref="AircraftListJson"/> from the aircraft list passed across.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public AircraftListJson Build(AircraftListJsonBuilderArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("aircraftList");
            }
            if (args.AircraftList == null)
            {
                throw new InvalidOperationException("The AircraftList must be supplied");
            }

            var result = new AircraftListJson()
            {
                FlagHeight              = 20,
                FlagWidth               = 85,
                ShowFlags               = _ShowFlags && !args.IsFlightSimulatorList,
                ShowPictures            = _ShowPictures && (!args.IsInternetClient || _ShowPicturesToInternetClients) && !args.IsFlightSimulatorList,
                ShowSilhouettes         = _ShowSilhouettes && !args.IsFlightSimulatorList,
                ShortTrailLengthSeconds = _ShortTrailLength,
                Source = (int)args.AircraftList.Source,
            };

            long timestamp, dataVersion;
            var  aircraft = args.AircraftList.TakeSnapshot(out timestamp, out dataVersion);

            result.AvailableAircraft = aircraft.Count;
            result.LastDataVersion   = dataVersion.ToString();
            result.ServerTime        = JavascriptHelper.ToJavascriptTicks(timestamp);

            Dictionary <int, double?> distances = new Dictionary <int, double?>();

            aircraft = FilterAircraft(aircraft, args, ref distances);
            SortAircraft(aircraft, args, distances);
            CopyAircraft(result, aircraft, args, distances);

            return(result);
        }
Example #5
0
 public void TestInitialise()
 {
     _Converter        = Factory.Resolve <IAircraftListJsonMessageConverter>();
     _AircraftListJson = new AircraftListJson();
 }
        /// <summary>
        /// Copies the aircraft from the snapshot to the JSON object.
        /// </summary>
        /// <param name="aircraftListJson"></param>
        /// <param name="aircraftListSnapshot"></param>
        /// <param name="args"></param>
        /// <param name="distances"></param>
        private void CopyAircraft(AircraftListJson aircraftListJson, List <IAircraft> aircraftListSnapshot, AircraftListJsonBuilderArgs args, Dictionary <int, double?> distances)
        {
            var now                      = Provider.UtcNow;
            var configuration            = _SharedConfiguration.Get();
            var positionTimeoutThreshold = now.AddSeconds(-(configuration.BaseStationSettings.DisplayTimeoutSeconds + BoostStalePositionSeconds));

            HashSet <int> previousAircraftSet = null;
            List <int>    previousAircraft    = args.PreviousAircraft;

            if (previousAircraft.Count > 15)
            {
                previousAircraftSet = new HashSet <int>(previousAircraft);
                previousAircraft    = null;
            }

            double?distance = null;

            for (var i = 0; i < aircraftListSnapshot.Count; ++i)
            {
                var aircraftSnapshot = aircraftListSnapshot[i];
                if (distances != null)
                {
                    distances.TryGetValue(aircraftSnapshot.UniqueId, out distance);
                }

                var aircraftJson = new AircraftJson()
                {
                    UniqueId     = aircraftSnapshot.UniqueId,
                    IsSatcomFeed = aircraftSnapshot.LastSatcomUpdate != DateTime.MinValue,
                };
                if (!args.OnlyIncludeMessageFields)
                {
                    aircraftJson.BearingFromHere  = GreatCircleMaths.Bearing(args.BrowserLatitude, args.BrowserLongitude, aircraftSnapshot.Latitude, aircraftSnapshot.Longitude, null, false, true);
                    aircraftJson.DistanceFromHere = distance == null ? (double?)null : Math.Round(distance.Value, 2);
                    if (aircraftJson.BearingFromHere != null)
                    {
                        aircraftJson.BearingFromHere = Math.Round(aircraftJson.BearingFromHere.Value, 1);
                    }
                }

                var firstTimeSeen = previousAircraft != null ? !previousAircraft.Contains(aircraftSnapshot.UniqueId)
                                                             : !previousAircraftSet.Contains(aircraftSnapshot.UniqueId);

                if (firstTimeSeen || aircraftSnapshot.AirPressureInHgChanged > args.PreviousDataVersion)
                {
                    aircraftJson.AirPressureInHg = aircraftSnapshot.AirPressureInHg;
                }
                if (firstTimeSeen || aircraftSnapshot.AltitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Altitude = aircraftSnapshot.Altitude;
                }
                if (firstTimeSeen || aircraftSnapshot.AltitudeTypeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.AltitudeType = (int)aircraftSnapshot.AltitudeType;
                }
                if (firstTimeSeen || aircraftSnapshot.CallsignChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Callsign = aircraftSnapshot.Callsign;
                }
                if (firstTimeSeen || aircraftSnapshot.CallsignIsSuspectChanged > args.PreviousDataVersion)
                {
                    aircraftJson.CallsignIsSuspect = aircraftSnapshot.CallsignIsSuspect;
                }
                if (firstTimeSeen || aircraftSnapshot.GroundSpeedChanged > args.PreviousDataVersion)
                {
                    aircraftJson.GroundSpeed = Round.GroundSpeed(aircraftSnapshot.GroundSpeed);
                }
                if (firstTimeSeen || aircraftSnapshot.EmergencyChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Emergency = aircraftSnapshot.Emergency;
                }
                if (firstTimeSeen || aircraftSnapshot.GeometricAltitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.GeometricAltitude = aircraftSnapshot.GeometricAltitude;
                }
                if (firstTimeSeen || args.AlwaysShowIcao || aircraftSnapshot.Icao24Changed > args.PreviousDataVersion)
                {
                    aircraftJson.Icao24 = aircraftSnapshot.Icao24;
                }
                if (firstTimeSeen || aircraftSnapshot.IsTisbChanged > args.PreviousDataVersion)
                {
                    aircraftJson.IsTisb = aircraftSnapshot.IsTisb;
                }
                if (firstTimeSeen || aircraftSnapshot.LatitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Latitude = Round.Coordinate(aircraftSnapshot.Latitude);
                }
                if (firstTimeSeen || aircraftSnapshot.LongitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Longitude = Round.Coordinate(aircraftSnapshot.Longitude);
                }
                if (firstTimeSeen || aircraftSnapshot.OnGroundChanged > args.PreviousDataVersion)
                {
                    aircraftJson.OnGround = aircraftSnapshot.OnGround;
                }
                if (firstTimeSeen || aircraftSnapshot.PositionIsMlatChanged > args.PreviousDataVersion)
                {
                    aircraftJson.PositionIsMlat = aircraftSnapshot.PositionIsMlat;
                }
                if (firstTimeSeen || aircraftSnapshot.SignalLevelChanged > args.PreviousDataVersion)
                {
                    aircraftJson.HasSignalLevel = aircraftSnapshot.SignalLevel != null; aircraftJson.SignalLevel = aircraftSnapshot.SignalLevel;
                }
                if (firstTimeSeen || aircraftSnapshot.SpeedTypeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.SpeedType = (int)aircraftSnapshot.SpeedType;
                }
                if (firstTimeSeen || aircraftSnapshot.SquawkChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Squawk = String.Format("{0:0000}", aircraftSnapshot.Squawk);
                }
                if (firstTimeSeen || aircraftSnapshot.TargetAltitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.TargetAltitude = aircraftSnapshot.TargetAltitude;
                }
                if (firstTimeSeen || aircraftSnapshot.TargetTrackChanged > args.PreviousDataVersion)
                {
                    aircraftJson.TargetTrack = aircraftSnapshot.TargetTrack;
                }
                if (firstTimeSeen || aircraftSnapshot.TrackChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Track = Round.Track(aircraftSnapshot.Track);
                }
                if (firstTimeSeen || aircraftSnapshot.TrackIsHeadingChanged > args.PreviousDataVersion)
                {
                    aircraftJson.TrackIsHeading = aircraftSnapshot.TrackIsHeading;
                }
                if (firstTimeSeen || aircraftSnapshot.TransponderTypeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.TransponderType = (int)aircraftSnapshot.TransponderType;
                }
                if (firstTimeSeen || aircraftSnapshot.VerticalRateChanged > args.PreviousDataVersion)
                {
                    aircraftJson.VerticalRate = aircraftSnapshot.VerticalRate;
                }
                if (firstTimeSeen || aircraftSnapshot.VerticalRateTypeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.VerticalRateType = (int)aircraftSnapshot.VerticalRateType;
                }

                if (args.OnlyIncludeMessageFields)
                {
                    if (aircraftJson.Latitude != null || aircraftJson.Longitude != null || aircraftJson.PositionIsMlat != null)
                    {
                        if (aircraftJson.Latitude == null)
                        {
                            aircraftJson.Latitude = Round.Coordinate(aircraftSnapshot.Latitude);
                        }
                        if (aircraftJson.Longitude == null)
                        {
                            aircraftJson.Longitude = Round.Coordinate(aircraftSnapshot.Longitude);
                        }
                        if (aircraftJson.PositionIsMlat == null)
                        {
                            aircraftJson.PositionIsMlat = aircraftSnapshot.PositionIsMlat;
                        }
                    }
                    if (aircraftJson.Altitude != null || aircraftJson.GeometricAltitude != null || aircraftJson.AltitudeType != null)
                    {
                        if (aircraftJson.Altitude == null)
                        {
                            aircraftJson.Altitude = aircraftSnapshot.Altitude;
                        }
                        if (aircraftJson.AltitudeType == null)
                        {
                            aircraftJson.AltitudeType = (int)aircraftSnapshot.AltitudeType;
                        }
                        if (aircraftJson.GeometricAltitude == null)
                        {
                            aircraftJson.GeometricAltitude = aircraftSnapshot.GeometricAltitude;
                        }
                    }
                }
                else if (!args.OnlyIncludeMessageFields)
                {
                    if (firstTimeSeen || aircraftSnapshot.ConstructionNumberChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.ConstructionNumber = aircraftSnapshot.ConstructionNumber;
                    }
                    if (firstTimeSeen || aircraftSnapshot.CountMessagesReceivedChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.CountMessagesReceived = aircraftSnapshot.CountMessagesReceived;
                    }
                    if (firstTimeSeen || aircraftSnapshot.DestinationChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Destination = aircraftSnapshot.Destination;
                    }
                    if (firstTimeSeen || aircraftSnapshot.EnginePlacementChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.EnginePlacement = (int)aircraftSnapshot.EnginePlacement;
                    }
                    if (firstTimeSeen || aircraftSnapshot.EngineTypeChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.EngineType = (int)aircraftSnapshot.EngineType;
                    }
                    if (firstTimeSeen || aircraftSnapshot.FirstSeenChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.FirstSeen = aircraftSnapshot.FirstSeen;
                    }
                    if (firstTimeSeen || aircraftSnapshot.FlightsCountChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.FlightsCount = aircraftSnapshot.FlightsCount;
                    }
                    if (firstTimeSeen || aircraftSnapshot.Icao24CountryChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Icao24Country = aircraftSnapshot.Icao24Country;
                    }
                    if (firstTimeSeen || aircraftSnapshot.Icao24InvalidChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Icao24Invalid = aircraftSnapshot.Icao24Invalid;
                    }
                    if (firstTimeSeen || aircraftSnapshot.IsInterestingChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.IsInteresting = aircraftSnapshot.IsInteresting;
                    }
                    if (firstTimeSeen || aircraftSnapshot.IsMilitaryChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.IsMilitary = aircraftSnapshot.IsMilitary;
                    }
                    if (firstTimeSeen || aircraftSnapshot.ManufacturerChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Manufacturer = aircraftSnapshot.Manufacturer;
                    }
                    if (firstTimeSeen || aircraftSnapshot.ModelChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Model = aircraftSnapshot.Model;
                    }
                    if (firstTimeSeen || aircraftSnapshot.NumberOfEnginesChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.NumberOfEngines = aircraftSnapshot.NumberOfEngines;
                    }
                    if (firstTimeSeen || aircraftSnapshot.OperatorChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Operator = aircraftSnapshot.Operator;
                    }
                    if (firstTimeSeen || aircraftSnapshot.OperatorIcaoChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.OperatorIcao = aircraftSnapshot.OperatorIcao;
                    }
                    if (firstTimeSeen || aircraftSnapshot.OriginChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Origin = aircraftSnapshot.Origin;
                    }
                    if (firstTimeSeen || aircraftSnapshot.PictureFileNameChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.HasPicture = !String.IsNullOrEmpty(aircraftSnapshot.PictureFileName);
                    }
                    if (firstTimeSeen || aircraftSnapshot.PictureHeightChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.PictureHeight = aircraftSnapshot.PictureHeight == 0 ? (int?)null : aircraftSnapshot.PictureHeight;
                    }
                    if (firstTimeSeen || aircraftSnapshot.PictureWidthChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.PictureWidth = aircraftSnapshot.PictureWidth == 0 ? (int?)null : aircraftSnapshot.PictureWidth;
                    }
                    if (firstTimeSeen || aircraftSnapshot.PositionTimeChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.PositionTime = aircraftSnapshot.PositionTime == null ? (long?)null : JavascriptHelper.ToJavascriptTicks(aircraftSnapshot.PositionTime.Value);
                    }
                    if (firstTimeSeen || aircraftSnapshot.ReceiverIdChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.ReceiverId = aircraftSnapshot.ReceiverId;
                    }
                    if (firstTimeSeen || aircraftSnapshot.RegistrationChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Registration = aircraftSnapshot.Registration;
                    }
                    if (firstTimeSeen || aircraftSnapshot.SpeciesChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Species = (int)aircraftSnapshot.Species;
                    }
                    if (firstTimeSeen || aircraftSnapshot.TypeChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.Type = aircraftSnapshot.Type;
                    }
                    if (firstTimeSeen || aircraftSnapshot.UserTagChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.UserTag = aircraftSnapshot.UserTag;
                    }
                    if (firstTimeSeen || aircraftSnapshot.WakeTurbulenceCategoryChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.WakeTurbulenceCategory = (int)aircraftSnapshot.WakeTurbulenceCategory;
                    }
                    if (firstTimeSeen || aircraftSnapshot.YearBuiltChanged > args.PreviousDataVersion)
                    {
                        aircraftJson.YearBuilt = aircraftSnapshot.YearBuilt;
                    }

                    if (aircraftSnapshot.Stopovers.Count > 0 && (firstTimeSeen || aircraftSnapshot.StopoversChanged > args.PreviousDataVersion))
                    {
                        aircraftJson.Stopovers = new List <string>();
                        aircraftJson.Stopovers.AddRange(aircraftSnapshot.Stopovers);
                    }

                    aircraftJson.SecondsTracked  = (long)((now - aircraftSnapshot.FirstSeen).TotalSeconds);
                    aircraftJson.PositionIsStale = aircraftSnapshot.LastSatcomUpdate == DateTime.MinValue &&        // Never flag satcom aircraft as having a stale position
                                                   aircraftSnapshot.PositionTime != null &&
                                                   aircraftSnapshot.PositionTime < positionTimeoutThreshold ? true : (bool?)null;
                }

                if (args.TrailType != TrailType.None)
                {
                    var hasTrail     = false;
                    var isShort      = false;
                    var showAltitude = false;
                    var showSpeed    = false;
                    switch (args.TrailType)
                    {
                    case TrailType.Short:           isShort = true; hasTrail = aircraftSnapshot.ShortCoordinates.Count > 0; break;

                    case TrailType.ShortAltitude:   showAltitude = true; aircraftJson.TrailType = "a"; goto case TrailType.Short;

                    case TrailType.ShortSpeed:      showSpeed = true; aircraftJson.TrailType = "s"; goto case TrailType.Short;

                    case TrailType.Full:            hasTrail = aircraftSnapshot.FullCoordinates.Count > 0; break;

                    case TrailType.FullAltitude:    showAltitude = true; aircraftJson.TrailType = "a"; goto case TrailType.Full;

                    case TrailType.FullSpeed:       showSpeed = true; aircraftJson.TrailType = "s"; goto case TrailType.Full;
                    }
                    if (hasTrail)
                    {
                        BuildCoordinatesList(isShort, firstTimeSeen, aircraftJson, aircraftSnapshot, args, showAltitude, showSpeed);
                    }
                }

                aircraftListJson.Aircraft.Add(aircraftJson);
            }
        }
        /// <summary>
        /// Returns a fully-formed <see cref="AircraftListJson"/> from the aircraft list passed across.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public AircraftListJson Build(AircraftListJsonBuilderArgs args)
        {
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            RefreshConfigurationSettings();

            var feedId = args.SourceFeedId == -1 ? _DefaultAircraftListFeedId : args.SourceFeedId;

            IAircraftList aircraftList = null;

            if (args.IsFlightSimulatorList)
            {
                aircraftList = args.AircraftList;
            }
            else
            {
                var selectedFeed = _FeedManager.GetByUniqueId(feedId, ignoreInvisibleFeeds: true);
                if (selectedFeed == null)
                {
                    feedId       = _DefaultAircraftListFeedId;
                    selectedFeed = _FeedManager.GetByUniqueId(feedId, ignoreInvisibleFeeds: true);
                }
                aircraftList = selectedFeed == null ? null : selectedFeed.AircraftList;
            }
            if (aircraftList == null)
            {
                aircraftList = _EmptyAircraftList;
                feedId       = args.SourceFeedId;
            }

            var result = new AircraftListJson()
            {
                FlagHeight              = 20,
                FlagWidth               = 85,
                ShowFlags               = _ShowFlags && !args.IsFlightSimulatorList,
                ShowPictures            = _ShowPictures && (!args.IsInternetClient || _ShowPicturesToInternetClients) && !args.IsFlightSimulatorList,
                ShowSilhouettes         = _ShowSilhouettes && !args.IsFlightSimulatorList,
                ShortTrailLengthSeconds = _ShortTrailLength,
                Source       = (int)aircraftList.Source,
                SourceFeedId = feedId,
            };

            if (!args.FeedsNotRequired)
            {
                foreach (var feed in _FeedManager.VisibleFeeds)
                {
                    result.Feeds.Add(new FeedJson()
                    {
                        UniqueId     = feed.UniqueId,
                        Name         = feed.Name,
                        HasPolarPlot = feed.AircraftList != null && feed.AircraftList.PolarPlotter != null
                    });
                }
            }

            long timestamp, dataVersion;
            var  aircraft = aircraftList.TakeSnapshot(out timestamp, out dataVersion);

            result.AvailableAircraft = aircraft.Count;

            if (args.IgnoreUnchanged)
            {
                aircraft = aircraft.Where(r => r.DataVersion > args.PreviousDataVersion).ToList();
            }

            result.LastDataVersion = dataVersion.ToString();
            result.ServerTime      = JavascriptHelper.ToJavascriptTicks(timestamp);

            Dictionary <int, double?> distances = new Dictionary <int, double?>();

            aircraft = FilterAircraft(aircraft, args, distances);
            SortAircraft(aircraft, args, distances);
            CopyAircraft(result, aircraft, args, distances);

            return(result);
        }
Example #8
0
        /// <summary>
        /// Copies the aircraft from the snapshot to the JSON object.
        /// </summary>
        /// <param name="aircraftListJson"></param>
        /// <param name="aircraftListSnapshot"></param>
        /// <param name="args"></param>
        /// <param name="distances"></param>
        private void CopyAircraft(AircraftListJson aircraftListJson, List <IAircraft> aircraftListSnapshot, AircraftListJsonBuilderArgs args, Dictionary <int, double?> distances)
        {
            var now = _Provider.UtcNow;

            foreach (var aircraftSnapshot in aircraftListSnapshot)
            {
                double?distance;
                if (!distances.TryGetValue(aircraftSnapshot.UniqueId, out distance))
                {
                    distance = null;
                }

                var aircraftJson = new AircraftJson()
                {
                    BearingFromHere  = GreatCircleMaths.Bearing(args.BrowserLatitude, args.BrowserLongitude, aircraftSnapshot.Latitude, aircraftSnapshot.Longitude, null, false, true),
                    DistanceFromHere = distance == null ? (double?)null : Math.Round(distance.Value, 2),
                    UniqueId         = aircraftSnapshot.UniqueId,
                };
                if (aircraftJson.BearingFromHere != null)
                {
                    aircraftJson.BearingFromHere = Math.Round(aircraftJson.BearingFromHere.Value, 1);
                }

                bool firstTimeSeen = !args.PreviousAircraft.Contains(aircraftSnapshot.UniqueId);

                if (firstTimeSeen || aircraftSnapshot.AltitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Altitude = aircraftSnapshot.Altitude;
                }
                if (firstTimeSeen || aircraftSnapshot.CallsignChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Callsign = aircraftSnapshot.Callsign;
                }
                if (firstTimeSeen || aircraftSnapshot.CallsignIsSuspectChanged > args.PreviousDataVersion)
                {
                    aircraftJson.CallsignIsSuspect = aircraftSnapshot.CallsignIsSuspect;
                }
                if (firstTimeSeen || aircraftSnapshot.ConstructionNumberChanged > args.PreviousDataVersion)
                {
                    aircraftJson.ConstructionNumber = aircraftSnapshot.ConstructionNumber;
                }
                if (firstTimeSeen || aircraftSnapshot.CountMessagesReceivedChanged > args.PreviousDataVersion)
                {
                    aircraftJson.CountMessagesReceived = aircraftSnapshot.CountMessagesReceived;
                }
                if (firstTimeSeen || aircraftSnapshot.DestinationChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Destination = aircraftSnapshot.Destination;
                }
                if (firstTimeSeen || aircraftSnapshot.GroundSpeedChanged > args.PreviousDataVersion)
                {
                    aircraftJson.GroundSpeed = Round.GroundSpeed(aircraftSnapshot.GroundSpeed);
                }
                if (firstTimeSeen || aircraftSnapshot.EmergencyChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Emergency = aircraftSnapshot.Emergency;
                }
                if (firstTimeSeen || aircraftSnapshot.EngineTypeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.EngineType = (int)aircraftSnapshot.EngineType;
                }
                if (firstTimeSeen || aircraftSnapshot.FirstSeenChanged > args.PreviousDataVersion)
                {
                    aircraftJson.FirstSeen = aircraftSnapshot.FirstSeen;
                }
                if (firstTimeSeen || aircraftSnapshot.FlightsCountChanged > args.PreviousDataVersion)
                {
                    aircraftJson.FlightsCount = aircraftSnapshot.FlightsCount;
                }
                if (firstTimeSeen || aircraftSnapshot.PictureFileNameChanged > args.PreviousDataVersion)
                {
                    aircraftJson.HasPicture = !String.IsNullOrEmpty(aircraftSnapshot.PictureFileName);
                }
                if (firstTimeSeen || aircraftSnapshot.Icao24Changed > args.PreviousDataVersion)
                {
                    aircraftJson.Icao24 = aircraftSnapshot.Icao24;
                }
                if (firstTimeSeen || aircraftSnapshot.Icao24CountryChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Icao24Country = aircraftSnapshot.Icao24Country;
                }
                if (firstTimeSeen || aircraftSnapshot.Icao24InvalidChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Icao24Invalid = aircraftSnapshot.Icao24Invalid;
                }
                if (firstTimeSeen || aircraftSnapshot.IsMilitaryChanged > args.PreviousDataVersion)
                {
                    aircraftJson.IsMilitary = aircraftSnapshot.IsMilitary;
                }
                if (firstTimeSeen || aircraftSnapshot.IsInterestingChanged > args.PreviousDataVersion)
                {
                    aircraftJson.IsInteresting = aircraftSnapshot.IsInteresting;
                }
                if (firstTimeSeen || aircraftSnapshot.LatitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Latitude = Round.Coordinate(aircraftSnapshot.Latitude);
                }
                if (firstTimeSeen || aircraftSnapshot.LongitudeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Longitude = Round.Coordinate(aircraftSnapshot.Longitude);
                }
                if (firstTimeSeen || aircraftSnapshot.ModelChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Model = aircraftSnapshot.Model;
                }
                if (firstTimeSeen || aircraftSnapshot.NumberOfEnginesChanged > args.PreviousDataVersion)
                {
                    aircraftJson.NumberOfEngines = aircraftSnapshot.NumberOfEngines;
                }
                if (firstTimeSeen || aircraftSnapshot.OnGroundChanged > args.PreviousDataVersion)
                {
                    aircraftJson.OnGround = aircraftSnapshot.OnGround;
                }
                if (firstTimeSeen || aircraftSnapshot.OperatorChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Operator = aircraftSnapshot.Operator;
                }
                if (firstTimeSeen || aircraftSnapshot.OperatorIcaoChanged > args.PreviousDataVersion)
                {
                    aircraftJson.OperatorIcao = aircraftSnapshot.OperatorIcao;
                }
                if (firstTimeSeen || aircraftSnapshot.OriginChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Origin = aircraftSnapshot.Origin;
                }
                if (firstTimeSeen || aircraftSnapshot.PositionTimeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.PositionTime = aircraftSnapshot.PositionTime == null ? (long?)null : JavascriptHelper.ToJavascriptTicks(aircraftSnapshot.PositionTime.Value);
                }
                if (firstTimeSeen || aircraftSnapshot.RegistrationChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Registration = aircraftSnapshot.Registration;
                }
                if (firstTimeSeen || aircraftSnapshot.SpeciesChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Species = (int)aircraftSnapshot.Species;
                }
                if (firstTimeSeen || aircraftSnapshot.SpeedTypeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.SpeedType = (int)aircraftSnapshot.SpeedType;
                }
                if (firstTimeSeen || aircraftSnapshot.SquawkChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Squawk = String.Format("{0:0000}", aircraftSnapshot.Squawk);
                }
                if (firstTimeSeen || aircraftSnapshot.TrackChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Track = Round.Track(aircraftSnapshot.Track);
                }
                if (firstTimeSeen || aircraftSnapshot.TypeChanged > args.PreviousDataVersion)
                {
                    aircraftJson.Type = aircraftSnapshot.Type;
                }
                if (firstTimeSeen || aircraftSnapshot.UserTagChanged > args.PreviousDataVersion)
                {
                    aircraftJson.UserTag = aircraftSnapshot.UserTag;
                }
                if (firstTimeSeen || aircraftSnapshot.VerticalRateChanged > args.PreviousDataVersion)
                {
                    aircraftJson.VerticalRate = aircraftSnapshot.VerticalRate;
                }
                if (firstTimeSeen || aircraftSnapshot.WakeTurbulenceCategoryChanged > args.PreviousDataVersion)
                {
                    aircraftJson.WakeTurbulenceCategory = (int)aircraftSnapshot.WakeTurbulenceCategory;
                }

                if (aircraftSnapshot.Stopovers.Count > 0 && (firstTimeSeen || aircraftSnapshot.StopoversChanged > args.PreviousDataVersion))
                {
                    aircraftJson.Stopovers = new List <string>();
                    aircraftJson.Stopovers.AddRange(aircraftSnapshot.Stopovers);
                }

                aircraftJson.SecondsTracked = (long)((now - aircraftSnapshot.FirstSeen).TotalSeconds);

                if (args.ShowShortTrail)
                {
                    if (aircraftSnapshot.ShortCoordinates.Count > 0)
                    {
                        BuildCoordinatesList(true, firstTimeSeen, aircraftJson, aircraftSnapshot, args);
                    }
                }
                else
                {
                    if (aircraftSnapshot.FullCoordinates.Count > 0)
                    {
                        BuildCoordinatesList(false, firstTimeSeen, aircraftJson, aircraftSnapshot, args);
                    }
                }

                aircraftListJson.Aircraft.Add(aircraftJson);
            }
        }
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="aircraftListJson"></param>
        /// <returns></returns>
        public IEnumerable <BaseStationMessageEventArgs> ConvertIntoBaseStationMessageEventArgs(AircraftListJson aircraftListJson)
        {
            var result = new List <BaseStationMessageEventArgs>();

            foreach (var json in aircraftListJson.Aircraft)
            {
                var message = new BaseStationMessage()
                {
                    MessageType      = BaseStationMessageType.Transmission,
                    TransmissionType = BaseStationTransmissionType.AirToAir,        // This needs to be something other than "None" or "AllCallReply" if we want to have these messages work with the message compressor
                    StatusCode       = BaseStationStatusCode.None,
                };

                if (json.Callsign != null)
                {
                    message.Callsign = json.Callsign;
                }
                if (json.Emergency != null)
                {
                    message.Emergency = json.Emergency;
                }
                if (json.GroundSpeed != null)
                {
                    message.GroundSpeed = json.GroundSpeed;
                }
                if (json.Icao24 != null)
                {
                    message.Icao24 = json.Icao24;
                }
                if (json.OnGround != null)
                {
                    message.OnGround = json.OnGround;
                }
                if (json.SignalLevel != null)
                {
                    message.SignalLevel = json.SignalLevel;
                }
                if (json.Track != null)
                {
                    message.Track = json.Track;
                }
                if (json.VerticalRate != null)
                {
                    message.VerticalRate = json.VerticalRate;
                }

                if (json.Latitude != null || json.Longitude != null)
                {
                    if (json.Latitude != null)
                    {
                        message.Latitude = json.Latitude;
                    }
                    if (json.Longitude != null)
                    {
                        message.Longitude = json.Longitude;
                    }
                    if (json.PositionIsMlat != null)
                    {
                        message.IsMlat = json.PositionIsMlat.Value;
                    }
                }

                if (json.Squawk != null)
                {
                    int squawk;
                    if (int.TryParse(json.Squawk, out squawk))
                    {
                        message.Squawk = squawk;
                    }
                }

                if (json.AltitudeType != null)
                {
                    AddSupplementaryValue(message, r => r.AltitudeIsGeometric = json.AltitudeType == 1);
                }
                if (json.CallsignIsSuspect != null)
                {
                    AddSupplementaryValue(message, r => r.CallsignIsSuspect = json.CallsignIsSuspect);
                }
                if (json.SpeedType != null)
                {
                    AddSupplementaryValue(message, r => r.SpeedType = (SpeedType)json.SpeedType);
                }
                if (json.TargetAltitude != null)
                {
                    AddSupplementaryValue(message, r => r.TargetAltitude = json.TargetAltitude);
                }
                if (json.TargetTrack != null)
                {
                    AddSupplementaryValue(message, r => r.TargetHeading = json.TargetTrack);
                }
                if (json.TrackIsHeading != null)
                {
                    AddSupplementaryValue(message, r => r.TrackIsHeading = json.TrackIsHeading);
                }
                if (json.TransponderType != null)
                {
                    AddSupplementaryValue(message, r => r.TransponderType = (TransponderType)json.TransponderType);
                }
                if (json.VerticalRateType != null)
                {
                    AddSupplementaryValue(message, r => r.VerticalRateIsGeometric = json.VerticalRateType == 1);
                }
                if (json.AirPressureInHg != null)
                {
                    AddSupplementaryValue(message, r => r.PressureSettingMb = AirPressure.InHgToMillibars(json.AirPressureInHg));
                }

                if (json.Altitude != null)
                {
                    // When pressure altitude calculations were introduced the sending side was modified to
                    // send Altitude, GeometricAltitude and AltitudeType as a set. However, before that was
                    // introduced they'd only be sent if they changed.
                    if (json.GeometricAltitude == null || json.AltitudeType == null)
                    {
                        message.Altitude = json.Altitude;
                    }
                    else
                    {
                        switch (json.AltitudeType)
                        {
                        case (int)AltitudeType.Barometric:
                            message.Altitude = json.Altitude;
                            break;

                        case (int)AltitudeType.Geometric:
                            message.Altitude = json.GeometricAltitude;
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                    }
                }

                var eventArgs = new BaseStationMessageEventArgs(message, isOutOfBand: false, isSatcomFeed: json.IsSatcomFeed);
                result.Add(eventArgs);
            }

            return(result);
        }