Ejemplo n.º 1
0
        /// <summary>
        /// Get a single day of solar altitude data entries
        /// </summary>
        /// <param name="date">The date for which data is desired</param>
        /// <param name="city">City for calculation</param>
        /// <param name="state">State for calculation</param>
        /// <returns></returns>
        public UsNavalSolarAltitudeGreaterThan50DegDailyEntry GetOneDay(DateTime date, string city, string state)
        {
            var requestUri = string.Format(UriPattern, date.Year, date.Month, date.Day, Interval, state, city);

            var    retriesLeft = 3;
            string body        = null;

            // try a few times (if necessary) to get a response body
            while (retriesLeft > 0)
            {
                body = _requestHelper.GetResponseBodyForUri(new Uri(requestUri));

                if (body != null)
                {
                    break;
                }

                retriesLeft--;
                Thread.Sleep(SingleEntryRetryDelay);
            }

            // If no body was ever found, time to move on
            if (body == null)
            {
                return(null);
            }

            var entry = new UsNavalSolarAltitudeGreaterThan50DegDailyEntry
            {
                EntryDate = date
            };

            // dig through the result for data lines
            foreach (Match match in _lineParseRegex.Matches(body))
            {
                var timeOffset = ParseTimeOffset(match.Groups["time"].Value);
                var altitude   = ParseAltitude(match.Groups["alt"].Value);

                // for when no begin time set
                if (altitude >= 50 && entry.BeginOffset == TimeSpan.Zero)
                {
                    entry.BeginOffset = timeOffset;
                    continue;
                }

                // for when no end time set
                if (altitude < 50 && entry.BeginOffset > TimeSpan.Zero && entry.EndOffset == TimeSpan.Zero)
                {
                    entry.EndOffset = timeOffset;
                }
            }

            return(entry);
        }