Exemplo n.º 1
0
        private void TryUpdateDevice(SSDPMessage notifyMessage)
        {
            lock (this.devices)
            {
                var device = GetDevice(notifyMessage.UDN);

                if (device != null)
                {
                    device.MaxAge        = TimeSpan.FromSeconds(notifyMessage.MaxAge);
                    device.LastCheckTime = DateTime.UtcNow;

                    this.ScheduleNextCheckForExpiredDevices();

                    this.logger.LogInformation("The device lifetime has been updated", "DeviceName".As(device.FriendlyName), "DeviceUDN".As(device.UDN), "MaxAge".As(notifyMessage.MaxAge));
                }
            }
        }
Exemplo n.º 2
0
        private async void TryAddDevice(SSDPMessage notifyMessage)
        {
            if (this.IsDeviceAdded(notifyMessage.UDN))
            {
                this.TryUpdateDevice(notifyMessage);
            }
            else
            {
                try
                {
                    var responseStream = await HttpClientHelper.GetAsync(notifyMessage.Location);

                    using (responseStream)
                    {
                        lock (this.devices)
                        {
                            if (this.IsDeviceAdded(notifyMessage.UDN) == false)
                            {
                                var location = new Uri(notifyMessage.Location);
                                var host     = "{0}:{1}".F(location.Host, location.Port);

                                var device = ParseDevice(host, responseStream);
                                if (device != null)
                                {
                                    device.MaxAge        = TimeSpan.FromSeconds(notifyMessage.MaxAge);
                                    device.LastCheckTime = DateTime.UtcNow;

                                    this.devices.Add(device);
                                    this.logger.LogInformation("The device has been added", "DeviceName".As(device.FriendlyName), "DeviceUDN".As(device.UDN));
                                    this.devicesActivity.OnNext(new DeviceActivityEventArgs <TDevice> {
                                        Activity = DeviceActivity.Available, Device = device
                                    });

                                    this.ScheduleNextCheckForExpiredDevices();
                                }
                            }
                        }
                    }
                }
                catch (HttpRequestException ex)
                {
                    this.logger.LogWarning(ex, "Failed to load description for device with '{0}'".F(notifyMessage));
                }
            }
        }