public ReferenceDescriptionCollection GetServerTagList() { ReferenceDescriptionCollection result = new ReferenceDescriptionCollection(); try { ReferenceDescriptionCollection referenceDescriptions = session.FetchReferences(ObjectIds.ObjectsFolder); ReferenceDescriptionCollection additionalReferenceDescriptions = new ReferenceDescriptionCollection(); result.AddRange(referenceDescriptions); for (var i = 0; i < referenceDescriptions.Count; i++) { try { var refDef = FetchReferences(ExpandedNodeId.ToNodeId(referenceDescriptions[i].NodeId, session.NamespaceUris)); foreach (var r in refDef) { var item = referenceDescriptions.FirstOrDefault(x => x.NodeId == r.NodeId); if (item == null) { referenceDescriptions.Add(r); } additionalReferenceDescriptions.Add(r); } }catch (Exception ex) { var e = new Exception($"{i}: {referenceDescriptions[i].NodeId}", ex); e.Data.Add($"{i}", referenceDescriptions[i]); throw e; } //Recurse(rd, result); } } catch (Exception ex) { } return(result); }
public Dictionary <string, OpcOperation> GetOpcOperationCollection(List <Operation> operations, ReferenceDescriptionCollection references) { try { Dictionary <string, OpcOperation> _operationTriggers = operations.ToDictionary(x => x.Name, x => new OpcOperation("Recipe_" + x.Name + "_xload", new NodeId((references.FirstOrDefault(n => 0 == string.Compare(n.DisplayName.ToString(), "Recipe_" + x.Name + "_xload", true))?.NodeId.ToString())), new NodeId((references.FirstOrDefault(n => 0 == string.Compare(n.DisplayName.ToString(), "Recipe_" + x.Name + "_iNumSet", true))?.NodeId.ToString())), new NodeId((references.FirstOrDefault(n => 0 == string.Compare(n.DisplayName.ToString(), "Recipe_" + x.Name + "_iNumGet", true))?.NodeId.ToString())), x.Items.Item.Select(z => new OpcOperationItem("Recipe_" + x.Name + "_" + z.Name, z.Type, new NodeId((references.FirstOrDefault(n => 0 == string.Compare(n.DisplayName.ToString(), "Recipe_" + x.Name + "_" + z.Name, true))?.NodeId.ToString())))))); return(_operationTriggers); } catch (NullReferenceException ex) { _logger.Error("Error _operationTriggers collection can't be used: {0}", ex); throw ex; } catch (Exception ex) { _logger.Error(ex, "Error _operationTriggers collection can't be used: {0}", ex.Message); throw ex; } }
public static void Run(string pathToXml, string connectionString, string databaseName) { try { var _opcClient = new OpcClientService(); if (pathToXml == null || pathToXml.Length == 0) { throw new ArgumentException(); } _connectionString = connectionString; _databaseName = databaseName; var _operations = XmlSerializer.GetOperations(pathToXml); _operationTriggers = new Dictionary <string, OpcOperation>(); _branchs = new List <string>() { "plc1", "hmi_recipe" }; _operationManager = new OperationManager(_logger); _applicationConfiguration = _applicationConfiguration ?? _opcClient.GetApplicationConfiguration(); _applicationInstance = _applicationInstance ?? _opcClient.GetApplicationInstance(_applicationConfiguration); _ifSertificate = _opcClient.CheckSertificate(_applicationInstance); _endpointConfiguration = _endpointConfiguration ?? _opcClient.GetEndpointConfiguration(_applicationConfiguration); try { _endpointDescription = _endpointDescription ?? _opcClient.GetEndpointDescription(); } catch (ServiceResultException ex) { throw ex; } _configuredEndPoint = _configuredEndPoint ?? _opcClient.GetConfiguredEndpoint(_endpointDescription, _endpointConfiguration); _session = _opcClient.GetSession(_applicationConfiguration, _configuredEndPoint); if (!_session.Connected) { Thread.Sleep(10000); return; } if (_session == null) { _session.KeepAliveInterval = 2000; _session.KeepAlive += new KeepAliveEventHandler(Session_KeepAlive); } SearchDataInTree(_session, ObjectIds.ObjectsFolder); foreach (var s in _branchs) { var _element = s; SearchDataInTree(_session, new NodeId((_references.FirstOrDefault(x => 0 == string.Compare(x.DisplayName.ToString(), _element, true)).NodeId.ToString()))); } _operationTriggers = _operationManager.GetOpcOperationCollection(_operations, _references); var _operationsListForSubscribe = _references.FindAll(x => true == _operationTriggers.Any(z => 0 == string.Compare(z.Value.key, x.DisplayName.ToString(), true))); var _subscription = new Subscription(_session.DefaultSubscription) { PublishingInterval = 1000 }; var _monitoredItems = Subscribe(_subscription, _operationsListForSubscribe); _monitoredItems.ForEach(i => i.Notification += OnNotification); Console.WriteLine("Step 1 - Add a list of items you wish to monitor to the subscription."); _subscription.AddItems(_monitoredItems); Console.WriteLine("Step 2 - Add the subscription to the session."); _session.AddSubscription(_subscription); _subscription.Create(); Console.WriteLine("===================//--//========================="); Console.ReadKey(true); } catch (Exception ex) { _logger.Error(ex, "Error SearchDataInTree method: {0}", ex.Message); } }