Update weather section on get configuration detail

This commit is contained in:
Thomas Fransolet 2024-04-10 17:15:10 +02:00
parent b87256e29d
commit 68b45333da
2 changed files with 45 additions and 32 deletions

View File

@ -129,54 +129,62 @@ namespace ManagerService.Controllers
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(id); List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(id);
if (configuration.WeatherCity != null && configuration.WeatherCity.Length >= 2 && var weatherSections = _sectionService.GetAllWeatherSectionsFromConfiguration(id);
(configuration.WeatherUpdatedDate == null || configuration.WeatherUpdatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
foreach (var weatherSection in weatherSections)
{ {
// Call Openweather api with token from appSettings and update result with json WeatherDTO weatherDTO = JsonConvert.DeserializeObject<WeatherDTO>(weatherSection.Data);
var apiKey = _configuration.GetSection("OpenWeatherApiKey").Get<string>(); if (weatherDTO.city != null && weatherDTO.city.Length >= 2 &&
(weatherDTO.updatedDate == null || weatherDTO.updatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
if (apiKey != null && apiKey.Length > 0)
{ {
string url = $"http://api.openweathermap.org/geo/1.0/direct?q={configuration.WeatherCity}&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); try
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
List<CityData> cities = JsonConvert.DeserializeObject<List<CityData>>(responseBody);
if (cities.Count > 0)
{ {
double lat = cities[0].Lat; HttpResponseMessage response = await client.GetAsync(url);
double lon = cities[0].Lon; response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
//string onecallUrl = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={apiKey}"; List<CityData> cities = JsonConvert.DeserializeObject<List<CityData>>(responseBody);
string callUrl = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&units=metric&appid={apiKey}";
HttpResponseMessage callResponse = await client.GetAsync(callUrl); if (cities.Count > 0)
callResponse.EnsureSuccessStatusCode(); {
string callResponseBody = await callResponse.Content.ReadAsStringAsync(); double lat = cities[0].Lat;
double lon = cities[0].Lon;
configuration.WeatherUpdatedDate = DateTimeOffset.Now; //string onecallUrl = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={apiKey}";
configuration.WeatherResult = callResponseBody; string callUrl = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&units=metric&appid={apiKey}";
_configurationService.Update(configuration.Id, configuration); 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}");
}
} }
} }
} }
return new OkObjectResult(configuration.ToDTO(sectionIds)); return new OkObjectResult(configuration.ToDTO(sectionIds));

View File

@ -39,6 +39,11 @@ namespace Manager.Services
return _Sections.Find(s => !s.IsSubSection && s.ConfigurationId == configurationId).ToList().Select(s => s.Id).ToList(); return _Sections.Find(s => !s.IsSubSection && s.ConfigurationId == configurationId).ToList().Select(s => s.Id).ToList();
} }
public List<Section> GetAllWeatherSectionsFromConfiguration(string configurationId)
{
return _Sections.Find(s => s.ConfigurationId == configurationId && s.Type == SectionType.Weather).ToList();
}
public List<Section> GetAllSubSection(string parentId) public List<Section> GetAllSubSection(string parentId)
{ {
return _Sections.Find(s => s.IsSubSection && s.ParentId == parentId).ToList(); return _Sections.Find(s => s.IsSubSection && s.ParentId == parentId).ToList();