예제 #1
0
        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);
        }