Пример #1
0
        /// <summary>
        /// Executes the startup operation.
        /// </summary>
        protected override async Task DoStartAsync()
        {
            try
            {
                // Update the live data (total and phase data).
                _logger?.LogDebug("HomeDataMonitor: DoStart...");
                await _homedata?.ReadAllAsync();

                await _hub.Clients.All.SendAsync("UpdateData", _homedata.Data);
            }
            catch (Exception ex)
            {
                _logger?.LogWarning(ex, "DoStartAsync: Exception");
            }
        }
Пример #2
0
        /// <summary>
        /// Method to run when command is executed.
        /// </summary>
        /// <returns>Zero if ok.</returns>
        public async Task <int> OnExecute(CommandLineApplication app)
        {
            try
            {
                if (CheckOptions(app))
                {
                    // Overriding EM300LR options.
                    _homedata.Meter1Address = Parent.Meter1Address;
                    _homedata.Meter2Address = Parent.Meter2Address;

                    await _homedata.ReadAllAsync(OptionU);

                    if (_homedata.Data.IsGood)
                    {
                        if (Property.Length > 0)
                        {
                            Console.WriteLine($"Reading property '{Property}' from home control system.");
                            Console.WriteLine($"Value of property '{Property}' = {JsonConvert.SerializeObject(_homedata.GetPropertyValue(Property), Formatting.Indented)}");
                        }
                        else
                        {
                            Console.WriteLine($"HomeData: {JsonConvert.SerializeObject(_homedata.Data, Formatting.Indented)}");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"Error getting updated data from home control system.");
                        Console.WriteLine($"Reason: {_homedata.Data.Status.Explanation}.");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, $"Exception ReadCommand.");
                return(-1);
            }

            return(0);
        }
Пример #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))
                {
                    try
                    {
                        // Overriding HomeData options.
                        _homedata.Timeout       = Parent.Timeout;
                        _homedata.Meter1Address = Parent.Meter1Address;
                        _homedata.Meter2Address = Parent.Meter2Address;

                        _homedata.Data.PropertyChanged += OnDataPropertyChanged;

                        Console.WriteLine("Monitoring HomeData has started. Ctrl-C to end");

                        try
                        {
                            var source            = new CancellationTokenSource();
                            var cancellationToken = source.Token;

                            await Task.Factory.StartNew(async() => {
                                await _homedata.ReadAllAsync(OptionU);

                                // Wait if necessary to reach 60 seconds.
                                var start    = DateTime.UtcNow;
                                double delay = 60.0 - start.Second - start.Millisecond / 1000.0;
                                await Task.Delay(TimeSpan.FromSeconds(delay), cancellationToken);

                                while (!cancellationToken.IsCancellationRequested)
                                {
                                    try
                                    {
                                        _logger?.LogDebug("HomeDataMonitor: Update data...");
                                        await _homedata?.ReadAllAsync(OptionU);
                                    }
                                    catch (Exception ex)
                                    {
                                        _logger?.LogWarning(ex, "HomeDataMonitor: Exception");
                                    }

                                    if (--Number <= 0)
                                    {
                                        _closing.Set();
                                    }

                                    // Wait if necessary to reach 60 seconds.
                                    var time = DateTime.Now;
                                    delay    = 60.0 - time.Second - time.Millisecond / 1000.0;
                                    await Task.Delay(TimeSpan.FromSeconds(delay), cancellationToken);
                                }
                            }, cancellationToken);

                            Console.CancelKeyPress += new ConsoleCancelEventHandler((sender, args) =>
                            {
                                Console.WriteLine($"Monitoring HomeData cancelled.");
                                _closing.Set();
                            });

                            _closing.WaitOne();
                        }
                        catch (AggregateException aex) when(aex.InnerExceptions.All(e => e is OperationCanceledException))
                        {
                            Console.WriteLine($"Monitoring HomeData cancelled.");
                        }
                        catch (OperationCanceledException)
                        {
                            Console.WriteLine($"Monitoring HomeData cancelled.");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger?.LogError(ex, $"Exception MonitorCommand.");
                        return(-1);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, $"Exception MonitorCommand.");
                return(-1);
            }

            return(0);
        }