Exemple #1
0
        async Task <ICoapClient> CreateClient(PythonDictionary parameters)
        {
            var host     = Convert.ToString(parameters.get("host", string.Empty));
            var port     = Convert.ToInt32(parameters.get("port", 5684));
            var protocol = Convert.ToString(parameters.get("protocol", "dtls"));
            var identity = Convert.ToString(parameters.get("identity", string.Empty));
            var key      = Convert.ToString(parameters.get("key", string.Empty));
            var timeout  = Convert.ToInt32(parameters.get("timeout", 5000));

            var connectOptionsBuilder = new CoapClientConnectOptionsBuilder()
                                        .WithHost(host)
                                        .WithPort(port);

            if (protocol == "dtls")
            {
                connectOptionsBuilder.WithDtlsTransportLayer(o => o.WithPreSharedKey(identity, key));
            }

            var connectOptions = connectOptionsBuilder.Build();

            var coapClient = new CoapFactory().CreateClient();

            using (var cancellationTokenSource = new CancellationTokenSource(timeout))
            {
                await coapClient.ConnectAsync(connectOptions, cancellationTokenSource.Token).ConfigureAwait(false);
            }

            return(coapClient);
        }
Exemple #2
0
 public async Task Connect_Invalid_Host()
 {
     using (var coapClient = new CoapFactory().CreateClient())
     {
         await coapClient.ConnectAsync(new CoapClientConnectOptions()
         {
             Host = "invalid_host"
         }, CancellationToken.None);
     }
 }
Exemple #3
0
        public async Task Connect_Valid_Host_With_Invalid_Port_Dtls()
        {
            using (var coapClient = new CoapFactory().CreateClient())
            {
                var options = new CoapClientConnectOptionsBuilder()
                              .WithHost("127.0.0.1")
                              .WithPort(5555)
                              .WithDtlsTransportLayer(o =>
                {
                    o.WithPreSharedKey("a", "b");
                })
                              .Build();

                await coapClient.ConnectAsync(options, CancellationToken.None);
            }
        }
Exemple #4
0
        public async Task Timeout_When_No_Response_Received()
        {
            using (var coapClient = new CoapFactory().CreateClient())
            {
                var options = new CoapClientConnectOptionsBuilder()
                              .WithHost("127.0.0.1")
                              .WithPort(5555)
                              .Build();

                await coapClient.ConnectAsync(options, CancellationToken.None);

                var request = new CoapRequestBuilder()
                              .WithMethod(CoapRequestMethod.Get)
                              .Build();

                await coapClient.RequestAsync(request, CancellationToken.None);
            }
        }
Exemple #5
0
        static async Task Main()
        {
            var optionBuilder = new CoapMessageOptionFactory();

            var coapClient = new CoapFactory().CreateClient();

            Console.WriteLine("CONNECTING...");

            var request = new CoapRequest
            {
                Method = CoapRequestMethod.Get,
                Uri    = "15001"
            };

            await coapClient.ConnectAsync(new CoapClientConnectOptions
            {
                Host           = "192.168.1.228",
                Port           = 5684,
                TransportLayer = new UdpWithDtlsCoapTransportLayer()
                {
                    Credentials = new PreSharedKey
                    {
                        Identity = Encoding.ASCII.GetBytes("IDENTITY"),
                        Key      = Encoding.ASCII.GetBytes("lqxbBH6o2eAKSo5A")
                    }
                }
            }, CancellationToken.None);

            var response = await coapClient.Request(request, CancellationToken.None);

            Console.WriteLine("DATA RECEIVED");

            Console.WriteLine("Code = " + response.StatusCode);
            Console.WriteLine("Payload = " + Encoding.ASCII.GetString(response.Payload.ToArray()));

            Console.ReadLine();
        }