示例#1
0
        private CloudMessage InvokeRawRequest(CloudMessage requestMessage)
        {
            try
            {
                WirehomeDictionary responseContent;

                var requestContent = requestMessage.GetContent <JToken>();

                // TODO: Refactor this and build converter for JSON to WirehomeDictionary and WirehomeList
                if (!(PythonConvert.ToPython(requestContent) is PythonDictionary parameters))
                {
                    responseContent = new WirehomeDictionary().WithType(ControlType.ParameterInvalidException);
                }
                else
                {
                    if (!_messageHandlers.TryGetValue(parameters.GetValueOr("type", string.Empty), out var messageHandler))
                    {
                        responseContent = new WirehomeDictionary().WithType(ControlType.NotSupportedException);
                    }
                    else
                    {
                        responseContent = messageHandler.Invoke(parameters);
                    }
                }

                var responseMessage = new CloudMessage();
                responseMessage.SetContent(responseContent);

                return(responseMessage);
            }
        public async Task <ActionResult> PostInvoke([FromBody] JToken content)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var request = new CloudMessage
            {
                Type = CloudMessageType.Raw
            };

            request.SetContent(content);

            var deviceSessionIdentifier = HttpContext.GetDeviceSessionIdentifier();
            var responseMessage         = await _deviceConnectorService.Invoke(deviceSessionIdentifier, request, HttpContext.RequestAborted).ConfigureAwait(false);

            return(new ContentResult
            {
                Content = responseMessage.GetContent <string>(),
                ContentType = "application/json"
            });
        }
        public async Task TryDispatchHttpRequestAsync(HttpContext httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            try
            {
                var deviceSessionIdentifier = httpContext.GetDeviceSessionIdentifier();
                if (deviceSessionIdentifier == null)
                {
                    httpContext.Response.Redirect("/Cloud/Account/Login");
                    return;
                }

                var requestContent = new HttpRequestMessageContent
                {
                    Method  = httpContext.Request.Method,
                    Uri     = httpContext.Request.Path + httpContext.Request.QueryString,
                    Content = LoadContent(httpContext.Request)
                };

                if (!string.IsNullOrEmpty(httpContext.Request.ContentType))
                {
                    requestContent.Headers = new Dictionary <string, string>
                    {
                        ["Content-Type"] = httpContext.Request.ContentType
                    };
                }

                var requestMessage = new CloudMessage
                {
                    Type = CloudMessageType.HttpInvoke
                };

                requestMessage.SetContent(requestContent);

                var responseMessage = await Invoke(deviceSessionIdentifier, requestMessage, httpContext.RequestAborted).ConfigureAwait(false);

                var responseContent = responseMessage.GetContent <HttpResponseMessageContent>();

                httpContext.Response.StatusCode = responseContent.StatusCode ?? 200;

                if (responseContent.Headers?.Any() == true)
                {
                    foreach (var header in responseContent.Headers)
                    {
                        httpContext.Response.Headers.Add(header.Key, new StringValues(header.Value));
                    }
                }

                if (responseContent.Content?.Length > 0)
                {
                    httpContext.Response.Body.Write(responseContent.Content);
                }
            }
            catch (OperationCanceledException)
            {
            }
            catch (Exception exception)
            {
                DefaultExceptionFilter.HandleException(exception, httpContext);
            }
        }