// This example shows how to subscribe to events and display the event message with each notification. It also shows how to
// unsubscribe afterwards.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.
using System;
using System.Threading;
using OpcLabs.EasyOpc.AlarmsAndEvents;
using OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel;
namespace DocExamples.AlarmsAndEvents._EasyAEClient
{
partial class SubscribeEvents
{
public static void Main1()
{
// Instantiate the client object.
using (var client = new EasyAEClient())
{
var eventHandler = new EasyAENotificationEventHandler(client_Notification);
client.Notification += eventHandler;
int handle = client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000);
Console.WriteLine("Processing event notifications for 1 minute...");
Thread.Sleep(60 * 1000);
client.UnsubscribeEvents(handle);
}
}
// Notification event handler
static void client_Notification(object sender, EasyAENotificationEventArgs e)
{
if (!e.Succeeded)
{
Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief);
return;
}
if (!(e.EventData is null))
Console.WriteLine(e.EventData.Message);
}
}
}
# This example shows how to subscribe to events and display the event message with each notification. It also shows how to
# unsubscribe afterwards.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in PowerShell on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-PowerShell .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
#requires -Version 5.1
using namespace OpcLabs.EasyOpc.AlarmsAndEvents
# The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows .
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicCore.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassic.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicComponents.dll"
# Instantiate the client object.
$client = New-Object EasyAEClient
# Notification event handler
Register-ObjectEvent -InputObject $client -EventName Notification -Action {
if (-not $EventArgs.Succeeded) {
Write-Host "*** Failure: $($EventArgs.ErrorMessageBrief)"
#return
}
if ($EventArgs.EventData -ne $null) {
Write-Host $EventArgs.EventData.Message
}
}
Write-Host "Subscribing events..."
$handle = [IEasyAEClientExtension]::SubscribeEvents($client, "", "OPCLabs.KitEventServer.2", 1000)
Write-Host "Processing event notifications for 1 minute..."
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while ($stopwatch.Elapsed.TotalSeconds -lt 60) {
Start-Sleep -Seconds 1
}
Write-Host "Unsubscribing events..."
$client.UnsubscribeEvents($handle)
Write-Host "Finished."
' This example shows how to subscribe to events and display the event message with each notification. It also shows how to
' unsubscribe afterwards.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.
Imports System.Threading
Imports OpcLabs.EasyOpc.AlarmsAndEvents
Imports OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel
Namespace AlarmsAndEvents._EasyAEClient
Partial Friend Class SubscribeEvents
Public Shared Sub Main1()
Using client = New EasyAEClient()
Dim eventHandler = New EasyAENotificationEventHandler(AddressOf client_Notification)
AddHandler client.Notification, eventHandler
Dim handle As Integer = client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 1000)
Console.WriteLine("Processing event notifications for 1 minute...")
Thread.Sleep(60 * 1000)
client.UnsubscribeEvents(handle)
End Using
End Sub
' Notification event handler
Private Shared Sub client_Notification(ByVal sender As Object, ByVal e As EasyAENotificationEventArgs)
If Not e.Succeeded Then
Console.WriteLine("*** Failure: {0}", e.ErrorMessageBrief)
Exit Sub
End If
If e.EventData IsNot Nothing Then
Console.WriteLine(e.EventData.Message)
End If
End Sub
End Class
End Namespace
// This example shows how to subscribe to events and display the event message with each notification. It also shows how to
// unsubscribe afterwards.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in PHP on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-PHP .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.
class DEasyEAClientEvents {
function Notification($Sender, $E)
{
if (!($E->Succeeded))
{
printf("*** Failure: %s\n", $E->ErrorMessageBrief);
Exit();
}
if (!is_null($E->EventData))
{
print $E->EventData->Message;
print "\n";
}
}
}
$ServerDescriptor = new COM("OpcLabs.EasyOpc.ServerDescriptor");
$ServerDescriptor->ServerClass = "OPCLabs.KitEventServer.2";
$Client = new COM("OpcLabs.EasyOpc.AlarmsAndEvents.EasyAEClient");
$Events = new DEasyEAClientEvents();
com_event_sink($Client, $Events, "DEasyEAClientEvents");
print "Subscribing events...\n";
$SubscriptionParameters = new COM("OpcLabs.EasyOpc.AlarmsAndEvents.AESubscriptionParameters");
$SubscriptionParameters->NotificationRate = 1000;
$handle = $Client->SubscribeEvents($ServerDescriptor, $SubscriptionParameters, TRUE, NULL);
print "Processing event notifications for 1 minute...\n";
$startTime = time(); do { com_message_pump(1000); } while (time() < $startTime + 60);
print "Unsubscribing events...\n";
$Client->UnsubscribeEvents($handle);
print "Finished.\n";
REM This example shows how to subscribe to events and display the event message with each notification. It also shows how to
REM unsubscribe afterwards.
REM
REM Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
REM OPC client and subscriber examples in Visual Basic on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VB .
REM Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
REM a commercial license in order to use Online Forums, and we reply to every post.
Private Sub SubscribeEvents_Main_Command_Click()
OutputText = ""
Dim serverDescriptor As New serverDescriptor
serverDescriptor.ServerClass = "OPCLabs.KitEventServer.2"
' Instantiate the client object and hook events
Set Client1 = New EasyAEClient
OutputText = OutputText & "Subscribing..." & vbCrLf
Dim subscriptionParameters As New AESubscriptionParameters
subscriptionParameters.notificationRate = 1000
Dim handle
Dim state
handle = Client1.SubscribeEvents(serverDescriptor, subscriptionParameters, True, state)
OutputText = OutputText & "Processing event notifications for 1 minute..." & vbCrLf
Pause 60000
OutputText = OutputText & "Unsubscribing events..." & vbCrLf
Client1.UnsubscribeEvents handle
OutputText = OutputText & "Waiting for 5 seconds..." & vbCrLf
Pause 5000
OutputText = OutputText & "Finished." & vbCrLf
Set Client1 = Nothing
End Sub
Private Sub Client1_OnNotification(ByVal sender As Variant, ByVal eventArgs As EasyAENotificationEventArgs)
If Not eventArgs.Succeeded Then
OutputText = OutputText & eventArgs.ErrorMessageBrief & vbCrLf
Exit Sub
End If
If Not eventArgs.EventData Is Nothing Then
OutputText = OutputText & eventArgs.EventData.Message & vbCrLf
End If
End Sub
# This example shows how to subscribe to events and display the event message with each notification. It also shows how
# to unsubscribe afterwards.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
# a commercial license in order to use Online Forums, and we reply to every post.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time
# Import .NET namespaces.
from OpcLabs.EasyOpc import *
from OpcLabs.EasyOpc.AlarmsAndEvents import *
# Notification event handler
def notification(sender, e):
if not e.Succeeded:
print('*** Failure: ', e.ErrorMessageBrief, sep='')
return
else:
if e.EventData is not None:
print(e.EventData.Message)
# Instantiate the client object
client = EasyAEClient()
client.Notification += notification
print('Subscribing events...')
handle = IEasyAEClientExtension.SubscribeEvents(client, '', 'OPCLabs.KitEventServer.2', 1000)
print('Processing event notifications for 1 minute...')
time.sleep(60)
print('Unsubscribing events...')
client.UnsubscribeAllEvents()
client.Notification -= notification
print('Finished.')
// This example shows how to work with Software Tolbox TOP Server 5 Alarms and Events.
// Use simdemo_WithA&E.opf configuration file and write a value above 1000 to Channel1.Device1.Tag1 or Channel1.Device1.Tag2.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
// OPC client and subscriber examples in C# on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-CSharp .
// Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
// a commercial license in order to use Online Forums, and we reply to every post.
using System;
using System.Diagnostics;
using System.Threading;
using OpcLabs.EasyOpc.AlarmsAndEvents;
using OpcLabs.EasyOpc.AlarmsAndEvents.AddressSpace;
using OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel;
using OpcLabs.EasyOpc.OperationModel;
namespace DocExamples.AlarmsAndEvents.SWToolbox
{
class TOPServer_AE
{
public static void Main1()
{
// Instantiate the client object.
var client = new EasyAEClient();
// Browse for some areas and sources
AENodeElementCollection areaElements;
try
{
areaElements = client.BrowseAreas("", "SWToolbox.TOPServer_AE.V5", "");
}
catch (OpcException opcException)
{
Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message);
return;
}
foreach (AENodeElement areaElement in areaElements)
{
Debug.Assert(!(areaElement is null));
Debug.Assert(!(areaElement.QualifiedName is null));
Console.WriteLine("areaElements[\"{0}\"]:", areaElement.Name);
Console.WriteLine(" .QualifiedName: {0}", areaElement.QualifiedName);
AENodeElementCollection sourceElements =
client.BrowseSources("", "SWToolbox.TOPServer_AE.V5", areaElement.QualifiedName);
foreach (AENodeElement sourceElement in sourceElements)
{
Debug.Assert(sourceElement != null);
Console.WriteLine(" sourceElements[\"{0}\"]:", sourceElement.Name);
Console.WriteLine(" .QualifiedName: {0}", sourceElement.QualifiedName);
}
}
// Query for event categories
AECategoryElementCollection categoryElements;
try
{
categoryElements = client.QueryEventCategories("", "SWToolbox.TOPServer_AE.V5");
}
catch (OpcException opcException)
{
Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message);
return;
}
foreach (AECategoryElement categoryElement in categoryElements)
{
Debug.Assert(categoryElement != null);
Console.WriteLine("CategoryElements[\"{0}\"].Description: {1}", categoryElement.CategoryId,
categoryElement.Description);
}
// Subscribe to events, wait, and unsubscribe
var eventHandler = new EasyAENotificationEventHandler(client_Notification);
client.Notification += eventHandler;
int handle = client.SubscribeEvents("", "SWToolbox.TOPServer_AE.V5", 1000);
Console.WriteLine("Processing event notifications for 1 minute...");
Thread.Sleep(60 * 1000);
client.UnsubscribeEvents(handle);
}
// Notification event handler
static void client_Notification(object sender, EasyAENotificationEventArgs e)
{
if (!(e.Exception is null)) Console.WriteLine("e.Exception.Message: {0}", e.Exception.Message);
if (!(e.Exception is null)) Console.WriteLine("e.Exception.Source: {0}", e.Exception.Source);
Console.WriteLine("e.Refresh: {0}", e.Refresh);
Console.WriteLine("e.RefreshComplete: {0}", e.RefreshComplete);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.QualifiedSourceName: {0}", e.EventData.QualifiedSourceName);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.Time: {0}", e.EventData.Time);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.Message: {0}", e.EventData.Message);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.EventType: {0}", e.EventData.EventType);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.CategoryId: {0}", e.EventData.CategoryId);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.Severity: {0}", e.EventData.Severity);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.ConditionName: {0}", e.EventData.ConditionName);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.SubconditionName: {0}", e.EventData.SubconditionName);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.Enabled: {0}", e.EventData.Enabled);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.Active: {0}", e.EventData.Active);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.Acknowledged: {0}", e.EventData.Acknowledged);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.Quality: {0}", e.EventData.Quality);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.AcknowledgeRequired: {0}", e.EventData.AcknowledgeRequired);
if (!(e.EventData is null)) Console.WriteLine("e.EventData.ActiveTime: {0}", e.EventData.ActiveTime);
}
}
}
' This example shows how to work with Software Tolbox TOP Server 5 Alarms and Events.
' Use simdemo_WithA&E.opf configuration file and write a value above 1000 to Channel1.Device1.Tag1 or Channel1.Device1.Tag2.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
' OPC client and subscriber examples in VB.NET on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-VBNET .
' Missing some example? Ask us for it on our Online Forums, https://www.opclabs.com/forum/index ! You do not have to own
' a commercial license in order to use Online Forums, and we reply to every post.
Imports System.Threading
Imports OpcLabs.EasyOpc.AlarmsAndEvents
Imports OpcLabs.EasyOpc.AlarmsAndEvents.AddressSpace
Imports OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel
Imports OpcLabs.EasyOpc.OperationModel
Namespace AlarmsAndEvents.SWToolbox
Friend Class TOPServer_AE
Public Shared Sub Main1()
Dim client = New EasyAEClient()
' Browse for some areas and sources
Dim areaElements As AENodeElementCollection
Try
areaElements = client.BrowseAreas("", "SWToolbox.TOPServer_AE.V5", "")
Catch opcException As OpcException
Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message)
Exit Sub
End Try
For Each areaElement As AENodeElement In areaElements
Debug.Assert(areaElement IsNot Nothing)
Debug.Assert(areaElement.QualifiedName IsNot Nothing)
Console.WriteLine("areaElements[""{0}""]:", areaElement.Name)
Console.WriteLine(" .QualifiedName: {0}", areaElement.QualifiedName)
Dim sourceElements As AENodeElementCollection = client.BrowseSources("", "SWToolbox.TOPServer_AE.V5", areaElement.QualifiedName)
For Each sourceElement As AENodeElement In sourceElements
Debug.Assert(sourceElement IsNot Nothing)
Console.WriteLine(" sourceElements[""{0}""]:", sourceElement.Name)
Console.WriteLine(" .QualifiedName: {0}", sourceElement.QualifiedName)
Next sourceElement
Next areaElement
' Query for event categories
Dim categoryElements As AECategoryElementCollection
Try
categoryElements = client.QueryEventCategories("", "SWToolbox.TOPServer_AE.V5")
Catch opcException As OpcException
Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message)
Exit Sub
End Try
For Each categoryElement As AECategoryElement In categoryElements
Debug.Assert(categoryElement IsNot Nothing)
Console.WriteLine("CategoryElements[""{0}""].Description: {1}", categoryElement.CategoryId, categoryElement.Description)
Next categoryElement
' Subscribe to events, wait, and unsubscribe
Dim eventHandler = New EasyAENotificationEventHandler(AddressOf client_Notification)
AddHandler client.Notification, eventHandler
Dim handle As Integer = client.SubscribeEvents("", "SWToolbox.TOPServer_AE.V5", 1000)
Console.WriteLine("Processing event notifications for 1 minute...")
Thread.Sleep(60 * 1000)
client.UnsubscribeEvents(handle)
End Sub
' Notification event handler
Private Shared Sub client_Notification(ByVal sender As Object, ByVal e As EasyAENotificationEventArgs)
If e.Exception IsNot Nothing Then
Console.WriteLine("e.Exception.Message: {0}", e.Exception.Message)
End If
If e.Exception IsNot Nothing Then
Console.WriteLine("e.Exception.Source: {0}", e.Exception.Source)
End If
Console.WriteLine("e.Refresh: {0}", e.Refresh)
Console.WriteLine("e.RefreshComplete: {0}", e.RefreshComplete)
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.QualifiedSourceName: {0}", e.EventData.QualifiedSourceName)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.Time: {0}", e.EventData.Time)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.Message: {0}", e.EventData.Message)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.EventType: {0}", e.EventData.EventType)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.CategoryId: {0}", e.EventData.CategoryId)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.Severity: {0}", e.EventData.Severity)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.ConditionName: {0}", e.EventData.ConditionName)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.SubconditionName: {0}", e.EventData.SubconditionName)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.Enabled: {0}", e.EventData.Enabled)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.Active: {0}", e.EventData.Active)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.Acknowledged: {0}", e.EventData.Acknowledged)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.Quality: {0}", e.EventData.Quality)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.AcknowledgeRequired: {0}", e.EventData.AcknowledgeRequired)
End If
If e.EventData IsNot Nothing Then
Console.WriteLine("e.EventData.ActiveTime: {0}", e.EventData.ActiveTime)
End If
End Sub
End Class
End Namespace