Пример #1
0
        private async Task Run(CancellationToken token)
        {
            await _Proxy.ConnectAsync(new Uri($"ws://localhost:{_Port}"), token);

            await _Proxy.Register(_RegisterEvent, _Uuid);


            var keepRunning = true;

            while (!token.IsCancellationRequested)
            {
                // Exit loop if the socket is closed or aborted
                switch (_Proxy.State)
                {
                case WebSocketState.CloseReceived:
                case WebSocketState.Closed:
                case WebSocketState.Aborted:
                    keepRunning = false;
                    break;
                }
                if (!keepRunning)
                {
                    break;
                }

                var jsonString = await _Proxy.GetMessageAsString(token);

                if (!string.IsNullOrEmpty(jsonString) && !jsonString.StartsWith("\0"))
                {
                    try
                    {
                        var msg = JsonConvert.DeserializeObject <StreamDeckEventPayload>(jsonString);
                        if (msg == null)
                        {
                            _Logger.LogError($"Unknown message received: {jsonString}");
                            continue;
                        }
                        if (!_ActionDictionary.ContainsKey(msg.Event))
                        {
                            _Logger.LogWarning($"Plugin does not handle the event '{msg.Event}'");
                            continue;
                        }
                        _ActionDictionary[msg.Event]?.Invoke(_Plugin, msg);
                    }
                    catch (Exception ex)
                    {
                        _Logger.LogError(ex, "Error while processing payload from StreamDeck");
                    }
                }

                await Task.Delay(100);
            }
            Dispose();
        }
Пример #2
0
        private async Task Run(CancellationToken token)
        {
            _logger?.LogTrace($"{nameof(ConnectionManager)}.{nameof(Run)} port:{_port}, event: {_registerEvent}, uuid: {_uuid}");

            await _proxy.ConnectAsync(new Uri($"ws://localhost:{_port}"), token);

            _logger?.LogTrace($"{nameof(ConnectionManager)}.{nameof(Run)} Connected");
            await _proxy.Register(_registerEvent, _uuid);

            _logger?.LogTrace($"{nameof(ConnectionManager)}.{nameof(Run)} Registered");

            var keepRunning = true;

            while (!token.IsCancellationRequested && keepRunning)
            {
                // Exit loop if the socket is closed or aborted
                switch (_proxy.State)
                {
                case WebSocketState.CloseReceived:
                case WebSocketState.Closed:
                case WebSocketState.Aborted:
                    keepRunning = false;

                    break;
                }

                if (!keepRunning)
                {
                    break;
                }

                var jsonString = await _proxy.GetMessageAsString(token);

                if (!string.IsNullOrEmpty(jsonString) && !jsonString.StartsWith("\u0000", StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        var msg = JsonConvert.DeserializeObject <StreamDeckEventPayload>(jsonString);

                        if (msg == null)
                        {
                            _logger?.LogError($"Unknown message received: {jsonString}");

                            continue;
                        }

                        if (string.IsNullOrWhiteSpace(msg.context) && string.IsNullOrWhiteSpace(msg.action))
                        {
                            this.BroadcastMessage(msg);
                        }
                        else
                        {
                            var action = GetInstanceOfAction(msg.context, msg.action);
                            if (action == null)
                            {
                                _logger?.LogWarning($"The action requested (\"{msg.action}\") was not found as being registered with the plugin");
                                continue;
                            }


                            if (!_EventDictionary.ContainsKey(msg.Event))
                            {
                                _logger?.LogWarning($"Plugin does not handle the event '{msg.Event}'");

                                continue;
                            }

                            _EventDictionary[msg.Event]?.Invoke(action, msg);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger?.LogError(ex, "Error while processing payload from StreamDeck");
                    }
                }

                await Task.Delay(100);
            }

            Dispose();
        }
        private async Task Run(CancellationToken token)
        {
            await _Proxy.ConnectAsync(new Uri($"ws://localhost:{_Port}"), token);

            await _Proxy.Register(_RegisterEvent, _Uuid);

            var keepRunning = true;

            while (!token.IsCancellationRequested && keepRunning)
            {
                // Exit loop if the socket is closed or aborted
                switch (_Proxy.State)
                {
                case WebSocketState.CloseReceived:
                case WebSocketState.Closed:
                case WebSocketState.Aborted:
                    keepRunning = false;

                    break;
                }

                if (!keepRunning)
                {
                    break;
                }

                var jsonString = await _Proxy.GetMessageAsString(token);

                if (!string.IsNullOrEmpty(jsonString) && !jsonString.StartsWith("\0"))
                {
                    try
                    {
                        var msg = JsonConvert.DeserializeObject <StreamDeckEventPayload>(jsonString);

                        if (msg == null)
                        {
                            _Logger.LogError($"Unknown message received: {jsonString}");

                            continue;
                        }

                        if (_ActionEventsIgnore.Contains(msg.Event))
                        {
                            continue;
                        }

                        // Make sure we have a registered BaseStreamDeckAction instance registered for the received action (UUID)
                        if (!_ActionsDictionary.ContainsKey(msg.action))
                        {
                            _Logger.LogWarning($"The action requested (\"{msg.action}\") was not found as being registered with the plugin");
                        }

                        var action = _ActionsDictionary[msg.action];


                        //property inspector payload
                        if (msg.Event == "sendToPlugin")
                        {
                            var piMsg = JsonConvert.DeserializeObject <PropertyInspectorEventPayload>(jsonString);
                            if (piMsg.PayloadHasProperty("property_inspector"))
                            {
                                //property inspector event
                                var piEvent = piMsg.GetPayloadValue <string>("property_inspector");
                                if (!_PropertyInspectorActionDictionary.ContainsKey(piEvent))
                                {
                                    _Logger.LogWarning($"Plugin does not handle the Property Inspector event '{piEvent}'");
                                    continue;
                                }
                                else
                                {
                                    _PropertyInspectorActionDictionary[piEvent]?.Invoke(action, piMsg);
                                    continue;
                                }
                            }

                            //property inspector property value event
                            _PropertyInspectorActionDictionary[piMsg.Event]?.Invoke(action, piMsg);
                            continue;
                        }

                        if (!_EventDictionary.ContainsKey(msg.Event))
                        {
                            _Logger.LogWarning($"Plugin does not handle the event '{msg.Event}'");

                            continue;
                        }

                        _EventDictionary[msg.Event]?.Invoke(action, msg);
                    }
                    catch (Exception ex)
                    {
                        _Logger.LogError(ex, "Error while processing payload from StreamDeck");
                    }
                }

                await Task.Delay(100);
            }

            Dispose();
        }
        //CHANGED
        private async Task <TaskStatus> ReceiveMessageAsync(CancellationToken token)
        {
            switch (_proxy.State)
            {
            case WebSocketState.CloseReceived:
            case WebSocketState.Closed:
            case WebSocketState.Aborted:
                return(TaskStatus.Canceled);
            }

            var jsonString = await _proxy.GetMessageAsString(token);

            if (!string.IsNullOrEmpty(jsonString) && !jsonString.StartsWith("\u0000", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    var msg = JsonConvert.DeserializeObject <StreamDeckEventPayload>(jsonString);

                    if (msg == null)
                    {
                        _logger?.LogError($"Unknown message received: {jsonString}");

                        return(TaskStatus.Faulted);
                    }
                    //CHANGED
                    if (string.IsNullOrWhiteSpace(msg.context) && string.IsNullOrWhiteSpace(msg.action))
                    {
                        _lastMessage = new KeyValuePair <StreamDeckEventPayload, BaseStreamDeckAction>(msg, null);
                        return(TaskStatus.RanToCompletion);
                    }
                    else
                    {
                        var action = GetInstanceOfAction(msg.context, msg.action);
                        if (action == null)
                        {
                            _logger?.LogWarning($"The action requested (\"{msg.action}\") was not found as being registered with the plugin");
                            return(TaskStatus.Faulted);
                        }


                        if (!_EventDictionary.ContainsKey(msg.Event))
                        {
                            _logger?.LogWarning($"Plugin does not handle the event '{msg.Event}'");
                            return(TaskStatus.Faulted);
                        }

                        //CHANGED
                        _lastMessage = new KeyValuePair <StreamDeckEventPayload, BaseStreamDeckAction>(msg, action);
                        return(TaskStatus.RanToCompletion);
                    }
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, "Error while processing payload from StreamDeck");
                    return(TaskStatus.Faulted);
                }
            }
            else
            {
                return(TaskStatus.Faulted);
            }
        }