private async void simconnect_OnRecvSystemState(SimConnect sender, SIMCONNECT_RECV_SYSTEM_STATE data) { switch (data.dwRequestID) { case (int)DATA_REQUESTS.FLIGHT_PLAN: if (!string.IsNullOrEmpty(data.szString)) { logger.LogInformation($"Receive flight plan {data.szString}"); var planName = data.szString; var parser = new FlightPlanParser(); var plan = await parser.ParseAsync(planName); var flightPlan = new FlightPlan { Title = plan.FlightPlanFlightPlan.Title, Departure = new Point { Id = plan.FlightPlanFlightPlan.DepartureID, Name = plan.FlightPlanFlightPlan.DepartureName, Position = plan.FlightPlanFlightPlan.DeparturePosition, }, Destination = new Point { Id = plan.FlightPlanFlightPlan.DestinationID, Name = plan.FlightPlanFlightPlan.DestinationName, }, CruisingAltitude = plan.FlightPlanFlightPlan.CruisingAlt }; (flightPlan.Departure.Latitude, flightPlan.Departure.Longitude) = GpsHelper.ConvertString(plan.FlightPlanFlightPlan.DepartureLLA); (flightPlan.Destination.Latitude, flightPlan.Destination.Longitude) = GpsHelper.ConvertString(plan.FlightPlanFlightPlan.DestinationLLA); if (plan.FlightPlanFlightPlan.ATCWaypoint != null) { flightPlan.Waypoints = new System.Collections.Generic.List <Waypoint>(); foreach (var waypoint in plan.FlightPlanFlightPlan.ATCWaypoint) { var w = new Waypoint { Id = waypoint.id, Type = waypoint.ATCWaypointType, Airway = waypoint.ATCAirway, }; (w.Latitude, w.Longitude) = GpsHelper.ConvertString(waypoint.WorldPosition); flightPlan.Waypoints.Add(w); } } FlightPlanUpdated?.Invoke(this, new FlightPlanUpdatedEventArgs(flightPlan)); } break; } }
private string GetNearestAirport(FlightStatusUpdatedEventArgs e) { string nearestIcao = null; var minDist = double.MaxValue; foreach (var airport in airports.ToList()) { var dist = GpsHelper.CalculateDistance( e.FlightStatus.Latitude, e.FlightStatus.Longitude, e.FlightStatus.Altitude, airport.Latitude, airport.Longitude, airport.Altitude); if (dist < minDist) { nearestIcao = airport.Icao; minDist = dist; } } return(nearestIcao); }