Example #1
0
        /// <summary>
        /// Creates database records and updates internal objects to track an aircraft that is currently transmitting messages.
        /// </summary>
        /// <param name="message"></param>
        private void TrackFlight(BaseStationMessage message)
        {
            if (IsTransmissionMessage(message))
            {
                lock (_SyncLock) {
                    if (_Session != null)
                    {
                        var localNow = Provider.LocalNow;

                        FlightRecords flightRecords;
                        if (!_FlightMap.TryGetValue(message.Icao24, out flightRecords))
                        {
                            flightRecords = new FlightRecords();
                            _Database.StartTransaction();
                            try {
                                flightRecords.Aircraft   = FetchOrCreateAircraft(localNow, message.Icao24);
                                flightRecords.Flight     = CreateFlight(localNow, flightRecords.Aircraft.AircraftID, message.Callsign);
                                flightRecords.EndTimeUtc = Provider.UtcNow;
                                _Database.EndTransaction();
                            } catch (ThreadAbortException) {
                            } catch (Exception ex) {
                                Debug.WriteLine(String.Format("BaseStationDatabaseWriter.Plugin.TrackFlight caught exception {0}", ex.ToString()));
                                _Database.RollbackTransaction();
                                throw;
                            }
                            _FlightMap.Add(message.Icao24, flightRecords);
                        }
                        else
                        {
                            if (flightRecords.Flight.Callsign.Length == 0 && !String.IsNullOrEmpty(message.Callsign))
                            {
                                var databaseVersion = _Database.GetFlightById(flightRecords.Flight.FlightID);
                                flightRecords.Flight.Callsign = databaseVersion.Callsign = message.Callsign;
                                _Database.UpdateFlight(databaseVersion);
                            }
                        }

                        var flight = flightRecords.Flight;
                        flightRecords.EndTimeUtc = Provider.UtcNow;
                        flight.EndTime           = localNow;
                        if (message.SquawkHasChanged.GetValueOrDefault())
                        {
                            flight.HadAlert = true;
                        }
                        if (message.IdentActive.GetValueOrDefault())
                        {
                            flight.HadSpi = true;
                        }
                        if (message.Squawk == 7500 || message.Squawk == 7600 || message.Squawk == 7700)
                        {
                            flight.HadEmergency = true;
                        }
                        UpdateFirstLastValues(message, flight, flightRecords);
                        UpdateMessageCounters(message, flight);
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Updates the flights that the plugin is tracking to show information about the beginning and end of the flight.
        /// </summary>
        /// <param name="flushAll"></param>
        private void FlushFlights(bool flushAll)
        {
            var utcNow = Provider.UtcNow;

            KeyValuePair <int, FlightRecords>[] flushEntries = null;
            lock (_SyncLock) {
                flushEntries = (flushAll ? _FlightMap : _FlightMap.Where(kvp => kvp.Value.EndTimeUtc.AddMinutes(25) <= utcNow)).ToArray();
            }

            _Database.StartTransaction();
            try {
                foreach (var kvp in flushEntries)
                {
                    _Database.UpdateFlight(kvp.Value.Flight);
                }
                _Database.EndTransaction();

                lock (_SyncLock) {
                    foreach (var kvp in flushEntries)
                    {
                        _FlightMap.Remove(kvp.Key);
                    }
                }
            } catch {
                _Database.RollbackTransaction();
                throw;
            }
        }