Blog

Ottimizzare le performance in OpcUA

In questo esempio viene utilizzata la lista delle sottoscrizioni del server opcua per modificare il tempo di aggiornamento del client. In particolare se non vi sono sottoscrizioni il client arresta l’aggiornamento periodico delle variabili, in caso contrario il tempo di aggiornamento viene riattivato.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OptimizeOpcUaPerformance
{
	using IToolS.Components;
	using IToolS.Components.Communication;
	using IToolS.Components.IOServers;

	class Program
	{
		static void Main(string[] args)
		{
			ComponentBase.RaiseEventsOnMainThread = false;

			var variable1 = new Variable() { VariableName = "variable1", Address = "0" };
			var variable2 = new Variable() { VariableName = "variable2", Address = "1" };
			var variable3 = new Variable() { VariableName = "variable3", Address = "2" };
			var variable4 = new Variable() { VariableName = "variable4", Address = "3" };

			var ioServerOpcUA = new IOServer() { Name = "OpcUAServer" };
			var group = new Group() { GroupName = "group", IOServer = ioServerOpcUA };
			group.Items.AddRange(new Variable[] { variable1, variable2, variable3, variable4 });

			var ioServer = new IOServer() { Name = "Simulation" };

			var client = new Client() { ClientName = "ClientSimulation", Group = group, IOServer = ioServer, UpdateTime = 0 };

			foreach (Variable variable in group.Items)
			{
				variable.Changed += (sender, e) =>
				{
					Console.WriteLine(String.Format("{0} = {1}", variable.VariableName, e.NewValue));
				};
			}

			client.Start();
			group.StartIOServer();

			var opcua = (IToolS.IOServers.OpcFoundation.OpcUAServer)ioServerOpcUA;
			var reference = opcua.ReferenceServer;

			reference.CurrentInstance.SubscriptionManager.SubscriptionCreated += (subscription, deleted) =>
			{
				var subs = reference.CurrentInstance.SubscriptionManager.GetSubscriptions();
				client.UpdateTime = subs.Count > 0 ? 1000 : 0;
			};

			reference.CurrentInstance.SubscriptionManager.SubscriptionDeleted += (subscription, deleted) =>
			{
				var subs = reference.CurrentInstance.SubscriptionManager.GetSubscriptions();
				client.UpdateTime = subs.Count > 0 ? 1000 : 0;
			};

		

			Console.WriteLine("Press ENTER to exit");
			Console.ReadLine();

			group.StopIOServer();
			client.Stop();
		}
	}
}

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *