Exemple #1
0
        public async Task TestDiscoverOnAllInterfaces()
        {
            // Arrange
            var mockInterface1 = new Mock <IOicTransport>();

            mockInterface1
            .Setup(b => b.BroadcastMessageAsync(It.IsAny <OicRequest>()))
            .Returns(Task.CompletedTask);
            var mockInterface2 = new Mock <IOicTransport>();

            mockInterface2
            .Setup(b => b.BroadcastMessageAsync(It.IsAny <OicRequest>()))
            .Returns(Task.CompletedTask);

            var client = new OicClient();

            client.AddTransport(mockInterface1.Object);
            client.AddTransport(mockInterface2.Object);

            // Act
            await new OicResourceDiscoverClient(client).DiscoverAsync();

            // Assert
            Mock.VerifyAll(mockInterface1, mockInterface2);
        }
Exemple #2
0
        public async Task Discover_InvokedTwiceAndCleared_TriggersNewDeviceTwice()
        {
            //Arange
            int newDeviceCallbackInvokations = 0;

            // Act
            using (var client = new OicClient())
            {
                var service = new OicResourceDiscoverClient(client);

                client.AddTransport(_mockTransport.Object);

                service.NewDevice += (s, e) =>
                {
                    newDeviceCallbackInvokations++;
                };

                await service.DiscoverAsync();

                await Task.Delay(100);

                await service.DiscoverAsync(true);

                await Task.Delay(100);
            }

            // Assert
            Assert.That(newDeviceCallbackInvokations, Is.EqualTo(2), $"{typeof(OicResourceDiscoverClient)}.{nameof(OicResourceDiscoverClient.NewDevice)} was not invoked twice");
        }
Exemple #3
0
        public void TestAddNullTransport()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var client = new OicClient();

                client.AddTransport(null);
            });
        }
Exemple #4
0
        static void Main(string[] args)
        {
            using (_client = new OicClient())
            {
                // Add a CoapTransport with a system assigned UDP Endpoint
                _client.AddTransport(new OicCoapTransport(new CoapUdpEndPoint()));

                // Create a discover client
                var discoverClient = new OicResourceDiscoverClient(_client);
                discoverClient.NewDevice += OnNewDevice;

                // Broadcast a discover request (GET /oic/res)
                discoverClient.DiscoverAsync().Wait();

                Console.WriteLine("Press <Enter> to exit");
                Console.ReadLine();
            }
        }
Exemple #5
0
        public async Task TestDiscoverDevice()
        {
            //Arange
            bool      newDeviceCallbackInvoked = false;
            OicDevice actualDevice             = null;

            // Act
            using (var client = new OicClient())
            {
                var service = new OicResourceDiscoverClient(client);

                client.AddTransport(_mockTransport.Object);

                service.NewDevice += (s, e) =>
                {
                    newDeviceCallbackInvoked = true;
                    actualDevice             = e.Device;
                };

                await service.DiscoverAsync();

                await Task.Delay(100);
            }

            // Assert
            Assert.IsTrue(newDeviceCallbackInvoked, $"{typeof(OicResourceDiscoverClient)}.{nameof(OicResourceDiscoverClient.NewDevice)} was not invoked");

            var expectedDevice = new OicDevice()
            {
                DeviceId  = Guid.Parse("0685B960-736F-46F7-BEC0-9E6CBD61ADC1"),
                Resources =
                {
                    new CoreResources.OicDeviceResource
                    {
                        RelativeUri   = "/oid/d",
                        ResourceTypes ={ "oic.d.light",                     "oic.wk.d" },
                        Interfaces    = OicResourceInterface.ReadOnly | OicResourceInterface.Baseline,
                    },
                    new CoreResources.OicPlatformResource
                    {
                        RelativeUri   = "/oid/p",
                        ResourceTypes ={ "oic.wk.p" },
                        Interfaces    = OicResourceInterface.ReadOnly | OicResourceInterface.Baseline,
                    },
                    new ResourceTypes.SwitchBinary
                    {
                        RelativeUri   = "/switch",
                        ResourceTypes ={ "oic.r.switch.binary" },
                        Interfaces    = OicResourceInterface.Actuator | OicResourceInterface.Baseline,
                    },
                    new ResourceTypes.LightBrightness
                    {
                        RelativeUri   = "/brightness",
                        ResourceTypes ={ "oic.r.light.brightness" },
                        Interfaces    = OicResourceInterface.Actuator | OicResourceInterface.Baseline,
                    }
                }
            };

            Assert.AreEqual(expectedDevice.DeviceId, actualDevice.DeviceId);
            Assert.AreEqual(expectedDevice.Resources, actualDevice.Resources);
        }