Exemplo n.º 1
0
        /// <summary>
        /// Creates a new session.
        /// </summary>
        private void StartSession()
        {
            var feedManager = Factory.ResolveSingleton <IFeedManager>();

            lock (_SyncLock) {
                if (_Session == null)
                {
                    if (!_Options.Enabled)
                    {
                        Status            = PluginStrings.Disabled;
                        StatusDescription = null;
                    }
                    else if (_Options.ReceiverId == 0)
                    {
                        Status = PluginStrings.EnabledNoReceiver;
                    }
                    else if (feedManager.GetByUniqueId(_Options.ReceiverId, ignoreInvisibleFeeds: false) == null)
                    {
                        Status = PluginStrings.EnabledBadReceiver;
                    }
                    else if (String.IsNullOrEmpty(_Database.FileName))
                    {
                        Status            = PluginStrings.EnabledNoDatabase;
                        StatusDescription = null;
                    }
                    else if (!_Database.FileExists())
                    {
                        Status            = PluginStrings.EnabledNotUpdating;
                        StatusDescription = String.Format(PluginStrings.SomethingDoesNotExist, _Database.FileName);
                    }
                    else if (_Database.FileIsEmpty())
                    {
                        Status            = PluginStrings.EnabledNotUpdating;
                        StatusDescription = String.Format(PluginStrings.SomethingIsZeroLength, _Database.FileName);
                    }
                    else if (!_Options.AllowUpdateOfOtherDatabases && !DatabaseCreatedByPlugin())
                    {
                        Status            = PluginStrings.EnabledNotUpdating;
                        StatusDescription = PluginStrings.UpdatingDatabasesNotCreatedByPluginForbidden;
                    }
                    else
                    {
                        Status            = String.Format(PluginStrings.EnabledAndUpdatingSomething, _Database.FileName);
                        StatusDescription = null;

                        try {
                            _Database.WriteSupportEnabled = true;

                            var location = _Database.GetLocations().OrderBy(c => c.LocationID).FirstOrDefault();

                            _Session = new BaseStationSession()
                            {
                                LocationID = location == null ? 0 : location.LocationID,
                                StartTime  = Provider.LocalNow,
                            };
                            _Database.InsertSession(_Session);

                            _OnlineLookupCache.Enabled = _Session != null && _Options.SaveDownloadedAircraftDetails;
                        } catch (ThreadAbortException) {
                        } catch (Exception ex) {
                            AbandonSession(ex, PluginStrings.ExceptionCaughtWhenStartingSession);
                            Debug.WriteLine(String.Format("BaseStationDatabaseWriter.Plugin.StartSession caught exception {0}", ex.ToString()));
                            Factory.ResolveSingleton <ILog>().WriteLine("Database writer plugin caught exception on starting session: {0}", ex.ToString());
                        }
                    }
                }
            }
        }