Simple C# Client Tutorial
*************************

This simple OPC UA client is written using the `TRAEGER OPC UA SDK <https://docs.traeger.de/en/software/sdk/opc-ua/net/start>`_.

Using this library, you can easily set up an OPC UA client. In this tutorial we will use Visual Studio Code and .NET Core to create an OPC UA console application. If you prefer, you can use `Visual Studio <https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio?pivots=dotnet-6-0>`_ as well.

.. note::

  Check the `license model <https://opcua.traeger.de/en/#licensemodel>`_ and `prices <https://opcua.traeger.de/en/#prices>`_ of the TREAGER OPC UA Client SDK before continuing.


1. Install .NET and VSCode
==========================

Start with installing .NET 6.0 on your PC. If you are using `Linux <https://learn.microsoft.com/en-us/dotnet/core/install/linux>`_ you can e.g. follow for `Ubuntu 22.04 this description <https://learn.microsoft.com/en-us/dotnet/core/install/linux-ubuntu-2204>`_. Having that done, you should get a similar output:

.. code-block:: bash

  $ dotnet --version
  6.0.113

Then, install VSCode by following this `tutorial <https://code.visualstudio.com/docs/setup/setup-overview>`_. Further, `install the C# language support extension <https://code.visualstudio.com/docs/languages/csharp>`_.

2. Create a C# console application
==================================

First, create a new console application using:

.. code-block:: bash

  $ dotnet new console -o opcua-csharp-client
  [...]
  $ cd opcua-csharp-client

A simple method to add the TRAEGER OPC UA Client SDK to your project is through NuGet:

.. code-block:: bash

  $ dotnet add package Opc.UaFx.Client
  [...]
  info : Adding PackageReference for package 'Opc.UaFx.Client' into project '<path>/opcua-csharp-client/opcua-csharp-client.csproj'.
  [...]

Open the current project in VSCode:

.. code-block:: bash

  $ code .


3. Run the Sample C# Client
===========================

With all necessary dependencies installed, we create the OPC UA sample client. This client accesses Variables like `BrakesOpen` or `CartesianPose`. Further, it reads and writes entries of / to the Key-Int-Map as well as the Key-Pose-Map of the robot's OPC UA server. Replace your `Program.cs` file with the following code block:


.. literalinclude:: :: ../../../examples/opcua-csharp-client/Program.cs
   :language: c#

Before continuing, replace `opcuaServerUrl`, `username` as well as the `password` variable with your robot's IP and credentials.

This example client can be downloaded :download:`here <_static/download/sampleClientCsharp.zip>`. Extract this zip to your machine. Then open a terminal, switch to the extracted directory and execute `dotnet restore`.

Now, you're ready to run the example:

.. code-block:: bash

  $ dotnet run
    Connection status: Connected

    Checking arm brake state: Brakes are closed

    # Accessing the KeyIntMap
      [Warning] KeyIntMap does not contain an entry for 'Franka Test Key'
      Writing KeyIntPair with key: 'Franka Test Key' and value: 132
      Getting value for key: 'Franka Test Key', value: 132

    # Accessing runtime data
      Cartesian pose read at 3/22/2023 3:49:22 PM:
      [0.7508472281473332 0.6590922337599904 -0.04250428545383584 0 0.658801559060077 -0.7519673171928284 -0.022503478731971595 0 -0.04679460250784976 -0.011105428708064309 -0.9988427977561546 0 0.3852891676296914 -0.011258086525858014 0.5045356314639352 1]

    # Accessing KeyPoseMap
      Wrote previously read Cartesian pose to key 'CartPose1'

The pose named `CartPose1` and the key int pair `Franka Test Key` is now available on the OPC UA server of the robot. It can now be used by OPC UA apps and other clients.

If you want to read different variables or call other methods, a simple way to get the corresponding NodeIDs is by :ref:`browsing the OPC UA Server through UA Expert <ua-expert-browse>`.
