Пример #1
0
        /// <summary>
        /// Retrieves the data for the weather
        /// </summary>
        /// <param name="cityId">Id of the city</param>
        /// <returns>Some(data) for the weather. None, when the cityId is unknown</returns>
        public async Task <Option <WeatherModel> > GetWeather(int cityId)
        {
            var currentTaskOption  = openWeathermapService.GetCurrentWeather(cityId);
            var forecastTaskOption = openWeathermapService.GetWeatherforecast(cityId);

            Task.WaitAll(currentTaskOption, forecastTaskOption);

            Option <OpenWeathermapCurrent>  openWeatherMapCurrentOption  = await currentTaskOption;
            Option <OpenWeathermapForecast> openWeatherMapForecastOption = await forecastTaskOption;

            return(openWeatherMapCurrentOption
                   .Some(openWeatherMapCurrent =>
            {
                Weather current = mapper.Map <OpenWeathermapCurrent, Weather>(openWeatherMapCurrent);
                return openWeatherMapForecastOption
                .Some(openWeatherMapForecast =>
                {
                    Weather[] forecast = mapper.Map <WeatherList[], Weather[]>(openWeatherMapForecast.list);

                    var model = new WeatherModel(current, forecast);
                    model.AverageHumidity = model.CalculateAverageHumidity();
                    model.AverageTemperature = model.CalculateAverageTemperature();

                    return Option <WeatherModel> .Some(model);
                })
                .None(() => Option <WeatherModel> .None);
            })
                   .None(() => Option <WeatherModel> .None));
        }
Пример #2
0
 public static float CalculateAverageHumidity(this WeatherModel model)
 {
     return(model.Forecast.Length == 0
         ? 0
         : (float)model.Forecast.Sum(c => c.Humidity) / model.Forecast.Length);
 }
Пример #3
0
 public static float CalculateAverageTemperature(this WeatherModel model)
 {
     return(model.Forecast.Length == 0
         ? 0
         : model.Forecast.Sum(c => c.Temperature) / model.Forecast.Length);
 }