diff --git a/ImmichFrame/App.config b/ImmichFrame/App.config index a84bea7..6f5f2a9 100644 --- a/ImmichFrame/App.config +++ b/ImmichFrame/App.config @@ -49,21 +49,21 @@ 36 - - True - 12 - - fahrenheit - 41.186660,-81.800830 1 + + imperial + + + + \ No newline at end of file diff --git a/ImmichFrame/Helpers/WeatherHelper.cs b/ImmichFrame/Helpers/WeatherHelper.cs new file mode 100644 index 0000000..4a9776a --- /dev/null +++ b/ImmichFrame/Helpers/WeatherHelper.cs @@ -0,0 +1,40 @@ +using ImmichFrame.Models; +using OpenWeatherMap; +using OpenWeatherMap.Models; +using System; +using System.Net.Http; +using System.Threading.Tasks; + +namespace ImmichFrame.Helpers +{ + public class WeatherHelper + { + private static readonly OpenWeatherMapOptions Options = new OpenWeatherMapOptions + { + ApiKey = Settings.CurrentSettings.WeatherApiKey, + UnitSystem = Settings.CurrentSettings.UnitSystem, + Language = Settings.CurrentSettings.Language, + }; + public static Task GetWeather() + { + var settings = Settings.CurrentSettings; + return GetWeather(settings.WeatherLat, settings.WeatherLong); + } + public static async Task GetWeather(double latitude, double longitude) + { + try + { + IOpenWeatherMapService openWeatherMapService = new OpenWeatherMapService(Options); + var weatherInfo = await openWeatherMapService.GetCurrentWeatherAsync(latitude, longitude); + + return weatherInfo; + } + catch + { + //do nothing and return null + } + + return null; + } + } +} diff --git a/ImmichFrame/ImmichFrame.csproj b/ImmichFrame/ImmichFrame.csproj index 63eda3c..8c1f293 100755 --- a/ImmichFrame/ImmichFrame.csproj +++ b/ImmichFrame/ImmichFrame.csproj @@ -46,6 +46,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/ImmichFrame/Models/Settings.cs b/ImmichFrame/Models/Settings.cs index f1ef651..f81e4fc 100644 --- a/ImmichFrame/Models/Settings.cs +++ b/ImmichFrame/Models/Settings.cs @@ -33,10 +33,14 @@ public class Settings public string? PhotoDateFormat { get; set; } = "MM/dd/yyyy"; public bool ShowImageDesc { get; set; } = true; public int ImageDescFontSize { get; set; } = 36; - public bool ShowWeather { get; set; } = false; + public bool ShowWeather => !string.IsNullOrWhiteSpace(WeatherApiKey); public int WeatherFontSize { get; set; } = 36; - public string? WeatherUnits { get; set; } = "fahrenheit"; + public string? UnitSystem { get; set; } = OpenWeatherMap.UnitSystem.Imperial; public string? WeatherLatLong { get; set; } = "40.7128,74.0060"; + public string? WeatherApiKey { get; set; } = string.Empty; + public float WeatherLat => !string.IsNullOrWhiteSpace(WeatherLatLong) ? float.Parse(WeatherLatLong!.Split(',')[0]) : 0f; + public float WeatherLong => !string.IsNullOrWhiteSpace(WeatherLatLong) ? float.Parse(WeatherLatLong!.Split(',')[1]) : 0f; + public string Language { get; set; } = "en"; private static Settings? _settings; public static Settings CurrentSettings @@ -90,10 +94,10 @@ public void Serialize() defaultSettings.PhotoDateFormat = this.PhotoDateFormat; defaultSettings.ShowImageDesc = this.ShowImageDesc; defaultSettings.ImageDescFontSize = this.ImageDescFontSize; - defaultSettings.ShowWeather = this.ShowWeather; defaultSettings.WeatherFontSize = this.WeatherFontSize; - defaultSettings.WeatherUnits = this.WeatherUnits; + defaultSettings.UnitSystem = this.UnitSystem?.ToString() ?? OpenWeatherMap.UnitSystem.Imperial; defaultSettings.WeatherLatLong = this.WeatherLatLong; + defaultSettings.WeatherApiKey = this.WeatherApiKey; var albums = new StringCollection(); if(this.Albums?.Any() ?? false) @@ -235,6 +239,7 @@ private static Settings ParseSettings(Dictionary SettingsValues) property.SetValue(settings, url); break; case "ApiKey": + case "WeatherApiKey": property.SetValue(settings, value); break; case "Albums": @@ -269,7 +274,6 @@ private static Settings ParseSettings(Dictionary SettingsValues) case "ShowClock": case "ShowPhotoDate": case "ShowImageDesc": - case "ShowWeather": if (!bool.TryParse(value.ToString(), out var boolValue)) throw new SettingsNotValidException($"Value of '{SettingsValue.Key}' is not valid. ('{value}')"); property.SetValue(settings, boolValue); @@ -280,9 +284,10 @@ private static Settings ParseSettings(Dictionary SettingsValues) case "PhotoDateFormat": property.SetValue(settings, value); break; - case "WeatherUnits": - if (!Regex.IsMatch(value.ToString(), @"^(?i)(celsius|fahrenheit)$")) + case "UnitSystem": + if (!Regex.IsMatch(value.ToString(), @"^(?i)(metric|imperial)$")) throw new SettingsNotValidException($"Value of '{SettingsValue.Key}' is not valid. ('{value}')"); + value = value.ToString().ToLower() == "metric" ? OpenWeatherMap.UnitSystem.Metric : OpenWeatherMap.UnitSystem.Imperial; property.SetValue(settings, value); break; case "WeatherLatLong": @@ -291,6 +296,9 @@ private static Settings ParseSettings(Dictionary SettingsValues) throw new SettingsNotValidException($"Value of '{SettingsValue.Key}' is not valid. ('{value}')"); property.SetValue(settings, value); break; + case "Language": + property.SetValue(settings, value); + break; default: throw new SettingsNotValidException($"Element '{SettingsValue.Key}' is unknown. ('{value}')"); } diff --git a/ImmichFrame/Properties/Settings.Designer.cs b/ImmichFrame/Properties/Settings.Designer.cs index eae7842..2f1922f 100644 --- a/ImmichFrame/Properties/Settings.Designer.cs +++ b/ImmichFrame/Properties/Settings.Designer.cs @@ -191,18 +191,6 @@ public int ImageDescFontSize { } } - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("True")] - public bool ShowWeather { - get { - return ((bool)(this["ShowWeather"])); - } - set { - this["ShowWeather"] = value; - } - } - [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("12")] @@ -215,18 +203,6 @@ public int WeatherFontSize { } } - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("fahrenheit")] - public string WeatherUnits { - get { - return ((string)(this["WeatherUnits"])); - } - set { - this["WeatherUnits"] = value; - } - } - [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("41.186660,-81.800830")] @@ -272,5 +248,29 @@ public double TransitionDuration { this["TransitionDuration"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("imperial")] + public string UnitSystem { + get { + return ((string)(this["UnitSystem"])); + } + set { + this["UnitSystem"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string WeatherApiKey { + get { + return ((string)(this["WeatherApiKey"])); + } + set { + this["WeatherApiKey"] = value; + } + } } } diff --git a/ImmichFrame/Properties/Settings.settings b/ImmichFrame/Properties/Settings.settings index 7d588cd..d8733cc 100644 --- a/ImmichFrame/Properties/Settings.settings +++ b/ImmichFrame/Properties/Settings.settings @@ -44,15 +44,9 @@ 36 - - True - 12 - - fahrenheit - 41.186660,-81.800830 @@ -65,5 +59,11 @@ 1 + + imperial + + + + \ No newline at end of file diff --git a/ImmichFrame/Settings.example.xml b/ImmichFrame/Settings.example.xml index dd85456..bea7188 100644 --- a/ImmichFrame/Settings.example.xml +++ b/ImmichFrame/Settings.example.xml @@ -16,16 +16,16 @@ First Person UID Second Person UID - + 8 1.0 - + false - + false @@ -48,11 +48,15 @@ true 36 - - false - 12 - - fahrenheit + + + + 12 + + imperial 40.7128,74.0060 + + + en \ No newline at end of file diff --git a/ImmichFrame/Views/MainView.axaml.cs b/ImmichFrame/Views/MainView.axaml.cs index faba891..52b002d 100755 --- a/ImmichFrame/Views/MainView.axaml.cs +++ b/ImmichFrame/Views/MainView.axaml.cs @@ -11,6 +11,7 @@ using MsBox.Avalonia; using MsBox.Avalonia.Enums; using System; +using System.Linq; using System.Threading.Tasks; namespace ImmichFrame.Views; @@ -93,14 +94,11 @@ private void timerLiveTimeTick(object? state) } private void timerWeatherTick(object? state) { - string latitude = _appSettings.WeatherLatLong!.Split(',')[0]; - string longitude = _appSettings.WeatherLatLong!.Split(',')[1]; - OpenMeteoResponse? openMeteoResponse = Task.Run(() => Weather.GetWeather(latitude, longitude, _appSettings.WeatherUnits!)).Result; - if (openMeteoResponse != null) + var weatherInfo = WeatherHelper.GetWeather().Result; + if (weatherInfo != null) { - _viewModel.WeatherTemperature = openMeteoResponse.current_weather!.temperature.ToString() + openMeteoResponse.current_weather_units!.temperature; - string description = WmoWeatherInterpreter.GetWeatherDescription(openMeteoResponse.current_weather.weathercode, Convert.ToBoolean(openMeteoResponse.current_weather.is_day)); - _viewModel.WeatherCurrent = description; + _viewModel.WeatherTemperature = $"{weatherInfo.Main.Temperature}{Environment.NewLine}{weatherInfo.CityName}"; + _viewModel.WeatherCurrent = $"{string.Join(',', weatherInfo.Weather.Select(x=>x.Description))}"; } } diff --git a/ImmichFrame/Views/SettingsView.axaml b/ImmichFrame/Views/SettingsView.axaml index b4cd72f..f64a148 100644 --- a/ImmichFrame/Views/SettingsView.axaml +++ b/ImmichFrame/Views/SettingsView.axaml @@ -131,17 +131,21 @@ - - - - - - + + + + + + + + + + diff --git a/ImmichFrame/Weather.cs b/ImmichFrame/Weather.cs deleted file mode 100644 index f264c46..0000000 --- a/ImmichFrame/Weather.cs +++ /dev/null @@ -1,81 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Http; -using System.Text.Json; -using System.Threading.Tasks; - -namespace ImmichFrame; -static class Weather -{ - public static async Task GetWeather(string latitude, string longitude, string temperatureUnit) - { - OpenMeteoResponse? openMeteoResponse = null; - string apiUrl = $"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=true&hourly=temperature_2m&temperature_unit={temperatureUnit}"; - using (var httpClient = new HttpClient()) - { - try - { - HttpResponseMessage response = await httpClient.GetAsync(apiUrl); - if (response.IsSuccessStatusCode) - { - string json = await response.Content.ReadAsStringAsync(); - openMeteoResponse = JsonSerializer.Deserialize(json); - } - } - catch - { - //do nothing and return null - } - return openMeteoResponse; - } - } -} - -public class CurrentWeather - { - public string? time { get; set; } - public int interval { get; set; } - public double temperature { get; set; } - public double windspeed { get; set; } - public int winddirection { get; set; } - public int is_day { get; set; } - public int weathercode { get; set; } - } - - public class CurrentWeatherUnits - { - public string? time { get; set; } - public string? interval { get; set; } - public string? temperature { get; set; } - public string? windspeed { get; set; } - public string? winddirection { get; set; } - public string? is_day { get; set; } - public string? weathercode { get; set; } - } - - public class Hourly - { - public List? time { get; set; } - public List? temperature_2m { get; set; } - } - - public class HourlyUnits - { - public string? time { get; set; } - public string? temperature_2m { get; set; } - } - - public class OpenMeteoResponse - { - public double latitude { get; set; } - public double longitude { get; set; } - public double generationtime_ms { get; set; } - public int utc_offset_seconds { get; set; } - public string? timezone { get; set; } - public string? timezone_abbreviation { get; set; } - public double elevation { get; set; } - public CurrentWeatherUnits? current_weather_units { get; set; } - public CurrentWeather? current_weather { get; set; } - public HourlyUnits? hourly_units { get; set; } - public Hourly? hourly { get; set; } - } \ No newline at end of file diff --git a/ImmichFrame/WmoWeatherInterpreter.cs b/ImmichFrame/WmoWeatherInterpreter.cs deleted file mode 100644 index 1b308ec..0000000 --- a/ImmichFrame/WmoWeatherInterpreter.cs +++ /dev/null @@ -1,336 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; - -class WmoWeatherInterpreter -{ - private static readonly string WeatherJson = @" - { - ""0"": { - ""day"": { - ""description"": ""Sunny"", - ""image"": ""http://openweathermap.org/img/wn/01d@2x.png"" - }, - ""night"": { - ""description"": ""Clear"", - ""image"": ""http://openweathermap.org/img/wn/01n@2x.png"" - } - }, - ""1"": { - ""day"": { - ""description"": ""Mainly Sunny"", - ""image"": ""http://openweathermap.org/img/wn/01d@2x.png"" - }, - ""night"": { - ""description"": ""Mainly Clear"", - ""image"": ""http://openweathermap.org/img/wn/01n@2x.png"" - } - }, - ""2"": { - ""day"": { - ""description"": ""Partly Cloudy"", - ""image"": ""http://openweathermap.org/img/wn/02d@2x.png"" - }, - ""night"": { - ""description"": ""Partly Cloudy"", - ""image"": ""http://openweathermap.org/img/wn/02n@2x.png"" - } - }, - ""3"": { - ""day"": { - ""description"": ""Cloudy"", - ""image"": ""http://openweathermap.org/img/wn/03d@2x.png"" - }, - ""night"": { - ""description"": ""Cloudy"", - ""image"": ""http://openweathermap.org/img/wn/03n@2x.png"" - } - }, - ""45"": { - ""day"": { - ""description"": ""Foggy"", - ""image"": ""http://openweathermap.org/img/wn/50d@2x.png"" - }, - ""night"": { - ""description"": ""Foggy"", - ""image"": ""http://openweathermap.org/img/wn/50n@2x.png"" - } - }, - ""48"": { - ""day"": { - ""description"": ""Rime Fog"", - ""image"": ""http://openweathermap.org/img/wn/50d@2x.png"" - }, - ""night"": { - ""description"": ""Rime Fog"", - ""image"": ""http://openweathermap.org/img/wn/50n@2x.png"" - } - }, - ""51"": { - ""day"": { - ""description"": ""Light Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Light Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""53"": { - ""day"": { - ""description"": ""Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""55"": { - ""day"": { - ""description"": ""Heavy Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Heavy Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""56"": { - ""day"": { - ""description"": ""Light Freezing Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Light Freezing Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""57"": { - ""day"": { - ""description"": ""Freezing Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Freezing Drizzle"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""61"": { - ""day"": { - ""description"": ""Light Rain"", - ""image"": ""http://openweathermap.org/img/wn/10d@2x.png"" - }, - ""night"": { - ""description"": ""Light Rain"", - ""image"": ""http://openweathermap.org/img/wn/10n@2x.png"" - } - }, - ""63"": { - ""day"": { - ""description"": ""Rain"", - ""image"": ""http://openweathermap.org/img/wn/10d@2x.png"" - }, - ""night"": { - ""description"": ""Rain"", - ""image"": ""http://openweathermap.org/img/wn/10n@2x.png"" - } - }, - ""65"": { - ""day"": { - ""description"": ""Heavy Rain"", - ""image"": ""http://openweathermap.org/img/wn/10d@2x.png"" - }, - ""night"": { - ""description"": ""Heavy Rain"", - ""image"": ""http://openweathermap.org/img/wn/10n@2x.png"" - } - }, - ""66"": { - ""day"": { - ""description"": ""Light Freezing Rain"", - ""image"": ""http://openweathermap.org/img/wn/10d@2x.png"" - }, - ""night"": { - ""description"": ""Light Freezing Rain"", - ""image"": ""http://openweathermap.org/img/wn/10n@2x.png"" - } - }, - ""67"": { - ""day"": { - ""description"": ""Freezing Rain"", - ""image"": ""http://openweathermap.org/img/wn/10d@2x.png"" - }, - ""night"": { - ""description"": ""Freezing Rain"", - ""image"": ""http://openweathermap.org/img/wn/10n@2x.png"" - } - }, - ""71"": { - ""day"": { - ""description"": ""Light Snow"", - ""image"": ""http://openweathermap.org/img/wn/13d@2x.png"" - }, - ""night"": { - ""description"": ""Light Snow"", - ""image"": ""http://openweathermap.org/img/wn/13n@2x.png"" - } - }, - ""73"": { - ""day"": { - ""description"": ""Snow"", - ""image"": ""http://openweathermap.org/img/wn/13d@2x.png"" - }, - ""night"": { - ""description"": ""Snow"", - ""image"": ""http://openweathermap.org/img/wn/13n@2x.png"" - } - }, - ""75"": { - ""day"": { - ""description"": ""Heavy Snow"", - ""image"": ""http://openweathermap.org/img/wn/13d@2x.png"" - }, - ""night"": { - ""description"": ""Heavy Snow"", - ""image"": ""http://openweathermap.org/img/wn/13n@2x.png"" - } - }, - ""77"": { - ""day"": { - ""description"": ""Snow Grains"", - ""image"": ""http://openweathermap.org/img/wn/13d@2x.png"" - }, - ""night"": { - ""description"": ""Snow Grains"", - ""image"": ""http://openweathermap.org/img/wn/13n@2x.png"" - } - }, - ""80"": { - ""day"": { - ""description"": ""Light Showers"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Light Showers"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""81"": { - ""day"": { - ""description"": ""Showers"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Showers"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""82"": { - ""day"": { - ""description"": ""Heavy Showers"", - ""image"": ""http://openweathermap.org/img/wn/09d@2x.png"" - }, - ""night"": { - ""description"": ""Heavy Showers"", - ""image"": ""http://openweathermap.org/img/wn/09n@2x.png"" - } - }, - ""85"": { - ""day"": { - ""description"": ""Light Snow Showers"", - ""image"": ""http://openweathermap.org/img/wn/13d@2x.png"" - }, - ""night"": { - ""description"": ""Light Snow Showers"", - ""image"": ""http://openweathermap.org/img/wn/13n@2x.png"" - } - }, - ""86"": { - ""day"": { - ""description"": ""Snow Showers"", - ""image"": ""http://openweathermap.org/img/wn/13d@2x.png"" - }, - ""night"": { - ""description"": ""Snow Showers"", - ""image"": ""http://openweathermap.org/img/wn/13n@2x.png"" - } - }, - ""95"": { - ""day"": { - ""description"": ""Thunderstorm"", - ""image"": ""http://openweathermap.org/img/wn/11d@2x.png"" - }, - ""night"": { - ""description"": ""Thunderstorm"", - ""image"": ""http://openweathermap.org/img/wn/11n@2x.png"" - } - }, - ""96"": { - ""day"": { - ""description"": ""Light Thunderstorms With Hail"", - ""image"": ""http://openweathermap.org/img/wn/11d@2x.png"" - }, - ""night"": { - ""description"": ""Light Thunderstorms With Hail"", - ""image"": ""http://openweathermap.org/img/wn/11n@2x.png"" - } - }, - ""99"": { - ""day"": { - ""description"": ""Thunderstorm With Hail"", - ""image"": ""http://openweathermap.org/img/wn/11d@2x.png"" - }, - ""night"": { - ""description"": ""Thunderstorm With Hail"", - ""image"": ""http://openweathermap.org/img/wn/11n@2x.png"" - } - } - }"; - - public class WeatherData - { - public class TimeOfDayData - { - [JsonPropertyName("description")] - public string? Description { get; set; } - } - - [JsonPropertyName("day")] - public TimeOfDayData Day { get; set; } - - [JsonPropertyName("night")] - public TimeOfDayData Night { get; set; } - public WeatherData() - { - Day = new TimeOfDayData(); - Night = new TimeOfDayData(); - } - } - public static string GetWeatherDescription(int code, bool isDay) - { - try - { - var weatherData = JsonSerializer.Deserialize>(WeatherJson); - - string timeOfDayKey = isDay ? "day" : "night"; - - if (weatherData!.TryGetValue(code.ToString(), out var codeData)) - { - WeatherData.TimeOfDayData timeData = isDay ? codeData.Day : codeData.Night; - - if (timeData != null) - { - return timeData.Description!; - } - } - } - catch - { - return "Weather description not found"; - } - - return "Weather description not found"; - } -}