public async Task <string> GetDarkSkyWeatherApi(string latitude = "21.028511", string longitude = "105.804817") { var username = "******"; try { username = Environment.MachineName; } catch (InvalidOperationException ex) { _logger.LogError($"GetDarkSkyWeatherApi -> InvalidOperationException -> Error: Cannot get Computer Name -> {ex.Message}"); } catch (Exception ex) { _logger.LogError($"GetDarkSkyWeatherApi -> Exception -> Error: Cannot get Computer Name -> {ex.Message}"); } _logger.LogInformation($"GetDarkSkyWeatherApi username: {username}"); var apiKey = ApiSettings.DarkSkyApiKey; var hostUri = ApiSettings.DarkSkyApiHostUrl; var uriString = $"{hostUri}forecast/{apiKey}/{latitude},{longitude}?units=si&lang=vi"; var responseString = await HttpClientHelpers.GetHttpClientHelper(uriString); if (responseString == null) { _logger.LogError("Error: GetDarkSkyWeatherApi cannot get api result from DarkSky"); return("False"); } if (responseString.Contains("Error")) { _logger.LogError($"Error: GetDarkSkyWeatherApi {responseString}"); return("False"); } var apiResult = JsonConvert.DeserializeObject <DarkSkyWeatherApiResult>(responseString); var latitudeResult = apiResult.latitude; var longitudeResult = apiResult.longitude; var timezoneResult = apiResult.timezone; // DarkSky Current Weathers await AddOrUpdateDarkSkyWeather(TodayType.Currently, apiResult.currently.time, latitudeResult, longitudeResult, timezoneResult, apiResult.currently.summary, apiResult.currently.icon, apiResult.currently.temperature, apiResult.currently.apparentTemperature, username); // DarkSky Hourly Weathers var hourlyDatas = apiResult.hourly.data; foreach (var hourlyData in hourlyDatas) { await AddOrUpdateDarkSkyWeather(TodayType.Hourly, hourlyData.time, latitudeResult, longitudeResult, timezoneResult, hourlyData.summary, hourlyData.icon, hourlyData.temperature, hourlyData.apparentTemperature, username); } // DarkSky Daily Weathers var dailyData = apiResult.daily.data; foreach (var item in dailyData) { var darkSkyDailyWeather = await _context.DarkSkyDailyWeathers .FirstOrDefaultAsync(m => m.latitude == latitudeResult && m.longitude == longitudeResult && m.time == item.time); if (darkSkyDailyWeather == null) { darkSkyDailyWeather = new DarkSkyDailyWeather() { latitude = latitudeResult, longitude = longitudeResult, timezone = timezoneResult, time = item.time, summary = item.summary, icon = item.icon, temperatureMax = item.temperatureMax, temperatureMin = item.temperatureMin, DateCreated = DateTime.Now, DateModified = DateTime.Now, UserCreated = username, UserModified = username, }; await _context.DarkSkyDailyWeathers.AddAsync(darkSkyDailyWeather); await _context.SaveChangesAsync(); } else { darkSkyDailyWeather.time = item.time; darkSkyDailyWeather.summary = item.summary; darkSkyDailyWeather.icon = item.icon; darkSkyDailyWeather.temperatureMax = item.temperatureMax; darkSkyDailyWeather.temperatureMin = item.temperatureMin; darkSkyDailyWeather.DateModified = DateTime.Now; darkSkyDailyWeather.UserModified = username; } } return("Success"); }