OPC A&E Notification Handler Pseudo-Code

From OPC Labs Knowledge Base
Jump to navigation Jump to search

In QuickOPC for OPC Alarms&Events, the Notification handler can be called for several different reasons. Your code needs to recognize the notification reason, and act accordingly. This article explains the various situations in which the Notification handler may be called, and suggests a pseudo-code that you can use to weite your own event handler.

When an OPC Alarms and Events server generates an event, the EasyAEClient object generates a Notification event. For subscription mechanism to be useful, you should hook one or more event handlers to this event.

To be more precise, the Notification event is actually generated in other cases, too - if there is any significant occurrence related to the event subscription. This can be for three reasons:

  1. You receive the Notification when a successful connection (or re-connection) is made. In this case, the Exception and EventData properties of the event arguments are null references.
  2. You receive the Notification when there is a problem with the event subscription, and it is disconnected. In this case, the Exception property contains information about the error. The EventData property is a null reference.
  3. You receive one additional Notification after the component has sent you all notifications for the forced “refresh”. In this case, the RefreshComplete property of the event arguments is set to True, and the Exception and EventData properties contain null references.

The notification for the Notification event contains an EasyAENotificationEventArgs argument. You will find all kind of relevant data in this object. Some properties in this object contain valid information under all circumstances. These properties are ServerDescriptor, SubscriptionParameters, and State. Other properties, such as EventData, contain null references when there is no associated information for them. When the Event property is not a null reference, it contains an AEEventData object describing the detail of the actual OPC event received from the OPC Alarms and Events server.

Before further processing, your code should always inspect the value of Exception property of the event arguments. If this property is not a null reference, there has been an error related to the event subscription, the Exception property contains information about the problem, and the Event property does not contain a valid object.

If the Exception property is a null reference, the notification may be informing you about the fact that a “forced” refresh is complete (in this case, the RefreshComplete property is True), or that an event subscription has been successfully connected or re-connected (in this case, the Event property is a null reference). If none of the previous applies, the EventData property contains a valid AEEventData object with details about the actual OPC event generated by the OPC server.

Pseudo-code for the full Notification event handler may look similar to this:

if notificationEventArgs.Exception is not null then
       An error occurred and the subscription is disconnected, handle it (or ignore)

else if notificationEventArgs.RefreshComplete then
       A “refresh” is complete; handle it (only needed if you are invoking a refresh explicitly)

else if notificationEventArgs.EventData is null then
       Subscription has been successfully connected or re-connected, handle it (or ignore)

       Handle the OPC event, details are in notificationEventArgs.EventData. You may use notificationEventArgs.Refresh flag for distinguishing refreshes from original notifications.

The Notification event handler is called on a thread determined by the EasyAEClient component. For details, please refer to “Multithreading and Synchronization” chapter under “Advanced Topics” in the "Concepts" document.