Skip to content

Commit

Permalink
Fixed issue with sitemap rendering
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Hofmann <oss@hoffe.org>
  • Loading branch information
hoffe86 committed Jan 19, 2024
1 parent ebfa0bb commit 83111d5
Show file tree
Hide file tree
Showing 14 changed files with 897 additions and 704 deletions.
1 change: 1 addition & 0 deletions src/openHAB.Core.Client/Contracts/IOpenHABClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public interface IOpenHABClient
/// Starts listening to server events.
/// </summary>
void StartItemUpdates(System.Threading.CancellationToken token);
Task<OpenHABSitemap> GetSitemap(string sitemapLink, OpenHABVersion version);
}
}
58 changes: 57 additions & 1 deletion src/openHAB.Core.Client/OpenHABClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,18 @@ public async Task<ICollection<OpenHABWidget>> LoadItemsFromSitemap(string sitema
if (version == OpenHABVersion.Two || version == OpenHABVersion.Three || version == OpenHABVersion.Four)
{
JObject jsonObject = JObject.Parse(resultString);
items = JsonConvert.DeserializeObject<List<OpenHABWidget>>(jsonObject["homepage"]["widgets"].ToString());

string content = string.Empty;
if (jsonObject["homepage"] == null)
{
content = jsonObject["widgets"].ToString();
}
else
{
content = jsonObject["homepage"]["widgets"].ToString();
}

items = JsonConvert.DeserializeObject<List<OpenHABWidget>>(content);
}
else
{
Expand All @@ -105,6 +116,51 @@ public async Task<ICollection<OpenHABWidget>> LoadItemsFromSitemap(string sitema
}
}

public async Task<OpenHABSitemap> GetSitemap(string sitemapLink, OpenHABVersion version)
{
try
{
_logger.LogInformation($"Get sitemap by link '{sitemapLink}'");

HttpResponseMessage result = await _openHABHttpClient.Client(_connection).GetAsync(sitemapLink).ConfigureAwait(false);
if (!result.IsSuccessStatusCode)
{
_logger.LogError($"Http request for loading sitemap failed, ErrorCode:'{result.StatusCode}'");
throw new OpenHABException($"{result.StatusCode} received from server");
}

string resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

OpenHABSitemap sitemap = null;
if (version == OpenHABVersion.Two || version == OpenHABVersion.Three || version == OpenHABVersion.Four)
{
JObject jsonObject = JObject.Parse(resultString);
sitemap = JsonConvert.DeserializeObject<OpenHABSitemap>(jsonObject["homepage"].ToString());
}
else
{
string message = "openHAB version is not supported.";
_logger.LogError(message);
throw new OpenHABException(message);
}

_logger.LogInformation($"Loaded '{sitemap.Name}' sitemap successfully from server");

return sitemap;
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "GetSitemap failed.");
throw new OpenHABException("Invalid call", ex);
}
catch (Exception ex)
{
_logger.LogError(ex, "GetSitemap failed.");
throw new OpenHABException("Invalid call", ex);
}
}


/// <inheritdoc />
public async Task<OpenHABItem> GetItemByName(string itemName)
{
Expand Down
76 changes: 67 additions & 9 deletions src/openHAB.Core/Services/SitemapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,68 @@

namespace openHAB.Core.Services
{
/// <summary>
/// Service for managing sitemaps in openHAB.
/// </summary>
public class SitemapService
{
private readonly ISettingsService _settingsService;
private readonly IOpenHABClient _openHABClient;
private readonly ILogger<SitemapService> _logger;
private readonly IOpenHABClient _openHABClient;
private readonly ISettingsService _settingsService;
private ServerInfo _serverInfo;

/// <summary>
/// Initializes a new instance of the <see cref="SitemapService"/> class.
/// </summary>
/// <param name="settingsService">The settings service.</param>
/// <param name="openHABClient">The openHAB client.</param>
/// <param name="logger">The logger.</param>
public SitemapService(ISettingsService settingsService, IOpenHABClient openHABClient, ILogger<SitemapService> logger)
{
_settingsService = settingsService;
_openHABClient = openHABClient;
_logger = logger;
}

public async Task<List<OpenHABSitemap>> GetSitemaps(CancellationToken loadCancellationToken)
/// <summary>
/// Gets the sitemap by URL.
/// </summary>
/// <param name="sitemapUrl">The sitemap URL.</param>
/// <returns>The <see cref="OpenHABSitemap"/> object representing the sitemap.</returns>
public async Task<OpenHABSitemap> GetSitemapByUrlAsync(string sitemapUrl)
{
try
{
_serverInfo = await InitalizeConnectionAsync();
if (_serverInfo == null)
{
return null;
}
_settingsService.ServerVersion = _serverInfo.Version;

OpenHABSitemap sitemap = await _openHABClient.GetSitemap(sitemapUrl, _serverInfo.Version).ConfigureAwait(false);
return sitemap;
}
catch (OpenHABException ex)
{
_logger.LogError(ex, $"Loading sitemap {sitemapUrl} failed.");
StrongReferenceMessenger.Default.Send(new ConnectionErrorMessage(ex.Message));
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, "Load sitemap data failed.");
}

return null;
}

/// <summary>
/// Gets the list of sitemaps.
/// </summary>
/// <param name="loadCancellationToken">The cancellation token for the load operation.</param>
/// <returns>The list of <see cref="OpenHABSitemap"/> objects representing the sitemaps.</returns>
public async Task<List<OpenHABSitemap>> GetSitemapsAsync(CancellationToken loadCancellationToken)
{
try
{
Expand Down Expand Up @@ -73,12 +120,29 @@ public async Task<List<OpenHABSitemap>> GetSitemaps(CancellationToken loadCancel
return null;
}

/// <summary>
/// Loads the items from a sitemap.
/// </summary>
/// <param name="model">The sitemap model.</param>
/// <returns>The collection of <see cref="OpenHABWidget"/> objects representing the items.</returns>
public async Task<ICollection<OpenHABWidget>> LoadItemsFromSitemapAsync(OpenHABSitemap model)
{
ICollection<OpenHABWidget> widgetModels = await _openHABClient.LoadItemsFromSitemap(model.Link, _serverInfo.Version).ConfigureAwait(false);
return widgetModels;
}

/// <summary>
/// Sends a command to an item.
/// </summary>
/// <param name="item">The item to send the command to.</param>
/// <param name="command">The command to send.</param>
/// <returns>The <see cref="HttpResponseResult{T}"/> object representing the result of the command.</returns>
public async Task<HttpResponseResult<bool>> SendItemCommand(OpenHABItem item, string command)
{
HttpResponseResult<bool> result = await _openHABClient.SendCommand(item, command).ConfigureAwait(false);
return result;
}

private async Task<ServerInfo> InitalizeConnectionAsync()
{
Settings settings = _settingsService.Load();
Expand Down Expand Up @@ -108,11 +172,5 @@ private async Task<ServerInfo> InitalizeConnectionAsync()

return serverInfo;
}

public async Task<HttpResponseResult<bool>> SendItemCommand(OpenHABItem item, string command)
{
HttpResponseResult<bool> result = await _openHABClient.SendCommand(item, command).ConfigureAwait(false);
return result;
}
}
}
25 changes: 13 additions & 12 deletions src/openHAB.Windows/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.Windows.AppLifecycle;
using openHAB.Core.Notification.Contracts;
using openHAB.Core.Services.Contracts;
Expand Down Expand Up @@ -81,21 +82,21 @@ protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventA

// Initialize MainWindow here
MainWindow = new MainWindow();
//Frame rootFrame = MainWindow.RootFrame;

Frame rootFrame = MainWindow.Content as Frame;
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
//if (rootFrame == null)
//{
// // Create a Frame to act as the navigation context and navigate to the first page
// rootFrame = new Frame();

// Place the frame in the current Window
MainWindow.Content = rootFrame;
}
// // Place the frame in the current Window
// MainWindow.Content = rootFrame;
//}

if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
//if (rootFrame.Content == null)
//{
// rootFrame.Navigate(typeof(MainPage), e.Arguments);
//}

MainWindow.Activate();
}
Expand Down
Loading

0 comments on commit 83111d5

Please sign in to comment.