Update weather result (in all configuration's section)

This commit is contained in:
Thomas Fransolet 2024-04-11 17:37:10 +02:00
parent 68b45333da
commit 1fa4e188fa

View File

@ -129,62 +129,68 @@ namespace ManagerService.Controllers
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(id);
var weatherSections = _sectionService.GetAllWeatherSectionsFromConfiguration(id);
foreach (var weatherSection in weatherSections)
try
{
WeatherDTO weatherDTO = JsonConvert.DeserializeObject<WeatherDTO>(weatherSection.Data);
if (weatherDTO.city != null && weatherDTO.city.Length >= 2 &&
(weatherDTO.updatedDate == null || weatherDTO.updatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
var weatherSections = _sectionService.GetAllWeatherSectionsFromConfiguration(id);
foreach (var weatherSection in weatherSections)
{
// Call Openweather api with token from appSettings and update result with json
var apiKey = _configuration.GetSection("OpenWeatherApiKey").Get<string>();
if (apiKey != null && apiKey.Length > 0)
WeatherDTO weatherDTO = JsonConvert.DeserializeObject<WeatherDTO>(weatherSection.Data);
if (weatherDTO.city != null && weatherDTO.city.Length >= 2 &&
(weatherDTO.updatedDate == null || weatherDTO.updatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
{
string url = $"http://api.openweathermap.org/geo/1.0/direct?q={weatherDTO.city}&limit=1&appid={apiKey}";
// Call Openweather api with token from appSettings and update result with json
var apiKey = _configuration.GetSection("OpenWeatherApiKey").Get<string>();
using (HttpClient client = new HttpClient())
if (apiKey != null && apiKey.Length > 0)
{
try
string url = $"http://api.openweathermap.org/geo/1.0/direct?q={weatherDTO.city}&limit=1&appid={apiKey}";
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
List<CityData> cities = JsonConvert.DeserializeObject<List<CityData>>(responseBody);
if (cities.Count > 0)
try
{
double lat = cities[0].Lat;
double lon = cities[0].Lon;
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
//string onecallUrl = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={apiKey}";
string callUrl = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&units=metric&appid={apiKey}";
List<CityData> cities = JsonConvert.DeserializeObject<List<CityData>>(responseBody);
HttpResponseMessage callResponse = await client.GetAsync(callUrl);
callResponse.EnsureSuccessStatusCode();
string callResponseBody = await callResponse.Content.ReadAsStringAsync();
if (cities.Count > 0)
{
double lat = cities[0].Lat;
double lon = cities[0].Lon;
weatherDTO.updatedDate = DateTimeOffset.Now;
weatherDTO.result = callResponseBody;
weatherSection.Data = JsonConvert.SerializeObject(weatherDTO);
//string onecallUrl = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={apiKey}";
string callUrl = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&units=metric&appid={apiKey}";
_sectionService.Update(weatherSection.Id, weatherSection);
HttpResponseMessage callResponse = await client.GetAsync(callUrl);
callResponse.EnsureSuccessStatusCode();
string callResponseBody = await callResponse.Content.ReadAsStringAsync();
weatherDTO.updatedDate = DateTimeOffset.Now;
weatherDTO.result = callResponseBody;
weatherSection.Data = JsonConvert.SerializeObject(weatherDTO);
_sectionService.Update(weatherSection.Id, weatherSection);
}
else
{
Console.WriteLine("Aucune ville trouvée.");
}
}
else
catch (HttpRequestException e)
{
Console.WriteLine("Aucune ville trouvée.");
Console.WriteLine($"Une erreur s'est produite lors de la requête HTTP : {e.Message}");
}
}
catch (HttpRequestException e)
{
Console.WriteLine($"Une erreur s'est produite lors de la requête HTTP : {e.Message}");
}
}
}
}
}
} catch (Exception e)
{
Console.WriteLine($"Une erreur s'est produite lors de la mise à jour des sections de type météo : {e.Message}");
}
return new OkObjectResult(configuration.ToDTO(sectionIds));