QuickOPC User's Guide and Reference
EasyUAServerConditionChangedEventHandler Delegate
ExampleExample 



OpcLabs.EasyOpcUA Assembly > OpcLabs.EasyOpc.UA Namespace : EasyUAServerConditionChangedEventHandler Delegate
The source of the event.
An OpcLabs.EasyOpc.UA.OperationModel.EasyUAServerConditionChangedEventArgs that contains the event data.
Represents the method that will handle an event raised for a change in the condition of an OPC server.
Syntax
'Declaration
 
<ComVisibleAttribute(False)>
Public Delegate Sub EasyUAServerConditionChangedEventHandler( _
   ByVal sender As Object, _
   ByVal e As EasyUAServerConditionChangedEventArgs _
) 
'Usage
 
Dim instance As New EasyUAServerConditionChangedEventHandler(AddressOf HandlerMethod)
[ComVisible(false)]
public delegate void EasyUAServerConditionChangedEventHandler( 
   object sender,
   EasyUAServerConditionChangedEventArgs e
)
[ComVisible(false)]
public delegate void EasyUAServerConditionChangedEventHandler( 
   Object^ sender,
   EasyUAServerConditionChangedEventArgs^ e
)

Parameters

sender
The source of the event.
e
An OpcLabs.EasyOpc.UA.OperationModel.EasyUAServerConditionChangedEventArgs that contains the event data.
Remarks

 

OPC UA Client-Server

In OPC UA, the IEasyUAClientConnectionMonitoring service that you can obtan from the EasyUAClient object provides a ServerConditionChanged event. This event is raised for a change in the condition of an OPC server. Specifically, you receive a notification whenever the connection to the server is established, or whenever the connection is terminated. Whenever there an error (exception) associated with the server condition, it is also available in the event arguments.

The event notification passes to you an EasyUAServerConditionChangedEventArgs object, which contains data about the new server condition. The EndpointDescriptor property identifies the server the notification applies to. The Boolean Connected property indicates whether the server is now connected. The Exception property contains a null reference in case of no error, or it contains an exception object in case of problems.

Additional properties of the event arguments distinguish the change further. For example, the ConnectionState property contains an enumerated value (ConnectionState Enumeration) with members DisconnectedConnectingConnected, and Disconnecting. The RetrialDelay property (valid in the Disconnected state) indicates the time (in milliseconds) to the next reconnection attempt.

// This example shows how to monitor connections to and disconnections from the OPC UA server.

using System;
using Microsoft.Extensions.DependencyInjection;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;
using OpcLabs.EasyOpc.UA.Services;

namespace UADocExamples._EasyUAClientConnectionMonitoring
{
    class ServerConditionChanged
    {
        public static void Main1()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            // Instantiate the client object.
            using (var client = new EasyUAClient())
            {
                // Obtain the client connection monitoring service.
                IEasyUAClientConnectionMonitoring clientConnectionMonitoring = client.GetService<IEasyUAClientConnectionMonitoring>();
                if (clientConnectionMonitoring is null)
                {
                    Console.WriteLine("The client connection monitoring service is not available.");
                    return;
                }

                // Hook events.
                client.DataChangeNotification += client_DataChangeNotification;
                clientConnectionMonitoring.ServerConditionChanged += clientConnectionMonitoring_OnServerConditionChanged;

                try
                {
                    Console.WriteLine("Reading (1)");
                    // The first read will cause a connection to the server.
                    UAAttributeData attributeData1 = client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853");
                    Console.WriteLine(attributeData1);

                    Console.WriteLine("Reading (2)");
                    // The second read, because it closely follows the first one, will reuse the connection that is already
                    // open.
                    UAAttributeData attributeData2 = client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853");
                    Console.WriteLine(attributeData2);
                }
                catch (UAException uaException)
                {
                    Console.WriteLine($"*** Failure: {uaException.GetBaseException().Message}");
                }

                Console.WriteLine("Waiting for 10 seconds...");
                // Since the connection is now not used for some time, it will be closed.
                System.Threading.Thread.Sleep(10 * 1000);

                Console.WriteLine("Subscribing...");
                // Subscribing to a monitored item will cause a connection to the server, if one is not yet open.
                client.SubscribeDataChange(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000);

                Console.WriteLine("Waiting for 10 seconds...");
                // The connection will not be closed as long as there are any subscriptions to the server.
                System.Threading.Thread.Sleep(10 * 1000);

                Console.WriteLine("Unsubscribing...");
                client.UnsubscribeAllMonitoredItems();

                Console.WriteLine("Waiting for 10 seconds...");
                // After some delay, the connection will be closed, because there are no subscriptions to the server.
                System.Threading.Thread.Sleep(10 * 1000);

                Console.WriteLine("Finished.");
            }
        }

        static void client_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
        {
            // Display value
            if (e.Succeeded)
                Console.WriteLine($"Value: {e.AttributeData.Value}");
            else
                Console.WriteLine($"*** Failure: {e.ErrorMessageBrief}");
        }

        static void clientConnectionMonitoring_OnServerConditionChanged(object sender, EasyUAServerConditionChangedEventArgs e)
        {
            // Display the event
            Console.WriteLine(e);
        }


        // Example output:
        //
        //Reading(1)
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        //-8.095801E+32 {System.Single} @2021-06-24T07:09:46.062 @@2021-06-24T07:09:46.062; Good
        //Reading(2)
        //-3.11389E-18 {System.Single} @2021 - 06 - 24T07: 09:46.125 @@2021 - 06 - 24T07: 09:46.125; Good
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(Infinite); Success
        //Subscribing...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        //Value: 3.293034E-31
        //Value: 6.838126E+37
        //Value: 5.837702E-29
        //Value: 0.02940081
        //Value: -9.653872E-17
        //Value: -1.012749E+29
        //Value: -1.422391E+20
        //Value: -3.56571E-22
        //Unsubscribing...
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(Infinite); Success
        //Finished.
    }
}
# This example shows how to monitor connections to and disconnections from the OPC UA server.

# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time

# Import .NET namespaces.
from Microsoft.Extensions.DependencyInjection import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.OperationModel import *
from OpcLabs.EasyOpc.UA.Services import *


def dataChangeNotification(sender, e):
    # Display value.
    if e.Succeeded:
        print('Value: ', e.AttributeData.Value, sep='')
    else:
        print('*** Failure: ', e.ErrorMessageBrief, sep='')

def onServerConditionChanged(sender, e):
    # Display the event.
    print(e)


endpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer')
# or 'http://opcua.demo-this.com:51211/UA/SampleServer' (currently not supported)
# or 'https://opcua.demo-this.com:51212/UA/SampleServer/'

# Instantiate the client object.
client = None
try:
    client = EasyUAClient()

    # Obtain the client connection monitoring service.
    clientConnectionMonitoring = ServiceProviderServiceExtensions.GetService[IEasyUAClientConnectionMonitoring](client)
    if clientConnectionMonitoring is None:
        print('The client connection monitoring service is not available.')
        exit()

    # Hook events
    client.DataChangeNotification += dataChangeNotification
    clientConnectionMonitoring.ServerConditionChanged += onServerConditionChanged

    try:
        print('Reading (1)')
        # The first read will cause a connection to the server.
        attributeData1 = IEasyUAClientExtension.Read(client,
                                                     endpointDescriptor,
                                                     UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'))
        print(attributeData1)

        print('Reading (2)')
        # The second read, because it closely follows the first one, will reuse the connection that is already
        # open.
        attributeData2 = IEasyUAClientExtension.Read(client,
                                                     endpointDescriptor,
                                                     UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'))
        print(attributeData2)

    except UAException as uaException:
        print('*** Failure: ' + uaException.GetBaseException().Message)

    print('Waiting for 10 seconds...')
    # Since the connection is now not used for some time, it will be closed.
    time.sleep(10)

    print('Subscribing...')
    # Subscribing to a monitored item will cause a connection to the server, if one is not yet open.
    IEasyUAClientExtension.SubscribeDataChange(client,
                                               endpointDescriptor,
                                               UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'),
                                               1000)

    print('Waiting for 10 seconds...')
    # The connection will not be closed as long as there are any subscriptions to the server.
    time.sleep(10)

    print('Unsubscribing...')
    client.UnsubscribeAllMonitoredItems()

    print('Waiting for 10 seconds...')
    # After some delay, the connection will be closed, because there are no subscriptions to the server.
    time.sleep(10)

    print('Finished.')

finally:
    client and client.Dispose()

 

Rem This example shows how to monitor connections to and disconnections from the OPC UA server with event pull mechanism.

Option Explicit

' Instantiate the client object.
Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.UA.EasyUAClient")

' Obtain the client connection monitoring service.
Dim ClientConnectionMonitoring: Set ClientConnectionMonitoring = Client.GetServiceByName("OpcLabs.EasyOpc.UA.Services.IEasyUAClientConnectionMonitoring, OpcLabs.EasyOpcUA")
If ClientConnectionMonitoring Is Nothing Then
    WScript.Echo "The client connection monitoring service is not available."
    WScript.Quit
End If

' In order to use event pull, you must set a non-zero queue capacity upfront.
ClientConnectionMonitoring.PullServerConditionChangedQueueCapacity = 1000

WScript.Echo "Reading (1)"
' The first read will cause a connection to the server.
Dim AttributeData1: Set AttributeData1 = Client.Read("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", "nsu=http://test.org/UA/Data/ ;i=10853")
WScript.Echo AttributeData1

WScript.Echo "Reading (2)"
' The second read, because it closely follows the first one, will reuse the connection that is already open.
Dim AttributeData2: Set AttributeData2 = Client.Read("opc.tcp://opcua.demo-this.com:51210/UA/SampleServer", "nsu=http://test.org/UA/Data/ ;i=10853")
WScript.Echo AttributeData2

WScript.Echo "Processing server condition changed events for 10 seconds..."
' Because we only started the processing after we have made the reads, there are now events related to opening the 
' connection already in the queue, and they will be consumed first.
' Since the connection is now not used for some time, it will be closed.
Dim endTime: endTime = Now() + 10*(1/24/60/60)
Do
    Dim EventArgs: Set EventArgs = ClientConnectionMonitoring.PullServerConditionChanged(2*1000)
    If Not (EventArgs Is Nothing) Then
        ' Handle the server condition changed event.
        WScript.Echo EventArgs
    End If    
Loop While Now() < endTime

WScript.Echo "Finished."

 

The Statistics Property of the EasyUAServerConditionChangedEventArgs Class contains a ConnectedConditionStatistics object, with large amount of information about the connection to the server. There is a sequential connection attempt count, and an AutomatonStateStatistics object for each of the connection states (Disconnected, Connecting, Connected, and Disconnecting, of ConnectionState Enumeration), and for an "Unconnected" state, which represents a state other than Connected. For each of these states, you can obtain information such as how many time it was entered, when it was first entered, when the last cycle started and how long it did last, what is the minimum, maximum, and accumulated duration of the state, and much more. For the current state, you can also obtain information such as when it was entered and how long it is active.

Note that the information provided by this event should be used for oversight, informational and diagnostics purposes only. Specifically, you should not use it to attempt to implement any error recovery mechanism, because error recovery is already built into the QuickOPC software, and having your own mechanism would inevitably cause conflicts.

OPC Classic

A corresponding service and event is not currently available for OPC Classic.

 

Example

.NET

// This example shows how to monitor connections to and disconnections from the OPC UA server.

using System;
using Microsoft.Extensions.DependencyInjection;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;
using OpcLabs.EasyOpc.UA.Services;

namespace UADocExamples._EasyUAClientConnectionMonitoring
{
    class ServerConditionChanged
    {
        public static void Main1()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            // Instantiate the client object.
            using (var client = new EasyUAClient())
            {
                // Obtain the client connection monitoring service.
                IEasyUAClientConnectionMonitoring clientConnectionMonitoring = client.GetService<IEasyUAClientConnectionMonitoring>();
                if (clientConnectionMonitoring is null)
                {
                    Console.WriteLine("The client connection monitoring service is not available.");
                    return;
                }

                // Hook events.
                client.DataChangeNotification += client_DataChangeNotification;
                clientConnectionMonitoring.ServerConditionChanged += clientConnectionMonitoring_OnServerConditionChanged;

                try
                {
                    Console.WriteLine("Reading (1)");
                    // The first read will cause a connection to the server.
                    UAAttributeData attributeData1 = client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853");
                    Console.WriteLine(attributeData1);

                    Console.WriteLine("Reading (2)");
                    // The second read, because it closely follows the first one, will reuse the connection that is already
                    // open.
                    UAAttributeData attributeData2 = client.Read(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853");
                    Console.WriteLine(attributeData2);
                }
                catch (UAException uaException)
                {
                    Console.WriteLine($"*** Failure: {uaException.GetBaseException().Message}");
                }

                Console.WriteLine("Waiting for 10 seconds...");
                // Since the connection is now not used for some time, it will be closed.
                System.Threading.Thread.Sleep(10 * 1000);

                Console.WriteLine("Subscribing...");
                // Subscribing to a monitored item will cause a connection to the server, if one is not yet open.
                client.SubscribeDataChange(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000);

                Console.WriteLine("Waiting for 10 seconds...");
                // The connection will not be closed as long as there are any subscriptions to the server.
                System.Threading.Thread.Sleep(10 * 1000);

                Console.WriteLine("Unsubscribing...");
                client.UnsubscribeAllMonitoredItems();

                Console.WriteLine("Waiting for 10 seconds...");
                // After some delay, the connection will be closed, because there are no subscriptions to the server.
                System.Threading.Thread.Sleep(10 * 1000);

                Console.WriteLine("Finished.");
            }
        }

        static void client_DataChangeNotification(object sender, EasyUADataChangeNotificationEventArgs e)
        {
            // Display value
            if (e.Succeeded)
                Console.WriteLine($"Value: {e.AttributeData.Value}");
            else
                Console.WriteLine($"*** Failure: {e.ErrorMessageBrief}");
        }

        static void clientConnectionMonitoring_OnServerConditionChanged(object sender, EasyUAServerConditionChangedEventArgs e)
        {
            // Display the event
            Console.WriteLine(e);
        }


        // Example output:
        //
        //Reading(1)
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        //-8.095801E+32 {System.Single} @2021-06-24T07:09:46.062 @@2021-06-24T07:09:46.062; Good
        //Reading(2)
        //-3.11389E-18 {System.Single} @2021 - 06 - 24T07: 09:46.125 @@2021 - 06 - 24T07: 09:46.125; Good
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(Infinite); Success
        //Subscribing...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connecting; Success
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Connected; Success
        //Value: 3.293034E-31
        //Value: 6.838126E+37
        //Value: 5.837702E-29
        //Value: 0.02940081
        //Value: -9.653872E-17
        //Value: -1.012749E+29
        //Value: -1.422391E+20
        //Value: -3.56571E-22
        //Unsubscribing...
        //Waiting for 10 seconds...
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnecting; Success
        //"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer" Disconnected(Infinite); Success
        //Finished.
    }
}
# This example shows how to monitor connections to and disconnections from the OPC UA server.

# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time

# Import .NET namespaces.
from Microsoft.Extensions.DependencyInjection import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.OperationModel import *
from OpcLabs.EasyOpc.UA.Services import *


def dataChangeNotification(sender, e):
    # Display value.
    if e.Succeeded:
        print('Value: ', e.AttributeData.Value, sep='')
    else:
        print('*** Failure: ', e.ErrorMessageBrief, sep='')

def onServerConditionChanged(sender, e):
    # Display the event.
    print(e)


endpointDescriptor = UAEndpointDescriptor('opc.tcp://opcua.demo-this.com:51210/UA/SampleServer')
# or 'http://opcua.demo-this.com:51211/UA/SampleServer' (currently not supported)
# or 'https://opcua.demo-this.com:51212/UA/SampleServer/'

# Instantiate the client object.
client = None
try:
    client = EasyUAClient()

    # Obtain the client connection monitoring service.
    clientConnectionMonitoring = ServiceProviderServiceExtensions.GetService[IEasyUAClientConnectionMonitoring](client)
    if clientConnectionMonitoring is None:
        print('The client connection monitoring service is not available.')
        exit()

    # Hook events
    client.DataChangeNotification += dataChangeNotification
    clientConnectionMonitoring.ServerConditionChanged += onServerConditionChanged

    try:
        print('Reading (1)')
        # The first read will cause a connection to the server.
        attributeData1 = IEasyUAClientExtension.Read(client,
                                                     endpointDescriptor,
                                                     UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'))
        print(attributeData1)

        print('Reading (2)')
        # The second read, because it closely follows the first one, will reuse the connection that is already
        # open.
        attributeData2 = IEasyUAClientExtension.Read(client,
                                                     endpointDescriptor,
                                                     UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'))
        print(attributeData2)

    except UAException as uaException:
        print('*** Failure: ' + uaException.GetBaseException().Message)

    print('Waiting for 10 seconds...')
    # Since the connection is now not used for some time, it will be closed.
    time.sleep(10)

    print('Subscribing...')
    # Subscribing to a monitored item will cause a connection to the server, if one is not yet open.
    IEasyUAClientExtension.SubscribeDataChange(client,
                                               endpointDescriptor,
                                               UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'),
                                               1000)

    print('Waiting for 10 seconds...')
    # The connection will not be closed as long as there are any subscriptions to the server.
    time.sleep(10)

    print('Unsubscribing...')
    client.UnsubscribeAllMonitoredItems()

    print('Waiting for 10 seconds...')
    # After some delay, the connection will be closed, because there are no subscriptions to the server.
    time.sleep(10)

    print('Finished.')

finally:
    client and client.Dispose()
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also