How to discover Alpaca devices (polling and events) |
This topic describes how to discover Alpaca devices and options for creating Alpaca clients to communicate with them. Please see Alpaca devices and ASCOM devices for information on the Alpaca device / ASCOM device terminology.
Alpaca devices that support network discovery can be detected using the AlpacaDiscovery class.
The AlpacaDiscovery class supports two ways to discover devices:
1// Create a Discovery component that can be used for one or more discoveries 2using (AlpacaDiscovery alpacaDiscovery = new AlpacaDiscovery()) 3{ 4 // Start a discovery with default parameter values 5 alpacaDiscovery.StartDiscovery(); 6 7 // Wait for the discovery to complete, testing the completion variable every 50ms. 8 do 9 { 10 Thread.Sleep(50); 11 } while (!alpacaDiscovery.DiscoveryComplete); 12 13 // Get lists of discovered Telescope and FilterWheel devices and print counts of each 14 List<AscomDevice> telescopeDevices1 = alpacaDiscovery.GetAscomDevices(DeviceTypes.Telescope); 15 List<AscomDevice> filterWheelDevices1 = alpacaDiscovery.GetAscomDevices(DeviceTypes.FilterWheel); 16 Console.WriteLine($"Found {telescopeDevices1.Count} Telescope devices and {filterWheelDevices1.Count} FilterWheel devices."); 17 18 // Start a discovery with specified parameters 19 alpacaDiscovery.StartDiscovery(2, 100, 32227, 1.5, false, true, false, ServiceType.Http); 20 21 // Wait for the discovery to complete, testing the completion variable every 50ms. 22 do 23 { 24 Thread.Sleep(50); 25 } while (!alpacaDiscovery.DiscoveryComplete); 26 27 // Get lists of discovered Telescope and FilterWheel devices and print counts of each 28 List<AscomDevice> telescopeDevices2 = alpacaDiscovery.GetAscomDevices(DeviceTypes.Telescope); 29 List<AscomDevice> filterWheelDevices2 = alpacaDiscovery.GetAscomDevices(DeviceTypes.FilterWheel); 30 Console.WriteLine($"Found {telescopeDevices2.Count} Telescope devices and {filterWheelDevices2.Count} FilterWheel devices."); 31}
This example illustrates the asynchronous, event based, discovery pattern where the application adds a callback to the DiscoveryCompleted Event before initiating the discovery and then continuing with its own processing. When the discovery is complete the callback will be called and the application can use the returned information.
1internal static void AsynchronousDiscovery() 2{ 3 // Create a Discovery component that can be used for one or more discoveries 4 AlpacaDiscovery alpacaDiscovery = new AlpacaDiscovery(); 5 6 // Add an event handler that will be called when discovery is complete. 7 alpacaDiscovery.DiscoveryCompleted += DiscoveryCompletedEventHandler; 8 9 // Start a discovery using specified parameters: 10 alpacaDiscovery.StartDiscovery(2, 100, 32227, 1.5, false, true, false, ASCOM.Common.Alpaca.ServiceType.Http); 11 12 // Continue with other processing while the discovery is running 13 // The DiscoveryCompletedEventHandler method will be called when discovery completes 14 15 //TODO: Dispose of the alpacaDiscovery object when it is no longer required 16} 17 18///<summary> 19///Event handler called when the configured discovery time is reached 20///</summary> 21static void DiscoveryCompletedEventHandler(object? sender, EventArgs e) 22{ 23 // Ensure that the sender is an AlpacaDiscovery object 24 if (sender is AlpacaDiscovery alpacaDiscovery) 25 { 26 // Get a list of available FilterWheel devices and print the number of devices found 27 List<AscomDevice> filterWheelDevices = alpacaDiscovery.GetAscomDevices(DeviceTypes.FilterWheel); 28 Console.WriteLine($"Found {filterWheelDevices.Count} FilterWheel devices."); 29 } 30}