Пример #1
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);
            }
        }
Пример #2
0
        private async Task <bool> CheckUrlReachability(string openHABUrl)
        {
            if (string.IsNullOrWhiteSpace(openHABUrl))
            {
                return(false);
            }

            if (!openHABUrl.EndsWith("/"))
            {
                openHABUrl = openHABUrl + "/";
            }

            try
            {
                var client = OpenHABHttpClient.DisposableClient();
                var result = await client.GetAsync(openHABUrl + "rest").ConfigureAwait(false);

                if (result.IsSuccessStatusCode)
                {
                    return(true);
                }
            }
            catch (InvalidOperationException)
            {
                return(false);
            }

            return(false);
        }
Пример #3
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);
            }
        }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OpenHABClient"/> class.
 /// </summary>
 /// <param name="settingsService">The service to fetch the settings.</param>
 /// <param name="messenger">The messenger instance.</param>
 /// <param name="logger">Logger class.</param>
 /// <param name="openHABHttpClient">OpenHab Http client factory.</param>
 public OpenHABClient(ISettingsService settingsService, IMessenger messenger, ILogger <OpenHABClient> logger, OpenHABHttpClient openHABHttpClient)
 {
     _settingsService   = settingsService;
     _messenger         = messenger;
     _logger            = logger;
     _openHABHttpClient = openHABHttpClient;
 }
Пример #5
0
        /// <inheritdoc />
        public async Task ResetConnection()
        {
            var settings = _settingsService.Load();

            await SetValidUrl(settings);

            OpenHABHttpClient.ResetClient();
        }
Пример #6
0
        /// <inheritdoc />
        public async Task <OpenHABVersion> GetOpenHABVersion()
        {
            try
            {
                var result = await OpenHABHttpClient.Client().GetAsync(Constants.Api.ServerVersion).ConfigureAwait(false);

                return(!result.IsSuccessStatusCode ? OpenHABVersion.One : OpenHABVersion.Two);
            }
            catch (ArgumentNullException ex)
            {
                throw new OpenHABException("Invalid call", ex);
            }
        }
Пример #7
0
        /// <inheritdoc />
        public async Task <bool> ResetConnection()
        {
            var  settings = _settingsService.Load();
            bool isValid  = await SetValidUrl(settings);

            if (!isValid)
            {
                return(false);
            }

            OpenHABHttpClient.ResetClient();

            return(true);
        }
Пример #8
0
        /// <inheritdoc />
        public async Task <OpenHABVersion> GetOpenHABVersion()
        {
            try
            {
                var httpClient = OpenHABHttpClient.Client();

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

                var result = await httpClient.GetAsync(Constants.Api.ServerVersion).ConfigureAwait(false);

                _settingsService.ServerVersion = !result.IsSuccessStatusCode ? OpenHABVersion.One : OpenHABVersion.Two;
                return(_settingsService.ServerVersion);
            }
            catch (ArgumentNullException ex)
            {
                throw new OpenHABException("Invalid call", ex);
            }
        }
Пример #9
0
        /// <inheritdoc />
        public async Task SendCommand(OpenHABItem item, string command)
        {
            try
            {
                var client  = OpenHABHttpClient.Client();
                var content = new StringContent(command);
                var result  = await client.PostAsync(item.Link, content);

                if (!result.IsSuccessStatusCode)
                {
                    throw new OpenHABException($"{result.StatusCode} received from server");
                }
            }
            catch (HttpRequestException ex)
            {
                throw new OpenHABException("Invalid call", ex);
            }
            catch (ArgumentNullException ex)
            {
                throw new OpenHABException("Invalid call", ex);
            }
        }
Пример #10
0
        /// <inheritdoc />
        public async void StartItemUpdates()
        {
            await Task.Run(async() =>
            {
                var settings   = _settingsService.Load();
                var client     = OpenHABHttpClient.Client(_connectionType, settings);
                var requestUri = Constants.Api.Events;

                try
                {
                    var stream = await client.GetStreamAsync(requestUri);

                    using (var reader = new StreamReader(stream))
                    {
                        while (!reader.EndOfStream)
                        {
                            var updateEvent = reader.ReadLine();
                            if (updateEvent?.StartsWith("data:") == true)
                            {
                                var data = JsonConvert.DeserializeObject <EventStreamData>(updateEvent.Remove(0, 6));
                                if (!data.Topic.EndsWith("state"))
                                {
                                    continue;
                                }

                                var payload = JsonConvert.DeserializeObject <EventStreamPayload>(data.Payload);
                                _messenger.Send(new UpdateItemMessage(data.Topic.Replace("smarthome/items/", string.Empty).Replace("/state", string.Empty), payload.Value));
                            }
                        }
                    }
                }
                catch (HttpRequestException)
                {
                    // running on 1.x, no event endpoint
                }
            });
        }