Click or drag to resize

How to create clients for discovered Alpaca devices

This topic describes how to create Alpaca clients for discovered ASCOM devices. Please see Alpaca devices and ASCOM devices for information on the Alpaca device / ASCOM device terminology.

How to create Alpaca clients for discovered Alpaca devices.

The AlpacaClient Class provides several static generic methods enabling simple or complex client creation. The following example illustrates a simple scenario where client creation just requires an AscomDevice instance provided by the AlpacaDiscovery.GetAscomDevices(DeviceTypes) method. If required, default parameter values can be changed after the device has been created through the AlpacaDevice.ClientConfiguration property.

Simple client creation from a discovered ASCOM device.
 1// Create a TraceLogger to record operational library messages
 2using (TraceLogger logger = new TraceLogger("SimpleClient", true))
 3{
 4
 5    // Get a list of filter wheel devices and record the number of devices discovered
 6    List<AscomDevice> filterWheelDevices = await AlpacaDiscovery.GetAscomDevicesAsync(DeviceTypes.FilterWheel);
 7    logger.LogMessage("SimpleClient", $"Found {filterWheelDevices.Count} FilterWheel devices.");
 8
 9    // Create an Alpaca client for the first device and use it to display the driver description
10    if (filterWheelDevices.Count > 0)
11    {
12        // Create the filter wheel Alpaca Client
13        using (AlpacaFilterWheel filterWheelClient = AlpacaClient.GetDevice<AlpacaFilterWheel>(filterWheelDevices.First(), logger: logger))
14        {
15            // Connect to the Alpaca device
16            filterWheelClient.Connected = true;
17
18            // Record some information
19            logger.LogMessage("SimpleClient", $"Found device: {filterWheelClient.Name} - Driver: {filterWheelClient.DriverInfo}, Version: {filterWheelClient.DriverVersion} containing {filterWheelClient.Names.Count()} filters.");
20
21            // Disconnect from the filter wheel
22            filterWheelClient.Connected = false;
23        }
24    }
25}

This example illustrates a detailed scenario that requires an AscomDevice instance together with other configuration values.

Detailed client creation from a discovered ASCOM device.
 1// Create a TraceLogger to record operational library messages
 2using (TraceLogger logger = new TraceLogger("DetailedClient", true))
 3{
 4    // Get a list of available focuser devices
 5    List<AscomDevice> focuserDevices = await AlpacaDiscovery.GetAscomDevicesAsync(DeviceTypes.Focuser, logger: logger);
 6    logger.LogMessage("DetailedClient", $"Found {focuserDevices.Count} Focuser devices.");
 7
 8    // Create a focuser client for the first device in the list and use it to display device information
 9    if (focuserDevices.Count > 0) // There is at least one Alpaca device
10    {
11        // Create a focuser client specifying all parameters
12        using (AlpacaFocuser focuserClient = AlpacaClient.GetDevice<AlpacaFocuser>(focuserDevices.First(), 3, 5, 100, 23549, "QuY89", "YYu8*9jK", true, logger))
13        {
14            // Connect to the Alpaca device
15            focuserClient.Connected = true;
16
17            // Record some information
18            logger.LogMessage("DetailedClient", $"Found device: {focuserClient.Name} - Driver: {focuserClient.DriverInfo}, Version: {focuserClient.DriverVersion}, Focuser position: {focuserClient.Position}.");
19
20            // Disconnect from the focuser
21            focuserClient.Connected = false;
22        }
23    }
24    else // No devices were discovered
25    {
26        logger.LogMessage("DetailedClient", $"No Focuser devices were discovered.");
27    }
28}

Finally, this example illustrates a manual creation scenario where all parameters are supplied individually.

Detailed manual client creation.
 1// Create a trace logger
 2using (TraceLogger logger = new TraceLogger("ManualClient", true))
 3{
 4    // Create the telescope Alpaca Client with specified parameters
 5    using (AlpacaTelescope telescopeClient = AlpacaClient.GetDevice<AlpacaTelescope>(ServiceType.Http, "127.0.0.1", 11111, 0, 3, 5, 100, 34892, "QuY89", "YYu8*9jK", true, logger))
 6    {
 7        // Connect to the Alpaca device
 8        telescopeClient.Connected = true;
 9
10        // Record some information
11        logger.LogMessage("ManualClient", $"Found device: {telescopeClient.Name} - Driver: {telescopeClient.DriverInfo}, Version: {telescopeClient.DriverVersion} Telescope is tracking: {telescopeClient.Tracking}.");
12
13        // Disconnect from the filter wheel
14        telescopeClient.Connected = false;
15    }
16}

See Also