private Task <dynamic> Proxy(dynamic parameters, CancellationToken ct) { return(Task <dynamic> .Factory.StartNew(() => { this.RequiresAuthentication(); this.RequiresAnyClaim(new[] { "superuser", "device-proxy-all", string.Format("device-proxy-{0}", (int)parameters.id) }); var device = _connection.SingleById <Device>((int)parameters.id); if (device == null) { return Negotiate .WithStatusCode(HttpStatusCode.NotFound) .WithModel(new { Error = string.Format("Unknown Device ID ({0})", parameters.id) }); } var httpResponse = Proxy(new CoapDotNetHttpRequest(Request, device.Url + "/" + (string)parameters.endpoint), new Uri("coap://" + device.Url + "/" + (string)parameters.endpoint)); if (httpResponse == null) { return HttpStatusCode.GatewayTimeout; } //Send HTTP response httpResponse.OutputStream.Position = 0; return Response .FromStream(httpResponse.OutputStream, httpResponse.ContentType) .WithHeaders(httpResponse.Headers.ToArray()) .WithStatusCode(httpResponse.StatusCode) .AsCacheable(DateTime.Now + TimeSpan.FromSeconds(httpResponse.MaxAge)); }, ct)); }
/// <summary> /// Creates a response to the specified request with the specified response code. /// The destination endpoint of the response is the source endpoint of the request. /// The response has the same token as the request. /// Type and ID are usually set automatically by the <see cref="CoAP.Stack.ReliabilityLayer"/>. /// </summary> public static Response CreateResponse(Request request, StatusCode code) { Response response = new Response(code); response.Destination = request.Source; response.Token = request.Token; return response; }
void IMessageDeliverer.DeliverResponse(Exchange exchange, CoAP.Response response) { if (exchange.Request == null) { throw new ArgumentException("Request should not be empty.", "exchange"); } exchange.Request.Response = response; }
/// <summary> /// Creates a piggy-backed response with the specified response code /// to the specified request. /// </summary> public static Response CreatePiggybackedResponse(Request request, StatusCode code) { Response response = new Response(code); response.ID = request.ID; response.Type = MessageType.ACK; response.Destination = request.Source; response.Token = request.Token; return response; }
public static async Task <T> DeserializeAsync <T>(this CoAP.Response response) where T : class, new() { if (!Code.IsSuccess(response.Code)) { throw new CoapRequestException(response.Code); } var body = response.ResponseText; if (string.IsNullOrWhiteSpace(body)) { throw new InvalidDataException("An empty response was returned"); } return(Connection.DeserializeJson <T>(body) ?? throw new InvalidDataException($"The response did not match Type '{typeof(T).Name}'")); }
private CoapDotNetHttpResponse Proxy(CoapDotNetHttpRequest request, Uri coapUri) { var coapRequest = HttpTranslator.GetCoapRequest(request, Request.Url.SiteBase, true); coapRequest.URI = coapUri; //Setup response handler Response response = null; EventHandler <ResponseEventArgs> responseHandler = null; responseHandler = (_, e) => { response = e.Response; coapRequest.Respond -= responseHandler; }; coapRequest.Respond += responseHandler; //send request coapRequest.Send(); DateTime start = DateTime.Now; // ReSharper disable once LoopVariableIsNeverChangedInsideLoop while (response == null && (DateTime.Now - start).TotalSeconds < 30) { Thread.Sleep(1); } if (response == null) { return(null); } //Turn COAP response into HTTP response var httpResponse = new CoapDotNetHttpResponse { MaxAge = response.MaxAge }; HttpTranslator.GetHttpResponse(request, response, httpResponse); return(httpResponse); }
private Property ParseProperty(ObjectDefinition objectDefinition, PropertyDefinition propertyDefinition, int requestContentType, Response response) { Property result = null; int contentType; if (response.ContentType == -1) { contentType = requestContentType; } else { contentType = response.ContentType; } if (contentType == TlvConstant.CONTENT_TYPE_TLV) { TlvReader reader = new TlvReader(response.Payload); Model.Object lwm2mObject = ObjectUtils.ParseObject(objectDefinition, reader); if ((lwm2mObject != null) && (lwm2mObject.Properties.Count > 0)) { foreach (Property item in lwm2mObject.Properties) { if (item.PropertyDefinitionID == propertyDefinition.PropertyDefinitionID) { result = item; break; } } } } else if ((contentType == MediaType.TextPlain) || (contentType == TlvConstant.CONTENT_TYPE_PLAIN)) { string text = Encoding.UTF8.GetString(response.Payload); result = new Property(); result.PropertyDefinitionID = propertyDefinition.PropertyDefinitionID; result.PropertyID = propertyDefinition.PropertyID; result.Value = new PropertyValue(ObjectUtils.GetValue(text, propertyDefinition)); } else if ((contentType == MediaType.ApplicationJson) || (contentType == TlvConstant.CONTENT_TYPE_JSON)) { JsonReader reader = new JsonReader(new MemoryStream(response.Payload)); //LWM2MObject lwm2mObject = ObjectUtils.ParseObject(objectDefinition, reader); //if ((lwm2mObject != null) && (lwm2mObject.Properties.Count > 0)) //{ // foreach (LWM2MProperty item in lwm2mObject.Properties) // { // if (item.PropertyDefinitionID == propertyDefinition.PropertyDefinitionID) // { // result = item; // break; // } // } //} result = ObjectUtils.ParseProperty(propertyDefinition, reader); } return result; }
private List<Model.Object> ParseObjects(ObjectDefinition objectDefinition, int requestContentType, Response response) { List<Model.Object> result = new List<Model.Object>(); int contentType; if (response.ContentType == -1) { contentType = requestContentType; } else { contentType = response.ContentType; } if (contentType == TlvConstant.CONTENT_TYPE_TLV) { TlvReader reader = new TlvReader(response.Payload); while (reader.Read()) { if (reader.TlvRecord.TypeIdentifier == TTlvTypeIdentifier.ObjectInstance) { TlvReader objectReader = new TlvReader(reader.TlvRecord.Value); Model.Object item = ObjectUtils.ParseObject(objectDefinition, objectReader); if (item != null) { item.InstanceID = reader.TlvRecord.Identifier.ToString(); result.Add(item); } } } } return result; }
private Model.Object ParseObject(ObjectDefinition objectDefinition, int requestContentType, Response response) { Model.Object result = null; int contentType; if (response.ContentType == -1) { contentType = requestContentType; } else { contentType = response.ContentType; } if (contentType == TlvConstant.CONTENT_TYPE_TLV) { TlvReader reader = new TlvReader(response.Payload); result = ObjectUtils.ParseObject(objectDefinition, reader); } else if (contentType == MediaType.ApplicationJson) { JsonReader reader = new JsonReader(new MemoryStream(response.Payload)); result = ObjectUtils.ParseObject(objectDefinition, reader); } return result; }
/// <summary> /// /// </summary> public ResponseEventArgs(Response response) { _response = response; }