// This example shows how to create and use two isolated client objects, resulting in two separate connections to the target
// OPC UA server.
using System;
using System.Diagnostics;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;
namespace UADocExamples._EasyUAClient
{
class Isolated
{
public static void Main1()
{
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 objects and make them isolated
var client1 = new EasyUAClient { Isolated = true };
var client2 = new EasyUAClient { Isolated = true };
// The callback is a local method the displays the value
void DataChangeCallback(object sender, EasyUADataChangeNotificationEventArgs eventArgs)
{
Debug.Assert(!(eventArgs is null));
Console.Write($"[{eventArgs.Arguments.State}] ");
if (eventArgs.Succeeded)
{
Debug.Assert(!(eventArgs.AttributeData is null));
Console.WriteLine(eventArgs.AttributeData.ToString());
}
else
Console.WriteLine("*** Failure: {0}", eventArgs.ErrorMessageBrief);
}
Console.WriteLine("Subscribing...");
client1.SubscribeDataChange(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000,
DataChangeCallback, state: 1);
client2.SubscribeDataChange(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853", 1000,
DataChangeCallback, state: 2);
Console.WriteLine("Processing data change events for 10 seconds...");
System.Threading.Thread.Sleep(10 * 1000);
Console.WriteLine("Unsubscribing...");
client1.UnsubscribeAllMonitoredItems();
client2.UnsubscribeAllMonitoredItems();
Console.WriteLine("Waiting for 2 seconds...");
System.Threading.Thread.Sleep(2 * 1000);
}
}
}