private async Task ConnectAsync() { if (_clientContext != null) { await _clientContext.CloseAsync(); _clientContext.Dispose(); } string host = HostTextBox.Text; int port = int.Parse(PortTextBox.Text); bool useHttps = UseHttpsCheckBox.IsChecked == true; string apiKey = ApiKeyTextBox.Text; _clientContext = ClientContext.Factory.CreateDirectClientContext(new DirectOptions { HostName = host, Port = port, UseHttps = useHttps, ApiKey = apiKey }); await _clientContext.OpenAsync(); }
public static async Task MainAsync() { string host = "localhost"; int port = 8080; bool useHttps = false; string apiKey; // Request a shortcode to be generated, then get the primary API key using the shortcode. using (DirectClientConnection connection = await DirectClientConnection.RequestShortCodeAsync(host, port, useHttps)) { Console.Write("Enter the shortcode provided by the instrument: "); apiKey = await connection.GetApiKeyAsync(Console.ReadLine(), ApiKeyType.Primary); } // Create the client context using (IClientContext clientContext = ClientContext.Factory.CreateDirectClientContext( new DirectOptions { UseHttps = false, HostName = host, Port = port, ApiKey = apiKey })) { // Open the connection to the instrument await clientContext.OpenAsync(); // List all of the instruments connected to the gateway instrument, individual instruments should only return one item in the results. ICollectionResult <IInstrumentInfo> instrumentsResult; string instrumentId = null; do { instrumentsResult = await clientContext.GetInstrumentsAsync(); foreach (IInstrumentInfo instrumentInfo in instrumentsResult.Items) { Console.WriteLine($"Instrument: {instrumentInfo.Id}"); if (instrumentId == null) { instrumentId = instrumentInfo.Id; } } } while (instrumentsResult.HasMoreItems); // Setup dark acquisition options for 5 samples, each at 1 second capture integration times, and average the 5 results into one sample. ISampleAcquisitionOptions darkSampleOptions = new SampleAcquisitionOptions { IntegrationTime = TimeSpan.FromSeconds(1), SampleAverageCount = 5, LaserPower = 0 // 0 - No laser - Dark sample }; // Acquire the sample. This function will block asynchronously until the sample has been acquired. ISampleInfo darkSampleInfo = await clientContext.AcquireSampleAsync(instrumentId, darkSampleOptions); // Retrieve the sample data. ISample darkSample = await clientContext.GetSampleAsync(instrumentId, darkSampleInfo.Id); // Setup acquisition options for 5 samples, each at 1 second capture integration times, and average the 5 results into one sample. ISampleAcquisitionOptions options = new SampleAcquisitionOptions { IntegrationTime = TimeSpan.FromSeconds(1), SampleAverageCount = 5, LaserPower = 200 }; // Acquire the sample. This function will block asynchronously until the sample has been acquired. ISampleInfo sampleInfo = await clientContext.AcquireSampleAsync(instrumentId, options); // Retrieve the sample data. ISample sample = await clientContext.GetSampleAsync(instrumentId, sampleInfo.Id); Console.WriteLine($"Acquired Sample. [{sampleInfo.Id}]"); // Close the connection to the instrument await clientContext.CloseAsync(); } }