4.1.2 Client API Tutorial |
The Client API tutorial handles exactly like the 4.1.1 Standalone API Tutorial. The only difference is that in this tutorial, a service is expected to be already running. The client connects to the service and from there on out, the API will handle the same. This enables not only for a running acquisition before and after the application has finished but also for multiple clients to connect to the service.
A real world use case of the Client API is implemented in the 4.2.1 Command Line Client.
//----------------------------------------------------------------------------- // This example demonstrates how to acquire data, export to .csv and what // steps are needed to achieve this with the ProcessDataAnalyzer Client API. // In contrast to the StandaloneAPITutorial, the ClientAPITutorial requires // the PDA API service to be already running. // The application will connect to the first available device, acquire // ten seconds worth of default configured data and save it to the // desktop from where the .csv file will be opened with the PCs current // default program for .csv. // The ClientAPITutorial application is implemented using the async interface. // The ProtoClientAPITutorial application and ClientAPITutorial application // demonstrate the same implementation with different dependencies. //----------------------------------------------------------------------------- using RAYLASE.PDA.API; using RAYLASE.PDA.ClientLib; using System.Diagnostics; public class Program { // Before running this example, please make sure that the PDA API service is running. // The default installation location of the ProcessDataAnalyzer.Service.exe is // %ProgramFiles%\RAYLASE\ProcessDataAnalyzer\bin\ProcessDataAnalyzer.Service.exe"; // // To install the PDA API service as Windows service and have it running in // the background: // - Open a command line as admin, navigate to the location and call: // > ProcessDataAnalyzer.Service.exe install // > ProcessDataAnalyzer.Service.exe start // Or you can start a single instance in a (non-admin) command line: // > ProcessDataAnalyzer.Service.exe server public static int Main( string[] args ) { Console.WriteLine( "\n\nClient API sample\n------------------\n\n" ); // Use the Client - this connects a client to the running API service instance. var api = new Client(); // The card could be set by IP or serial number var suggestedCard = args[0]; if ( !string.IsNullOrEmpty( suggestedCard ) ) { Console.WriteLine( $"Connecting to card {suggestedCard} given as argument" ); } else { // Discover all available cards Console.WriteLine( "Discovering available cards" ); var discoverTask = api.Acquisition.DiscoverAsync(); var discoveryDuration = 0; while ( !discoverTask.IsCompleted ) { Console.WriteLine( $"Discovering... {++discoveryDuration} s" ); Thread.Sleep( 1000 ); } var cards = discoverTask.Result; if ( cards.Count == 0 ) { Console.WriteLine( "No cards found, please check your card configuration and network setup." ); return -1; } else Console.WriteLine( $"Found {cards.Count} cards" ); suggestedCard = cards[0].SuggestedIp; Console.WriteLine( $"Connecting to first card with suggested IP {suggestedCard}" ); } api.Acquisition.Connect( suggestedCard ); Console.WriteLine( "Starting acquisition (non-blocking)" ); // The specified timeout will override the PDA's default timeout value. // Since there is no overload for the acquistition auto stop timeout, we set it as a proto message. var acquisitionStartTask = api.Acquisition.StartAsync( new AcquisitionStartRequest { AutoStopSeconds = 60 } ).ResponseAsync; // Do something else while the acquisition is running and stop after 10 seconds manually var processingDuration = 0; while ( processingDuration < 10 ) { Console.WriteLine( $"Doing some dummy processing while acquisition is running... completed task {++processingDuration} of 10" ); Thread.Sleep( 1000 ); } // After "processing", manually stop the acquisition var stopwatch = new Stopwatch(); stopwatch.Start(); var acquisitionStopTask = api.Acquisition.StopAsync(); while ( !acquisitionStartTask.IsCompleted && !acquisitionStopTask.IsCompleted ) { Thread.Sleep( 10 ); } stopwatch.Stop(); Console.WriteLine( $"Manually stopped acquisition after processing finished, stopping took {stopwatch.ElapsedMilliseconds} ms" ); // Some commands return a status in their reply which can transport a "soft" error without throwing. // In case of the Acquisition, if ADC signals are configured and the license can not be found, // The ADC signals will be excluded from acquisition and a warning is returned. // It might be a good idea to check the status code if available: var acquisitionReply = acquisitionStartTask.Result; if ( acquisitionReply.Status.Code != RAYLASE.PDA.API.Error.StatusCode.Ok ) { Console.WriteLine( $"Acquisition encountered an error: {acquisitionReply.Status}" ); return -1; } // Export the acquired 10 seconds to a .csv file to load into an excel table. // Please note that this path is as seen from the service.exe. If you set a relative path, it will // be relative to where the PDA service is running! Console.WriteLine( "Exporting acquired data to .csv" ); var exportPath = Path.Combine( Environment.GetFolderPath( Environment.SpecialFolder.Desktop ), "ClientAPI_Export.csv" ); api.Acquisition.Export( new( exportPath, useDecimalPoint: true ) ); //// Now you could open the file with Excel (or the default .csv reader on the system) //// Open the file with Excel (or the default .csv reader on the system) // using ( var fileopener = new Process() ) // { // fileopener.StartInfo.FileName = "explorer"; // fileopener.StartInfo.Arguments = "\"" + exportPath + "\""; // fileopener.Start(); // } // Exiting this client application will keep the service running. // This means that you could start an acquisition here and immediately exit. The acquisition will // keep running until a stop condition is met (e.g. trigger off or auto-stop timeout). return 0; } }