Skip to content

Commit

Permalink
Merge pull request #52 from 3rob3/dev.jw.weatherapi
Browse files Browse the repository at this point in the history
Remove old weather api and replace with openweathermap
  • Loading branch information
3rob3 authored Apr 11, 2024
2 parents 1f41972 + cc667fb commit c3455e6
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 481 deletions.
12 changes: 6 additions & 6 deletions ImmichFrame/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@
<setting name="ImageDescFontSize" serializeAs="String">
<value>36</value>
</setting>
<setting name="ShowWeather" serializeAs="String">
<value>True</value>
</setting>
<setting name="WeatherFontSize" serializeAs="String">
<value>12</value>
</setting>
<setting name="WeatherUnits" serializeAs="String">
<value>fahrenheit</value>
</setting>
<setting name="WeatherLatLong" serializeAs="String">
<value>41.186660,-81.800830</value>
</setting>
<setting name="TransitionDuration" serializeAs="String">
<value>1</value>
</setting>
<setting name="UnitSystem" serializeAs="String">
<value>imperial</value>
</setting>
<setting name="WeatherApiKey" serializeAs="String">
<value />
</setting>
</ImmichFrame.Properties.Settings>
</userSettings>
</configuration>
40 changes: 40 additions & 0 deletions ImmichFrame/Helpers/WeatherHelper.cs
Original file line number Diff line number Diff line change
@@ -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<WeatherInfo?> GetWeather()
{
var settings = Settings.CurrentSettings;
return GetWeather(settings.WeatherLat, settings.WeatherLong);
}
public static async Task<WeatherInfo?> 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;
}
}
}
1 change: 1 addition & 0 deletions ImmichFrame/ImmichFrame.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="OpenWeatherMap.API" Version="2.0.50" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
<PackageReference Include="ThumbHash" Version="2.1.1" />
</ItemGroup>
Expand Down
22 changes: 15 additions & 7 deletions ImmichFrame/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -235,6 +239,7 @@ private static Settings ParseSettings(Dictionary<string, object> SettingsValues)
property.SetValue(settings, url);
break;
case "ApiKey":
case "WeatherApiKey":
property.SetValue(settings, value);
break;
case "Albums":
Expand Down Expand Up @@ -269,7 +274,6 @@ private static Settings ParseSettings(Dictionary<string, object> 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);
Expand All @@ -280,9 +284,10 @@ private static Settings ParseSettings(Dictionary<string, object> 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":
Expand All @@ -291,6 +296,9 @@ private static Settings ParseSettings(Dictionary<string, object> 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}')");
}
Expand Down
48 changes: 24 additions & 24 deletions ImmichFrame/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions ImmichFrame/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@
<Setting Name="ImageDescFontSize" Type="System.Int32" Scope="User">
<Value Profile="(Default)">36</Value>
</Setting>
<Setting Name="ShowWeather" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="WeatherFontSize" Type="System.Int32" Scope="User">
<Value Profile="(Default)">12</Value>
</Setting>
<Setting Name="WeatherUnits" Type="System.String" Scope="User">
<Value Profile="(Default)">fahrenheit</Value>
</Setting>
<Setting Name="WeatherLatLong" Type="System.String" Scope="User">
<Value Profile="(Default)">41.186660,-81.800830</Value>
</Setting>
Expand All @@ -65,5 +59,11 @@
<Setting Name="TransitionDuration" Type="System.Double" Scope="User">
<Value Profile="(Default)">1</Value>
</Setting>
<Setting Name="UnitSystem" Type="System.String" Scope="User">
<Value Profile="(Default)">imperial</Value>
</Setting>
<Setting Name="WeatherApiKey" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
20 changes: 12 additions & 8 deletions ImmichFrame/Settings.example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
<Person>First Person UID</Person>
<Person>Second Person UID</Person>
</People>

<!--Interval for photo cycling in seconds-->
<Interval>8</Interval>

<!--Photo fade in/out time in seconds-->
<TransitionDuration>1.0</TransitionDuration>

<!--Determines if images should be saved locally-->
<DownloadImages>false</DownloadImages>

<!--Show todays memories-->
<ShowMemories>false</ShowMemories>

Expand All @@ -48,11 +48,15 @@
<ShowImageDesc>true</ShowImageDesc>
<ImageDescFontSize>36</ImageDescFontSize>

<!--Show the current weather-->
<ShowWeather>false</ShowWeather>
<WeatherFontSize>12</WeatherFontSize>
<!--The temperature units. either celsius or fahrenheit-->
<WeatherUnits>fahrenheit</WeatherUnits>
<!--The current weather is only shown, when a WeatherApiKey is specified-->
<!--Weather ApiKey from https://openweathermap.org/-->
<!--<WeatherApiKey>xxx</WeatherApiKey>-->
<WeatherFontSize>12</WeatherFontSize>
<!--The temperature units. either metric or imperial-->
<UnitSystem>imperial</UnitSystem>
<!--Your latitude/longitude coordinates (ex. 40.7128,74.0060)-->
<WeatherLatLong>40.7128,74.0060</WeatherLatLong>

<!--Your ISO2 Language Code-->
<Language>en</Language>
</Settings>
12 changes: 5 additions & 7 deletions ImmichFrame/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace ImmichFrame.Views;
Expand Down Expand Up @@ -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))}";
}
}

Expand Down
16 changes: 10 additions & 6 deletions ImmichFrame/Views/SettingsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,21 @@
<CheckBox IsChecked="{Binding Settings.ShowWeather}" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="WeatherFontSize" VerticalAlignment="Center" Margin="10"/>
<TextBox Text="{Binding Settings.ShowWeather}" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="WeatherUnits" VerticalAlignment="Center" Margin="10"/>
<TextBox Text="{Binding Settings.WeatherUnits}" VerticalAlignment="Center" Margin="10"/>
<TextBlock Text="UnitSystem" VerticalAlignment="Center" Margin="10"/>
<TextBox Text="{Binding Settings.UnitSystem}" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="WeatherLatLong" VerticalAlignment="Center" Margin="10"/>
<TextBox Text="{Binding Settings.WeatherLatLong}" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="WeatherApiKey" VerticalAlignment="Center" Margin="10"/>
<TextBox Text="{Binding Settings.WeatherApiKey}" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Language" VerticalAlignment="Center" Margin="10"/>
<TextBox Text="{Binding Settings.Language}" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
</StackPanel>
</ScrollViewer>
</DockPanel>
Expand Down
Loading

0 comments on commit c3455e6

Please sign in to comment.