public async Task <T> GetResponseAsync <T>(RequestPrimitive <TPrimitiveContent> body) where T : class, new() { if (body.To == null) { body.To = AeId; } else if (!body.To.StartsWith("/")) { body.To = $"{AeId}/{body.To}"; } if (body.From == null) { body.From = AeId; } return(await Connection.GetResponseAsync <T>(body)); }
public async Task <ResponseContent <TPrimitiveContent> > GetResponseAsync(RequestPrimitive <TPrimitiveContent> body) => await GetResponseAsync <ResponseContent <TPrimitiveContent> >(body);
internal CoAP.Request GetRequest(RequestPrimitive <TPrimitiveContent> body) { var args = GetRequestParameters(body); var fc = body.FilterCriteria; if (fc != null) { if (fc.Attribute != null) { foreach (var attr in fc.Attribute) { if (attr.Value != null) { args.Add("atr", $"{attr.Name},{attr.Value.ToString()}"); } } } } var method = body.Operation switch { Operation.Retrieve => CoAP.Method.GET, Operation.Update => CoAP.Method.PUT, Operation.Delete => CoAP.Method.DELETE, _ => CoAP.Method.POST, }; var request = new CoAP.Request(method); request.AckTimeout = 10000 * 1000; request.URI = _pnClient.Uri; var to = body.To; var pathParts = to.Split("/", StringSplitOptions.RemoveEmptyEntries); if (to.StartsWith("//")) { request.AddUriPath("_"); } else if (to.StartsWith("/")) { request.AddUriPath("~"); } foreach (var pathPart in pathParts) { request.AddUriPath(pathPart); } foreach (var query in args.AllKeys.SelectMany(args.GetValues, (k, v) => $"{k}={/*Uri.EscapeDataString*/(v)}")) { request.AddUriQuery(query); } if (body.ResourceType != null) { request.AddOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.TY, (int)body.ResourceType)); } if (body.PrimitiveContent != null) { var bodyJson = SerializeJson(body.PrimitiveContent); request.SetPayload(bodyJson, (int)ContentFormats.Json); } if (body.From != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.FR, body.From)); } request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.RQI, body.RequestIdentifier ?? NextRequestId)); if (body.GroupRequestIdentifier != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.GID, body.GroupRequestIdentifier)); } if (body.OriginatingTimestamp != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.OT, body.OriginatingTimestamp.Value.ToString(_dateTimeFormat))); } if (body.ResultExpirationTimestamp != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.RSET, body.ResultExpirationTimestamp)); } if (body.RequestExpirationTimestamp != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.RQET, body.RequestExpirationTimestamp)); } if (body.OperationExecutionTime != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.OET, body.OperationExecutionTime)); } if (body.EventCategory != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.EC, body.EventCategory)); } if (body.ResponseType?.NotificationURI != null) { request.SetOption(Option.Create((CoAP.OptionType)OneM2mRequestOptions.RTURI, string.Join("&", body.ResponseType.NotificationURI))); } return(request); }
public override async Task <T> GetResponseAsync <T>(RequestPrimitive <TPrimitiveContent> body) { var request = GetRequest(body); return(await GetResponseAsync <T>(request)); }
internal HttpRequestMessage GetRequest(RequestPrimitive <TPrimitiveContent> body) { var args = GetRequestParameters(body); var fc = body.FilterCriteria; if (fc != null) { foreach (var attr in fc.Attribute) { if (attr.Value != null) { args.Add(attr.Name, attr.Value.ToString()); } } } var method = body.Operation switch { Operation.Retrieve => HttpMethod.Get, Operation.Update => HttpMethod.Put, Operation.Delete => HttpMethod.Delete, _ => HttpMethod.Post, }; var url = body.To; if (url.StartsWith("//")) { url = "/_/" + url.Substring(2); } else if (url.StartsWith("/")) { url = "/~/" + url.Substring(1); } else { url = "/" + url; } var urlBuilder = new UriBuilder(_iotApiUrl) { Path = url, Query = string.Join("&", args.AllKeys.SelectMany(args.GetValues, (k, v) => $"{k}={Uri.EscapeDataString(v)}")), }; var httpRequestMessage = new HttpRequestMessage(method, urlBuilder.ToString()); var contentTypeHeader = new MediaTypeHeaderValue(OneM2MResponseContentType); //if (method == HttpMethod.Post || method == HttpMethod.Put) { if (body.ResourceType != null) { contentTypeHeader.Parameters.Add(new NameValueHeaderValue("ty", ((int)body.ResourceType).ToString())); } if (body.PrimitiveContent != null) { var bodyJson = SerializeJson(body.PrimitiveContent); httpRequestMessage.Content = new StringContent(bodyJson, Encoding.UTF8); httpRequestMessage.Content.Headers.ContentType = contentTypeHeader; } } httpRequestMessage.Headers.Add("X-M2M-Origin", body.From); httpRequestMessage.Headers.Add("X-M2M-RI", body.RequestIdentifier ?? NextRequestId); if (body.GroupRequestIdentifier != null) { httpRequestMessage.Headers.Add("X-M2M-GID", body.GroupRequestIdentifier); } if (body.OriginatingTimestamp != null) { httpRequestMessage.Headers.Add("X-M2M-OT", body.OriginatingTimestamp.Value.ToString(_dateTimeFormat)); } if (body.ResultExpirationTimestamp != null) { httpRequestMessage.Headers.Add("X-M2M-RST", body.ResultExpirationTimestamp); } if (body.RequestExpirationTimestamp != null) { httpRequestMessage.Headers.Add("X-M2M-RET", body.RequestExpirationTimestamp); } if (body.OperationExecutionTime != null) { httpRequestMessage.Headers.Add("X-M2M-OET", body.OperationExecutionTime); } if (body.EventCategory != null) { httpRequestMessage.Headers.Add("X-M2M-EC", body.EventCategory); } if (body.ResponseType?.NotificationURI != null) { httpRequestMessage.Headers.Add("X-M2M-RTU", string.Join("&", body.ResponseType.NotificationURI)); } return(httpRequestMessage); }