Beispiel #1
0
        private void dowork(object o, DoWorkEventArgs args)
        {
            BackgroundWorker b = o as BackgroundWorker;

            StringBuilder sb           = new StringBuilder();
            var           finder       = new UPnPDeviceFinder();
            var           foundDevices = new List <UPnPDevice>();

            var deviceType = "upnp:rootdevice";
            var devices    = finder.FindByType(deviceType, 1);

            foreach (UPnPDevice upnpDevice in devices)
            {
                sb.AppendHtmlLine("FriendlyName: " + upnpDevice.FriendlyName);
                sb.AppendHtmlLine("Description: " + upnpDevice.Description);
                sb.AppendHtmlLine("PresentationURL: " + upnpDevice.PresentationURL);


                string port    = new Uri(upnpDevice.PresentationURL).Port.ToString();
                string baseUrl = new Uri(upnpDevice.PresentationURL).DnsSafeHost.ToString();

                sb.AppendHtmlLine("Type: " + upnpDevice.Type);
                sb.AppendHtmlLine("Port: " + port);
                sb.AppendHtmlLine("baseUrl: " + baseUrl);

                sb.AppendLine("<hr/>");
            }

            WebPage.DisplayWebPage("Universal Plug and Play devices", sb.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// Performs a search for UPnP devices on all active network interfaces.
        /// </summary>
        /// <param name="searchTarget">The search target. If this is null, a search
        /// for all UPnP devices will be performed.</param>
        /// <param name="timeout">The timespan after which to cancel the search
        /// and return the results to the caller. If this is null, the method blocks
        /// until the search has been completed.</param>
        /// <returns>An enumerable collection of discovered UPnP devices.</returns>
        /// <exception cref="InvalidOperationException">An error occurred while
        /// performing an SSDP search-operation.</exception>
        /// <remarks>
        /// A full UPnP search can take 9 seconds or longer;
        ///
        /// Possible values for the searchTarget parameter include:
        ///  * ssdp:all (searches for all devices and services)
        ///  * ssdp:rootdevice (searches for root devices only)
        /// For details on all possible values for the searchTarget parameter, refer
        /// to the 'UPnP Device Architecture 1.1' document, page 33.
        /// </remarks>
        static IEnumerable <UPnPDevice> FindDevices(string searchTarget = null,
                                                    TimeSpan?timeout    = null)
        {
            UPnPDeviceFinder     deviceFinder = new UPnPDeviceFinder();
            DeviceFinderCallback dfCallback   = new DeviceFinderCallback();

            if (searchTarget == null)
            {
                searchTarget = "ssdp:all";
            }
            int findHandle = deviceFinder.CreateAsyncFind(searchTarget, 0, dfCallback);

            if (findHandle == 0)
            {
                throw new InvalidOperationException("Asynchronous search operation could " +
                                                    "not be created.");
            }
            // Start the asynchronous search.
            deviceFinder.StartAsyncFind(findHandle);
            if (timeout == null)
            {
                timeout = TimeSpan.FromMilliseconds(-1);
            }
            // Wait until the search has been completed or the specified timeout has
            // expired.
            if (!dfCallback.SearchCompleted.WaitOne(timeout.Value))
            {
                deviceFinder.CancelAsyncFind(findHandle);
            }
            // The Devices property of the DeviceFinderCallback contains the UPnP devices
            // that were discovered.
            return(dfCallback.Devices);
        }
Beispiel #3
0
        public static bool Discover()
        {
            service = null;
            UPnPDeviceFinder finder = (UPnPDeviceFinder)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("E2085F28-FEB7-404A-B8E7-E659BDEAAA02")));

            service = CheckDevices(finder.FindByType("urn:schemas-upnp-org:device:InternetGatewayDevice:1", 0));
            return(service != null);
        }
Beispiel #4
0
 /// <summary>
 /// Internal dispose method.
 /// </summary>
 /// <param name="disposeManaged">True to dispose managed objects.</param>
 protected virtual void Dispose(bool disposeManaged)
 {
     if (disposeManaged)
     {
         if (mfFinder != null)
         {
             Stop();
             mfFinder = null;
         }
     }
 }
Beispiel #5
0
        /// <summary>
        /// Finds a native device by UDN.
        /// </summary>
        /// <param name="udn">The UDN for the device.</param>
        /// <param name="addressFamily">The address family to search in. (Vista or above only).</param>
        /// <returns>The device if one is found or null if not found.</returns>
        internal static IUPnPDevice FindNativeDeviceByUDN(
            string udn, AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth)
        {
            UPnPDeviceFinder lfFinder = new UPnPDeviceFinder();

            if (lfFinder is IUPnPAddressFamilyControl)
            {
                ((IUPnPAddressFamilyControl)lfFinder).SetAddressFamily((int)addressFamily);
            }

            return(lfFinder.FindByUDN(udn));
        }
        public static List<MediaServer> All()
        {
            List<MediaServer> mediaServers = new List<MediaServer>();
            UPnPDeviceFinder deviceFinder = new UPnPDeviceFinder();
            UPnPDevices mediaServerDevices = deviceFinder.FindByType("urn:schemas-upnp-org:device:MediaServer:1", 0);
            foreach (UPnPDevice mediaServerDevice in mediaServerDevices)
            {
                mediaServers.Add(new MediaServer(mediaServerDevice, mediaServerDevice.FriendlyName));
            }

            return mediaServers;
        }
Beispiel #7
0
        /// <summary>
        /// Finds native devices by device or service type.
        /// </summary>
        /// <param name="deviceOrServiceType">The partial or full device or service type to search for.</param>
        /// <param name="addressFamily">The address family to search in. (Vista or above only).</param>
        /// <returns>A UPnP Devices collection containing the found devices.</returns>
        internal static IUPnPDevices FindNativeDevices(
            string deviceOrServiceType,
            AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth)
        {
            UPnPDeviceFinder lfFinder = new UPnPDeviceFinder();

            if (lfFinder is IUPnPAddressFamilyControl)
            {
                ((IUPnPAddressFamilyControl)lfFinder).SetAddressFamily((int)addressFamily);
            }

            return(lfFinder.FindByType(deviceOrServiceType, 0));
        }
        public static List <MediaServer> All()
        {
            List <MediaServer> mediaServers       = new List <MediaServer>();
            UPnPDeviceFinder   deviceFinder       = new UPnPDeviceFinder();
            UPnPDevices        mediaServerDevices = deviceFinder.FindByType("urn:schemas-upnp-org:device:MediaServer:1", 0);

            foreach (UPnPDevice mediaServerDevice in mediaServerDevices)
            {
                mediaServers.Add(new MediaServer(mediaServerDevice, mediaServerDevice.FriendlyName));
            }

            return(mediaServers);
        }
Beispiel #9
0
        public void Refresh()
        {
            UPnPDeviceFinder finder = new UPnPDeviceFinder();
            UPnPDevices      devs   = finder.FindByType("upnp:rootdevice", 0);

            lock (this)
            {
                this.devices.Clear();
                foreach (UPnPDevice device in GetAllDevices(devs.OfType <UPnPDevice>()))
                {
                    this.devices[device.UniqueDeviceName] = device;
                }
            }
        }
Beispiel #10
0
        private void runGetAllDevices()
        {
            BackgroundWorker bw = new BackgroundWorker();

            // this allows our worker to report progress during work
            bw.WorkerReportsProgress = true;

            // what to do in the background thread
            bw.DoWork += new DoWorkEventHandler(
                delegate(object o, DoWorkEventArgs args)
            {
                BackgroundWorker b = o as BackgroundWorker;
                var finder         = new UPnPDeviceFinder();
                var foundDevices   = new List <UPnPDevice>();

                var deviceType = "upnp:rootdevice";
                var devices    = finder.FindByType(deviceType, 1);

                foreach (UPnPDevice upnpDevice in devices)
                {
                    string port    = new Uri(upnpDevice.PresentationURL).Port.ToString();
                    string baseUrl = new Uri(upnpDevice.PresentationURL).DnsSafeHost.ToString();

                    Console.WriteLine("Type: " + upnpDevice.Type);
                    Console.WriteLine("Port: " + port);
                    Console.WriteLine("baseUrl: " + baseUrl);
                }

                //                    b.ReportProgress(i * 10);
            });

            // what to do when progress changed (update the progress bar for example)
            bw.ProgressChanged += new ProgressChangedEventHandler(
                delegate(object o, ProgressChangedEventArgs args)
            {
                Console.WriteLine(string.Format("{0}% Completed", args.ProgressPercentage));
            });

            // what to do when worker completes its task (notify the user)
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
                delegate(object o, RunWorkerCompletedEventArgs args)
            {
                Console.WriteLine("Finished!");
            });

            bw.RunWorkerAsync();
        }
Beispiel #11
0
        private IEnumerable <WeMoDevice> Find()
        {
            UPnPDeviceFinder finder = new UPnPDeviceFinder();
            UPnPDevices      found  = finder.FindByType("urn:Belkin:device:controllee:1", 0);

            var devices = found.Cast <UPnPDevice>().Select(device =>
            {
                var uri = new Uri(device.PresentationURL);
                return(new WeMoDevice
                {
                    Name = device.FriendlyName,
                    IpAddress = uri.Host,
                    Port = uri.Port
                });
            });

            return(devices.ToList());
        }
        private void FillList()
        {
             
           var finder = new UPnPDeviceFinder();
           var devices = finder.FindByType("upnp:rootdevice", 0);
            Console.WriteLine("FillList");
           //var devices = ScoutHelper.UpnpGetDevices();

            foreach (UPnPDevice upnpDevice in devices)
            {
                if (IsHueBridge(upnpDevice))
                {
                    var device = CreateDevice(upnpDevice);

                    currentDeviceList.InsertDevice(device);
                }
            }
        }
        private void FillList()
        {
            var finder  = new UPnPDeviceFinder();
            var devices = finder.FindByType("upnp:rootdevice", 0);

            Console.WriteLine("FillList");
            //var devices = ScoutHelper.UpnpGetDevices();

            foreach (UPnPDevice upnpDevice in devices)
            {
                if (IsHueBridge(upnpDevice))
                {
                    var device = CreateDevice(upnpDevice);

                    currentDeviceList.InsertDevice(device);
                }
            }
        }
Beispiel #14
0
        private void GetAllDevices()
        {
            var finder       = new UPnPDeviceFinder();
            var foundDevices = new List <UPnPDevice>();

            var deviceType = "upnp:rootdevice";
            var devices    = finder.FindByType(deviceType, 1);

            foreach (UPnPDevice upnpDevice in devices)
            {
                string port    = new Uri(upnpDevice.PresentationURL).Port.ToString();
                string baseUrl = new Uri(upnpDevice.PresentationURL).DnsSafeHost.ToString();

                Console.WriteLine("Type: " + upnpDevice.Type);
                Console.WriteLine("Port: " + port);
                Console.WriteLine("baseUrl: " + baseUrl);
            }
        }
Beispiel #15
0
        /// <summary>
        /// Gets the devices by upnp.
        /// </summary>
        /// <returns></returns>
        public static List <WeMoDevice> GetDevicesByUpnp()
        {
            UPnPDeviceFinder  finder       = new UPnPDeviceFinder();
            List <WeMoDevice> foundDevices = new List <WeMoDevice>();

            //
            //  Query all UPNP root devices
            //
            string      deviceType = "upnp:rootdevice";
            UPnPDevices devices    = finder.FindByType(deviceType, 1);

            //
            //  Iterate devices and create proper parent types based on WeMo device type and store in collection
            //
            foreach (UPnPDevice device in devices)
            {
                string type = device.Type;
                //Console.WriteLine("Found " + type);

                if (type.StartsWith("urn:Belkin:"))
                {
                    switch (GetDeviceType(device))
                    {
                    case WeMoDeviceType.Switch:
                        WeMoSwitch wemoSwitch = new WeMoSwitch(device);
                        foundDevices.Add(wemoSwitch);
                        break;

                    case WeMoDeviceType.Sensor:
                        WeMoSensor wemoSensor = new WeMoSensor(device);
                        foundDevices.Add(wemoSensor);
                        break;

                    default:
                        // TODO: Decide what to do with non Sensor/Switch devices
                        break;
                    }
                }
            }

            return(foundDevices);
        }
        private static void UpnpScan(object source, ElapsedEventArgs e)
        {
            //lets stop the timer while we do our work
            upnpScanTimer.Enabled = false;

            var finder = new UPnPDeviceFinder();
            var devices = finder.FindByType("upnp:rootdevice", 0);

            lock (lockObject)
            {
                upnpDevices.Clear();

                foreach (UPnPDevice device in devices)
                {
                    upnpDevices.Add(device);

                    //logger.Log("UPnPDevice: model={0} present={1} type={2} sn={3} upc={4} udn={5}", device.ModelURL, device.PresentationURL, device.Type, device.SerialNumber, device.UPC, device.UniqueDeviceName);
                }
            }

           //lets re-start the timer now
           upnpScanTimer.Enabled = true;
        }
Beispiel #17
0
        private static void UpnpScan(object source, ElapsedEventArgs e)
        {
            //lets stop the timer while we do our work
            upnpScanTimer.Enabled = false;

            var finder  = new UPnPDeviceFinder();
            var devices = finder.FindByType("upnp:rootdevice", 0);

            lock (lockObject)
            {
                upnpDevices.Clear();

                foreach (UPnPDevice device in devices)
                {
                    upnpDevices.Add(device);

                    //logger.Log("UPnPDevice: model={0} present={1} type={2} sn={3} upc={4} udn={5}", device.ModelURL, device.PresentationURL, device.Type, device.SerialNumber, device.UPC, device.UniqueDeviceName);
                }
            }

            //lets re-start the timer now
            upnpScanTimer.Enabled = true;
        }
Beispiel #18
0
 /// <summary>
 /// Internal dispose method.
 /// </summary>
 /// <param name="disposeManaged">True to dispose managed objects.</param>
 protected virtual void Dispose(bool disposeManaged)
 {
     if (disposeManaged)
     {
         if (mfFinder != null)
         {
             Stop();
             mfFinder = null;
         }
     }
 }
Beispiel #19
0
        /// <summary>
        /// Finds native devices by device or service type.
        /// </summary>
        /// <param name="deviceOrServiceType">The partial or full device or service type to search for.</param>
        /// <param name="addressFamily">The address family to search in. (Vista or above only).</param>
        /// <returns>A UPnP Devices collection containing the found devices.</returns>
        internal static IUPnPDevices FindNativeDevices(
            string deviceOrServiceType,
            AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth)
        {
            UPnPDeviceFinder lfFinder = new UPnPDeviceFinder();

            if (lfFinder is IUPnPAddressFamilyControl)
                ((IUPnPAddressFamilyControl)lfFinder).SetAddressFamily((int)addressFamily);

            return lfFinder.FindByType(deviceOrServiceType, 0);
        }
Beispiel #20
0
        /// <summary>
        /// Finds a native device by UDN.
        /// </summary>
        /// <param name="udn">The UDN for the device.</param>
        /// <param name="addressFamily">The address family to search in. (Vista or above only).</param>
        /// <returns>The device if one is found or null if not found.</returns>
        internal static IUPnPDevice FindNativeDeviceByUDN(
            string udn, AddressFamilyFlags addressFamily = AddressFamilyFlags.IPvBoth)
        {
            UPnPDeviceFinder lfFinder = new UPnPDeviceFinder();

            if (lfFinder is IUPnPAddressFamilyControl)
                ((IUPnPAddressFamilyControl)lfFinder).SetAddressFamily((int)addressFamily);

            return lfFinder.FindByUDN(udn);
        }
Beispiel #21
0
        private IEnumerable<WeMoDevice> Find()
        {
            UPnPDeviceFinder finder = new UPnPDeviceFinder();
            UPnPDevices found = finder.FindByType("urn:Belkin:device:controllee:1", 0);

            var devices = found.Cast<UPnPDevice>().Select(device =>
            {
                var uri = new Uri(device.PresentationURL);

                return new WeMoDevice
                {
                    Name = device.FriendlyName,
                    IpAddress = uri.Host,
                    Port = uri.Port
                };
            });

            return devices.ToList();
        }