Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA Enterprise SCADA PubSub API Reference

Subscribe using a Data Access Layer (DAL) connection

Subscribe using a Data Access Layer (DAL) connection

This scenario guides you through the steps required to subscribe to a topic using the Data Access Layer (DAL).

To identify the code associated with each step, see the code comments in the Example code sections.

To subscribe to a topic using the DAL

  1. Open the Program.cs file from the created project.
  2. Add the following private global variables that will be processed in the callback:
    • AutoResetEvent waitHandle = new AutoResetEvent(false);
  3. Create the status connection callback method to process the status of the connection call.
  4. Create the subscribe callback method to process the subscription call by doing the following:
    1. Handle the output of the data returned by the subscription if there are no errors.
    2. Set the state of the event to signal after processing the subscription callback.
  5. Verify that the DAL service has initialized. 
  6. Get the global connection. 
  7. Block the current thread until the current instance receives a signal or the timeout expires.
  8. Reset the global AutoResetEvent waitHandle variable declared out of scope.
  9. Subscribe to a topic with the callback that will process the subscription.
  10. Block the current thread until the current instance receives the subscription callback signal or the timeout expires.
  11. Unsubscribe from the topic.

Example code

The following example code details creating the status connection callback.

private void StatusConnectionCallback(BaseMessage response)
{
    switch (response.MessageType)
    {
        case DALMessageType.ConnectionDisconnectedNotification:
            Console.WriteLine("Subscriber:Connection disconnected from the driver.");
            break;
        case DALMessageType.ConnectionEstablishedNotification:
                Console.WriteLine("Subscriber:Connection established with the driver.");
            break;
        case DALMessageType.DataSourceConnectionNotification:
            Console.WriteLine("Subscriber:Connection established with pubsub.");
            waitHandle.Set();
            break;
        case DALMessageType.ConnectionTimeoutNotification:
            Console.WriteLine("Subscriber:Connection timed out.");
            break;
        default:
            Console.WriteLine("Subscriber:Unknown Connection notification: " + response.MessageType.ToString());
            break;
    }
}

The following example code details creating the subscription callback.

private void SubscriptionCallback(String topic, PubSubStatus psStatus, DALResponseStatus dalResponseStatus, PubSubHeaderData psHeaderData, PubSubBuffer dataBuffer, Object clientObject, String failureReason)
{
    if (psStatus == PubSubStatus.PS_OK)
    {
        HandleTopicMessage(dataBuffer);
    }
}
private void HandleTopicMessage(PubSubBuffer dataBuffer)
{
    if (dataBuffer.GetNextItemType() == PubSubBuffer.PSDataType.PSDdouble)
    {
        object[] objValues = null;
        while (dataBuffer.MoreItemsToRead())
        {
            dataBuffer.Read(out objValues);
        }
        if (objValues != null)
        {
            Console.WriteLine(
quot;Value read:
{(Double)objValues[0]}"); } } waitHandle.Set(); }

The following example code details subscribing to a topic.

public void AddSubscription()
{   
    try
    {
        // Verify that the DAL service has initialized.
        if (DALService.IsInitialized == false)
        {
            DALService.Initialize();
        }
        var StatusConnectionDelegate = new DALConnectionStatusCallBackMethod(StatusConnectionCallback);
        var SubscriptionDelegate = new DALPSConnection.DALPSSubscripionDelegate(SubscriptionCallback);
        // Get the global connection.
        var psSubConn = DALPSClient.GetGlobalConnection("common", StatusConnectionDelegate);
        // Block the current thread until the current instance receives a signal or the timeout expires.
        waitHandle.WaitOne(TimeSpan.FromSeconds(10));
        if (psSubConn.IsConnected)
        {
            // Reset the global AutoResetEvent waitHandle variable declared out of scope.
            waitHandle = new AutoResetEvent(false);
            var topic = "es.realtime.db.analog.AutoCommandAnalog01.curval";
            // Subscribe to a topic with the callback that will process the subscription.
            psSubConn.Subscribe(topic, SubscriptionDelegate, PubSubUpdateFlag.PS_INTEGRITY, null);
            // Block the current thread until the current instance receives the subscription callback signal or the timeout expires.
            waitHandle.WaitOne(TimeSpan.FromSeconds(10));
            // Unsubscribe from the topic.
            psSubConn.UnSubscribe(topic, SubscriptionDelegate, null, null);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Error:" + e.Message);
    }
}
TitleResults for “How to create a CRG?”Also Available in