/// <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);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Looks for flight records where the records haven't been created yet and creates them.
        /// </summary>
        private void WriteNewFlights()
        {
            FlightRecords[] newFlights = null;
            lock (_SyncLock) {
                newFlights = _FlightMap.Where(r => r.Value.Aircraft.AircraftID == 0 && r.Value.Flight.FlightID == 0).Select(r => r.Value).ToArray();
            }

            _Database.StartTransaction();
            try {
                foreach (var flight in newFlights)
                {
                    WriteFlightRecords(flight);
                }
                _Database.EndTransaction();
            } catch {
                _Database.RollbackTransaction();
                throw;
            }
        }