public void ProximityGadgetAircraftJson_Constructor_Initialises_To_Known_State_And_Properties_Work()
        {
            var json = new ProximityGadgetAircraftJson();

            TestUtilities.TestProperty(json, "ClosestAircraft", null, new ProximityGadgetClosestAircraftJson());
            TestUtilities.TestProperty(json, "WarningMessage", null, "Abc");
            Assert.AreEqual(0, json.EmergencyAircraft.Count);
        }
        /// <summary>
        /// See base class.
        /// </summary>
        /// <param name="server"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected override bool DoHandleRequest(Interface.WebServer.IWebServer server, Interface.WebServer.RequestReceivedEventArgs args)
        {
            bool result = false;

            if((!args.IsInternetRequest || _InternetClientCanRequestClosestAircraft) && args.PathAndFile.Equals("/ClosestAircraft.json", StringComparison.OrdinalIgnoreCase)) {
                result = true;

                var latitude = QueryNDouble(args, "lat");
                var longitude = QueryNDouble(args, "lng");

                var json = new ProximityGadgetAircraftJson();

                if(latitude == null || longitude == null) json.WarningMessage = "Position not supplied";
                else {
                    long timeStamp, dataVersion;
                    var aircraftList = BaseStationAircraftList.TakeSnapshot(out timeStamp, out dataVersion);

                    IAircraft closestAircraft = null;
                    double? closestDistance = null;

                    foreach(var aircraft in aircraftList) {
                        double? distance = null;
                        if(aircraft.Latitude != null && aircraft.Longitude != null) {
                            distance = GreatCircleMaths.Distance(latitude, longitude, aircraft.Latitude, aircraft.Longitude);
                            if(distance != null && closestAircraft == null || distance < closestDistance) {
                                closestAircraft = aircraft;
                                closestDistance = distance;
                            }
                        }

                        if(aircraft.Emergency == true) json.EmergencyAircraft.Add(CreateProximityGadgetClosestAircraftJson(aircraft, distance, latitude, longitude));
                    }

                    if(closestAircraft != null) {
                        var closestJsonAircraft = CreateProximityGadgetClosestAircraftJson(closestAircraft, closestDistance, latitude, longitude);
                        json.ClosestAircraft = closestJsonAircraft;
                    }
                }

                Responder.SendJson(args.Response, json, null);
                args.Classification = ContentClassification.Json;
            }

            return result;
        }
Example #3
0
        /// <summary>
        /// Returns a model given an aircraft list and a point to measure from.
        /// </summary>
        /// <param name="aircraftList"></param>
        /// <param name="originLatitude"></param>
        /// <param name="originLongitude"></param>
        /// <returns></returns>
        public static ProximityGadgetAircraftJson ToModel(IEnumerable <IAircraft> aircraftList, double?originLatitude, double?originLongitude)
        {
            ProximityGadgetAircraftJson result = null;

            if (aircraftList != null)
            {
                result = new ProximityGadgetAircraftJson();

                if (originLatitude == null || originLongitude == null)
                {
                    result.WarningMessage = "Position not supplied";
                }
                else
                {
                    IAircraft closestAircraft = null;
                    double?   closestDistance = null;

                    foreach (var aircraft in aircraftList)
                    {
                        double?distance = null;
                        if (aircraft.Latitude != null && aircraft.Longitude != null)
                        {
                            distance = GreatCircleMaths.Distance(originLatitude, originLongitude, aircraft.Latitude, aircraft.Longitude);
                            if (distance != null && closestAircraft == null || distance < closestDistance)
                            {
                                closestAircraft = aircraft;
                                closestDistance = distance;
                            }
                        }

                        if (aircraft.Emergency == true)
                        {
                            result.EmergencyAircraft.Add(ProximityGadgetClosestAircraftJson.ToModel(aircraft, originLatitude, originLongitude));
                        }
                    }

                    if (closestAircraft != null)
                    {
                        result.ClosestAircraft = ProximityGadgetClosestAircraftJson.ToModel(closestAircraft, originLatitude, originLongitude);
                    }
                }
            }

            return(result);
        }