private async void FlightSimInterface_FlightStatusUpdated(object sender, FlightStatusUpdatedEventArgs e) { if (lastStatus != null) { // Try to reduce the number of status if (e.FlightStatus.IsOnGround && e.FlightStatus.GroundSpeed < 1f && lastStatus.IsOnGround && lastStatus.GroundSpeed < 1f) { return; } if (e.FlightStatus.AltitudeAboveGround > 1000 && e.FlightStatus.SimTime - lastStatus.SimTime < 1f) { return; } if (e.FlightStatus.AltitudeAboveGround > 5000 && e.FlightStatus.SimTime - lastStatus.SimTime < 2f) { return; } if (e.FlightStatus.AltitudeAboveGround > 10000 && e.FlightStatus.SimTime - lastStatus.SimTime < 3f) { return; } if (e.FlightStatus.AltitudeAboveGround > 15000 && e.FlightStatus.SimTime - lastStatus.SimTime < 4f) { return; } if (e.FlightStatus.AltitudeAboveGround > 20000 && e.FlightStatus.SimTime - lastStatus.SimTime < 5f) { return; } } try { if (connection.State == HubConnectionState.Connected) { await connection.InvokeAsync("Update", e.FlightStatus); } lastStatus = e.FlightStatus; } catch (Exception ex) { logger.LogError(ex, "Cannot send to SignalR!"); } }
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); }
private async void FlightSimInterface_FlightStatusUpdated(object sender, FlightStatusUpdatedEventArgs e) { try { // Augment environment time e.FlightStatus.LocalTime = localTime; e.FlightStatus.ZuluTime = zuluTime; e.FlightStatus.AbsoluteTime = absoluteTime; if (screenshotsQueue.TryDequeue(out var screenshot)) { e.FlightStatus.ScreenshotUrl = screenshot; } var lastStatus = FlightRoute.LastOrDefault(); if (lastStatus != null && lastStatus.IsOnGround && !e.FlightStatus.IsOnGround && FlightData.State == FlightState.Started) { // Took off logger.LogInformation("Aircraft has taken off"); FlightData.StatusTakeOff = e.FlightStatus; FlightData.State = FlightState.Enroute; FlightData.TakeOffDateTime = DateTimeOffset.Now; // Try to determine airport if (airports != null && FlightData.AirportFrom == null) { FlightData.AirportFrom = GetNearestAirport(e); } HandleFlightDataUpdated(); await AddOrUpdateFlightAsync(); } else if (lastStatus != null && !lastStatus.IsOnGround && e.FlightStatus.IsOnGround && FlightData.StatusLanding == null) { // Landing if (FlightData.State == FlightState.Enroute) { logger.LogInformation("Aircraft has landed"); FlightData.StatusLanding = lastStatus; FlightData.State = FlightState.Arrived; if (airports != null && FlightData.AirportTo == null) { FlightData.AirportTo = GetNearestAirport(e); } HandleFlightDataUpdated(); } await AddOrUpdateFlightAsync(); } else { if (!e.FlightStatus.IsOnGround && FlightData.State == FlightState.Started && e.FlightStatus.GroundSpeed > 5) { // Flight start when the airplane is already flying FlightData.State = FlightState.Enroute; HandleFlightDataUpdated(); } // Try to reduce the number of status by skipping status recording if (!forceStatusAdd && string.IsNullOrEmpty(screenshot)) { if (lastStatus == null && e.FlightStatus.IsOnGround && e.FlightStatus.GroundSpeed < 1f) { return; } else if (lastStatus != null && lastStatus.ScreenshotUrl == null) { if (e.FlightStatus.AltitudeAboveGround > 1000 && e.FlightStatus.SimTime - lastStatus.SimTime < 1f) { return; } if (e.FlightStatus.AltitudeAboveGround > 5000 && e.FlightStatus.SimTime - lastStatus.SimTime < 2f) { return; } if (e.FlightStatus.AltitudeAboveGround > 10000 && e.FlightStatus.SimTime - lastStatus.SimTime < 3f) { return; } if (e.FlightStatus.AltitudeAboveGround > 15000 && e.FlightStatus.SimTime - lastStatus.SimTime < 4f) { return; } if (e.FlightStatus.AltitudeAboveGround > 20000 && e.FlightStatus.SimTime - lastStatus.SimTime < 5f) { return; } } } } FlightRoute.Add(e.FlightStatus); forceStatusAdd = false; if ((FlightData.State == FlightState.Enroute || FlightData.State == FlightState.Arrived) && (!lastSaveAttempt.HasValue || DateTime.Now - lastSaveAttempt.Value > TimeSpan.FromMilliseconds(SaveDelay))) { // Throttle save attempt await AddOrUpdateFlightAsync(); } } catch (Exception ex) { logger.LogError(ex, "Cannot process flight status update: {0}", ex.Message); } }