static async System.Threading.Tasks.Task Main(string[] args)
        {
            Client client = new Client();
            await client.RetrieveJSON();

            WeatherCollection weatherCollection = JsonConvert.DeserializeObject <WeatherCollection>(client.RawJSONResponse);

            FileWriter fileWriter = new FileWriter(weatherCollection);

            string filePath = "";

            if (args.Length == 1)
            {
                filePath = args[0];
            }
            else if (args.Length == 0)
            {
                Console.WriteLine("Please enter path for the output file: ");
                filePath = Console.ReadLine();
            }
            else
            {
                Console.WriteLine("Incorrect number of arguments.");
            }

            try
            {
                fileWriter.WriteOutputToFile(filePath);
            }
            catch (Exception e)
            {
                Console.WriteLine("Invalid Path");
                Console.WriteLine(e);
            }
        }
Esempio n. 2
0
        private void WorkerLoop()
        {
            StationYearRequest item = null;
            bool ready = false;

            while (true)                        // Keep consuming until
            {                                   // told otherwise.
                while (!ready)
                {
                    _readyEvent.WaitOne(300);
                    lock (_collectionLock)
                    {
                        item = _queue.Min;
                        if (item != null)
                        {
                            ready = true;
                            _queueIndex.Remove(item.StationYear);
                            _queue.Remove(item);
                        }
                    }
                }

                WeatherCollection weatherCollection = StationYearBloomMaker.Instance.PassFilter(item.StationYear)
                    ? DoWithDisk(item)
                    : new WeatherCollection {
                    RunTime = DateTime.Now, WeatherReading = new WeatherReading[0]
                };

                lock (_collectionLock)
                {
                    _cache[item.StationYear].SetResult(weatherCollection);

                    if (_cache.Count > _maxStationYears)
                    {
                        var pairs = _cache.Where(kvp => kvp.Value.Task.IsCompleted).ToArray();
                        if (pairs.Length > _maxStationYears)
                        {
                            pairs = pairs.OrderByDescending(kvp => kvp.Value.Task.Result.LastUpdated)
                                    .Take(_maxStationYears / 2).ToArray();
                            foreach (var pair in pairs)
                            {
                                _cache.Remove(pair.Key);
                            }
                        }
                    }

                    item = _queue.Min;
                    if (item != null)
                    {
                        ready = true;
                        _queueIndex.Remove(item.StationYear);
                        _queue.Remove(item);
                    }
                    else
                    {
                        ready = false;
                    }
                }
            }
        }
Esempio n. 3
0
        private void DeleteItem(WeatherItem weatherItem)
        {
            var navParams = new NavigationParameters();

            navParams.Add("WeatherItemInfo", weatherItem);

            WeatherCollection.Remove(weatherItem);
        }
        public void TickerSymbolChanged(string newCity)
        {
            this.CurrentCity = newCity;
            WeatherCollection newHistoryCollection = _weatherService.GetWeatherHistory(newCity);

            HistoryCollection = newHistoryCollection;

            GroupWeatherCollection newGroupCollection = _weatherService.GetWeatherBreakdown(newCity);

            GroupHistoryCollection = newGroupCollection;
        }
Esempio n. 5
0
        private WeatherCollection GetCurrentCityHistory()
        {
            var startDate = DateTime.Now.AddDays(-DaysAgoQty);
            var result    = new WeatherCollection();

            for (var i = 0; i < DaysAgoQty * 24; i++)
            {
                var item = new WeatherHistoryItem
                {
                    DateTimeMarker = startDate.AddHours(i),
                    Value          = MathUtilities.GetRandomNumber(65, 95)
                };
                result.Add(item);
            }

            return(result);
        }
Esempio n. 6
0
        internal async void GetWeatherForLocation()
        {
            HttpClient client = new HttpClient();
            var        uri    = new Uri(
                string.Format(
                    $"https://jsonplaceholder.typicode.com/posts/{LocationEnteredByUser}"));
            var response = await client.GetAsync(uri);

            WeatherItem weatherData = null;

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                weatherData = WeatherItem.FromJson(content);
            }
            WeatherCollection.Add(weatherData);
        }
Esempio n. 7
0
        private GroupWeatherCollection GetCurrentGroupHistory(WeatherCollection history)
        {
            var result    = new GroupWeatherCollection();
            var tempQuery =
                from hour in history
                group hour by hour.Value into newGroup
                orderby newGroup.Key
                select newGroup;

            foreach (var g in tempQuery)
            {
                result.Add(new WeatherGroupItem {
                    Counts      = g.Count(),
                    Temperature = g.Key
                });
            }

            return(result);
        }
Esempio n. 8
0
        internal async void GetWeatherForLocation()
        {
            HttpClient client = new HttpClient();
            var        uri    = new Uri(
                string.Format(
                    $"http://api.openweathermap.org/data/2.5/weather?q={LocationEnteredByUser}&units=imperial&APPID=" + $"5da8d96113a5c72f1f9836c3c84a6351"));
            //$"{ApiKeys.WeatherKey}"));

            var response = await client.GetAsync(uri);

            WeatherItem weatherData = null;

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                weatherData = WeatherItem.FromJson(content);
            }
            WeatherCollection.Add(weatherData);
        }
Esempio n. 9
0
 private void UpdateSharedInfo(WeatherCollection collection)
 {
     if (collection == null && collection.results == null)
     {
         return;
     }
     try
     {
         string code = collection.results.FirstOrDefault().Now?.Code;
         Location = collection.results?.FirstOrDefault().Location;
         var lastUpdate = DateTime.Now;
         DateTime.TryParse(collection.results?.FirstOrDefault().LastUpdate, out lastUpdate);
         LastUpdateTime      = lastUpdate.ToString("hh:mm");
         BGImage.ImageSource = GetBGImage(code);
         Icon = GetIcon(code);
     }
     catch (Exception ex)
     {
         Debug.WriteLine($"UpdateSharedInfo Error:{ex.Message}");
     }
 }
Esempio n. 10
0
        public static IEnumerable <WeatherReading> Get(StationIdentifier stationIdentifier, DateTime start, DateTime end)
        {
            if (end < start)
            {
                throw new ArgumentException("end before start");
            }
            int startYear   = start.Year;
            int endYear     = end.Year;
            int currentYear = startYear;

            while (currentYear <= endYear)
            {
                WeatherCollection weatherCollection = WeatherMaker.Instance.Get(new StationYear {
                    StationIdentifier = stationIdentifier, Year = currentYear
                });
                foreach (WeatherReading weather in weatherCollection.WeatherReading.SkipWhile(w => w.ReadingTime < start).TakeWhile(w => w.ReadingTime < end))
                {
                    yield return(weather);
                }
                currentYear++;
            }
        }
Esempio n. 11
0
        internal async void GetWeatherForLocation()
        {
            Analytics.TrackEvent("GetWeatherButtonTapped", new Dictionary <string, string> {
                { "WeatherLocation", LocationEnteredByUser },
            });

            HttpClient client = new HttpClient();
            var        uri    = new Uri(
                string.Format(
                    $"http://api.openweathermap.org/data/2.5/forecast?q={LocationEnteredByUser}&units=imperial&APPID=" +
                    $"{ApiKeys.WeatherKey}"));
            var response = await client.GetAsync(uri);

            WeatherItem weatherData = null;

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();

                weatherData = WeatherItem.FromJson(content);
            }
            PopulateListData(weatherData);
            WeatherCollection.Add(weatherData);
        }
Esempio n. 12
0
        private WeatherCollection Do(StationYearRequest stationYearRequest)
        {
            var    stationYear = stationYearRequest.StationYear;
            string url         = String.Format(@"http://www1.ncdc.noaa.gov/pub/data/noaa/isd-lite/{2}/{0:D6}-{1:D5}-{2:D4}.gz"
                                               , stationYear.StationIdentifier.UsafId
                                               , stationYear.StationIdentifier.WbanId
                                               , stationYear.Year);
            var request = (HttpWebRequest)WebRequest.Create(url);

            //request.Method = "HEAD";

            if (stationYearRequest.LastRun != default(WeatherCollection))
            {
                request.Headers.Add(HttpRequestHeader.CacheControl, "max-age=0");
                if (!String.IsNullOrWhiteSpace(stationYearRequest.LastRun.ETag))
                {
                    request.Headers.Add(HttpRequestHeader.IfNoneMatch, stationYearRequest.LastRun.ETag);
                }
                request.IfModifiedSince = (stationYearRequest.LastRun.LastUpdated ?? stationYearRequest.LastRun.RunTime);
            }

            List <WeatherReading> list = new List <WeatherReading>();
            var collection             = new WeatherCollection {
                RunTime = DateTime.Now
            };

            try
            {
                using (WebResponse webResponse = request.GetResponse())
                    using (Stream stream = webResponse.GetResponseStream())
                        if (stream != null)
                        {
                            using (var decompress = new GZipStream(stream, CompressionMode.Decompress))
                                using (var reader = new StreamReader(decompress))
                                //using (MemoryStream ms = new MemoryStream())
                                {
                                    string line;
                                    while ((line = reader.ReadLine()) != null)
                                    {
                                        WeatherReading weatherReading = ProcessWeatherLine(line);
                                        if (weatherReading != default(WeatherReading))
                                        {
                                            list.Add(weatherReading);
                                        }
                                    }
                                    collection.ETag = webResponse.Headers["ETag"];
                                    DateTime tempDateTime;
                                    if (DateTime.TryParse(webResponse.Headers["Last-Modified"], out tempDateTime))
                                    {
                                        collection.LastUpdated = tempDateTime;
                                    }
                                }
                        }
            }
            catch (WebException e)
            {
                HttpStatusCode httpStatusCode = ((HttpWebResponse)e.Response).StatusCode;
                if (stationYearRequest.LastRun != default(WeatherCollection) && httpStatusCode == HttpStatusCode.NotModified)
                {
                    collection         = stationYearRequest.LastRun;
                    collection.RunTime = DateTime.Now;
                    return(collection);
                }
                WebExceptionStatus webExceptionStatus = e.Status;
            }
// ReSharper disable EmptyGeneralCatchClause
            catch (Exception e)
// ReSharper restore EmptyGeneralCatchClause
            {
            }

            WeatherReading[] weatherReadings = list.ToArray();
            weatherReadings.EnusureSorted();
            collection.WeatherReading = weatherReadings;

            return(collection);
        }
Esempio n. 13
0
        public WeatherCollection GetWeatherHistory(string citySymbol)
        {
            WeatherCollection items = _history[citySymbol];

            return(items);
        }