/// <summary> /// Добавление тегов к по подписке /// </summary> public override void AddTags(List <TagId> taglist) { TagListBackup = taglist; if (Activated) { try { var subscription = new Subscription(session.DefaultSubscription) { PublishingInterval = 1 }; int tag_counter = 0; var list = new List <MonitoredItem>(); //foreach (TagId tag in taglist) //пока конфигуратор пуст поиск делаем по всей сфере foreach (var tag in discoveredTags) { string identifier = ""; //if (discoveredTags.ContainsKey(tag.TagName)) //пока конфигуратор пуст поиск делаем по всей сфере if (tag.Key.Contains("Sfera")) { //logger.Logged("Info", "#" + PollerId + ": добавляем тег '" + tag.TagName + "' в подписку", logger.Logged("Info", "#" + PollerId + ": добавляем тег '" + tag.Key + "' в подписку", "OpcUaPoller", "AddTags"); //identifier = discoveredTags[tag.TagName]; //пока конфигуратор пуст поиск делаем по всей сфере identifier = discoveredTags[tag.Key]; tag_counter++; var item = new MonitoredItem(subscription.DefaultItem) { // DisplayName = tag.TagName, DisplayName = tag.Key, StartNodeId = identifier }; list.Add(item); } else { logger.Logged("Error", //"#" + PollerId + ": тег '" + tag.TagName + "' не обнаружен на сервере", "OpcUaPoller", "#" + PollerId + ": тег '" + tag.Key + "' не обнаружен на сервере", "OpcUaPoller", "AddTags"); } } if (tag_counter > 0) { list.ForEach(i => i.Notification += OnNotification); subscription.AddItems(list); session.AddSubscription(subscription); subscription.Create(); logger.Logged("Info", "Добавлено " + tag_counter + " тегов для контроля с OPC UA сервера #" + PollerId + "", "OpcUaPoller", "AddTags"); } else { logger.Logged("Error", "Не найдено ни одного тега для контроля OPC UA сервера #" + PollerId + "", "OpcUaPoller", "AddTags"); } } catch (Exception ex) { logger.Logged("Error", "Не удалось добавить теги для контроля OPC UA сервером #" + PollerId + ":" + ex.Message, "OpcUaPoller", "AddTags"); } } }
public async Task EndpointConnect() { var endpointCollection = DiscoverEndpoints(Module.Configuration, ServerUrl, 60); var selectedEndpoints = new List <EndpointDescription>(); // Select endpoints foreach (EndpointDescription endpoint in endpointCollection) { if (endpoint.TransportProfileUri == Profiles.UaTcpTransport && endpoint.SecurityLevel >= MinimumSecurityLevel && endpoint.SecurityMode >= MinimumSecurityMode) { // patch endpoint to set the original host name we want to connect to. var url = new UriBuilder(endpoint.EndpointUrl); url.Host = ServerUrl.Host; endpoint.EndpointUrl = url.ToString(); selectedEndpoints.Add(endpoint); } } // // Sort, but descending with highest level first i.e. return // < 0 if x is less than y // > 0 if x is greater than y // 0 if x and y are equal // selectedEndpoints.Sort((y, x) => x.SecurityLevel - y.SecurityLevel); // Do not emit all exceptions as they occur, only throw them all when no connection can be made. var exceptions = new List <Exception>(selectedEndpoints.Count); foreach (EndpointDescription endpoint in selectedEndpoints) { ConfiguredEndpoint configuredEndpoint = new ConfiguredEndpoint( endpoint.Server, EndpointConfiguration.Create(Module.Configuration)); configuredEndpoint.Update(endpoint); try { Console.WriteLine($"Opc.Ua.Client.SampleModule: Trying to create session with mode: {endpoint.SecurityMode}, level:{endpoint.SecurityLevel} to {configuredEndpoint.EndpointUrl}..."); _session = await Session.Create( Module.Configuration, configuredEndpoint, true, false, Module.Configuration.ApplicationName, 60000, // TODO: Make user identity configurable, plus add dedicated security policy new UserIdentity(new AnonymousIdentityToken()), null); if (_session != null) { var subscription = new Subscription(_session.DefaultSubscription); subscription.PublishingInterval = PublishingInterval; // TODO: Make other subscription settings configurable... subscription.AddItems(MonitoredItems); _session.AddSubscription(subscription); subscription.Create(); Console.WriteLine($"Opc.Ua.Client.SampleModule: Session with mode: {endpoint.SecurityMode}, level:{endpoint.SecurityLevel} to {configuredEndpoint.EndpointUrl} established!"); _session.KeepAlive += new KeepAliveEventHandler(StandardClient_KeepAlive); // Done return; } exceptions.Add(new Exception($"ERROR: Create session to endpoint {endpoint.ToString()} returned null.")); } catch (AggregateException ae) { exceptions.AddRange(ae.InnerExceptions); } catch (Exception ex) { exceptions.Add(ex); } // ... try another endpoint until we do not have any more... Console.WriteLine($"Opc.Ua.Client.SampleModule: WARNING Could not create session to endpoint {endpoint.ToString()}..."); } throw new AggregateException("Failed to find acceptable endpoint to connect to.", exceptions); }