Skip to content

Commit

Permalink
The most significant changes in the code include the update of projec…
Browse files Browse the repository at this point in the history
…t reference for "openHAB.Common", removal of namespaces from "NotificationManager.cs", addition of a new parameter in "ResolveIconPath" method, and the inclusion of a new dependency in "IconCaching.cs". Several converters were removed from "App.xaml" and usage of certain converters was removed from multiple files. The logic for resolving the icon path was updated in "ImageLabel.xaml.cs" and a boolean return type was added to "DownloadAndSaveIconToCache" method in "IconCaching.cs". Code formatting and alignment changes were made in multiple files for better readability. The type of `Widget` property was changed in `WidgetBase.cs` and the logic for generating the icon path was simplified in `IconToBitmapConverter.cs`. The `Visibility` property binding was simplified in all `.xaml` files. The `IconToPathConverter.cs`, `StringToColorBrushConverter.cs` files were removed from the project. Various UI elements were updated in `MainWindow.xaml` and a `nativeDebugging` property was added to the `openHAB.Windows (Package)` profile in `launchSettings.json`. A new file `WidgetViewModel.cs` was added to the project.

Here is a list of the changes:

1. The project reference for "openHAB.Common" was updated to use a different project type GUID.
2. The "openHAB.Core.Client" and "openHAB.Core.Common" namespaces were removed from the "NotificationManager.cs" file.
3. The method signature for "ResolveIconPath" in "IIconCaching.cs" was updated to include a new parameter for "state".
4. The "IconCaching.cs" file was updated to include a new dependency on "ISettingsService", and the "ResolveIconPath" method was updated to construct the icon URL based on the server version and state.
5. Several converters were removed from "App.xaml".
6. The "ChartWidget.xaml", "ColorWidget.xaml", "FrameWidget.xaml", "ImageWidget.xaml", "MapViewWidget.xaml", "MjpegWidget.xaml", "PageLinkWidget.xaml", "RollershutterWidget.xaml", and "SectionSwitchWidget.xaml" files were updated to remove usage of the "BooleanToVisibilityConverter", "IconToPathConverter", and "StringToColorConverter". The "IconPath" property is now directly bound to the "IconPath" property of the widget.
7. The "ImageLabel.xaml.cs" file was updated to remove the dependency on "IIconCaching" and to update the logic for resolving the icon path.
8. The "DownloadAndSaveIconToCache" method in "IconCaching.cs" was updated to return a boolean indicating whether the download was successful.
9. Code formatting and alignment changes were made in multiple files for better readability.
10. The type of `Widget` property was changed in `WidgetBase.cs` and the logic for generating the icon path was simplified in `IconToBitmapConverter.cs`.
11. The `Visibility` property binding was simplified in all `.xaml` files.
12. The `IconToPathConverter.cs`, `StringToColorBrushConverter.cs` files were removed from the project.
13. Various UI elements were updated in `MainWindow.xaml` and a `nativeDebugging` property was added to the `openHAB.Windows (Package)` profile in `launchSettings.json`.
14. A new file `WidgetViewModel.cs` was added to the project.
  • Loading branch information
hoffe86 committed Mar 15, 2024
1 parent 12c6fd4 commit fbb48d5
Show file tree
Hide file tree
Showing 34 changed files with 844 additions and 683 deletions.
2 changes: 1 addition & 1 deletion OpenHAB.Windows.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "openHAB.Windows", "src\open
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "openHAB.Core.Client", "src\openHAB.Core.Client\openHAB.Core.Client.csproj", "{3B8F31DD-1AF2-47CF-B7DB-3B732FE4EC5A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "openHAB.Common", "src\openHAB.Common\openHAB.Common.csproj", "{E53B599D-4BDB-40B1-86E4-47FB90420138}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "openHAB.Common", "src\openHAB.Common\openHAB.Common.csproj", "{E53B599D-4BDB-40B1-86E4-47FB90420138}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
6 changes: 2 additions & 4 deletions src/openHAB.Core/Notification/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Toolkit.Uwp.Notifications;
using openHAB.Common;
using openHAB.Core.Client;
using openHAB.Core.Client.Messages;
using openHAB.Core.Client.Models;
using openHAB.Core.Common;
using openHAB.Core.Model;
using openHAB.Core.Notification.Contracts;
using openHAB.Core.Services.Contracts;
Expand Down Expand Up @@ -53,8 +51,8 @@ private async void HandleUpdateItemMessage(object receipts, ItemStateChangedMess
string state = item?.State ?? "ON";
state = HttpUtility.UrlEncode(state);

itemImage = $"{OpenHABHttpClient.BaseUrl}icon/{item?.Category?.ToLower()}?state={state}&format={_iconFormat}";
itemImage = await _iconCaching.ResolveIconPath(itemImage, _iconFormat);
string icon = item?.Category?.ToLower();
itemImage = await _iconCaching.ResolveIconPath(icon, state, _iconFormat);
}

TriggerToastNotificationForItem(itemName, itemImage, obj.Value, obj.OldValue);
Expand Down
3 changes: 2 additions & 1 deletion src/openHAB.Core/Services/Contracts/IIconCaching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public interface IIconCaching
/// <summary>Resolves the path to local cached icon file.<br />
/// If the icon is not present in cache the file will be downloaded from OpenHAB server.</summary>
/// <param name="iconUrl">The icon URL.</param>
/// <param name="state"> Item state. </param>
/// <param name="iconFormat">The icon format.</param>
/// <returns>Path to icon file in local cache.</returns>
Task<string> ResolveIconPath(string iconUrl, string iconFormat);
Task<string> ResolveIconPath(string iconUrl, string state, string iconFormat);
}
}
28 changes: 21 additions & 7 deletions src/openHAB.Core/Services/IconCaching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Services.Description;
using Microsoft.Extensions.Logging;
using openHAB.Core.Client;
using openHAB.Core.Client.Connection.Contracts;
Expand All @@ -18,6 +19,7 @@ public class IconCaching : IIconCaching
{
private readonly OpenHABHttpClient _openHABHttpClient;
private readonly IConnectionService _connectionService;
private readonly ISettingsService _settingsService;
private readonly Settings _settings;
private readonly AppPaths _applicationContext;
private readonly ILogger<IconCaching> _logger;
Expand All @@ -40,14 +42,22 @@ public IconCaching(
_logger = logger;
_openHABHttpClient = openHABHttpClient;
_connectionService = connectionService;
_settingsService = settingsService;
_settings = settingsService.Load();

_applicationContext = appPaths;
}

/// <inheritdoc/>
public async Task<string> ResolveIconPath(string iconUrl, string iconFormat)
public async Task<string> ResolveIconPath(string icon, string state, string iconFormat)
{
string serverUrl = _connectionService.CurrentConnection.Url;
OpenHABVersion openHABVersion = _settingsService.ServerVersion;

string iconUrl = openHABVersion == OpenHABVersion.Two || openHABVersion == OpenHABVersion.Three || openHABVersion == OpenHABVersion.Four ?
$"{serverUrl}icon/{icon}?state={state}&format={iconFormat}" :
$"{serverUrl}images/{icon}.png";

try
{
Match iconName = Regex.Match(iconUrl, "icon/[0-9a-zA-Z]*", RegexOptions.None, TimeSpan.FromMilliseconds(100));
Expand All @@ -70,16 +80,18 @@ public async Task<string> ResolveIconPath(string iconUrl, string iconFormat)

DirectoryInfo iconDirectory = EnsureIconCacheFolder();

string iconFileName = $"{iconName.Value.Replace("icon/", string.Empty)}{iconState.Value.Replace("state=", string.Empty)}.{iconFormat}";
//string iconFileName = $"{iconName.Value.Replace("icon/", string.Empty)}{iconState.Value.Replace("state=", string.Empty)}.{iconFormat}";
string iconFileName = $"{iconName.Value.Replace("icon/", string.Empty)}.{iconFormat}";
string iconFilePath = Path.Combine(iconDirectory.FullName, iconFileName).Replace("NULL", string.Empty);

if (File.Exists(iconFilePath))
{
return iconFilePath;
}

await DownloadAndSaveIconToCache(iconUrl, iconFilePath);
return iconFilePath;
bool downloadSuccessfull = await DownloadAndSaveIconToCache(iconUrl, iconFilePath);

return downloadSuccessfull ? iconFilePath : iconUrl;
}
catch (Exception ex)
{
Expand All @@ -88,15 +100,15 @@ public async Task<string> ResolveIconPath(string iconUrl, string iconFormat)
}
}

private async Task DownloadAndSaveIconToCache(string iconUrl, string iconFilePath)
private async Task<bool> DownloadAndSaveIconToCache(string iconUrl, string iconFilePath)
{
bool isRunningInDemoMode = _settings.IsRunningInDemoMode.HasValue && _settings.IsRunningInDemoMode.Value;
Connection connection = await _connectionService.DetectAndRetriveConnection(_settings.LocalConnection, _settings.RemoteConnection, isRunningInDemoMode)
.ConfigureAwait(false);
if (connection == null)
{
_logger.LogError("Failed to retrieve connection details to download icon");
return;
return false;
}

using (HttpClient httpClient = _openHABHttpClient.DisposableClient(connection))
Expand All @@ -106,14 +118,16 @@ private async Task DownloadAndSaveIconToCache(string iconUrl, string iconFilePat
if (!httpResponse.IsSuccessStatusCode)
{
_logger.LogWarning($"Failed to download icon from '{iconUrl}' with status code '{httpResponse.StatusCode}'");
return;
return false;
}

byte[] iconContent = await httpResponse.Content.ReadAsByteArrayAsync();
using (FileStream file = File.Create(iconFilePath))
{
await file.WriteAsync(iconContent, 0, iconContent.Length);
}

return true;
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/openHAB.Windows/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="Styles/DefaultTheme.xaml" />
</ResourceDictionary.MergedDictionaries>

<converters:StateToBoolConverter x:Key="StateToBoolConverter" />
<converters:IconToPathConverter x:Key="IconToPathConverter" />
<converters:StringToColorBrushConverter x:Key="StringToColorConverter" />
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

<DataTemplate x:Key="FrameTemplate">
<controls:FrameWidget Widget="{Binding Mode=OneWay}" />
</DataTemplate>
Expand Down
77 changes: 37 additions & 40 deletions src/openHAB.Windows/Controls/ChartWidget.xaml
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
<local:WidgetBase x:Class="openHAB.Windows.Controls.ChartWidget"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:openHAB.Windows.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Visibility="{x:Bind Widget.Visibility, Converter={StaticResource BooleanToVisibilityConverter}}">

<Grid Style="{StaticResource Widget}"
Tapped="ImageWidget_OnTapped">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<local:ImageLabel Grid.Column="0"
IconPath="{x:Bind Widget, Mode=OneWay, Converter={StaticResource IconToPathConverter}}"
LabelText="{x:Bind Widget.Label, Mode=OneWay}"
LabelForeground="{x:Bind Widget.LabelColor,Converter={StaticResource StringToColorConverter}, Mode=OneWay}"/>

<ContentDialog x:Name="PopupDialog"
Title="{x:Bind Widget.Label}"
Grid.Column="0"
Grid.ColumnSpan="2"
Background="{StaticResource OpenHABLightColor}"
IsPrimaryButtonEnabled="True"
PrimaryButtonText="Close">
<Image x:Name="FullImage"
HorizontalAlignment="Right"
Source="http://demo.openhab.org:8080/chart?groups=Weather_Chart&amp;period=w"
Stretch="Uniform" />
</ContentDialog>

<Image x:Name="ThumbImage"
Grid.Column="1"
Margin="8"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Stretch="UniformToFill" />
</Grid>
</local:WidgetBase>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:openHAB.Windows.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Visibility="{x:Bind Widget.Visibility}">
<Grid Style="{StaticResource Widget}"
Tapped="ImageWidget_OnTapped">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<local:ImageLabel Grid.Column="0"
IconPath="{x:Bind Widget.IconPath, Mode=OneWay}"
LabelText="{x:Bind Widget.Label, Mode=OneWay}"
LabelForeground="{x:Bind Widget.LabelColor, Mode=OneWay}" />
<ContentDialog x:Name="PopupDialog"
Title="{x:Bind Widget.Label}"
Grid.Column="0"
Grid.ColumnSpan="2"
Background="{StaticResource OpenHABLightColor}"
IsPrimaryButtonEnabled="True"
PrimaryButtonText="Close">
<Image x:Name="FullImage"
HorizontalAlignment="Right"
Source="http://demo.openhab.org:8080/chart?groups=Weather_Chart&amp;period=w"
Stretch="Uniform" />
</ContentDialog>
<Image x:Name="ThumbImage"
Grid.Column="1"
Margin="8"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Stretch="UniformToFill" />
</Grid>
</local:WidgetBase>
9 changes: 4 additions & 5 deletions src/openHAB.Windows/Controls/ColorWidget.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<local:WidgetBase x:Class="openHAB.Windows.Controls.ColorWidget"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:WinRTXamlToolkit.Controls"
xmlns:converters="using:openHAB.Windows.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:openHAB.Windows.Controls"
Expand All @@ -10,7 +9,7 @@
d:DesignWidth="400"
VariableSizedWrapGrid.ColumnSpan="2"
mc:Ignorable="d"
Visibility="{x:Bind Widget.Visibility, Converter={StaticResource BooleanToVisibilityConverter}}">
Visibility="{x:Bind Widget.Visibility}">

<Grid Style="{StaticResource Widget}">
<Grid.ColumnDefinitions>
Expand All @@ -19,9 +18,9 @@
</Grid.ColumnDefinitions>

<local:ImageLabel VerticalAlignment="Center"
IconPath="{x:Bind Widget, Converter={StaticResource IconToPathConverter}, Mode=OneWay}"
LabelText="{x:Bind Widget.Label, Mode=OneWay}"
LabelForeground="{x:Bind Widget.LabelColor,Converter={StaticResource StringToColorConverter}, Mode=OneWay}"/>
IconPath="{x:Bind Widget.IconPath, Mode=OneWay}"
LabelText="{x:Bind Widget.Label, Mode=OneWay}"
LabelForeground="{x:Bind Widget.LabelColor, Mode=OneWay}"/>

<Grid Grid.Column="1" HorizontalAlignment="Right" Margin="0,0,10,0">
<Grid.ColumnDefinitions>
Expand Down
2 changes: 1 addition & 1 deletion src/openHAB.Windows/Controls/FrameWidget.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d"
Visibility="{x:Bind Widget.Visibility, Converter={StaticResource BooleanToVisibilityConverter}}">
Visibility="{x:Bind Widget.Visibility}">

<local:WidgetBase.Resources>
<Style TargetType="GridViewItem">
Expand Down
19 changes: 3 additions & 16 deletions src/openHAB.Windows/Controls/ImageLabel.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Text.RegularExpressions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using openHAB.Core.Services.Contracts;
using openHAB.Windows.Services;
using System;
using System.Text.RegularExpressions;

namespace openHAB.Windows.Controls
{
Expand Down Expand Up @@ -38,19 +36,8 @@ private static async void IconChangedCallback(DependencyObject dependencyObject,
return;
}

// Fix IconPathState by removing empty space and special characters
string iconPath = control.IconPath;

Match format = Regex.Match(iconPath, @"format=svg", RegexOptions.None, TimeSpan.FromMilliseconds(100));
Match state = Regex.Match(iconPath, @"state=(.+?)&", RegexOptions.None, TimeSpan.FromMilliseconds(100));
if (state != null && !string.IsNullOrEmpty(state.Value))
{
string newstate = Regex.Replace(state.Groups[1].Value, "[^0-9a-zA-Z.&]", string.Empty, RegexOptions.None, TimeSpan.FromMilliseconds(100));
iconPath = control.IconPath.Replace(state.Groups[1].Value, newstate, StringComparison.InvariantCulture);
}

IIconCaching iconCaching = DIService.Instance.GetService<IIconCaching>();
iconPath = await iconCaching.ResolveIconPath(iconPath, format.Success ? "svg" : "png");
Match format = Regex.Match(iconPath, @".svg", RegexOptions.None, TimeSpan.FromMilliseconds(100));

if (format.Success)
{
Expand Down
77 changes: 37 additions & 40 deletions src/openHAB.Windows/Controls/ImageWidget.xaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
<local:WidgetBase x:Class="openHAB.Windows.Controls.ImageWidget"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:openHAB.Windows.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d"
Visibility="{x:Bind Widget.Visibility, Converter={StaticResource BooleanToVisibilityConverter}}">

<Grid Style="{StaticResource Widget}"
Tapped="ImageWidget_OnTapped">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<local:ImageLabel Grid.Column="0"
IconPath="{x:Bind Widget, Mode=OneWay, Converter={StaticResource IconToPathConverter}}"
LabelText="{x:Bind Widget.Label, Mode=OneWay}"
LabelForeground="{x:Bind Widget.LabelColor,Converter={StaticResource StringToColorConverter}, Mode=OneWay}"/>

<ContentDialog x:Name="PopupDialog"
Title="{x:Bind Widget.Label}"
Grid.Column="0"
Grid.ColumnSpan="2"
Background="{StaticResource OpenHABLightColor}"
IsPrimaryButtonEnabled="True"
PrimaryButtonText="Close">
<Image x:Name="FullImage"
HorizontalAlignment="Right"
Stretch="Uniform" />
</ContentDialog>

<Image x:Name="ThumbImage"
Grid.Column="1"
Margin="8"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Stretch="UniformToFill" />
</Grid>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:openHAB.Windows.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignHeight="300"
d:DesignWidth="400"
mc:Ignorable="d"
Visibility="{x:Bind Widget.Visibility}">
<Grid Style="{StaticResource Widget}"
Tapped="ImageWidget_OnTapped">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<local:ImageLabel Grid.Column="0"
IconPath="{x:Bind Widget.IconPath, Mode=OneWay}"
LabelText="{x:Bind Widget.Label, Mode=OneWay}"
LabelForeground="{x:Bind Widget.LabelColor, Mode=OneWay}" />
<ContentDialog x:Name="PopupDialog"
Title="{x:Bind Widget.Label}"
Grid.Column="0"
Grid.ColumnSpan="2"
Background="{StaticResource OpenHABLightColor}"
IsPrimaryButtonEnabled="True"
PrimaryButtonText="Close">
<Image x:Name="FullImage"
HorizontalAlignment="Right"
Stretch="Uniform" />
</ContentDialog>
<Image x:Name="ThumbImage"
Grid.Column="1"
Margin="8"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Stretch="UniformToFill" />
</Grid>
</local:WidgetBase>
2 changes: 1 addition & 1 deletion src/openHAB.Windows/Controls/MapViewWidget.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
Visibility="{x:Bind Widget.Visibility, Converter={StaticResource BooleanToVisibilityConverter}}">
Visibility="{x:Bind Widget.Visibility}">

<Grid Style="{StaticResource Widget}"
Tapped="OnTapped">
Expand Down
Loading

0 comments on commit fbb48d5

Please sign in to comment.