Esempio n. 1
0
        public async Task <IActionResult> GetNetatmoData(string name, bool update = false)
        {
            if (string.IsNullOrEmpty(name))
            {
                _logger?.LogDebug($"GetNetatmoData() invalid property.");
                return(StatusCode(StatusCodes.Status400BadRequest, $"Property is invalid."));
            }

            try
            {
                _logger?.LogDebug($"GetNetatmoData({name})...");

                if (Netatmo.IsProperty(name))
                {
                    if (!_netatmo.IsLocked)
                    {
                        return(StatusCode(StatusCodes.Status406NotAcceptable, "Locked: update not yet finished."));
                    }

                    if (update)
                    {
                        var status = await _netatmo.ReadAllAsync();

                        if (!status.IsGood)
                        {
                            return(StatusCode(StatusCodes.Status502BadGateway, status));
                        }
                    }

                    return(Ok(_netatmo.GetPropertyValue(name)));
                }
                else
                {
                    _logger?.LogDebug($"GetNetatmoData('{name}') property not found.");
                    return(NotFound($"Property '{name}' not found."));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
Esempio n. 2
0
 public void TestNetatmoReadProperty(string property)
 {
     Assert.True(Netatmo.IsProperty(property));
     Assert.NotNull(_netatmo.GetPropertyValue(property));
 }
Esempio n. 3
0
        /// <summary>
        /// Method to run when command is executed.
        /// </summary>
        /// <returns>Zero if ok.</returns>
        public async Task <int> OnExecuteAsync(CommandLineApplication app)
        {
            try
            {
                if (CheckOptions(app))
                {
                    // Overriding Netatmo options.
                    _netatmo.BaseAddress = Parent.BaseAddress;
                    _netatmo.Timeout     = Parent.Timeout;

                    if (Property.Length == 0)
                    {
                        Console.WriteLine($"Reading all data from Netatmo weather station.");
                        var status = await _netatmo.ReadAllAsync();

                        if (status != DataValue.Good)
                        {
                            Console.WriteLine($"Error reading property '{Property}' from Netatmo weather station.");
                            Console.WriteLine($"Reason: {status.Explanation}.");
                        }
                        else
                        {
                            Console.WriteLine($"Netatmo: {JsonConvert.SerializeObject(_netatmo, Formatting.Indented)}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Reading property '{Property}' from Netatmo weather station.");
                        var status = await _netatmo.ReadAllAsync();

                        if (status != DataValue.Good)
                        {
                            Console.WriteLine($"Error reading property '{Property}' from Netatmo weather station.");
                            Console.WriteLine($"Reason: {status.Explanation}.");
                        }
                        else
                        {
                            Console.WriteLine($"Value of property '{Property}' = {JsonConvert.SerializeObject(_netatmo.GetPropertyValue(Property), Formatting.Indented)}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, $"Exception ReadCommand.");
                return(-1);
            }

            return(0);
        }