Example #1
0
        private void _chain(FluentAction action, bool append = true)
        {
            if (chain == null)
            {
                chain = action;
                return;
            }
            var localchain = chain;

            if (append)
            {
                chain = (inv, tmp) => {
                    localchain(inv, tmp);
                    action(inv, tmp);
                }
            }
            ;
            else
            {
                chain = (inv, tmp) => {
                    action(inv, tmp);
                    localchain(inv, tmp);
                }
            };
        }
Example #2
0
        /// <inheritdoc/>
        private async Task HandleAction(FluentAction fluentAction)
        {
            var attributes = fluentAction.Attributes.Select(n => (n.Key, n.Value)).ToArray();

            var taskList = new List <Task>();

            foreach (var entityId in _entityIds)
            {
                var task = fluentAction.ActionType switch
                {
                    FluentActionType.TurnOff => _daemon.TurnOffAsync(entityId, attributes),
                    FluentActionType.TurnOn => _daemon.TurnOnAsync(entityId, attributes),
                    FluentActionType.Toggle => _daemon.ToggleAsync(entityId, attributes),
                    FluentActionType.SetState => _daemon.SetState(entityId, fluentAction.State, attributes),
                    _ => throw new NotSupportedException($"Fluent action type not handled! {fluentAction.ActionType}")
                };
                taskList.Add(task);
            }
            // Wait for all tasks to complete or max 5 seconds
            if (taskList.Count > 0)
            {
                await Task.WhenAny(Task.WhenAll(taskList.ToArray()), Task.Delay(5000)).ConfigureAwait(false);
            }
        }
    }
Example #3
0
 /// <inheritdoc/>
 IAction ISetState <IAction> .SetState(dynamic state)
 {
     _currentAction       = new FluentAction(FluentActionType.SetState);
     _currentAction.State = state;
     _actions.Enqueue(_currentAction);
     return(this);
 }
        public void FluentControllerBuilder_FluentActionWithDescriptionAsync()
        {
            var action = new FluentAction("/route/url", HttpMethod.Get)
                         .WithDescription("Custom Description")
                         .To(async() => { await Task.Delay(1); return("Hello"); });

            Assert.Equal("Custom Description", action.Definition.Description);
        }
Example #5
0
        public void FluentControllerBuilder_FluentActionWithGroupBy()
        {
            var action = new FluentAction("/route/url", HttpMethod.Get)
                         .GroupBy("CustomGroupName")
                         .To(() => "Hello");

            Assert.Equal("CustomGroupName", action.Definition.GroupName);
        }
Example #6
0
        public void FluentControllerBuilder_FluentActionWithGroupByAsync()
        {
            var action = new FluentAction("/route/url", HttpMethod.Get)
                         .GroupBy("CustomGroupName")
                         .To(async() => { await Task.Delay(1); return("Hello"); });

            Assert.Equal("CustomGroupName", action.Definition.GroupName);
        }
Example #8
0
 /// <summary>
 ///     Add an action manually.
 /// </summary>
 /// <param name="action"></param>
 /// <param name="append">True (default) will add this action to the end, false to the start.</param>
 /// <returns></returns>
 public FluentTemplate <TInvoker> Chain(FluentAction action, bool append = true)
 {
     if (action == null)
     {
         throw new ArgumentNullException(nameof(action));
     }
     _chain(action, append);
     return(this);
 }
Example #9
0
        /// <summary>
        /// Creates and adds a fluent action to this collection.
        /// </summary>
        /// <param name="id">Optional unique Id (between all fluent actions) for better debuggability and/or meta
        /// programming (such as generating docs or APIs).</param>
        public FluentAction Route(string routeTemplate, HttpMethod httpMethod, string id = null)
        {
            var fluentAction = new FluentAction(routeTemplate, httpMethod, id);

            PreConfigureAction(fluentAction);
            FluentActions.Add(fluentAction);

            return(fluentAction);
        }
Example #10
0
 /// <summary>
 ///     Will temporarly change the <see cref="FluentInvoker"/> for the passed given actions
 /// </summary>
 /// <param name="actions"></param>
 /// <param name="invoker"></param>
 /// <returns></returns>
 public FluentTemplate <TInvoker> ChangeTemporarlyInvoker(FluentAction actions, TInvoker invoker)
 {
     if (invoker == null)
     {
         throw new ArgumentNullException(nameof(invoker));
     }
     _chain((inv, tmp) => {
         var last    = tmp.Invoker;
         tmp.Invoker = invoker;
         actions(invoker, tmp);
         tmp.Invoker = last;
     });
     return(this);
 }
Example #11
0
 /// <inheritdoc/>
 public IMediaPlayerExecuteAsync Stop()
 {
     _currentAction = new FluentAction(FluentActionType.Stop);
     return(this);
 }
Example #12
0
 /// <inheritdoc/>
 public IAction TurnOn()
 {
     _currentAction = new FluentAction(FluentActionType.TurnOn);
     _actions.Enqueue(_currentAction);
     return(this);
 }
Example #13
0
 private FluentAction CreateAction(TimeSpan delay)
 {
     timeline = timeline.Add(delay);
     FluentAction action = new FluentAction(this, timeline);
     actions.Add(action);
     action.Priority = actions.Count;
     return action;
 }
Example #14
0
 public IFluentAction Then()
 {
     action = parent.CreateAction(TimeSpan.Zero);
     return action;
 }
Example #15
0
 public IFluentAction AfterDelay(TimeSpan delta)
 {
     action = parent.CreateAction(delta);
     return action;
 }
Example #16
0
 /// <inheritdoc/>
 public IMediaPlayerExecuteAsync Speak(string message)
 {
     _currentAction = new FluentAction(FluentActionType.Speak);
     _currentAction.MessageToSpeak = message;
     return(this);
 }
Example #17
0
 /// <inheritdoc/>
 public IMediaPlayerExecuteAsync PlayPause()
 {
     _currentAction = new FluentAction(FluentActionType.PlayPause);
     return(this);
 }
Example #18
0
        public void FluentControllerBuilder_ThrowsOnFluentActionWithoutHandler()
        {
            var fluentAction = new FluentAction("/route/url", HttpMethod.Get);

            Assert.Throws <FluentActionValidationException>(() => BuilderTestUtils.BuildAction(fluentAction));
        }