Ejemplo n.º 1
0
        private void AttachToMessageBus()
        {
            var filter = new WirehomeDictionary()
                         .WithValue("type", "component_registry.event.status_changed");

            _messageBusService.Subscribe("history_receiver", filter, OnComponentStatusChanged);
        }
Ejemplo n.º 2
0
        void AttachToMessageBus()
        {
            var filter = new Dictionary <object, object>
            {
                ["type"] = "macro_registry.execute"
            };

            _messageBusService.Subscribe("macro_registry.execute", filter, OnExecuteMacroBusMessage);
        }
Ejemplo n.º 3
0
    public async Task <IDictionary <object, object> > PostWaitForAsync([FromBody] IEnumerable <IDictionary <object, object> > filters, int timeout = 60)
    {
        if (filters == null)
        {
            throw new ArgumentNullException(nameof(filters));
        }

        var subscriptions = new List <string>();

        try
        {
            var tcs = new TaskCompletionSource <IDictionary <object, object> >();

            foreach (var filter in filters)
            {
                var subscriptionUid = "api_wait_for:" + Guid.NewGuid().ToString("D");
                subscriptions.Add(_messageBusService.Subscribe(subscriptionUid, filter, m => tcs.TrySetResult(m)));
            }

            using (var timeoutToken = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)))
            {
                using (var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(timeoutToken.Token, HttpContext.RequestAborted))
                {
                    using (linkedToken.Token.Register(() => { tcs.TrySetCanceled(); }))
                    {
                        return(await tcs.Task.ConfigureAwait(false));
                    }
                }
            }
        }
        catch (OperationCanceledException)
        {
            return(new Dictionary <object, object>
            {
                ["type"] = "exception.timeout"
            });
        }
        finally
        {
            foreach (var subscription in subscriptions)
            {
                _messageBusService.Unsubscribe(subscription);
            }
        }
    }
Ejemplo n.º 4
0
        public async Task <WirehomeDictionary> PostWaitForAsync([FromBody] IEnumerable <WirehomeDictionary> filters, int timeout = 60)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }

            var subscriptions = new List <string>();

            try
            {
                var tcs = new TaskCompletionSource <MessageBusMessage>();
                foreach (var filter in filters)
                {
                    var subscriptionUid = "api_wait_for:" + Guid.NewGuid().ToString("D");
                    subscriptions.Add(_messageBusService.Subscribe(subscriptionUid, filter, m => tcs.TrySetResult(m)));
                }

                using (var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(timeout)))
                {
                    timeoutCts.Token.Register(() =>
                    {
                        if (!tcs.Task.IsCanceled && !tcs.Task.IsCompleted && !tcs.Task.IsFaulted)
                        {
                            tcs.TrySetCanceled();
                        }
                    });

                    return((await tcs.Task).Message);
                }
            }
            catch (OperationCanceledException)
            {
                return(new WirehomeDictionary().WithType("exception.timeout"));
            }
            finally
            {
                foreach (var subscription in subscriptions)
                {
                    _messageBusService.Unsubscribe(subscription);
                }
            }
        }
Ejemplo n.º 5
0
        public async Task <WirehomeDictionary> PostWaitForAsync([FromBody] IEnumerable <WirehomeDictionary> filters, int timeout = 60)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }

            var subscriptions = new List <string>();

            try
            {
                var tcs = new TaskCompletionSource <MessageBusMessage>();
                foreach (var filter in filters)
                {
                    var subscriptionUid = "api_wait_for:" + Guid.NewGuid().ToString("D");
                    subscriptions.Add(_messageBusService.Subscribe(subscriptionUid, filter, m => tcs.TrySetResult(m)));
                }

                var timeoutCts   = new CancellationTokenSource(TimeSpan.FromSeconds(60));
                var cts          = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, HttpContext.RequestAborted);
                var timeoutTask  = Task.Run(async() => await Task.Delay(TimeSpan.FromSeconds(timeout), cts.Token), cts.Token);
                var finishedTask = await Task.WhenAny(tcs.Task, timeoutTask);

                if (finishedTask == timeoutTask)
                {
                    return(new WirehomeDictionary().WithType("exception.timeout"));
                }

                return(tcs.Task.Result.Message);
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
            finally
            {
                foreach (var subscription in subscriptions)
                {
                    _messageBusService.Unsubscribe(subscription);
                }
            }
        }
Ejemplo n.º 6
0
 private void AttachToMessageBus()
 {
     _messageBusService.Subscribe("macros.execute", new WirehomeDictionary().WithType("macros.execute"), OnExecuteMacroBusMessage);
 }
Ejemplo n.º 7
0
        private void AttachToMessageBus()
        {
            var filter = new WirehomeDictionary().WithType("component_registry.process_message");

            _messageBusService.Subscribe("component_registry.process_message", filter, OnBusMessageExecuteCommand);
        }
Ejemplo n.º 8
0
 public string subscribe(string uid, PythonDictionary filter, MessageCallback callback)
 {
     return(_messageBusService.Subscribe(uid, filter, m => { callback(PythonConvert.ToPythonDictionary(m)); }));
 }
Ejemplo n.º 9
0
 public string subscribe(string uid, PythonDictionary filter, Action <PythonDictionary> callback)
 {
     return(_messageBusService.Subscribe(uid, filter, m => callback(PythonConvert.ToPythonDictionary(m.Message))));
 }