// This example shows how to subscribe to changes of multiple monitored items and use a data change filter.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
using System;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;
namespace UADocExamples._EasyUAClient
{
partial class SubscribeMultipleMonitoredItems
{
public static void Filter()
{
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 and hook events.
var client = new EasyUAClient();
client.DataChangeNotification += client_DataChangeNotification_Filter;
Console.WriteLine("Subscribing...");
// Report a notification if either the StatusCode or the value change.
// The UADataChangeTrigger has an implicit conversion to UADataChangeFilter and can thus be used in its place.
client.SubscribeMultipleMonitoredItems(new[]
{
new EasyUAMonitoredItemArguments(null, endpointDescriptor,
"nsu=http://test.org/UA/Data/ ;i=10845",
new UAMonitoringParameters(1000, UADataChangeTrigger.StatusValue)),
new EasyUAMonitoredItemArguments(null, endpointDescriptor,
"nsu=http://test.org/UA/Data/ ;i=10853",
new UAMonitoringParameters(1000, UADataChangeTrigger.StatusValue)),
new EasyUAMonitoredItemArguments(null, endpointDescriptor,
"nsu=http://test.org/UA/Data/ ;i=10855",
new UAMonitoringParameters(1000, UADataChangeTrigger.StatusValue))
});
Console.WriteLine("Processing monitored item changed events for 10 seconds...");
System.Threading.Thread.Sleep(10 * 1000);
Console.WriteLine("Unsubscribing...");
client.UnsubscribeAllMonitoredItems();
Console.WriteLine("Waiting for 5 seconds...");
System.Threading.Thread.Sleep(5 * 1000);
Console.WriteLine("Finished.");
}
static void client_DataChangeNotification_Filter(object sender, EasyUADataChangeNotificationEventArgs e)
{
// Display value.
if (e.Succeeded)
Console.WriteLine($"{e.Arguments.NodeDescriptor}: {e.AttributeData.Value}");
else
Console.WriteLine($"{e.Arguments.NodeDescriptor} *** Failure: {e.ErrorMessageBrief}");
}
}
}
# This example shows how to subscribe to changes of multiple monitored items and use a data change filter.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
#requires -Version 5.1
using namespace OpcLabs.EasyOpc.UA
using namespace OpcLabs.EasyOpc.UA.OperationModel
# The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows .
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUA.dll"
Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcUAComponents.dll"
[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.
$client = New-Object EasyUAClient
# Data change notification handler
Register-ObjectEvent -InputObject $client -EventName DataChangeNotification -Action {
# Display value.
if ($EventArgs.Succeeded) {
Write-Host "$($EventArgs.Arguments.NodeDescriptor): $($EventArgs.AttributeData.Value)"
}
else {
Write-Host "$($EventArgs.Arguments.NodeDescriptor) *** Failure: $($EventArgs.ErrorMessageBrief)"
}
}
Write-Host "Subscribing..."
# Report a notification if either the StatusCode or the value change.
# The UADataChangeTrigger has an implicit conversion to UADataChangeFilter and can thus be used in its place.
$handleArray = $client.SubscribeMultipleMonitoredItems(@(
(New-Object UAMonitoredItemArguments(
(New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10845")),
(New-Object UAMonitoringParameters(1000, [UADataChangeTrigger]::StatusValue)))),
(New-Object UAMonitoredItemArguments(
(New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10853")),
(New-Object UAMonitoringParameters(1000, [UADataChangeTrigger]::StatusValue)))),
(New-Object UAMonitoredItemArguments(
(New-Object UAAttributeArguments($endpointDescriptor, [UANodeDescriptor]"nsu=http://test.org/UA/Data/ ;i=10855")),
(New-Object UAMonitoringParameters(1000, [UADataChangeTrigger]::StatusValue))))
))
Write-Host
Write-Host "Processing monitored item changed events for 10 seconds..."
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
while ($stopwatch.Elapsed.TotalSeconds -lt 10) {
Start-Sleep -Seconds 1
}
Write-Host "Unsubscribing..."
$client.UnsubscribeAllMonitoredItems()
Write-Host "Waiting for 5 seconds..."
Start-Sleep -Seconds 5
Write-Host "Finished."
# This example shows how to subscribe to changes of multiple monitored items and use a data change filter.
#
# 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 .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time
# Import .NET namespaces.
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.OperationModel import *
def dataChangeNotification(sender, e):
# Display value.
if e.Succeeded:
print(e.Arguments.NodeDescriptor, ': ', e.AttributeData.Value, sep='')
else:
print(e.Arguments.NodeDescriptor, ' *** Failure: ', e.ErrorMessageBrief, sep='')
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 and hook events.
client = EasyUAClient()
client.DataChangeNotification += dataChangeNotification
print('Subscribing...')
# Report a notification if either the StatusCode or the value change.
# The UADataChangeTrigger has an implicit conversion to UADataChangeFilter.
client.SubscribeMultipleMonitoredItems([
EasyUAMonitoredItemArguments(
None,
endpointDescriptor,
UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10845'),
UAMonitoringParameters(1000, UADataChangeFilter(UADataChangeTrigger.StatusValue))),
EasyUAMonitoredItemArguments(
None,
endpointDescriptor,
UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10853'),
UAMonitoringParameters(1000, UADataChangeFilter(UADataChangeTrigger.StatusValue))),
EasyUAMonitoredItemArguments(
None,
endpointDescriptor,
UANodeDescriptor('nsu=http://test.org/UA/Data/ ;i=10855'),
UAMonitoringParameters(1000, UADataChangeFilter(UADataChangeTrigger.StatusValue))),
])
print('Processing data change events for 10 seconds...')
time.sleep(10)
print('Unsubscribing...')
client.UnsubscribeAllMonitoredItems()
print('Waiting for 5 seconds...')
time.sleep(5)
print('Finished.')
' This example shows how to subscribe to changes of multiple monitored items and use a data change filter.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.OperationModel
Namespace _EasyUAClient
Partial Friend Class SubscribeMultipleMonitoredItems
Public Shared Sub Filter()
' Define which server we will work with.
Dim endpointDescriptor As 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 and hook events
Dim client = New EasyUAClient()
AddHandler client.DataChangeNotification, AddressOf client_DataChangeNotification_Filter
Console.WriteLine("Subscribing...")
' Report a notification if either the StatusCode or the value change.
' The UADataChangeTrigger has an implicit conversion to UADataChangeFilter and can thus be used in its place.
client.SubscribeMultipleMonitoredItems(New EasyUAMonitoredItemArguments() _
{
New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10845", New UAMonitoringParameters(1000, UADataChangeTrigger.StatusValue)),
New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", New UAMonitoringParameters(1000, UADataChangeTrigger.StatusValue)),
New EasyUAMonitoredItemArguments(Nothing, endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10855", New UAMonitoringParameters(1000, UADataChangeTrigger.StatusValue))
}
)
Console.WriteLine("Processing monitored item changed events for 10 seconds...")
Threading.Thread.Sleep(10 * 1000)
Console.WriteLine("Unsubscribing...")
client.UnsubscribeAllMonitoredItems()
Console.WriteLine("Waiting for 5 seconds...")
Threading.Thread.Sleep(5 * 1000)
End Sub
Private Shared Sub client_DataChangeNotification_Filter(ByVal sender As Object, ByVal e As EasyUADataChangeNotificationEventArgs)
' Display value
If e.Succeeded Then
Console.WriteLine("{0}: {1}", e.Arguments.NodeDescriptor, e.AttributeData.Value)
Else
Console.WriteLine("{0} *** Failure: {1}", e.Arguments.NodeDescriptor, e.ErrorMessageBrief)
End If
End Sub
End Class
End Namespace