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
        private PythonDictionary ToPythonDictionary(DiscoveredSsdpDevice device)
        {
            var headersWrapper = new List();

            foreach (var header in device.ResponseHeaders)
            {
                if (header.Value == null)
                {
                    continue;
                }

                headersWrapper.Add(new PythonDictionary().WithValue("key", header.Key).WithValue("value", header.Value.FirstOrDefault()));
            }

            var deviceInfo = device.GetDeviceInfo().GetAwaiter().GetResult();

            var servicesWrapper = new List();

            foreach (var service in deviceInfo.Services)
            {
                servicesWrapper.Add(new PythonDictionary()
                                    .WithValue("control_url", service.ControlUrl.ToString())
                                    .WithValue("event_sub_url", service.EventSubUrl.ToString())
                                    .WithValue("full_service_type", service.FullServiceType)
                                    .WithValue("scpd_url", service.ScpdUrl.ToString())
                                    .WithValue("service_id", service.ServiceId)
                                    .WithValue("service_type", service.ServiceType)
                                    .WithValue("service_type_namespace", service.ServiceTypeNamespace)
                                    .WithValue("service_version", service.ServiceVersion)
                                    .WithValue("uuid", service.Uuid));
            }

            return(new PythonDictionary()
                   .WithValue("usn", device.Usn)
                   .WithValue("as_at", device.AsAt.ToString("O"))
                   .WithValue("description_location", device.DescriptionLocation.ToString())
                   .WithValue("cache_lifetime", device.CacheLifetime.ToString("c"))
                   .WithValue("notification_type", device.NotificationType)
                   .WithValue("response_headers", headersWrapper)
                   .WithValue("device_type", deviceInfo.DeviceType)
                   .WithValue("device_type_namespace", deviceInfo.DeviceTypeNamespace)
                   .WithValue("friendly_name", deviceInfo.FriendlyName)
                   .WithValue("full_device_type", deviceInfo.FullDeviceType)
                   .WithValue("device_version", deviceInfo.DeviceVersion)
                   .WithValue("serial_number", deviceInfo.SerialNumber)
                   .WithValue("udn", deviceInfo.Udn)
                   .WithValue("upc", deviceInfo.Upc)
                   .WithValue("uuid", deviceInfo.Uuid)
                   .WithValue("manufacturer", deviceInfo.Manufacturer)
                   .WithValue("manufacturer_url", deviceInfo.ManufacturerUrl.ToString())
                   .WithValue("model_description", deviceInfo.ModelDescription)
                   .WithValue("model_name", deviceInfo.ModelName)
                   .WithValue("model_number", deviceInfo.ModelNumber)
                   .WithValue("services", servicesWrapper));
        }
        private async Task <ChromecastOptions> GetChromecastDetails(DiscoveredSsdpDevice foundDevice)
        {
            if (foundDevice == null)
            {
                return(null);
            }

            var fullDevice = await foundDevice.GetDeviceInfo();

            var chromecast = new ChromecastOptions
            {
                Ip   = foundDevice.DescriptionLocation.Host,
                Name = fullDevice.FriendlyName
            };

            return(chromecast);
        }
Example #4
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);
        }