// ------------------ Get Weather Description ---------------------------------- public async Task GetWeatherCondition(Park park, WeatherJsonInfo weatherJsonInfo) { string weatherCondition = weatherJsonInfo.weather[0].main; park.CurrentWeatherInfo.condition = weatherCondition; await db.SaveChangesAsync(); }
// ------------------ Get Wind Condition ---------------------------------- public async Task GetWindCondition(Park park, WeatherJsonInfo weatherJsonInfo) { float windCondition = weatherJsonInfo.wind.speed; double simpleWindMeasure = Math.Round(windCondition, 2); park.CurrentWeatherInfo.wind = simpleWindMeasure; await db.SaveChangesAsync(); }
// -------///////------START WEATHER RELATED METHODS-----------\\\\\\\\\\\\\\\\\\\--------------- // ------------------ Get Temperature ---------------------------------- public async Task GetCurrentTemperature(Park park, WeatherJsonInfo weatherJsonInfo) { float tempInKelvin = weatherJsonInfo.main.temp; double convertKelvinToFahrenheit = ((tempInKelvin - 273.15) * 9 / 5) + 32; int simpleDegree = Convert.ToInt32(convertKelvinToFahrenheit); park.CurrentWeatherInfo.temperature = simpleDegree; // Object reference not set to an instance of an object. // Line 260: park.CurrentWeatherInfo.temperature = simpleDegree; await db.SaveChangesAsync(); }
// ------------------ Get Weather JSON ----------------------------- public async Task RunWeatherJson(ApiKeys apiKeys, Park park) { string weatherKey = apiKeys.OpenWeatherKey; string parkLatitude = park.ParkLat; string parkLongitude = park.ParkLng; string url = $"https://api.openweathermap.org/data/2.5/weather?lat={parkLatitude}&lon={parkLongitude}&APPID={weatherKey}"; HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(url); string jsonresult = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { WeatherJsonInfo weatherJsonInfo = JsonConvert.DeserializeObject <WeatherJsonInfo>(jsonresult); await GetCurrentTemperature(park, weatherJsonInfo); await GetWindCondition(park, weatherJsonInfo); await GetWeatherCondition(park, weatherJsonInfo); await db.SaveChangesAsync(); } }