Exemplo n.º 1
0
        protected override void DoGet(CoapExchange exchange)
        {
            // Accept the request to promise the client this request will be acted.
            exchange.Accept();

            // ... and then do nothing. Pretty mean...
        }
Exemplo n.º 2
0
 protected override void DoPut(CoapExchange exchange)
 {
     Assert.AreEqual(_currentRequestText, exchange.Request.PayloadString);
     exchange.Accept();
     _currentResponseText = "";
     exchange.Respond(StatusCode.Changed);
 }
Exemplo n.º 3
0
        protected override void DoPost(CoapExchange exchange)
        {
            try {
                Request    request = exchange.Request;
                CBORObject obj     = CBORObject.DecodeFromBytes(request.Payload);

                exchange.Accept();

                String uri = obj[0].AsString();
                byte[] key = obj[1].GetByteString();
                List <SecurityContext> contexts = SecurityContextSet.AllContexts.FindByKid(key);
                if (contexts.Count == 0)
                {
                    exchange.Respond(StatusCode.BadRequest, "No matching key identifier found");
                    return;
                }

                Codec.IMessageDecoder me = Spec.Default.NewMessageDecoder(obj[2].GetByteString());
                Request newRequest       = me.DecodeRequest();

                newRequest.URI           = new System.Uri(uri);
                newRequest.OscoapContext = contexts[0];

                newRequest.Send();
                Response response = newRequest.WaitForResponse();

                exchange.Respond(response);
            }
            catch (Exception e) {
                exchange.Respond(StatusCode.BadRequest, e.ToString());
            }
        }
Exemplo n.º 4
0
 protected override void DoGet(CoapExchange exchange)
 {
     if (_mode == Mode.Separate)
     {
         exchange.Accept();
     }
     _currentResponseText = "hello get " + _status;
     exchange.Respond(_currentResponseText);
 }
Exemplo n.º 5
0
        protected override void DoGet(CoapExchange exchange)
        {
            exchange.Accept();

            Thread.Sleep(1000);

            Request       req = exchange.Request;
            StringBuilder sb  = new StringBuilder("Deferred Response");

            exchange.Respond(StatusCode.Content, sb.ToString());
        }
Exemplo n.º 6
0
            protected override void DoPost(CoapExchange exchange)
            {
                Assert.AreEqual(_currentRequestText, exchange.Request.PayloadString);
                if (_mode == Mode.Separate)
                {
                    exchange.Accept();
                }

                Console.WriteLine("TestResource " + Name + " received POST message: " + exchange.Request.PayloadString);
                String ten = "123456789.";

                _currentResponseText = "hello post " + _status + ten + ten + ten;
                exchange.Respond(StatusCode.Created, _currentResponseText);
            }
Exemplo n.º 7
0
        protected override void DoGet(CoapExchange exchange)
        {
            // Accept the request to promise the client this request will be acted.
            exchange.Accept();

            // Do sth. time-consuming
            Thread.Sleep(2000);

            // Now respond the previous request.
            Response response = new Response(StatusCode.Content);

            response.PayloadString = "This message was sent by a separate response.\n" +
                                     "Your client will need to acknowledge it, otherwise it will be retransmitted.";

            exchange.Respond(response);
        }
Exemplo n.º 8
0
        protected override void DoPost(CoapExchange exchange)
        {
            Task.Run(async() =>
            {
                try
                {
                    Int32 ct = MediaType.TextPlain;
                    Dictionary <string, object> keyValues = new Dictionary <string, object>();
                    if ((ct = MediaType.NegotiationContent(ct, _supported, exchange.Request.GetOptions(OptionType.Accept))) == MediaType.Undefined)
                    {
                        exchange.Respond(StatusCode.NotAcceptable, "supported list: ApplicationJson,TextPlain,TextXml,ApplicationOctetStream");
                        exchange.Reject();
                    }
                    else
                    {
                        if (!exchange.Request.UriQueries.Any())
                        {
                            exchange.Respond(StatusCode.BadRequest, "Forgot the parameters?");
                            exchange.Reject();
                        }
                        else
                        {
                            var querys   = exchange.Request.UriQueries.ToArray();
                            var acctoken = exchange.Request.UriQueries.FirstOrDefault();
                            switch (ct)
                            {
                            case MediaType.ApplicationJson:
                            case MediaType.TextPlain:
                                keyValues = JToken.Parse(exchange.Request.PayloadString)?.JsonToDictionary();
                                break;

                            case MediaType.TextXml:
                                if (querys.Length >= 2)
                                {
                                    var xml = new System.Xml.XmlDocument();
                                    try
                                    {
                                        xml.LoadXml(exchange.Request.PayloadString);
                                    }
                                    catch (Exception ex)
                                    {
                                        exchange.Respond(StatusCode.BadRequest, $"Can't load xml ,{ex.Message}");
                                    }
                                    keyValues.Add(querys[1], xml);
                                }
                                else
                                {
                                    exchange.Respond(StatusCode.BadRequest, "You did not specify  key name for xml.");
                                    exchange.Reject();
                                }
                                break;

                            case MediaType.ApplicationOctetStream:
                                if (querys.Length >= 2)
                                {
                                    keyValues.Add(querys[1], exchange.Request.Payload);
                                }
                                else
                                {
                                    exchange.Respond(StatusCode.BadRequest, "You did not specify  key name for binary.");
                                    exchange.Reject();
                                }
                                break;

                            default:
                                break;
                            }
                            var mcr = await _dbContext.DeviceIdentities.Include(d => d.Device).FirstOrDefaultAsync(di => di.IdentityType == IdentityType.AccessToken && di.IdentityId == acctoken);
                            var dev = mcr?.Device;
                            if (mcr != null && dev != null)
                            {
                                switch (_res)
                                {
                                case CoApRes.Attributes:
                                    _eventBus.PublishAttributeData(new RawMsg()
                                    {
                                        MsgType = MsgType.CoAP, MsgBody = keyValues, DataCatalog = DataCatalog.AttributeData, DataSide = DataSide.ClientSide, DeviceId = dev.Id
                                    });
                                    exchange.Respond(StatusCode.Changed, $"OK");
                                    break;

                                case CoApRes.Telemetry:
                                    _eventBus.PublishTelemetryData(new RawMsg()
                                    {
                                        MsgType = MsgType.CoAP, MsgBody = keyValues, DataCatalog = DataCatalog.AttributeData, DataSide = DataSide.ClientSide, DeviceId = dev.Id
                                    });
                                    exchange.Respond(StatusCode.Created, $"OK");
                                    break;

                                default:
                                    break;
                                }
                                exchange.Accept();
                            }
                            else
                            {
                                exchange.Respond(StatusCode.NotFound, "Can't found  device.");
                                exchange.Reject();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    exchange.Respond(StatusCode.BadRequest, ex.Message);
                    exchange.Reject();
                }
            });
        }
Exemplo n.º 9
0
 protected override void DoPost(CoapExchange exchange)
 {
     exchange.Accept();
     exchange.Respond(SERVER_RESPONSE);
 }