Пример #1
0
        /// <summary>
        /// Constructs a weather report object out of a ipma object
        /// that comes from IPMA's API, extracting only the relevant information.
        /// </summary>
        /// <param name="ipma">IPMA object from JSON deserialization of IPMA's API response</param>
        /// <param name="date">The weather forecast date of the report</param>
        /// <param name="locationcode">The location code of the city in the weather report</param>
        /// <returns>The WeatherReport object constructed from IPMA info</returns>
        private WeatherReport IPMAToWeatherReport(IPMA ipma, DateTime date, string locationcode)
        {
            WeatherReport wr = new WeatherReport();

            wr.Location    = locationCodes[locationcode];
            wr.Date        = date;
            wr.TempMax     = ipma.tMax.ToString();
            wr.TempMin     = ipma.tMin.ToString();
            wr.WeatherType = forecastDescPT[int.Parse(ipma.idTipoTempo)];

            return(wr);
        }
Пример #2
0
        /// <summary>
        /// Gets weather forecast info from IPMA's API for a given date and location.
        /// It deserializes info into multiple IPMA objects and selects the relevant one
        /// (the one that includes average temp for the date provided)
        /// and returns the final weather report object constructed from this IPMA object
        /// </summary>
        /// <param name="locationCode">The IPMA code for the location for which the weather forecast is to be obtained</param>
        /// <param name="dateOfForecast">The date for which the weather forecast is to be obtained</param>
        /// <returns>Final WeatherReport for a given location and date</returns>
        private async Task <WeatherReport> GetWeatherReport(string locationCode, DateTime dateOfForecast)
        {
            //get and deserialize all forecast (for all date/time available) for a given location
            var url      = $"http://api.ipma.pt/json/alldata/{locationCode}.json";
            var response = await _client.GetAsync(url);

            string      jsonString = response.Content.ReadAsStringAsync().Result;
            List <IPMA> l          = JsonConvert.DeserializeObject <List <IPMA> >(jsonString);

            //select the ipma object corresponding to the given date of forecast and to a time which has average day temperature available
            string aux  = dateOfForecast.ToString("yyyy-MM-dd");
            IPMA   ipma = l.Where(x => x.dataPrev.StartsWith(aux) && !x.tMax.StartsWith("-99")).FirstOrDefault();

            return(IPMAToWeatherReport(ipma, dateOfForecast, locationCode));
        }