Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cityList"></param>
        /// <returns></returns>
        public async Task <string> GetWeatherByCity(List <CityModel> cityList)
        {
            List <WeatherFileModel> AllCityWeatherList = new List <WeatherFileModel>();
            string result = string.Empty;

            try
            {
                using (var httpClient = new HttpClient())
                {
                    Console.WriteLine("Calling wheather Api");
                    foreach (CityModel item in cityList)
                    {
                        url = "http://api.openweathermap.org/data/2.5/weather?id=" + item.id + "&appid=" + appID + "&units=" + units;
                        Task <HttpResponseMessage> response = httpClient.GetAsync(url);
                        string responseText = await response.Result.Content.ReadAsStringAsync();

                        CityWeather cityWeather = JsonConvert.DeserializeObject <CityWeather>(responseText);

                        WeatherFileModel fileModel = MapWeatherResponse(cityWeather);

                        result = CreateJsonFile(fileModel);
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex.Message.ToString();
            }

            Console.WriteLine("Weather data retrived successfully");
            return(result);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cityWeatherResponse"></param>
        /// <returns></returns>
        public WeatherFileModel MapWeatherResponse(CityWeather cityWeatherResponse)
        {
            WeatherFileModel fileModel = new WeatherFileModel();

            fileModel.CityName    = cityWeatherResponse.name;
            fileModel.Date        = getDateTimeFromUnixTimeStamp(cityWeatherResponse.dt);
            fileModel.Description = cityWeatherResponse.weather.Select(d => d.description).FirstOrDefault();
            fileModel.Temperature = cityWeatherResponse.main.temp;
            fileModel.Pressure    = cityWeatherResponse.main.pressure;
            fileModel.Humidity    = cityWeatherResponse.main.humidity;
            fileModel.Temp_max    = cityWeatherResponse.main.temp_max;
            fileModel.Temp_min    = cityWeatherResponse.main.temp_min;
            fileModel.WindSpeed   = cityWeatherResponse.wind.speed;
            fileModel.Sunrise     = getDateTimeFromUnixTimeStamp(cityWeatherResponse.sys.sunrise);
            fileModel.Sunset      = getDateTimeFromUnixTimeStamp(cityWeatherResponse.sys.sunset);

            return(fileModel);
        }