示例#1
0
        public async Task <HttpResponseMessage> Get(string format = "array")
        {
            var        user          = GetSessionUser(Request.Headers.GetCookies().FirstOrDefault());
            JDBCEntity currentEntity = null;//当前处理的Entity信息

            try
            {
                Uri uri = ParseToQueryUri(Request.RequestUri);
                List <JDBCEntity> findNode = (await MyCoreApi.FindNodeByUriAsync(uri)).ToList(); //根据uri查找父节点
                if (findNode.Count != 1)                                                         //节点数不唯一
                {
                    throw new Exception("Please specify one Node!");
                }
                else
                {
                    currentEntity = findNode.FirstOrDefault();
                }
                if (!await MyCoreApi.Authorization(currentEntity.Id, user, "4"))
                {
                    throw new Exception("Not authorization!");
                }
                if (currentEntity.EntityType != JDBCEntityType.Signal)
                {
                    throw new Exception("The specifed node is not signal!");
                }

                //get data
                ITypedSignal typedSignal = currentEntity as ITypedSignal;
                var          result      = await typedSignal.GetDataAsync(uri.Fragment, format);

                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.OK, Content = new StringContent(SerializeObjectToString(result), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
                });
            }
            catch (Exception e)
            {
                if (currentEntity == null)
                {
                    return(new HttpResponseMessage {
                        StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(e.InnerException != null ? e.InnerException.Message : e.Message)
                    });
                }
                var response = new ResponseDataMessage
                {
                    Fail = new
                    {
                        Description = e.InnerException != null ? e.InnerException.Message : e.Message,
                        Id          = currentEntity.Id,
                        Path        = currentEntity.Path
                    }
                };
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(SerializeObjectToString(response), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
                });
            }
        }
示例#2
0
        public async Task <HttpResponseMessage> Get()
        {
            JDBCEntity currentEntity = null;//当前处理的Entity信息

            try
            {
                Uri uri = ParseToQueryUri(Request.RequestUri);
                List <JDBCEntity> findNode = (await MyCoreApi.FindNodeByUriAsync(uri)).ToList(); //根据uri查找父节点
                if (findNode.Count != 1)                                                         //节点数不唯一
                {
                    throw new Exception("Please specify one Node!");
                }
                else
                {
                    currentEntity = findNode.FirstOrDefault();
                }
                if (currentEntity.EntityType != JDBCEntityType.Signal)
                {
                    throw new Exception("The specifed node is not signal!");
                }
                var stream = new SignalDataStream((ITypedSignal)currentEntity, uri.Fragment);

                var response = Request.CreateResponse();
                response.Content = new PushStreamContent(stream.WriteToStream, new MediaTypeHeaderValue("data/stream"));

                return(response);
            }
            catch (Exception e)
            {
                if (currentEntity == null)
                {
                    return(new HttpResponseMessage {
                        StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(e.Message)
                    });
                }
                var response = new ResponseDataMessage
                {
                    Fail = new
                    {
                        Description = e.Message,
                        Id          = currentEntity.Id,
                        Path        = currentEntity.Path
                    }
                };
                StringWriter   tw             = new StringWriter();
                JsonSerializer jsonSerializer = new JsonSerializer();
                jsonSerializer.Serialize(tw, response, response.GetType());
                return(new HttpResponseMessage {
                    StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(tw.ToString(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
                });
            }
        }