public override BaseIdentifiableMessage ProccessCommand(MockHassServerRequestContext context, BaseIdentifiableMessage receivedCommand)
        {
            var commandRenderTemplate = receivedCommand as RenderTemplateMessage;

            this.entityIds = new HashSet <string>();
            var result    = Regex.Replace(commandRenderTemplate.Template, @"{{ (.*) }}", this.RenderTemplateValue);
            var listeners = new ListenersTemplateInfo()
            {
                All  = false,
                Time = false,
            };

            listeners.Entities = this.entityIds.ToArray();
            listeners.Domains  = listeners.Entities.Select(x => x.GetDomain()).ToArray();
            this.entityIds     = null;

            var renderTemplateEvent = new TemplateEventInfo()
            {
                Result    = result,
                Listeners = listeners,
            };

            var eventMsg = new EventResultMessage()
            {
                Id    = commandRenderTemplate.Id,
                Event = new JRaw(HassSerializer.SerializeObject(renderTemplateEvent))
            };

            Task.Factory.StartNew(async() =>
            {
                await Task.Delay(40);
                await context.SendMessageAsync(eventMsg, CancellationToken.None);
            });

            return(this.CreateResultMessageWithResult(null));
        }
Example #2
0
        protected override async Task RespondToWebSocketRequestAsync(WebSocket webSocket, CancellationToken cancellationToken)
        {
            var context = new MockHassServerRequestContext(this.hassDB, webSocket);

            await context.SendMessageAsync(new AuthenticationRequiredMessage()
            {
                HAVersion = this.HAVersion.ToString()
            }, cancellationToken);

            try
            {
                while (true)
                {
                    if (context.IsAuthenticating)
                    {
                        var incomingMessage = await context.ReceiveMessageAsync <BaseMessage>(cancellationToken);

                        await Task.Delay(this.ResponseSimulatedDelay);

                        if (!this.IgnoreAuthenticationMessages &&
                            incomingMessage is AuthenticationMessage authMessage)
                        {
                            if (authMessage.AccessToken == this.ConnectionParameters.AccessToken)
                            {
                                await context.SendMessageAsync(new AuthenticationOkMessage()
                                {
                                    HAVersion = this.HAVersion.ToString()
                                }, cancellationToken);

                                context.IsAuthenticating  = false;
                                this.activeRequestContext = context;
                            }
                            else
                            {
                                await context.SendMessageAsync(new AuthenticationInvalidMessage(), cancellationToken);

                                break;
                            }
                        }
                    }
                    else
                    {
                        var receivedMessage = await context.ReceiveMessageAsync <BaseOutgoingMessage>(cancellationToken);

                        var receivedMessageId = receivedMessage.Id;

                        await Task.Delay(this.ResponseSimulatedDelay);

                        BaseIdentifiableMessage response;
                        if (context.LastReceivedID >= receivedMessageId)
                        {
                            response = new ResultMessage()
                            {
                                Error = new ErrorInfo(ErrorCodes.IdReuse)
                            };
                        }
                        else
                        {
                            context.LastReceivedID = receivedMessageId;

                            if (receivedMessage is PingMessage)
                            {
                                response = new PongMessage();
                            }
                            else if (!context.TryProccesMessage(receivedMessage, out response))
                            {
                                response = new ResultMessage()
                                {
                                    Error = new ErrorInfo(ErrorCodes.UnknownCommand)
                                };
                            }
                        }

                        response.Id = receivedMessageId;
                        await context.SendMessageAsync(response, cancellationToken);
                    }
                }
            }
            catch
            {
                Trace.WriteLine("A problem occured while attending client. Closing connection.");
                await webSocket.CloseAsync(WebSocketCloseStatus.InternalServerError, string.Empty, default);
            }
        }