示例#1
0
        /// <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);
            }
        }
示例#2
0
        public async Task LoadWidgets(OpenHABVersion version)
        {
            Widgets.Clear();

            ICollection <OpenHABWidget> widgetModels = await _openHabsdk.LoadItemsFromSitemap(Model, version).ConfigureAwait(false);

            widgetModels.ToList().ForEach(x => Widgets.Add(x));
        }
示例#3
0
        /// <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 object Convert(object value, Type targetType, object parameter, string language)
        {
            var            settingsService = (ISettingsService)DIService.Instance.Services.GetService(typeof(ISettingsService));
            OpenHABVersion openHABVersion  = settingsService.ServerVersion;

            var    serverUrl = Core.Common.OpenHABHttpClient.BaseUrl;
            string url       = openHABVersion == OpenHABVersion.Two || openHABVersion == OpenHABVersion.Three ? $"{serverUrl}icon/{value}?state=UNDEF&format=png" : $"{serverUrl}images/{value}.png";

            return(new BitmapImage(new Uri(url)));
        }
示例#5
0
        private async Task LoadData()
        {
            await _openHabsdk.ResetConnection();

            _version = await _openHabsdk.GetOpenHABVersion();

            var sitemaps = await _openHabsdk.LoadSiteMaps(_version);

            Sitemaps = new ObservableCollection <OpenHABSitemap>(sitemaps);
        }
示例#6
0
        /// <inheritdoc />
        public async Task <OpenHABVersion> GetOpenHABVersion()
        {
            try
            {
                var settings   = _settingsService.Load();
                var httpClient = _openHABHttpClient.Client(_connection, settings);

                if (httpClient == null)
                {
                    return(OpenHABVersion.None);
                }

                HttpResponseMessage result = await httpClient.GetAsync(Constants.API.ServerInformation).ConfigureAwait(false);

                if (!result.IsSuccessStatusCode)
                {
                    _logger.LogError($"Http request get OpenHab version failed, ErrorCode:'{result.StatusCode}'");
                    throw new OpenHABException($"{result.StatusCode} received from server");
                }

                string responseBody = await result.Content.ReadAsStringAsync();

                OpenHABAPIInfo apiInfo = JsonConvert.DeserializeObject <OpenHABAPIInfo>(responseBody);
                if (apiInfo.Version < 4)
                {
                    return(OpenHABVersion.Three);
                }

                string runtimeversion = Regex.Replace(apiInfo?.RuntimeInfo.Version, "[^.0-9]", string.Empty);
                if (!Version.TryParse(runtimeversion, out Version serverVersion))
                {
                    string message = "Not able to parse runtime verion from openHAB server";
                    _logger.LogError(message);

                    throw new OpenHABException(message);
                }

                OpenHABVersion openHABVersion = (OpenHABVersion)serverVersion.Major;

                return(openHABVersion);
            }
            catch (ArgumentNullException ex)
            {
                throw new OpenHABException("Invalid call", ex);
            }
        }
        /// <inheritdoc/>
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var            settingsService = (ISettingsService)DIService.Instance.Services.GetService(typeof(ISettingsService));
            OpenHABVersion openHABVersion  = settingsService.ServerVersion;

            var serverUrl = OpenHABHttpClient.BaseUrl;

            var    widget     = value as OpenHABWidget;
            var    state      = widget.Item?.State ?? "ON";
            string iconFormat = _settings.UseSVGIcons ? "svg" : "png";

            var regMatch = Regex.Match(state, @"\d+");

            if (regMatch.Success)
            {
                state = regMatch.Value;
            }

            return(openHABVersion == OpenHABVersion.Two || openHABVersion == OpenHABVersion.Three ?
                   $"{serverUrl}icon/{widget.Icon}?state={state}&format={iconFormat}" :
                   $"{serverUrl}images/{widget.Icon}.png");
        }
示例#8
0
        /// <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);
            }
        }
示例#9
0
        /// <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);
            }
        }