Example #1
0
        private StationCollection DoWithDisk(StaionCollectionRequest staionCollectionRequest)
        {
            string path = Path.GetTempPath() + "Weather\\";

            Directory.CreateDirectory(path);
            path += "StationData.dat";
            StationCollection stationCollection;

            if (File.Exists(path))
            {
                using (FileStream fileStream = File.OpenRead(path))
                {
                    stationCollection = Serializer.Deserialize <StationCollection>(fileStream);
                }
                if (!stationCollection.NeedsToRevalidate())
                {
                    return(stationCollection);
                }
                staionCollectionRequest.LastRun = stationCollection;
            }

            stationCollection = Do(staionCollectionRequest);

            using (FileStream fileStream = File.Create(path))
            {
                Serializer.Serialize(fileStream, stationCollection);
            }

            return(stationCollection);
        }
Example #2
0
        private StationCollection Do(StaionCollectionRequest staionCollectionRequest)
        {
            HttpWebRequest stationRequest =
                WebRequest.CreateHttp("http://www1.ncdc.noaa.gov/pub/data/noaa/isd-history.csv");

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

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

            try
            {
                using (WebResponse webResponse = stationRequest.GetResponse())
                    using (Stream stream = webResponse.GetResponseStream())
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                string stationLine;
                                bool   hasSeenHeader = false;
                                while ((stationLine = reader.ReadLine()) != null)
                                {
                                    if (hasSeenHeader)
                                    {
                                        Station station = ParseStation(stationLine);
                                        if (station != default(Station))
                                        {
                                            list.Add(station);
                                        }
                                    }
                                    else if (stationLine ==
                                             @"""USAF"",""WBAN"",""STATION NAME"",""CTRY"",""STATE"",""ICAO"",""LAT"",""LON"",""ELEV(M)"",""BEGIN"",""END""")
                                    {
                                        hasSeenHeader = true;
                                    }
                                    else
                                    {
                                        throw new NotSupportedException("station csv bad");
                                    }
                                }

                                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 (staionCollectionRequest.LastRun != null && httpStatusCode == HttpStatusCode.NotModified)
                {
                    collection         = staionCollectionRequest.LastRun;
                    collection.RunTime = DateTime.Now;
                    return(collection);
                }
                WebExceptionStatus webExceptionStatus = e.Status;
            }
// ReSharper disable EmptyGeneralCatchClause
            catch (Exception e)
// ReSharper restore EmptyGeneralCatchClause
            {}

            collection.Stations = list.ToArray();

            return(collection);
        }