/// <inheritdoc /> public async Task <ICollection <OpenHABWidget> > LoadItemsFromSitemap(OpenHABSitemap sitemap, OpenHABVersion version) { try { var result = await OpenHABHttpClient.Client().GetAsync(sitemap.Link).ConfigureAwait(false); if (!result.IsSuccessStatusCode) { throw new OpenHABException($"{result.StatusCode} received from server"); } string resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false); // V1 = xml if (version == OpenHABVersion.One) { var widgets = ParseWidgets(resultString); return(widgets); } // V2 = JSON return(JsonConvert.DeserializeObject <List <OpenHABWidget> >(resultString)); } catch (ArgumentNullException ex) { throw new OpenHABException("Invalid call", ex); } }
/// <inheritdoc /> public async Task <ICollection <OpenHABSitemap> > LoadSiteMaps(OpenHABVersion version) { try { var result = await OpenHABHttpClient.Client().GetAsync(Constants.Api.Sitemaps).ConfigureAwait(false); if (!result.IsSuccessStatusCode) { throw new OpenHABException($"{result.StatusCode} received from server"); } string resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false); // V1 = xml if (version == OpenHABVersion.One) { var sitemaps = new List <OpenHABSitemap>(); 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 return(JsonConvert.DeserializeObject <List <OpenHABSitemap> >(resultString)); } catch (ArgumentNullException ex) { throw new OpenHABException("Invalid call", ex); } }
/// <inheritdoc /> public async Task <ICollection <OpenHABSitemap> > LoadSiteMaps(OpenHABVersion version, List <Func <OpenHABSitemap, bool> > filters) { try { _logger.LogInformation($"Load sitemaps for OpenHab server version '{version.ToString()}'"); var settings = _settingsService.Load(); var result = await _openHABHttpClient.Client(_connection, settings).GetAsync(Constants.API.Sitemaps).ConfigureAwait(false); if (!result.IsSuccessStatusCode) { _logger.LogError($"Http request for loading sitemaps failed, ErrorCode:'{result.StatusCode}'"); throw new OpenHABException($"{result.StatusCode} received from server"); } string resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false); var sitemaps = new List <OpenHABSitemap>(); // 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 <List <OpenHABSitemap> >(resultString); _logger.LogInformation($"Loaded '{sitemaps.Count}' sitemaps from server"); return(sitemaps.Where(sitemap => { bool isIncluded = true; filters.ForEach(filter => isIncluded &= filter(sitemap)); return isIncluded; }).ToList()); } catch (ArgumentNullException ex) { _logger.LogError(ex, "LoadSiteMaps failed."); throw new OpenHABException("Invalid call", ex); } catch (Exception ex) { _logger.LogError(ex, "LoadSiteMaps failed."); throw new OpenHABException("Invalid call", ex); } }
/// <inheritdoc /> public async Task <ICollection <OpenHABWidget> > LoadItemsFromSitemap(OpenHABSitemap sitemap, OpenHABVersion version) { try { _logger.LogInformation($"Load sitemaps items for sitemap '{sitemap.Name}'"); var settings = _settingsService.Load(); var result = await _openHABHttpClient.Client(_connection, settings).GetAsync(sitemap.Link).ConfigureAwait(false); if (!result.IsSuccessStatusCode) { _logger.LogError($"Http request for loading sitemaps items failed, ErrorCode:'{result.StatusCode}'"); throw new OpenHABException($"{result.StatusCode} received from server"); } string resultString = await result.Content.ReadAsStringAsync().ConfigureAwait(false); ICollection <OpenHABWidget> items = null; if (version == OpenHABVersion.One) { // V1 = xml items = ParseWidgets(resultString); } else { // V2 = JSON var jsonObject = JObject.Parse(resultString); items = JsonConvert.DeserializeObject <List <OpenHABWidget> >(jsonObject["homepage"]["widgets"].ToString()); } _logger.LogInformation($"Loaded '{items.Count}' sitemaps items from server"); return(items); } catch (ArgumentNullException ex) { _logger.LogError(ex, "LoadItemsFromSitemap failed."); throw new OpenHABException("Invalid call", ex); } catch (Exception ex) { _logger.LogError(ex, "LoadItemsFromSitemap failed."); throw new OpenHABException("Invalid call", ex); } }