diff --git a/handlenett-backend/web-api/HandlenettAPI/HandlenettAPI.csproj b/handlenett-backend/web-api/HandlenettAPI/HandlenettAPI.csproj index 1903045..5a18943 100644 --- a/handlenett-backend/web-api/HandlenettAPI/HandlenettAPI.csproj +++ b/handlenett-backend/web-api/HandlenettAPI/HandlenettAPI.csproj @@ -30,6 +30,7 @@ + diff --git a/handlenett-backend/web-api/HandlenettAPI/Models/Weather.cs b/handlenett-backend/web-api/HandlenettAPI/Models/Weather.cs index e6c1576..919aef0 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Models/Weather.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Models/Weather.cs @@ -4,5 +4,6 @@ public class Weather { public string Temperature { get; set; } public Uri ImageUri { get; set; } + public DateTime LastUpdated { get; set; } } } diff --git a/handlenett-backend/web-api/HandlenettAPI/Services/WeatherService.cs b/handlenett-backend/web-api/HandlenettAPI/Services/WeatherService.cs index e53e650..a8178f3 100644 --- a/handlenett-backend/web-api/HandlenettAPI/Services/WeatherService.cs +++ b/handlenett-backend/web-api/HandlenettAPI/Services/WeatherService.cs @@ -1,19 +1,43 @@ -using HandlenettAPI.Models; +using Azure.Identity; +using HandlenettAPI.Models; using Newtonsoft.Json.Linq; +using StackExchange.Redis; +using System.Text.Json; namespace HandlenettAPI.Services { public class WeatherService { private readonly HttpClient _httpClient; + private readonly ConnectionMultiplexer _redis; - public WeatherService(HttpClient httpClient) + public WeatherService(HttpClient httpClient, IConfiguration config) { _httpClient = httpClient; _httpClient.DefaultRequestHeaders.Add("User-Agent", "Miles Haugesund Handlenett/1.0 (roger.torkelsen@miles.no)"); + _redis = ConnectionMultiplexer.Connect(config.GetConnectionString("AzureRedisCache")); } public async Task GetWeatherByCoordinatesAsync(double latitude, double longitude) + { + Weather? weather = null; + + var cachedValue = GetCacheValue("Weather"); + if (cachedValue != null) + { + weather = JsonSerializer.Deserialize(cachedValue.ToString()); + } + + if (weather == null || weather.LastUpdated <= DateTime.UtcNow.AddHours(-1)) + { + weather = await GetDataFromMet(latitude, longitude); + weather.LastUpdated = DateTime.UtcNow; + SetCacheValue("Weather", JsonSerializer.Serialize(weather)); + } + return weather; + } + + private async Task GetDataFromMet(double latitude, double longitude) { var returnModel = new Weather(); var url = $"https://api.met.no/weatherapi/locationforecast/2.0/compact?lat={latitude}&lon={longitude}"; @@ -27,16 +51,29 @@ public async Task GetWeatherByCoordinatesAsync(double latitude, double returnModel.Temperature = json["properties"]["timeseries"][0]["data"]["instant"]["details"]["air_temperature"].ToString(); var symbol = json["properties"]["timeseries"][0]["data"]["next_1_hours"]["summary"]["symbol_code"].ToString(); + + //TODO: add icons for displaying weather in web app //returnModel.ImageUri = new Uri($"https://api.met.no/weatherapi/weathericon/2.0/?symbol={symbol}&content_type=image/png"); //https://github.com/metno/weathericons + return returnModel; } else { - throw new Exception ("Unable to retrieve weather data from MET Norway."); + throw new Exception("Unable to retrieve weather data from MET Norway."); } } + private void SetCacheValue(string key, string value) + { + var db = _redis.GetDatabase(); + db.StringSet(key, value); + } + private string? GetCacheValue(string key) + { + var db = _redis.GetDatabase(); + return db.StringGet(key); + } } }