Example #1
0
        public void DiscoveredDevice_GetDeviceInfo_DoesNotMakeHttpRequestIfDataCached()
        {
            var publishedDevice = new SsdpRootDevice()
            {
                Location = new Uri("http://192.168.1.100:1702/description"),
                CacheLifetime = TimeSpan.FromMinutes(1),
                DeviceType = "TestDeviceType",
                Uuid = System.Guid.NewGuid().ToString()
            };

            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.Usn = "test usn";
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = publishedDevice.CacheLifetime;
            discoveredDevice.DescriptionLocation = publishedDevice.Location;

            var client = new MockHttpClient(publishedDevice.ToDescriptionDocument());
            var device = discoveredDevice.GetDeviceInfo(client).GetAwaiter().GetResult();

            client = new MockHttpClient(publishedDevice.ToDescriptionDocument());
            device = discoveredDevice.GetDeviceInfo(client).GetAwaiter().GetResult();

            Assert.IsNull(client.LastRequest);
            Assert.AreEqual(device.Uuid, publishedDevice.Uuid);
            Assert.AreEqual(device.DeviceType, publishedDevice.DeviceType);
        }
Example #2
0
        public void DiscoveredDevice_GetDeviceInfo_CreatesDefaultClient()
        {
            var publishedDevice = new SsdpRootDevice()
            {
                Location = new Uri("http://192.168.1.100:1702/description"),
                CacheLifetime = TimeSpan.FromMinutes(1),
                DeviceType = "TestDeviceType",
                Uuid = System.Guid.NewGuid().ToString()
            };

            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.Usn = "test usn";
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = TimeSpan.FromSeconds(1);
            discoveredDevice.DescriptionLocation = publishedDevice.Location;

            var device = discoveredDevice.GetDeviceInfo().GetAwaiter().GetResult();
        }
        private async Task<UpnpDeviceResponse> CreateUpnpDeviceAsync(DiscoveredSsdpDevice device)
        {
            var info = await device.GetDeviceInfo();
            var headers = device.ResponseHeaders?.ToDictionary(
                h => h.Key, h => string.Join(" ", h.Value), StringComparer.OrdinalIgnoreCase);

            headers = headers ?? new Dictionary<string, string>(0);

            string server;
            string location;
            string usn = info.Udn;

            if (!headers.TryGetValue("location", out location))
            {
                location = device.DescriptionLocation.AbsoluteUri;
            }

            if (!headers.TryGetValue("server", out server))
            {
                server = string.Empty;
            }

            var response = new UpnpDeviceResponse(location, server, usn)
            {
                FriendlyName = info.FriendlyName,
                Manufacturer = info.Manufacturer,
                ModelName = info.ModelName
            };

            foreach (var keyValuePair in headers)
            {
                response.AddHeader(keyValuePair.Key, keyValuePair.Value);
            }

            return response;
        }