// This example shows how subscribe to changes of multiple items and display each change, identifying the different
// subscriptions by an object.
using System;
using System.Threading;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.OperationModel;
namespace DocExamples.DataAccess._EasyDAClient
{
partial class SubscribeMultipleItems
{
class CustomObject
{
public CustomObject(string name)
{
Name = name;
}
public string Name { get; }
}
public static void StateAsObject()
{
// Instantiate the client object.
using (var client = new EasyDAClient())
{
// Hook events
client.ItemChanged += client_StateAsObject_ItemChanged;
Console.WriteLine("Subscribing...");
int[] handleArray = client.SubscribeMultipleItems(new[]
{
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Random", 1000,
new CustomObject("First")), // A custom object that corresponds to the subscription
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Ramp (1 min)", 1000,
new CustomObject("Second")), // A custom object that corresponds to the subscription
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Trends.Sine (1 min)", 1000,
new CustomObject("Third")), // A custom object that corresponds to the subscription
new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_I4", 1000,
new CustomObject("Fourth")) // A custom object that corresponds to the subscription
});
for (int i = 0; i < handleArray.Length; i++)
Console.WriteLine($"handleArray[{i}]: {handleArray[i]}");
Console.WriteLine("Processing item changed events for 10 seconds...");
Thread.Sleep(10 * 1000);
Console.WriteLine("Unsubscribing...");
}
Console.WriteLine("Waiting for 5 seconds...");
Thread.Sleep(5 * 1000);
Console.WriteLine("Finished.");
}
// Item changed event handler
static void client_StateAsObject_ItemChanged(object sender, EasyDAItemChangedEventArgs eventArgs)
{
// Obtain the custom object we have passed in.
var stateAsObject = (CustomObject)eventArgs.Arguments.State;
// Display the data
if (eventArgs.Succeeded)
Console.WriteLine($"{stateAsObject.Name}: {eventArgs.Vtq}");
else
Console.WriteLine($"{stateAsObject.Name} *** Failure: {eventArgs.ErrorMessageBrief}");
}
}
}
# This example shows how subscribe to changes of multiple items and display each change, identifying the different
# subscriptions by an object.
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc
import time
# Import .NET namespaces.
from OpcLabs.EasyOpc.DataAccess import *
from OpcLabs.EasyOpc.DataAccess.OperationModel import *
class CustomObject(object):
def __init__(self, name):
self.name = name
# Item changed event handler.
def itemChanged(sender, eventArgs):
# Obtain the custom object we have passed in.
stateAsObject = eventArgs.Arguments.State
if eventArgs.Succeeded:
print(stateAsObject.name, ': ', eventArgs.Vtq, sep='')
else:
print(stateAsObject.name, ' *** Failure: ', eventArgs.ErrorMessageBrief, sep='')
# Instantiate the client object.
client = EasyDAClient()
# Hook events.
client.ItemChanged += itemChanged
print('Subscribing item changes...')
handleArray = IEasyDAClientExtension.SubscribeMultipleItems(client, [
DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Simulation.Random', 1000,
CustomObject('First')), # a custom object that corresponds to the subscription
DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Trends.Ramp (1 min)', 1000,
CustomObject('Second')), # a custom object that corresponds to the subscription
DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Trends.Sine (1 min)', 1000,
CustomObject('Third')), # a custom object that corresponds to the subscription
DAItemGroupArguments('', 'OPCLabs.KitServer.2', 'Simulation.Register_I4', 1000,
CustomObject('Fourth')), # a custom object that corresponds to the subscription
])
for i in range(len(handleArray)):
print('handleArray[', i, ']: ', handleArray[i], sep='')
print('Processing item change notifications for 1 minute...')
time.sleep(60)
print('Unsubscribing all items...')
client.UnsubscribeAllItems()
client.ItemChanged -= itemChanged
print('Finished.')