Example #1
0
        public SsmlOutputSpeech HandleNearbyAircraftIntent(IntentRequest intentRequest)
        {
            // build the speech response
            var speech = new Alexa.NET.Response.SsmlOutputSpeech();

            Slot distanceSlot      = null;
            Slot distanceUnitsSlot = null;

            intentRequest.Intent.Slots?.TryGetValue("distance", out distanceSlot);
            intentRequest.Intent.Slots?.TryGetValue("distanceUnits", out distanceUnitsSlot);

            double distanceValue = distanceSlot != null?Convert.ToDouble(distanceSlot.Value) : 20;

            string distanceUnitsValue = distanceUnitsSlot != null ? distanceUnitsSlot.Value : "kilometres";

            var distance = Distance.FromKilometres(distanceValue);

            var location = new GeoLocation(52.041808, 1.208131);
            var north    = GeoLocation.FindPointAtDistanceFrom(location, Angle.FromDegrees(0), distance);
            var south    = GeoLocation.FindPointAtDistanceFrom(location, Angle.FromDegrees(180), distance);
            var east     = GeoLocation.FindPointAtDistanceFrom(location, Angle.FromDegrees(90), distance);
            var west     = GeoLocation.FindPointAtDistanceFrom(location, Angle.FromDegrees(270), distance);
            var flights  = FlightRadar24.GetFlights(north.Latitude, south.Latitude, east.Longitude, west.Longitude);

            if (flights == null)
            {
                speech.Ssml = "I'm unable to contact Flight Radar.";
                return(speech);
            }

            var withinRangeOrderedByClosest = (from sighting in flights.Sightings
                                               let d = GeoLocation.DistanceBetween(sighting.Location, location)
                                                       where d.Kilometres < distance.Kilometres
                                                       orderby d.Kilometres descending
                                                       select new { sighting, d }).ToArray();



            if (withinRangeOrderedByClosest.Length == 0)
            {
                speech.Ssml = "There are no flights within {distanceValue} {distanceUnitsValue}.";
                return(speech);
            }

            var closest = withinRangeOrderedByClosest.First();
            var bearing = GeoLocation.RhumbBearing(location, closest.sighting.Location).Degrees;

            var id = AddIndefiniteArticle(closest.sighting.AircraftType) + (closest.sighting.FlightNumber != null
                ? $", flight {closest.sighting.FlightNumber}"
                : "");
            var destination = closest.sighting.Arrving != null ? $"to {closest.sighting.Arrving}" : "";
            var callsign    = closest.sighting.CallSign != null? $"{closest.sighting.CallSign}":"";

            speech.Ssml = $"<speak>There {GetIsOrAre(withinRangeOrderedByClosest.Length)} {withinRangeOrderedByClosest.Length} aircraft within {distanceValue:N0} {distanceUnitsValue}. The closest is {callsign} {destination}, range {closest.d.Kilometres:N0} kilometres, bearing {bearing:N0}, travelling at {closest.sighting.GroundSpeedKts} knots and altitude {Distance.FromFeet(closest.sighting.AltitudeFt).Metres:N0} metres.</speak>";
            return(speech);
        }