コード例 #1
0
        public async Task <(List <PowerStanding> standings, string cycle, DateTime updated)> GetData(CancellationTokenSource cancelToken, bool ignoreCache = false)
        {
            TimeSpan expiry = TimeSpan.FromMinutes(5);

            if ((powerStandings?.Any() == false) || (cycle != CycleService.CurrentCycle() && (lastUpdated + expiry < DateTime.Now)))
            {
                // download the standings
                string          csvText;
                DownloadService downloadService = DownloadService.Instance();
                (csvText, lastUpdated) = await downloadService.GetData(URL, dataKey, lastUpdatedKey, expiry, cancelToken, ignoreCache).ConfigureAwait(false);

                // parse the standings
                powerStandings = ParseStandings(csvText);
                if (!string.IsNullOrWhiteSpace(powerStandings[0].Cycle))
                {
                    int p = powerStandings[0].Cycle.IndexOf(" ") + 1;
                    Int32.TryParse(powerStandings[0].Cycle.Substring(p, powerStandings[0].Cycle.Length - p), out cycle);
                }
                else
                {
                    cycle = 0;
                }

                // cache till next cycle if updated
                if (cycle == CycleService.CurrentCycle())
                {
                    expiry = CycleService.TimeTillTick();
                    Barrel.Current.Add(dataKey, csvText, expiry);
                    Barrel.Current.Add(lastUpdatedKey, lastUpdated.ToString(), expiry);
                }
            }
            return(powerStandings, powerStandings[0].Cycle, lastUpdated);
コード例 #2
0
        public async Task <(string data, DateTime updated)> GetData(CancellationTokenSource cancelToken, bool ignoreCache = false)
        {
            DateTime lastUpdated;
            TimeSpan expiry = TimeSpan.FromHours(settings.NewsCacheTime);

            // download the json feed
            DownloadService downloadService = DownloadService.Instance();
            string          json;

            (json, lastUpdated) = await downloadService.GetData(GalNetURL, dataKey, lastUpdatedKey, expiry, cancelToken, ignoreCache).ConfigureAwait(false);

            return(json, lastUpdated);
        }
コード例 #3
0
        private async Task GetPowerCommsListAsync(int cacheDays)
        {
            string json;

            if (cacheDays < 1)
            {
                cacheDays = 1;
            }
            TimeSpan        expiry          = TimeSpan.FromDays(cacheDays);
            DownloadService downloadService = DownloadService.Instance();

            (json, _) = await downloadService.GetData(URL, dataKey, lastUpdatedKey, expiry).ConfigureAwait(false);

            commsList = JsonConvert.DeserializeObject <List <PowerComms> >(json);
        }
コード例 #4
0
ファイル: GalNetService.cs プロジェクト: irongut/XF40Demo
        public async Task <(List <NewsArticle> news, DateTime updated)> GetData(int articleCount,
                                                                                int expiryHours,
                                                                                CancellationTokenSource cancelToken,
                                                                                string BoW       = null,
                                                                                string ignoreBoW = null,
                                                                                bool ignoreCache = false)
        {
            if (expiryHours < 1)
            {
                expiryHours = 1;
            }

            TimeSpan expiry = TimeSpan.FromHours(expiryHours);

            if (galnetNews?.Any() == false || galnetNews.Count < articleCount || lastUpdated + expiry < DateTime.Now)
            {
                // download the json
                string          json;
                DownloadOptions options = new DownloadOptions(cancelToken, expiry, ignoreCache);
                (json, lastUpdated) = await dService.GetData(GalNetURL, options).ConfigureAwait(false);

                // parse the news articles
                galnetNews.Clear();
                await Task.Run(() =>
                {
                    List <NewsArticle> fullNews = JsonConvert.DeserializeObject <List <NewsArticle> >(json, NewsArticleConverter.Instance());
                    List <Topic> topics         = GetTopics(BoW);
                    List <Topic> ignoreTopics   = GetIgnoreTopics(ignoreBoW);
                    foreach (NewsArticle item in fullNews.Where(o => !string.IsNullOrEmpty(o.Body)).OrderByDescending(o => o.PublishDateTime).Take(articleCount))
                    {
                        item.ClassifyArticle(topics, ignoreTopics);
                        galnetNews.Add(item);

                        foreach (Topic topic in topics)
                        {
                            topic.Count = 0;
                        }
                        foreach (Topic topic in ignoreTopics)
                        {
                            topic.Count = 0;
                        }
                    }
                }).ConfigureAwait(false);
            }
            return(galnetNews.Take(articleCount).ToList(), lastUpdated);
        }
コード例 #5
0
ファイル: StandingsService.cs プロジェクト: irongut/XF40Demo
        public async Task <GalacticStandings> GetData(CancellationTokenSource cancelToken, bool ignoreCache = false)
        {
            TimeSpan expiry = TimeSpan.FromMinutes(15);

            if ((galacticStandings == null) || (galacticStandings.Cycle != CycleService.CurrentCycle() && (lastUpdated + expiry < DateTime.Now)))
            {
                // download the standings
                string          json;
                DownloadService downloadService = DownloadService.Instance();
                (json, lastUpdated) = await downloadService.GetData(URL, dataKey, lastUpdatedKey, expiry, cancelToken, ignoreCache).ConfigureAwait(false);

                // parse the standings
                galacticStandings = JsonConvert.DeserializeObject <GalacticStandings>(json);

                // cache till next cycle if updated
                if (galacticStandings.Cycle == CycleService.CurrentCycle())
                {
                    expiry = CycleService.TimeTillTick();
                    Barrel.Current.Add(dataKey, json, expiry);
                    Barrel.Current.Add(lastUpdatedKey, lastUpdated.ToString(), expiry);
                }
            }
            return(galacticStandings);
        }
コード例 #6
0
        public async Task GetDataAsync(CancellationTokenSource cancelToken, bool ignoreCache = false)
        {
            if (Weather.Count < 1 || ignoreCache || Barrel.Current.IsExpired(dataKey))
            {
                TimeSpan        expiry          = TimeSpan.FromHours(1);
                DownloadService downloadService = DownloadService.Instance();
                string          json;
                (json, LastUpdated) = await downloadService.GetData(marsWeatherUrl, dataKey, lastUpdatedKey, expiry, cancelToken, ignoreCache).ConfigureAwait(false);

                Dictionary <string, object> weatherData = JsonConvert.DeserializeObject <Dictionary <string, object> >(json);
                if (weatherData.TryGetValue("sol_keys", out object solKeys))
                {
                    Weather.Clear();
                    foreach (JToken sol in (JArray)solKeys)
                    {
                        // get the day
                        string solName = sol.Value <string>();
                        if (weatherData.TryGetValue(solName, out object solJson))
                        {
                            Dictionary <string, object> solData = JsonConvert.DeserializeObject <Dictionary <string, object> >(solJson.ToString());

                            // get first / last UTC + season
                            DateTime firstUTC = solData.TryGetValue("First_UTC", out object fUTC) ? ParseTimestamp(fUTC.ToString()) : DateTime.MinValue;
                            DateTime lastUTC  = solData.TryGetValue("Last_UTC", out object lUTC) ? ParseTimestamp(lUTC.ToString()) : DateTime.MinValue;
                            string   season   = solData.TryGetValue("Season", out object s) ? ParseSeason(s.ToString()) : String.Empty;

                            // get air temp, pressure + wind speed
                            TemperatureSensorData airTemp = solData.TryGetValue("AT", out object at) ? JsonConvert.DeserializeObject <TemperatureSensorData>(at.ToString()) : null;
                            if (at != null)
                            {
                                airTemp.Scale = TemperatureScale.Celsius;
                            }
                            SensorData airPressure = solData.TryGetValue("PRE", out object pre) ? JsonConvert.DeserializeObject <SensorData>(pre.ToString()) : null;
                            SensorData windSpeed   = solData.TryGetValue("HWS", out object hws) ? JsonConvert.DeserializeObject <SensorData>(hws.ToString()) : null;

                            // get wind direction
                            WindDirectionSensorData windDirection = new WindDirectionSensorData();
                            if (solData.TryGetValue("WD", out object wd) && wd != null)
                            {
                                Dictionary <string, object> windData = JsonConvert.DeserializeObject <Dictionary <string, object> >(wd.ToString());
                                foreach (string pointName in windData.Keys)
                                {
                                    if (windData.TryGetValue(pointName, out object cp) && cp != null)
                                    {
                                        CompassPoint compassPoint = JsonConvert.DeserializeObject <CompassPoint>(cp.ToString());
                                        if (pointName != "most_common")
                                        {
                                            windDirection.CompassPoints.Add(compassPoint);
                                        }
                                        else
                                        {
                                            windDirection.MostCommon = compassPoint;
                                        }
                                    }
                                }
                            }

                            Weather.Add(new MartianDay(
                                            uint.Parse(solName),
                                            airTemp,
                                            windSpeed,
                                            airPressure,
                                            windDirection,
                                            season,
                                            firstUTC,
                                            lastUTC));
                        }
                    }
                    if (Weather.Count > 0)
                    {
                        // increase cache expiry to last recorded time + 25h, minimum 1h
                        DateTime lastMeasurement = Weather.OrderBy(d => d.Sol).Last <MartianDay>().LastUTC;
                        if (lastMeasurement != DateTime.MinValue)
                        {
                            expiry = lastMeasurement.AddHours(25) - DateTime.UtcNow;
                            if (expiry > TimeSpan.FromHours(1))
                            {
                                Barrel.Current.Add(dataKey, json, expiry);
                                Barrel.Current.Add(lastUpdatedKey, LastUpdated.ToString(), expiry);
                            }
                        }
                    }
                }
            }
        }