Example #1
0
        /// <summary>
        /// Creates a new automation
        /// </summary>
        /// <param name="name">The friendly name of the automation</param>
        /// <param name="host">The host that manages this automation</param>
        protected Automation(string id, string name, IAutomationHost host)
        {
            Id        = id ?? name;
            Name      = name ?? throw new ArgumentNullException(nameof(name));
            Scheduler = host.Scheduler ?? throw new InvalidOperationException("Host's scheduler is 'null'.");

            _hostSubscription = Observable
                                .DeferAsync(async ct =>
            {
                await host.Initialized(ct, this);

                return(host.GetAndObserveIsEnabled(this));
            })
                                .Do(isEnabled =>
            {
                // Make sure to always abort the pending execution, even if 'Enable()' fails
                _automationSubscription.Disposable = null;
                if (isEnabled)
                {
                    using (new AsyncContext(Scheduler))                             // AsyncContext is used here only to propagate the IScheduler
                    {
                        _automationSubscription.Disposable = Enable();
                    }
                }

                this.Log().Info($"Automation '{Name}' is now enabled: {isEnabled}");
            })
                                .Retry(Constants.DefaultRetryDelay, Scheduler)
                                .Subscribe();
        }
Example #2
0
        /// <summary>
        /// Creates a new automation
        /// </summary>
        /// <param name="name">The friendly name of the automation</param>
        /// <param name="host">The host that manages this automation</param>
        protected Automation(string id, string name, IAutomationHost host)
        {
            Id        = id ?? name;
            Name      = name ?? throw new ArgumentNullException(nameof(name));
            Scheduler = host.Scheduler ?? throw new InvalidOperationException("Host's scheduler is 'null'.");

            _hostSubscription = Observable
                                .DeferAsync(async ct =>
            {
                await host.Initialized(ct, this);

                return(host.GetAndObserveIsEnabled(this));
            })
                                .DistinctUntilChanged()
                                .Do(isEnabled =>
            {
                // Make sure to always abort the pending execution, even if 'Enable()' fails
                _automationSubscription.Disposable = null;
                if (isEnabled)
                {
                    _automationSubscription.Disposable = new CompositeDisposable
                    {
                        // AsyncContext is used here mainly to propagate the IScheduler, but might flow in the subscriptions
                        // made in the "Enable". So we make sure to dispose it only when the automation is disable.
                        new AsyncContext(Scheduler),
                        Enable()
                    };
                }

                this.Log().Info($"Automation '{Name}' is now enabled: {isEnabled}");
            })
                                .Retry(Constants.DefaultRetryDelay, Scheduler)
                                .Subscribe();
        }
Example #3
0
 /// <summary>
 /// Creates a new automation
 /// </summary>
 /// <param name="name">The friendly name of the automation</param>
 /// <param name="host">The host that manages this automation</param>
 protected Automation(string name, IAutomationHost host)
     : this(null, name, host)
 {
 }
Example #4
0
 /// <inheritdoc />
 protected Automation(string id, string name, IAutomationHost host = null)
     : base(id, name, host ?? HomeBase <THome> .Current.GetDefaultAutomationHost(name))
 {
 }
Example #5
0
 public TestAutomation(IAutomationHost host, Func <IDisposable> onEnabled = null)
     : base(nameof(TestAutomation), host)
 {
     _onEnabled = onEnabled ?? (() => Disposable.Empty);
 }