From 6c0b698e71791a110cee4744d069c7ce6b5542b7 Mon Sep 17 00:00:00 2001 From: Christoph Hofmann Date: Wed, 14 Jul 2021 20:07:01 +0200 Subject: [PATCH] Removed OH 1 support form REST client #178 Signed-off-by: Christoph Hofmann --- .../Model/OpenHABWidgetFactory.cs | 101 ------------------ src/openHAB.Core/Openhab.Core.csproj | 1 - src/openHAB.Core/SDK/OpenHABClient.cs | 46 ++------ 3 files changed, 9 insertions(+), 139 deletions(-) delete mode 100644 src/openHAB.Core/Model/OpenHABWidgetFactory.cs diff --git a/src/openHAB.Core/Model/OpenHABWidgetFactory.cs b/src/openHAB.Core/Model/OpenHABWidgetFactory.cs deleted file mode 100644 index fc8ba452..00000000 --- a/src/openHAB.Core/Model/OpenHABWidgetFactory.cs +++ /dev/null @@ -1,101 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Runtime.CompilerServices; -using System.Xml.Linq; -using Newtonsoft.Json; - -namespace OpenHAB.Core.Model -{ - /// - /// Factory class for OpenHAB widgets. - /// - public class OpenHABWidgetFactory - { - public static OpenHABWidget Parse(XElement startNode) - { - return ParseNode(startNode); - } - - private static OpenHABWidget ParseNode(XElement startNode) - { - if (!startNode.HasElements) - { - return null; - } - - OpenHABWidget widget = new OpenHABWidget() - { - WidgetId = startNode.Element("widgetId")?.Value, - Type = startNode.Element("type")?.Value, - Label = startNode.Element("label")?.Value, - State = startNode.Element("state")?.Value, - Icon = startNode.Element("icon")?.Value, - Url = startNode.Element("url")?.Value, - Children = new List(), - }; - - XElement linkedPage = startNode.Element("linkedPage"); - if (linkedPage != null) - { - ParseLinkedPage(linkedPage, widget); - } - - ParseItem(startNode.Element("item"), widget); - ParseChildren(startNode, widget); - ParseMappings(startNode, widget); - - return widget; - } - - private static void ParseMappings(XElement startNode, OpenHABWidget widget) - { - widget.Mappings = new List(); - - foreach (XElement childNode in startNode.Elements("mapping")) - { - string command = childNode.Element("command")?.Value; - string label = childNode.Element("label")?.Value; - widget.Mappings.Add(new OpenHABWidgetMapping(command, label)); - } - } - - private static void ParseLinkedPage(XElement linkedPage, OpenHABWidget parentWidget) - { - parentWidget.LinkedPage = new OpenHABSitemap(linkedPage) { Widgets = new List() }; - foreach (XElement childNode in linkedPage.Elements("widget")) - { - var widget = Parse(childNode); - widget.Parent = parentWidget; - widget.LinkedPage.Widgets.Add(widget); - } - } - - private static void ParseChildren(XElement startNode, OpenHABWidget parentWidget) - { - foreach (XElement childNode in startNode.Elements("widget")) - { - var widget = Parse(childNode); - widget.Parent = parentWidget; - widget.Children.Add(widget); - - XElement linkedPage = childNode.Element("linkedPage"); - - if (linkedPage != null) - { - ParseLinkedPage(linkedPage, widget); - } - } - } - - private static void ParseItem(XElement element, OpenHABWidget widget) - { - if (element == null) - { - return; - } - - widget.Item = new OpenHABItem(element); - } - } -} diff --git a/src/openHAB.Core/Openhab.Core.csproj b/src/openHAB.Core/Openhab.Core.csproj index cb68cc2f..080f7014 100644 --- a/src/openHAB.Core/Openhab.Core.csproj +++ b/src/openHAB.Core/Openhab.Core.csproj @@ -145,7 +145,6 @@ - diff --git a/src/openHAB.Core/SDK/OpenHABClient.cs b/src/openHAB.Core/SDK/OpenHABClient.cs index 0d78751c..43a8199b 100644 --- a/src/openHAB.Core/SDK/OpenHABClient.cs +++ b/src/openHAB.Core/SDK/OpenHABClient.cs @@ -125,7 +125,7 @@ public async Task GetOpenHABVersion() OpenHABAPIInfo apiInfo = JsonConvert.DeserializeObject(responseBody); if (apiInfo.Version < 4) { - return OpenHABVersion.Three; + return OpenHABVersion.Two; } string runtimeversion = Regex.Replace(apiInfo?.RuntimeInfo.Version, "[^.0-9]", string.Empty); @@ -165,16 +165,16 @@ public async Task> LoadItemsFromSitemap(OpenHABSitema string resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false); ICollection items = null; - if (version == OpenHABVersion.One) + if (version == OpenHABVersion.Two || version == OpenHABVersion.Three) { - // V1 = xml - items = ParseWidgets(resultString); + var jsonObject = JObject.Parse(resultString); + items = JsonConvert.DeserializeObject>(jsonObject["homepage"]["widgets"].ToString()); } else { - // V2 = JSON - var jsonObject = JObject.Parse(resultString); - items = JsonConvert.DeserializeObject>(jsonObject["homepage"]["widgets"].ToString()); + string message = "openHAB version is not supported."; + _logger.LogError(message); + throw new OpenHABException(message); } _logger.LogInformation($"Loaded '{items.Count}' sitemaps items from server"); @@ -247,22 +247,6 @@ public async Task> LoadSiteMaps(OpenHABVersion versi string resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false); var sitemaps = new List(); - - // V1 = xml - if (version == OpenHABVersion.One) - { - XDocument xml = XDocument.Parse(resultString); - - foreach (XElement xElement in xml.Element("sitemaps").Elements()) - { - var sitemap = new OpenHABSitemap(xElement); - sitemaps.Add(sitemap); - } - - return sitemaps; - } - - // V2 = JSON sitemaps = JsonConvert.DeserializeObject>(resultString); _logger.LogInformation($"Loaded '{sitemaps.Count}' sitemaps from server"); @@ -408,18 +392,6 @@ await Task.Run((Func)(async () => })).ConfigureAwait(false); } - private ICollection ParseWidgets(string resultString) - { - var xml = XDocument.Parse(resultString); - - return - xml.Element("sitemap") - .Element("homepage") - .Elements("widget") - .Select(xElement => OpenHABWidgetFactory.Parse(xElement)) - .ToList(); - } - private async Task SetValidUrl(Settings settings) { _logger.LogInformation("Validate Url"); @@ -467,8 +439,8 @@ private async Task SetValidUrl(Settings settings) if (!result.Content) { - Messenger.Default.Send(new FireErrorMessage(AppResources.Errors.GetString("ConnectionTestFailed"))); - return false; + Messenger.Default.Send(new FireErrorMessage(AppResources.Errors.GetString("ConnectionTestFailed"))); + return false; } if (result.Content)