How to make Alpaca devices and COM drivers appear the same |
This topic addresses the requirement that "client applications are able to support use of both Alpaca devices and COM drivers through a single application code path". i.e. that, after selection, the application is unaware of whether it is working with an Alpaca device or a COM driver.
Alpaca devices and COM drivers will always need to be identified independently because they have very different discovery mechanics: Profile/COM for COM drivers and Alpaca Discovery for Alpaca devices.
However, once a required device is identified, the question becomes "how do I wrap up this object so that I can have a single path through my application that doesn't care whether the user selected an Alpaca device or a COM driver. The answer to this question is to use the Library's Alpaca client and COM client toolkits to wrap the Alpaca device or COM driver.
The following section uses the ASCOM Rotator device type as an example.
Both of the toolkits' Rotator classes implement the IRotatorV3 interface. So, if you define an application variable of type IRotatorV3, you can store either an Alpaca client instance or a COM client instance in that variable. This will give you have a strongly typed variable that exposes all of the rotator's properties and methods and which your application can use as necessary.
From this point on the application will never need to know whether it is working with an Alpaca device or a COM device.
The following example shows this approach in practice.
1// This example intentionally avoids using statements to illustrate use of the ASCOM Library namespaces 2 3// Define an IRotatorV3 variable that can hold both an Alpaca toolkit object and a COM client toolkit object 4ASCOM.Common.DeviceInterfaces.IRotatorV3 rotator; 5 6// Define a COM client toolkit object 7ASCOM.Com.DriverAccess.Rotator comRotator; 8 9// Define an Alpaca client toolkit object 10ASCOM.Alpaca.Clients.AlpacaRotator alpacaRotator; 11 12// Create and activate a TraceLogger device to receive operational information from the Library components 13ASCOM.Tools.TraceLogger logger = new ASCOM.Tools.TraceLogger("ToolkitTester", true); 14logger.SetMinimumLoggingLevel(ASCOM.Common.Interfaces.LogLevel.Verbose); 15logger.LogMessage("Main", "Created trace logger OK"); 16 17// Set some arbitrary Alpaca device configuration values 18string deviceIpAddress = "127.0.0.1"; // Device IP address 19int deviceIpPort = 11111; // Device's IP port number 20int remoteDeviceNumber = 0; // This rotator's device number on the Alpaca device 21bool strictCasing = true; // Require the decide to strictly adhere to the Alpaca protocol name casing rules 22Console.WriteLine($"Chosen Alpaca device: {deviceIpAddress}:{deviceIpPort}/api/v1/rotator/{remoteDeviceNumber}"); 23 24// Create an Alpaca client to represent the an Alpaca device 25alpacaRotator = new ASCOM.Alpaca.Clients.AlpacaRotator(ASCOM.Common.Alpaca.ServiceType.Http, deviceIpAddress, deviceIpPort, remoteDeviceNumber, strictCasing, logger); 26 27// Assign the Alpaca client object to the IRotatorV3 variable and use it 28rotator = alpacaRotator; 29rotator.Connected = true; 30string driverVersion = rotator.DriverVersion; 31Console.WriteLine($"Alpaca device driver version: {driverVersion}\r\n"); 32 33rotator.Connected = false; 34rotator.Dispose(); 35 36// Create an arbitrary COM device ProgID 37string deviceProgId = "ASCOM.Simulator.Rotator"; 38Console.WriteLine($"Chosen COM device: {deviceProgId}"); 39 40// Create a COM client toolkit object to represent the rotator driver 41comRotator = new ASCOM.Com.DriverAccess.Rotator(deviceProgId); 42 43// Assign the COM client object to the IRotatorV3 variable and use it 44rotator = comRotator; 45rotator.Connected = true; 46string description = rotator.Description; 47Console.WriteLine($"COM device description: {description}"); 48 49rotator.Connected = false; 50rotator.Dispose();