コード例 #1
0
        public TradfriAuth GenerateAppSecret(string GatewaySecret, string applicationName)
        {
            Response resp = new Response(StatusCode.Valid);

            OneKey authKey = new OneKey();

            authKey.Add(CoseKeyKeys.KeyType, GeneralValues.KeyType_Octet);
            authKey.Add(CoseKeyParameterKeys.Octet_k, CBORObject.FromObject(Encoding.UTF8.GetBytes(GatewaySecret)));
            authKey.Add(CoseKeyKeys.KeyIdentifier, CBORObject.FromObject(Encoding.UTF8.GetBytes("Client_identity")));
            DTLSClientEndPoint ep = new DTLSClientEndPoint(authKey);

            ep.Start();

            Request r = new Request(Method.POST);

            r.SetUri($"coaps://{_gatewayIp}" + $"/{(int)TradfriConstRoot.Gateway}/{(int)TradfriConstAttr.Auth}/");
            r.EndPoint   = ep;
            r.AckTimeout = 5000;
            r.SetPayload($@"{{""{(int)TradfriConstAttr.Identity}"":""{applicationName}""}}");

            r.Send();
            resp = r.WaitForResponse();

            if ((int)resp.StatusCode != 201)
            {
                RequestException <Response> .ConvertToException(MapToHttpStatusCode(resp.StatusCode), resp.StatusCode.ToString(), resp.UriQuery, "", resp.ResponseText, resp);
            }

            return(Convert <TradfriAuth>(resp.PayloadString));
        }
コード例 #2
0
        //This is the interface of the entire library. Every request that is made outside of this class will use this method to communicate.
        protected override async Task <string> HandleRequest(string url, Call call = Call.GET, List <Param> parameters = null, List <Param> headers = null, object content = null, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            Request request = new Request(ConvertToMethod(call));

            request.UriPath = url;

            if (content != null)
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };

                request.SetPayload(JsonConvert.SerializeObject(content, settings));
            }

            Task <Response> t = new Task <Response>(() =>
            {
                return(_coapClient.Send(request));
            });

            t.Start();

            Response resp = await t;

            if (MapToHttpStatusCode(resp.StatusCode) != (int)statusCode)
            {
                RequestException <Response> .ConvertToException(MapToHttpStatusCode(resp.StatusCode), resp.StatusCode.ToString(), resp.UriQuery, "", resp.ResponseText, resp);
            }

            return(resp.ResponseText);
        }
コード例 #3
0
        //This is the interface of the entire library. Every request that is made outside of this class will use this method to communicate.
        protected override async Task <string> HandleRequest(string url, Call call = Call.GET, List <Param> parameters = null, List <Param> headers = null, object content = null, HttpStatusCode statusCode = HttpStatusCode.OK)
        {
            Request request = new Request(ConvertToMethod(call));

            request.UriPath = url;

            // this is done on purpose to handle ObserveDevice from DeviceController
            if (statusCode.Equals(HttpStatusCode.Continue))
            {
                request.MarkObserve();
                request.Respond += (Object sender, ResponseEventArgs e) =>
                {
                    ((Action <Response>)content).Invoke(request.Response);
                };
            }

            if (content != null && !statusCode.Equals(HttpStatusCode.Continue))
            {
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                };

                request.SetPayload(JsonConvert.SerializeObject(content, settings));
            }

            Task <Response> t = new Task <Response>(() =>
            {
                return(_coapClient.Send(request));
            });

            t.Start();

            Response resp = await t;

            if (MapToHttpStatusCode(resp.StatusCode) != (int)statusCode)
            {
                RequestException <Response> .ConvertToException(MapToHttpStatusCode(resp.StatusCode), resp.StatusCode.ToString(), resp.UriQuery, "", resp.ResponseText, resp);
            }

            return(resp.ResponseText);
        }