/// <inheritdoc />
        public async Task SendCommand(OpenHABItem item, string command)
        {
            try
            {
                _logger.LogInformation($"Send Command '{command}' for item '{item.Name} of type '{item.Type}'");

                var settings = _settingsService.Load();
                var client   = _openHABHttpClient.Client(_connection, settings);
                var content  = new StringContent(command);

                var result = await client.PostAsync(item.Link, content);

                if (!result.IsSuccessStatusCode)
                {
                    _logger.LogError($"Http request for command failed, ErrorCode:'{result.StatusCode}'");
                    throw new OpenHABException($"{result.StatusCode} received from server");
                }
            }
            catch (HttpRequestException ex)
            {
                _logger.LogError(ex, "SendCommand failed.");
                throw new OpenHABException("Invalid call", ex);
            }
            catch (ArgumentNullException ex)
            {
                _logger.LogError(ex, "SendCommand failed.");
                throw new OpenHABException("Invalid call", ex);
            }
        }
示例#2
0
        /// <inheritdoc/>
        public bool TryGetItem(string itemName, out OpenHABItem item)
        {
            if (!_nameToStateDictionary.TryGetValue(itemName, out item))
            {
                item = _openHABClient.GetItemByName(itemName).Result;
                return(_nameToStateDictionary.TryAdd(itemName, item));
            }

            return(_nameToStateDictionary.TryGetValue(itemName, out item));
        }
示例#3
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);
            }
        }
示例#4
0
        /// <inheritdoc />
        public async Task <HttpResponseResult <bool> > SendCommand(OpenHABItem item, string command)
        {
            try
            {
                if (item == null)
                {
                    _logger.LogInformation("Send command is canceled, because item is null (e.g. widget not linked to item)");
                    return(new HttpResponseResult <bool>(false, null, null));
                }

                _logger.LogInformation($"Send Command '{command}' for item '{item.Name} of type '{item.Type}'");

                var settings = _settingsService.Load();
                var client   = _openHABHttpClient.Client(_connection, settings);
                var content  = new StringContent(command);

                var result = await client.PostAsync(item.Link, content);

                if (!result.IsSuccessStatusCode)
                {
                    _logger.LogError($"Http request for command failed, ErrorCode:'{result.StatusCode}'");
                }

                return(new HttpResponseResult <bool>(result.IsSuccessStatusCode, result.StatusCode));
            }
            catch (HttpRequestException ex)
            {
                _logger.LogError(ex, "SendCommand failed.");
                return(new HttpResponseResult <bool>(false, null, ex));
            }
            catch (ArgumentNullException ex)
            {
                _logger.LogError(ex, "SendCommand failed.");
                return(new HttpResponseResult <bool>(false, null, ex));
            }
        }
示例#5
0
        /// <inheritdoc />
        public async Task <OpenHABItem> GetItemByName(string itemName)
        {
            try
            {
                _logger.LogInformation($"Load item by name '{itemName}' from openHAB server");

                var settings    = _settingsService.Load();
                Uri resourceUrl = new Uri($"{Constants.API.Items}/{itemName}", UriKind.Relative);

                var result = await _openHABHttpClient.Client(_connection, settings).GetAsync(resourceUrl).ConfigureAwait(false);

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

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

                OpenHABItem item = JsonConvert.DeserializeObject <OpenHABItem>(resultString);

                _logger.LogInformation($"Loaded item '{itemName}' from server");

                return(item);
            }
            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);
            }
        }
示例#6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TriggerCommandMessage"/> class.
 /// </summary>
 /// <param name="item">The OpenHAB item that triggered the command.</param>
 /// <param name="command">The command that was triggered.</param>
 public TriggerCommandMessage(OpenHABItem item, string command)
 {
     Item    = item;
     Command = command;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TriggerCommandMessage"/> class.
 /// </summary>
 /// <param name="item">The OpenHAB item that triggered the command.</param>
 /// <param name="command">The command that was triggered.</param>
 public TriggerCommandMessage(OpenHABItem item, string command)
 {
     Id      = Guid.NewGuid();
     Item    = item;
     Command = command;
 }