// This example shows how to filter the events by their category.
using System;
using System.Threading;
using OpcLabs.EasyOpc.AlarmsAndEvents;
using OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel;
using OpcLabs.EasyOpc.DataAccess;
namespace DocExamples.AlarmsAndEvents._EasyAEClient
{
partial class SubscribeEvents
{
// Instantiate the OPC-A&E client object.
static readonly EasyAEClient AEClient = new EasyAEClient();
// Instantiate the OPC-DA client object.
static readonly EasyDAClient DAClient = new EasyDAClient();
public static void FilterByCategories()
{
var eventHandler = new EasyAENotificationEventHandler(AEClient_Notification_FilterByCategories);
AEClient.Notification += eventHandler;
Console.WriteLine("Processing event notifications...");
var subscriptionFilter = new AESubscriptionFilter
{
Categories = new long[] { 15531778 }
};
// You can also filter using event types, severity, areas, and sources.
int handle = AEClient.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000, null, subscriptionFilter);
// Allow time for initial refresh
Thread.Sleep(5 * 1000);
// Set some events to active state.
DAClient.WriteItemValue("", "OPCLabs.KitServer.2", "SimulateEvents.ConditionState1.Activate", true);
DAClient.WriteItemValue("", "OPCLabs.KitServer.2", "SimulateEvents.ConditionState2.Activate", true);
Thread.Sleep(10 * 1000);
AEClient.UnsubscribeEvents(handle);
AEClient.Notification -= eventHandler;
}
// Notification event handler
static void AEClient_Notification_FilterByCategories(object sender, EasyAENotificationEventArgs e)
{
Console.WriteLine();
Console.WriteLine(e);
if (!e.Succeeded)
return;
Console.WriteLine("Refresh: {0}", e.Refresh);
Console.WriteLine("RefreshComplete: {0}", e.RefreshComplete);
AEEventData eventData = e.EventData;
if (!(eventData is null))
{
Console.WriteLine("Event.CategoryId: {0}", eventData.CategoryId);
Console.WriteLine("Event.QualifiedSourceName: {0}", eventData.QualifiedSourceName);
Console.WriteLine("Event.Message: {0}", eventData.Message);
Console.WriteLine("Event.Active: {0}", eventData.Active);
Console.WriteLine("Event.Acknowledged: {0}", eventData.Acknowledged);
}
}
}
}
' This example shows how to filter the events by their category.
Imports System.Threading
Imports OpcLabs.EasyOpc.AlarmsAndEvents
Imports OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel
Imports OpcLabs.EasyOpc.DataAccess
Namespace AlarmsAndEvents._EasyAEClient
Partial Friend Class SubscribeEvents
_
Private Shared ReadOnly AEClient As New EasyAEClient()
_
Private Shared ReadOnly DAClient As New EasyDAClient()
Public Shared Sub FilterByCategories()
Dim eventHandler = New EasyAENotificationEventHandler(AddressOf AEClient_Notification_FilterByCategories)
AddHandler AEClient.Notification, eventHandler
Console.WriteLine("Processing event notifications...")
Dim subscriptionFilter As New AESubscriptionFilter() With _
{.Categories = New Long() {15531778}}
' You can also filter using event types, severity, areas, and sources.
Dim handle As Integer = AEClient.SubscribeEvents("", "OPCLabs.KitEventServer.2", Nothing, subscriptionFilter)
' Allow time for initial refresh
Thread.Sleep(5 * 1000)
' Set some events to active state.
DAClient.WriteItemValue("", "OPCLabs.KitServer.2", "SimulateEvents.ConditionState1.Activate", True)
DAClient.WriteItemValue("", "OPCLabs.KitServer.2", "SimulateEvents.ConditionState2.Activate", True)
Thread.Sleep(10 * 1000)
AEClient.UnsubscribeEvents(handle)
RemoveHandler AEClient.Notification, eventHandler
End Sub
' Notification event handler
Private Shared Sub AEClient_Notification_FilterByCategories(ByVal sender As Object, ByVal e As EasyAENotificationEventArgs)
Console.WriteLine()
Console.WriteLine(e)
If Not e.Succeeded Then
Exit Sub
End If
Console.WriteLine("Refresh: {0}", e.Refresh)
Console.WriteLine("RefreshComplete: {0}", e.RefreshComplete)
Dim eventData As AEEventData = e.EventData
If e.EventData IsNot Nothing Then
Console.WriteLine("Event.CategoryId: {0}", eventData.CategoryId)
Console.WriteLine("Event.QualifiedSourceName: {0}", eventData.QualifiedSourceName)
Console.WriteLine("Event.Message: {0}", eventData.Message)
Console.WriteLine("Event.Active: {0}", eventData.Active)
Console.WriteLine("Event.Acknowledged: {0}", eventData.Acknowledged)
End If
End Sub
End Class
End Namespace