Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        public Task <int[]> GetDeviceIds(CancellationToken cancellationToken = default)
        {
            if (!_connected)
            {
                throw new InvalidOperationException("Please connect to Gateway");
            }

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

            return(_coapClient.RequestAsync <int[]>(request, cancellationToken));
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        public async Task <Credentials> AuthenticateAsyncInternal(string secret, CancellationToken cancellationToken = default)
        {
            await ConnectAsync(new Credentials { Identity = "Client_identity", PresharedKey = _settings.Secret }, cancellationToken);

            var identity = RandomString(16);
            var payload  = new AuthenticationRequest {
                Identity = identity
            };
            var request = new CoapRequestBuilder()
                          .WithMethod(CoapRequestMethod.Post)
                          .WithPath(ApiEndPoint.Authentication)
                          .WithPayload(JsonSerializer.Serialize(payload))
                          .Build();

            var authResponse = await _coapClient.RequestAsync <AuthenticationResponse>(request, cancellationToken);

            return(new Credentials {
                Identity = identity, PresharedKey = authResponse.PreshardKey
            });
        }
Exemplo n.º 5
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));
            }
        }
Exemplo n.º 6
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();
            }
        }