예제 #1
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="discoveredDevice">A <see cref="DiscoveredSsdpDevice"/> instance representing the available device.</param>
        /// <param name="isNewlyDiscovered">A boolean value indicating whether or not this device came from the cache. See <see cref="IsNewlyDiscovered"/> for more detail.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="discoveredDevice"/> parameter is null.</exception>
        public DeviceAvailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool isNewlyDiscovered)
        {
            if (discoveredDevice == null) throw new ArgumentNullException("discoveredDevice");

            _DiscoveredDevice = discoveredDevice;
            _IsNewlyDiscovered = isNewlyDiscovered;
        }
예제 #2
0
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="discoveredDevice">A <see cref="DiscoveredSsdpDevice"/> instance representing the device that has become unavailable.</param>
        /// <param name="expired">A boolean value indicating whether this device is unavailable because it expired, or because it explicitly sent a byebye notification.. See <see cref="Expired"/> for more detail.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="discoveredDevice"/> parameter is null.</exception>
        public DeviceUnavailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool expired)
        {
            if (discoveredDevice == null) throw new ArgumentNullException("discoveredDevice");

            _DiscoveredDevice = discoveredDevice;
            _Expired = expired;
        }
예제 #3
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);
        }
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="discoveredDevice">A <see cref="DiscoveredSsdpDevice"/> instance representing the device that has become unavailable.</param>
        /// <param name="expired">A boolean value indicating whether this device is unavailable because it expired, or because it explicitly sent a byebye notification.. See <see cref="Expired"/> for more detail.</param>
        /// <exception cref="ArgumentNullException">Thrown if the <paramref name="discoveredDevice"/> parameter is null.</exception>
        public DeviceUnavailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool expired)
        {
            if (discoveredDevice == null)
            {
                throw new ArgumentNullException(nameof(discoveredDevice));
            }

            _DiscoveredDevice = discoveredDevice;
            _Expired          = expired;
        }
        /// <summary>
        /// Full constructor.
        /// </summary>
        /// <param name="discoveredDevice">A <see cref="DiscoveredSsdpDevice"/> instance representing the available device.</param>
        /// <param name="isNewlyDiscovered">A boolean value indicating whether or not this device came from the cache. See <see cref="IsNewlyDiscovered"/> for more detail.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="discoveredDevice"/> parameter is null.</exception>
        public DeviceAvailableEventArgs(DiscoveredSsdpDevice discoveredDevice, bool isNewlyDiscovered)
        {
            if (discoveredDevice == null)
            {
                throw new ArgumentNullException("discoveredDevice");
            }

            _DiscoveredDevice  = discoveredDevice;
            _IsNewlyDiscovered = isNewlyDiscovered;
        }
예제 #6
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 UpnpDeviceFound(DiscoveredSsdpDevice device)
        {
            try
            {
                var response = await CreateUpnpDeviceAsync(device);

                if (response == null)
                {
                    return;
                }

                var key = $"{device.DescriptionLocation.Host}/{response.Usn}";

                DateTime lastOccurrence;
                if (_occurrences.TryGetValue(key, out lastOccurrence))
                {
                    if ((DateTime.UtcNow - lastOccurrence) < TimeSpan.FromSeconds(5))
                    {
                        return;
                    }
                }

                _occurrences.AddOrUpdate(key, DateTime.UtcNow, (k, v) => DateTime.UtcNow);

                OnDeviceFound(response);
            }
            catch (HttpRequestException)
            {
            }
            catch (WebException)
            {
            }
            catch (Exception e)
            {
                _log.Error(e.Message, e);
            }
        }
예제 #8
0
        public void DiscoveredDevice_ToStringReturnsUsn()
        {
            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.Usn = "test usn";
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = TimeSpan.FromSeconds(1);
            System.Threading.Thread.Sleep(1000);

            Assert.AreEqual(discoveredDevice.Usn, discoveredDevice.ToString());
        }
예제 #9
0
        public void DiscoveredDevice_IsExpired_ReportsTrueAfterCacheLifetimeExpires()
        {
            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = TimeSpan.FromMilliseconds(100);
            System.Threading.Thread.Sleep(500);

            Assert.IsTrue(discoveredDevice.IsExpired());
        }
예제 #10
0
        public void DiscoveredDevice_IsExpired_ImmediatelyReportsTrueIfCacheLifetimeIsZero()
        {
            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = TimeSpan.Zero;

            Assert.IsTrue(discoveredDevice.IsExpired());
        }
예제 #11
0
        public void DiscoveredDevice_IsExpired_DoesNotImmediatelyReportTrue()
        {
            var discoveredDevice = new DiscoveredSsdpDevice();
            discoveredDevice.AsAt = DateTimeOffset.Now;
            discoveredDevice.CacheLifetime = TimeSpan.FromSeconds(1);

            Assert.IsFalse(discoveredDevice.IsExpired());
        }
        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;
        }