예제 #1
0
        static async Task Main()
        {
            var coapFactory = new CoapFactory();

            coapFactory.DefaultLogger.RegisterSink(new CoapNetLoggerConsoleSink());

            using var coapClient = coapFactory.CreateClient();

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

            var connectOptions = new CoapClientConnectOptionsBuilder()
                                 .WithHost("GW-B8D7AF2B3EA3.fritz.box")
                                 .WithHost("127.0.0.1")
                                 .WithDtlsTransportLayer(o =>
                                                         o.WithPreSharedKey("727360dd-d27a-4ca0-9be8-f626de849d7a", "7x3A1gqWvu9cBGD7"))
                                 .Build();

            using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
            {
                await coapClient.ConnectAsync(connectOptions, cancellationTokenSource.Token);
            }

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

            using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
            {
                var response = await coapClient.RequestAsync(request, cancellationTokenSource.Token);

                PrintResponse(response);
            }
        }
예제 #2
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", 1000));

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

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

            var connectOptions = connectOptionsBuilder.Build();

            var coapClient = _coapFactory.CreateClient();

            try
            {
                using (var cancellationTokenSource = new CancellationTokenSource(timeout))
                {
                    Console.WriteLine("CONNECTING COAP CLIENT");
                    await coapClient.ConnectAsync(connectOptions, cancellationTokenSource.Token).ConfigureAwait(false);
                }
            }
            catch
            {
                coapClient?.Dispose();
                throw;
            }

            return(coapClient);
        }
예제 #3
0
        static async Task Main2()
        {
            var coapFactory = new CoapFactory();

            coapFactory.DefaultLogger.RegisterSink(new CoapNetLoggerConsoleSink());

            using (var coapClient = coapFactory.CreateClient())
            {
                Console.WriteLine("< CONNECTING...");

                var connectOptions = new CoapClientConnectOptionsBuilder()
                                     .WithHost("coap.me")
                                     .Build();

                await coapClient.ConnectAsync(connectOptions, CancellationToken.None).ConfigureAwait(false);

                // separate

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

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

                PrintResponse(response);

                // hello

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Get)
                          .WithPath("hello")
                          .Build();

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

                PrintResponse(response);

                // broken

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Get)
                          .WithPath("broken")
                          .Build();

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

                PrintResponse(response);

                // secret

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Get)
                          .WithPath("secret")
                          .Build();

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

                PrintResponse(response);

                // large-create

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Get)
                          .WithPath("large-create")
                          .Build();

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

                PrintResponse(response);

                // location1/location2/location3

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Get)
                          .WithPath("location1/location2/location3")
                          .Build();

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

                PrintResponse(response);

                // large

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Get)
                          .WithPath("large")
                          .Build();

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

                PrintResponse(response);

                await Task.Delay(TimeSpan.FromSeconds(10));
            }
        }
예제 #4
0
        static async Task Main()
        {
            //await Main2();

            //Console.WriteLine("Press any key to exit.");
            //Console.ReadLine();

            var coapFactory = new CoapFactory();

            coapFactory.DefaultLogger.RegisterSink(new CoapNetLoggerConsoleSink());

            using (var coapClient = coapFactory.CreateClient())
            {
                Console.WriteLine("< CONNECTING...");

                var connectOptions = new CoapClientConnectOptionsBuilder()
                                     .WithHost("GW-B8D7AF2B3EA3.fritz.box")
                                     .WithDtlsTransportLayer(o =>
                                                             o.WithPreSharedKey("IDENTITY", "lqxbBH6o2eAKSo5A"))
                                     .Build();

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

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

                var response = await coapClient.RequestAsync(request, CancellationToken.None).ConfigureAwait(false);

                PrintResponse(response);

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Get)
                          .WithPath("15001/65550")
                          .Build();

                response = await coapClient.RequestAsync(request, CancellationToken.None).ConfigureAwait(false);

                PrintResponse(response);

                request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Put)
                          .WithPath("15001/65550")
                          .WithPayload("{\"3311\": [{\"5850\": 1}]}")
                          .Build();

                response = await coapClient.RequestAsync(request, CancellationToken.None).ConfigureAwait(false);

                PrintResponse(response);

                var observeOptions = new CoapObserveOptionsBuilder()
                                     .WithPath("15001/65550")
                                     .WithResponseHandler(new ResponseHandler())
                                     .Build();

                var observeResponse = await coapClient.ObserveAsync(observeOptions, CancellationToken.None).ConfigureAwait(false);

                PrintResponse(observeResponse.Response);

                Console.WriteLine("Observed messages for lamp!");

                Console.WriteLine("Press any key to unobserve.");
                Console.ReadLine();

                await coapClient.StopObservationAsync(observeResponse, CancellationToken.None).ConfigureAwait(false);

                Console.WriteLine("Press any key to exit.");
                Console.ReadLine();
            }
        }