Beispiel #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);
                }
            };
        }
Beispiel #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);
            }
        }
    }
Beispiel #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);
        }
Beispiel #5
0
        public void FluentControllerBuilder_FluentActionWithGroupBy()
        {
            var action = new FluentAction("/route/url", HttpMethod.Get)
                         .GroupBy("CustomGroupName")
                         .To(() => "Hello");

            Assert.Equal("CustomGroupName", action.Definition.GroupName);
        }
Beispiel #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);
        }
Beispiel #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);
 }
Beispiel #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);
        }
Beispiel #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);
 }
Beispiel #11
0
 /// <inheritdoc/>
 public IMediaPlayerExecuteAsync Stop()
 {
     _currentAction = new FluentAction(FluentActionType.Stop);
     return(this);
 }
Beispiel #12
0
 /// <inheritdoc/>
 public IAction TurnOn()
 {
     _currentAction = new FluentAction(FluentActionType.TurnOn);
     _actions.Enqueue(_currentAction);
     return(this);
 }
Beispiel #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;
 }
Beispiel #14
0
 public IFluentAction Then()
 {
     action = parent.CreateAction(TimeSpan.Zero);
     return action;
 }
Beispiel #15
0
 public IFluentAction AfterDelay(TimeSpan delta)
 {
     action = parent.CreateAction(delta);
     return action;
 }
Beispiel #16
0
 /// <inheritdoc/>
 public IMediaPlayerExecuteAsync Speak(string message)
 {
     _currentAction = new FluentAction(FluentActionType.Speak);
     _currentAction.MessageToSpeak = message;
     return(this);
 }
Beispiel #17
0
 /// <inheritdoc/>
 public IMediaPlayerExecuteAsync PlayPause()
 {
     _currentAction = new FluentAction(FluentActionType.PlayPause);
     return(this);
 }
Beispiel #18
0
        public void FluentControllerBuilder_ThrowsOnFluentActionWithoutHandler()
        {
            var fluentAction = new FluentAction("/route/url", HttpMethod.Get);

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