private void galnetAlwaysOnUnchecked(object sender, RoutedEventArgs e)
        {
            GalnetConfiguration configuration = GalnetConfiguration.FromFile();

            configuration.galnetAlwaysOn = galnetAlwaysOn.IsChecked.Value;
            configuration.ToFile();
            galnetMonitor()?.Reload();
        }
        private void onLanguageChanged(object sender, SelectionChangedEventArgs e)
        {
            string language = (string)((ComboBox)e.Source).SelectedValue;
            GalnetConfiguration configuration = GalnetConfiguration.FromFile();

            if (language != null && language != configuration.language)
            {
                // If the language changes we clear out the old articles
                GalnetSqLiteRepository.Instance.DeleteNews();
                configuration.lastuuid = null;
                configuration.language = language;
                configuration.ToFile();
                galnetMonitor()?.Reload();
            }
        }
Example #3
0
        private void monitor()
        {
            const int inGameOnlyStartDelayMilliSecs = 5 * 60 * 1000; // 5 mins
            const int alwaysOnIntervalMilliSecs     = 2 * 60 * 1000; // 2 mins
            const int inGameOnlyIntervalMilliSecs   = 30 * 1000;     // 30 secs

            if (!configuration.galnetAlwaysOn)
            {
                // Wait at least 5 minutes after starting before polling for new articles, but only if galnetAlwaysOn is false
                Thread.Sleep(inGameOnlyStartDelayMilliSecs);
            }

            while (running)
            {
                if (configuration.galnetAlwaysOn)
                {
                    monitorGalnet();
                    Thread.Sleep(alwaysOnIntervalMilliSecs);
                }
                else
                {
                    // We'll update the Galnet Monitor only if a journal event has taken place within the specified number of minutes
                    if ((DateTime.UtcNow - EDDI.Instance.JournalTimeStamp).TotalMinutes < 10)
                    {
                        monitorGalnet();
                    }
                    else
                    {
                        Logging.Debug("No in-game activity detected, skipping galnet feed update");
                    }
                    Thread.Sleep(inGameOnlyIntervalMilliSecs);
                }

                void monitorGalnet()
                {
                    List <News> newsItems = new List <News>();
                    string      firstUid  = null;

                    try
                    {
                        locales.TryGetValue(configuration.language, out locale);
                        string url = GetGalnetResource("sourceURL");

                        Logging.Debug("Fetching Galnet articles from " + url);
                        IEnumerable <FeedItem> items = new FeedReader(new GalnetFeedItemNormalizer(), true).RetrieveFeed(url);
                        if (items != null)
                        {
                            foreach (GalnetFeedItemNormalizer.ExtendedFeedItem item in items)
                            {
                                if (firstUid == null)
                                {
                                    // Obtain the ID of the first item that we read as a marker
                                    firstUid = item.Id;
                                }

                                if (item.Id == configuration.lastuuid)
                                {
                                    // Reached the first item we have already seen - go no further
                                    break;
                                }

                                News newsItem = new News(item.Id, assignCategory(item.Title, item.GetContent()), item.Title, item.GetContent(), item.PublishDate.DateTime, false);
                                newsItems.Add(newsItem);
                                GalnetSqLiteRepository.Instance.SaveNews(newsItem);
                            }
                        }
                    }
                    catch (WebException wex)
                    {
                        Logging.Debug("Exception attempting to obtain galnet feed: ", wex);
                    }

                    if (firstUid != configuration.lastuuid)
                    {
                        Logging.Debug("Updated latest UID to " + firstUid);
                        configuration.lastuuid = firstUid;
                        configuration.ToFile();
                    }

                    if (newsItems.Count > 0)
                    {
                        // Spin out event in to a different thread to stop blocking
                        Thread thread = new Thread(() =>
                        {
                            try
                            {
                                EDDI.Instance.eventHandler(new GalnetNewsPublishedEvent(DateTime.UtcNow, newsItems));
                            }
                            catch (ThreadAbortException)
                            {
                                Logging.Debug("Thread aborted");
                            }
                        })
                        {
                            IsBackground = true
                        };
                        thread.Start();
                    }
                }
            }
        }
Example #4
0
        private void monitor()
        {
            const int inGameOnlyStartDelayMilliSecs = 5 * 60 * 1000; // 5 mins
            const int alwaysOnIntervalMilliSecs     = 2 * 60 * 1000; // 2 mins
            const int inGameOnlyIntervalMilliSecs   = 30 * 1000;     // 30 secs

            if (!configuration.galnetAlwaysOn)
            {
                // Wait at least 5 minutes after starting before polling for new articles, but only if galnetAlwaysOn is false
                Thread.Sleep(inGameOnlyStartDelayMilliSecs);
            }

            while (running)
            {
                if (configuration.galnetAlwaysOn)
                {
                    monitorGalnet();
                    Thread.Sleep(alwaysOnIntervalMilliSecs);
                }
                else
                {
                    // We'll update the Galnet Monitor only if a journal event has taken place within the specified number of minutes
                    if ((DateTime.UtcNow - EDDI.Instance.JournalTimeStamp).TotalMinutes < 10)
                    {
                        monitorGalnet();
                    }
                    else
                    {
                        Logging.Debug("No in-game activity detected, skipping galnet feed update");
                    }
                    Thread.Sleep(inGameOnlyIntervalMilliSecs);
                }

                void monitorGalnet()
                {
                    List <News> newsItems = new List <News>();
                    string      firstUid  = null;

                    locales.TryGetValue(configuration.language, out locale);
                    string url = GetGalnetResource("sourceURL");

                    altURL = false;

                    Logging.Debug("Fetching Galnet articles from " + url);
                    FeedReader             feedReader = new FeedReader(new GalnetFeedItemNormalizer(), true);
                    IEnumerable <FeedItem> items      = null;

                    try
                    {
                        items = feedReader.RetrieveFeed(url);
                    }
                    catch (WebException wex)
                    {
                        // FDev has in the past made available an alternate Galnet feed. We'll try the alternate feed.
                        // If the alternate feed fails, the page may not currently be available without an FDev login.
                        Logging.Warn("Exception contacting primary Galnet feed: ", wex);
                        url    = GetGalnetResource("alternateURL");
                        altURL = true;
                        Logging.Warn("Trying alternate Galnet feed (may not work)" + url);
                        try
                        {
                            items = feedReader.RetrieveFeed(url);
                        }
                        catch (Exception ex)
                        {
                            Logging.Warn("Galnet feed exception (alternate url unsuccessful): ", ex);
                        }
                    }
                    catch (System.Xml.XmlException xex)
                    {
                        Logging.Error("Exception attempting to obtain galnet feed: ", xex);
                    }

                    if (items != null)
                    {
                        try
                        {
                            foreach (GalnetFeedItemNormalizer.ExtendedFeedItem item in items)
                            {
                                try
                                {
                                    if (firstUid == null)
                                    {
                                        // Obtain the ID of the first item that we read as a marker
                                        firstUid = item.Id;
                                    }

                                    if (item.Id == configuration.lastuuid)
                                    {
                                        // Reached the first item we have already seen - go no further
                                        break;
                                    }

                                    if (item.Title is null || item.GetContent() is null)
                                    {
                                        // Skip items which do not contain useful content.
                                        continue;
                                    }

                                    News newsItem = new News(item.Id, assignCategory(item.Title, item.GetContent()), item.Title, item.GetContent(), item.PublishDate.DateTime, false);
                                    newsItems.Add(newsItem);
                                    GalnetSqLiteRepository.Instance.SaveNews(newsItem);
                                }
                                catch (Exception ex)
                                {
                                    Dictionary <string, object> data = new Dictionary <string, object>()
                                    {
                                        { "item", item },
                                        { "exception", ex }
                                    };
                                    Logging.Error("Exception handling Galnet news item.", data);
                                }
                            }

                            if (firstUid != null && firstUid != configuration.lastuuid)
                            {
                                Logging.Debug("Updated latest UID to " + firstUid);
                                configuration.lastuuid = firstUid;
                                configuration.ToFile();
                            }

                            if (newsItems.Count > 0)
                            {
                                // Spin out event in to a different thread to stop blocking
                                Thread thread = new Thread(() =>
                                {
                                    try
                                    {
                                        EDDI.Instance.enqueueEvent(new GalnetNewsPublishedEvent(DateTime.UtcNow, newsItems));
                                    }
                                    catch (ThreadAbortException)
                                    {
                                        Logging.Debug("Thread aborted");
                                    }
                                })
                                {
                                    IsBackground = true
                                };
                                thread.Start();
                            }
                        }
                        catch (Exception ex)
                        {
                            Logging.Error("Exception attempting to handle galnet feed: ", ex);
                        }
                    }
                }
            }
        }
Example #5
0
        private void monitor()
        {
            while (running)
            {
                List <News> newsItems = new List <News>();
                string      firstUid  = null;
                try
                {
                    string locale = "en";
                    locales.TryGetValue(configuration.language, out locale);
                    string url = SOURCE + locale + RESOURCE;
                    Logging.Debug("Fetching Galnet articles from " + url);
                    IEnumerable <FeedItem> items = new FeedReader(new GalnetFeedItemNormalizer(), true).RetrieveFeed(url);
                    if (items != null)
                    {
                        foreach (FeedItem item in items)
                        {
                            if (firstUid == null)
                            {
                                // Obtain the ID of the first item that we read as a marker
                                firstUid = item.Id;
                            }

                            if (item.Id == configuration.lastuuid)
                            {
                                // Reached the first item we have already seen - go no further
                                break;
                            }

                            if (isInteresting(item.Title))
                            {
                                News newsItem = new News(item.Id, categoryFromTitle(item.Title), item.Title, item.GetContent(), item.PublishDate.DateTime, false);
                                newsItems.Add(newsItem);
                                GalnetSqLiteRepository.Instance.SaveNews(newsItem);
                            }
                        }
                    }
                }
                catch (WebException wex)
                {
                    Logging.Debug("Exception attempting to obtain galnet feed: ", wex);
                }

                if (firstUid != configuration.lastuuid)
                {
                    Logging.Debug("Updated latest UID to " + firstUid);
                    configuration.lastuuid = firstUid;
                    configuration.ToFile();
                }

                if (newsItems.Count > 0)
                {
                    // Spin out event in to a different thread to stop blocking
                    Thread thread = new Thread(() =>
                    {
                        try
                        {
                            EDDI.Instance.eventHandler(new GalnetNewsPublishedEvent(DateTime.Now, newsItems));
                        }
                        catch (ThreadAbortException)
                        {
                            Logging.Debug("Thread aborted");
                        }
                    });
                    thread.IsBackground = true;
                    thread.Start();
                }

                Thread.Sleep(120000);
            }
        }