/// <summary> /// Updates the application after connecting to or disconnecting from the server. /// </summary> private void Server_ConnectComplete(object sender, EventArgs e) { try { m_session = ConnectServerCTRL.Session; if (m_session == null) { return; } // set a suitable initial state. if (m_session != null && !m_connectedOnce) { m_connectedOnce = true; EventsLV.IsSubscribed = false; EventsLV.DisplayConditions = true; EventsLV.ChangeArea(Opc.Ua.ObjectIds.Server, false); FilterDeclaration filter = new FilterDeclaration(); ushort namespaceIndex = (ushort)m_session.NamespaceUris.GetIndex(DsatsDemo.Namespaces.DsatsDemo); filter.EventTypeId = ExpandedNodeId.ToNodeId(DsatsDemo.ObjectTypeIds.LockConditionType, m_session.NamespaceUris); filter.AddSimpleField(String.Empty, BuiltInType.NodeId, false); filter.AddSimpleField(Opc.Ua.BrowseNames.EventId, BuiltInType.ByteString, false); filter.AddSimpleField(Opc.Ua.BrowseNames.EventType, BuiltInType.NodeId, false); filter.AddSimpleField(Opc.Ua.BrowseNames.ConditionName, BuiltInType.String, true); filter.AddSimpleField(new QualifiedName[] { new QualifiedName(DsatsDemo.BrowseNames.LockState, namespaceIndex), new QualifiedName(Opc.Ua.BrowseNames.CurrentState) }, BuiltInType.String, ValueRanks.Scalar, true); filter.AddSimpleField(new QualifiedName(DsatsDemo.BrowseNames.ClientUserId, namespaceIndex), BuiltInType.String, true); filter.AddSimpleField(new QualifiedName(DsatsDemo.BrowseNames.SubjectName, namespaceIndex), BuiltInType.String, true); filter.AddSimpleField(new QualifiedName(DsatsDemo.BrowseNames.SessionId, namespaceIndex), BuiltInType.NodeId, true); EventsLV.ChangeFilter(filter, true); m_filter = filter; PhaseCB.Items.Clear(); BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = new NodeId(DsatsDemo.Objects.Rig_Phases, namespaceIndex); nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HierarchicalReferences; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; DataValue value = m_session.ReadValue(new NodeId(DsatsDemo.Variables.Rig_CurrentPhase, namespaceIndex)); NodeId currentPhase = value.GetValue<NodeId>(null); ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, nodeToBrowse, false); if (references != null) { for (int ii = 0; ii < references.Count; ii++) { PhaseCB.Items.Add(references[ii]); if (currentPhase == references[ii].NodeId) { PhaseCB.SelectedIndex = ii; } } } m_connectedOnce = true; } EventsLV.IsSubscribed = true; EventsLV.ChangeSession(m_session, true); EventsLV.ConditionRefresh(); } catch (Exception exception) { MessageBox.Show(exception.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public static ReferenceDescriptionCollection Browse(Session session, BrowseDescription nodeToBrowse, bool throwOnError) { try { var descriptionCollection = new ReferenceDescriptionCollection(); var nodesToBrowse = new BrowseDescriptionCollection { nodeToBrowse }; BrowseResultCollection results; DiagnosticInfoCollection diagnosticInfos; session.Browse(null, null, 0U, nodesToBrowse, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToBrowse); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse); while (!StatusCode.IsBad(results[0].StatusCode)) { for (var index = 0; index < results[0].References.Count; ++index) descriptionCollection.Add(results[0].References[index]); if (results[0].References.Count == 0 || results[0].ContinuationPoint == null) return descriptionCollection; var continuationPoints = new ByteStringCollection(); continuationPoints.Add(results[0].ContinuationPoint); session.BrowseNext(null, false, continuationPoints, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, continuationPoints); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, continuationPoints); } throw new ServiceResultException(results[0].StatusCode); } catch (Exception ex) { if (throwOnError) throw new ServiceResultException(ex, 2147549184U); return null; } }
public static ReferenceDescriptionCollection Browse(Session session, NodeId nodeId) { var desc = new BrowseDescription { NodeId = nodeId, BrowseDirection = BrowseDirection.Forward, IncludeSubtypes = true, NodeClassMask = 0U, ResultMask = 63U, }; return Browse(session, desc, true); }
/// <summary> /// Collects the fields for the instance node. /// </summary> private static void CollectInstanceDeclarations( Session session, NodeId typeId, InstanceDeclaration parent, List<InstanceDeclaration> instances, IDictionary<string, InstanceDeclaration> map) { // find the children. BrowseDescription nodeToBrowse = new BrowseDescription(); if (parent == null) { nodeToBrowse.NodeId = typeId; } else { nodeToBrowse.NodeId = parent.NodeId; } nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = ReferenceTypeIds.HasChild; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable | NodeClass.Method); nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; // ignore any browsing errors. ReferenceDescriptionCollection references = ClientUtils.Browse(session, nodeToBrowse, false); if (references == null) { return; } // process the children. List<NodeId> nodeIds = new List<NodeId>(); List<InstanceDeclaration> children = new List<InstanceDeclaration>(); for (int ii = 0; ii < references.Count; ii++) { ReferenceDescription reference = references[ii]; if (reference.NodeId.IsAbsolute) { continue; } // create a new declaration. InstanceDeclaration child = new InstanceDeclaration(); child.RootTypeId = typeId; child.NodeId = (NodeId)reference.NodeId; child.BrowseName = reference.BrowseName; child.NodeClass = reference.NodeClass; if (!LocalizedText.IsNullOrEmpty(reference.DisplayName)) { child.DisplayName = reference.DisplayName.Text; } else { child.DisplayName = reference.BrowseName.Name; } if (parent != null) { child.BrowsePath = new QualifiedNameCollection(parent.BrowsePath); child.BrowsePathDisplayText = Utils.Format("{0}/{1}", parent.BrowsePathDisplayText, reference.BrowseName); child.DisplayPath = Utils.Format("{0}/{1}", parent.DisplayPath, reference.DisplayName); } else { child.BrowsePath = new QualifiedNameCollection(); child.BrowsePathDisplayText = Utils.Format("{0}", reference.BrowseName); child.DisplayPath = Utils.Format("{0}", reference.DisplayName); } child.BrowsePath.Add(reference.BrowseName); // check if reading an overridden declaration. InstanceDeclaration overriden = null; if (map.TryGetValue(child.BrowsePathDisplayText, out overriden)) { child.OverriddenDeclaration = overriden; } map[child.BrowsePathDisplayText] = child; // add to list. children.Add(child); nodeIds.Add(child.NodeId); } // check if nothing more to do. if (children.Count == 0) { return; } // find the modelling rules. List<NodeId> modellingRules = FindTargetOfReference(session, nodeIds, Opc.Ua.ReferenceTypeIds.HasModellingRule, false); if (modellingRules != null) { for (int ii = 0; ii < nodeIds.Count; ii++) { children[ii].ModellingRule = modellingRules[ii]; // if the modelling rule is null then the instance is not part of the type declaration. if (NodeId.IsNull(modellingRules[ii])) { map.Remove(children[ii].BrowsePathDisplayText); } } } // update the descriptions. UpdateInstanceDescriptions(session, children, false); // recusively collect instance declarations for the tree below. for (int ii = 0; ii < children.Count; ii++) { if (!NodeId.IsNull(children[ii].ModellingRule)) { instances.Add(children[ii]); CollectInstanceDeclarations(session, typeId, children[ii], instances, map); } } }
/// <summary> /// Finds the targets for the specified reference. /// </summary> private static List<NodeId> FindTargetOfReference(Session session, List<NodeId> nodeIds, NodeId referenceTypeId, bool throwOnError) { try { // construct browse request. BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); for (int ii = 0; ii < nodeIds.Count; ii++) { BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = nodeIds[ii]; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = referenceTypeId; nodeToBrowse.IncludeSubtypes = false; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.None; nodesToBrowse.Add(nodeToBrowse); } // start the browse operation. BrowseResultCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; session.Browse( null, null, 1, nodesToBrowse, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToBrowse); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse); List<NodeId> targetIds = new List<NodeId>(); ByteStringCollection continuationPoints = new ByteStringCollection(); for (int ii = 0; ii < nodeIds.Count; ii++) { targetIds.Add(null); // check for error. if (StatusCode.IsBad(results[ii].StatusCode)) { continue; } // check for continuation point. if (results[ii].ContinuationPoint != null && results[ii].ContinuationPoint.Length > 0) { continuationPoints.Add(results[ii].ContinuationPoint); } // get the node id. if (results[ii].References.Count > 0) { if (NodeId.IsNull(results[ii].References[0].NodeId) || results[ii].References[0].NodeId.IsAbsolute) { continue; } targetIds[ii] = (NodeId)results[ii].References[0].NodeId; } } // release continuation points. if (continuationPoints.Count > 0) { session.BrowseNext( null, true, continuationPoints, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToBrowse); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse); } //return complete list. return targetIds; } catch (Exception exception) { if (throwOnError) { throw new ServiceResultException(exception, StatusCodes.BadUnexpectedError); } return null; } }
/// <summary> /// Gets the boilers. /// </summary> private void GetBoilers() { BoilerCB.Items.Clear(); BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = Opc.Ua.ObjectIds.ObjectsFolder; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HierarchicalReferences; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = (uint)(NodeClass.Object); nodeToBrowse.ResultMask = (uint)(BrowseResultMask.All); ReferenceDescriptionCollection references = ClientUtils.Browse( m_session, nodeToBrowse, false); if (references != null) { NodeId boilerTypeId = ExpandedNodeId.ToNodeId(ObjectTypeIds.BoilerType, m_session.NamespaceUris); for (int ii = 0; ii < references.Count; ii++) { if (boilerTypeId == references[ii].TypeDefinition) { BoilerCB.Items.Add(references[ii]); } } if (BoilerCB.Items.Count > 0) { BoilerCB.SelectedIndex = 0; } } }
/// <summary> /// Populates the branch in the tree view. /// </summary> /// <param name="sourceId">The NodeId of the Node to browse.</param> /// <param name="nodes">The node collect to populate.</param> private void PopulateBranch(NodeId sourceId, TreeNodeCollection nodes) { try { nodes.Clear(); // find all of the components of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.Aggregates; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; // find all nodes organized by the node. BrowseDescription nodeToBrowse2 = new BrowseDescription(); nodeToBrowse2.NodeId = sourceId; nodeToBrowse2.BrowseDirection = BrowseDirection.Forward; nodeToBrowse2.ReferenceTypeId = ReferenceTypeIds.Organizes; nodeToBrowse2.IncludeSubtypes = true; nodeToBrowse2.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse2.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); nodesToBrowse.Add(nodeToBrowse2); // fetch references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false); // process results. for (int ii = 0; ii < references.Count; ii++) { ReferenceDescription target = references[ii]; // add node. TreeNode child = new TreeNode(Utils.Format("{0}", target)); child.Tag = target; child.Nodes.Add(new TreeNode()); nodes.Add(child); } // update the attributes display. DisplayAttributes(sourceId); } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// Handles the BeforeExpand event of the BrowseTV control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param> private void BrowseTV_BeforeExpand(object sender, TreeViewCancelEventArgs e) { try { ReferenceDescription reference = (ReferenceDescription)e.Node.Tag; e.Node.Nodes.Clear(); // browse HasEventSource to display the sources but it won't be possible to select them. BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = Opc.Ua.ObjectTypeIds.BaseEventType; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = ReferenceTypeIds.HasSubtype; nodeToBrowse.IncludeSubtypes = false; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; if (reference != null) { nodeToBrowse.NodeId = (NodeId)reference.NodeId; } // add the childen to the control. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodeToBrowse, false); for (int ii = 0; ii < references.Count; ii++) { reference = references[ii]; // ignore out of server references. if (reference.NodeId.IsAbsolute) { continue; } TreeNode child = new TreeNode(reference.ToString()); child.Nodes.Add(new TreeNode()); child.Tag = reference; e.Node.Nodes.Add(child); } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// Browses the address space and returns all of the supertypes of the specified type node. /// </summary> /// <param name="session">The session.</param> /// <param name="typeId">The NodeId for a type node in the address space.</param> /// <param name="throwOnError">if set to <c>true</c> a exception will be thrown on an error.</param> /// <returns> /// The references found. Null if an error occurred. /// </returns> public static ReferenceDescriptionCollection BrowseSuperTypes(Session session, NodeId typeId, bool throwOnError) { ReferenceDescriptionCollection supertypes = new ReferenceDescriptionCollection(); try { // find all of the children of the field. BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = typeId; nodeToBrowse.BrowseDirection = BrowseDirection.Inverse; nodeToBrowse.ReferenceTypeId = ReferenceTypeIds.HasSubtype; nodeToBrowse.IncludeSubtypes = false; // more efficient to use IncludeSubtypes=False when possible. nodeToBrowse.NodeClassMask = 0; // the HasSubtype reference already restricts the targets to Types. nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; ReferenceDescriptionCollection references = Browse(session, nodeToBrowse, throwOnError); while (references != null && references.Count > 0) { // should never be more than one supertype. supertypes.Add(references[0]); // only follow references within this server. if (references[0].NodeId.IsAbsolute) { break; } // get the references for the next level up. nodeToBrowse.NodeId = (NodeId)references[0].NodeId; references = Browse(session, nodeToBrowse, throwOnError); } // return complete list. return supertypes; } catch (Exception exception) { if (throwOnError) { throw new ServiceResultException(exception, StatusCodes.BadUnexpectedError); } return null; } }
/// <summary> /// Browses the address space and returns the references found. /// </summary> /// <param name="session">The session.</param> /// <param name="nodeToBrowse">The NodeId for the starting node.</param> /// <param name="throwOnError">if set to <c>true</c> a exception will be thrown on an error.</param> /// <returns> /// The references found. Null if an error occurred. /// </returns> public static ReferenceDescriptionCollection Browse(Session session, BrowseDescription nodeToBrowse, bool throwOnError) { return Browse(session, null, nodeToBrowse, throwOnError); }
private void ReadNode(NodeId nodeid, bool ignoreroot, bool ignorechildren, String DriverName) { INode node = m_session.NodeCache.Find(nodeid); if (node != null) { //TreeNode root = new TreeNode(node.ToString()); //root.ImageIndex = ClientUtils.GetImageIndex(m_session, node.NodeClass, node.TypeDefinitionId, false); //root.SelectedImageIndex = ClientUtils.GetImageIndex(m_session, node.NodeClass, node.TypeDefinitionId, true); ReferenceDescription reference = new ReferenceDescription(); reference.NodeId = node.NodeId; reference.NodeClass = node.NodeClass; reference.BrowseName = node.BrowseName; reference.DisplayName = node.DisplayName; reference.TypeDefinition = node.TypeDefinitionId; // build list of attributes to read. ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); foreach (uint attributeId in Attributes.GetIdentifiers()) { ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)reference.NodeId; nodeToRead.AttributeId = attributeId; nodesToRead.Add(nodeToRead); } // read the attributes. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); if (ignoreroot == false) { //Log(results[0].ToString() + " \t| " + results[3].ToString() + " \t| "+ results[4].ToString() + " \t| " + results[12].ToString()); Log("UID:" + S80(results[(int)Attributes.NodeId - 1].ToString()) + " \tBrowseName: " + s40(results[(int)Attributes.BrowseName - 1].ToString()) + " \tDisplayName: " + s40(results[(int)Attributes.DisplayName - 1].ToString()) + " \tDescription: " + S80(results[(int)Attributes.Description - 1].ToString()) + " \tValue: " + results[(int)Attributes.Value - 1].ToString()); dr = dt.NewRow(); dr["UID"] = results[(int)Attributes.NodeId - 1].ToString(); dr["BrowseName"] = results[(int)Attributes.BrowseName - 1].ToString(); dr["DisplayName"] = results[(int)Attributes.DisplayName - 1].ToString(); dr["Description"] = results[(int)Attributes.Description - 1].ToString(); dr["Value"] = results[(int)Attributes.Value - 1].ToString(); dr["Name"] = DriverName; dt.Rows.Add(dr); if (results[(int)Attributes.AccessLevel - 1].Value != null) { if (((byte)(results[(int)Attributes.AccessLevel - 1].GetValue(typeof(byte))) & AccessLevels.HistoryRead) == AccessLevels.HistoryRead) { //gv.DataSource = dt; ReadHistory(DateTime.Now.AddDays(-1), DateTime.Now, new NodeId(results[0].ToString()), 25, ""); } } } if (ignorechildren == false) { BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = nodeid; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HierarchicalReferences; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; if (reference != null) { nodeToBrowse.NodeId = (NodeId)reference.NodeId; } nodesToBrowse.Add(nodeToBrowse); ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, null, nodesToBrowse, false); for (int ii = 0; references != null && ii < references.Count; ii++) { reference = references[ii]; // build list of attributes to read. nodesToRead = new ReadValueIdCollection(); foreach (uint attributeId in Attributes.GetIdentifiers()) { ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)reference.NodeId; nodeToRead.AttributeId = attributeId; nodesToRead.Add(nodeToRead); } // read the attributes. results = null; diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); // Log("UID:"+results[0].ToString() + " \tBrowseName: " + results[(int)Attributes.BrowseName].ToString() + " \tDisplayName: " + results[(int)Attributes.DisplayName].ToString() + " \tDescription: " + results[(int)Attributes.Description].ToString() + " \t| " + results[(int)Attributes.Value].ToString()); //Log("UID:" + S80(results[(int)Attributes.NodeId - 1].ToString()) + " \tBrowseName: " + results[(int)Attributes.BrowseName - 1].ToString() + " \tDisplayName: " + results[(int)Attributes.DisplayName - 1].ToString() + " \tDescription: " + results[(int)Attributes.Description - 1].ToString() + " \tValue: " + results[(int)Attributes.Value - 1].ToString()); Log("UID:" + S80(results[(int)Attributes.NodeId - 1].ToString()) + " \tBrowseName: " + s40(results[(int)Attributes.BrowseName - 1].ToString()) + " \tDisplayName: " + s40(results[(int)Attributes.DisplayName - 1].ToString()) + " \tDescription: " + S80(results[(int)Attributes.Description - 1].ToString()) + " \tValue: " + results[(int)Attributes.Value - 1].ToString()); dr = dt.NewRow(); dr["UID"] = results[(int)Attributes.NodeId - 1].ToString(); dr["BrowseName"] = results[(int)Attributes.BrowseName - 1].ToString(); dr["DisplayName"] = results[(int)Attributes.DisplayName - 1].ToString(); dr["Description"] = results[(int)Attributes.Description - 1].ToString(); dr["Value"] = results[(int)Attributes.Value - 1].ToString(); dt.Rows.Add(dr); try { if (results[(int)Attributes.AccessLevel - 1].Value != null) { if (((byte)(results[(int)Attributes.AccessLevel - 1].GetValue(typeof(byte))) & AccessLevels.HistoryRead) == AccessLevels.HistoryRead) { //gv.DataSource = dt; ReadHistory(DateTime.Today, DateTime.Now, new NodeId(results[0].ToString()), 25, ""); } } else { if (results[(int)Attributes.Value - 1].Value == null) { // gv.DataSource = dt; ReadNode(new NodeId(results[(int)Attributes.NodeId - 1].ToString()), true, false, ""); } } } catch { Log("V=" + results[17].ToString()); } } } } }
/// <summary> /// Handles the BeforeExpand event of the BrowseTV control. /// </summary> private void BrowseTV_BeforeExpand(object sender, TreeViewCancelEventArgs e) { try { ReferenceDescription reference = (ReferenceDescription)e.Node.Tag; e.Node.Nodes.Clear(); // build list of references to browse. BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); for (int ii = 0; ii < m_referenceTypeIds.Length; ii++) { BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = m_rootId; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = m_referenceTypeIds[ii]; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; if (reference != null) { nodeToBrowse.NodeId = (NodeId)reference.NodeId; } nodesToBrowse.Add(nodeToBrowse); } // add the childen to the control. ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, View, nodesToBrowse, false); for (int ii = 0; references != null && ii < references.Count; ii++) { reference = references[ii]; // ignore out of server references. if (reference.NodeId.IsAbsolute) { continue; } TreeNode child = new TreeNode(reference.ToString()); child.Nodes.Add(new TreeNode()); child.Tag = reference; e.Node.Nodes.Add(child); } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// Reads the historical configuration for the node. /// </summary> private List<PropertyWithHistory> FindPropertiesWithHistory() { BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = m_nodeId; nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HasProperty; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)(BrowseResultMask.DisplayName | BrowseResultMask.BrowseName); ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, nodeToBrowse, false); ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); for (int ii = 0; ii < references.Count; ii++) { if (references[ii].NodeId.IsAbsolute) { continue; } ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)references[ii].NodeId; nodeToRead.AttributeId = Attributes.AccessLevel; nodeToRead.Handle = references[ii]; nodesToRead.Add(nodeToRead); } List<PropertyWithHistory> properties = new List<PropertyWithHistory>(); properties.Add(new PropertyWithHistory() { DisplayText = "(none)", NodeId = m_nodeId, AccessLevel = AccessLevels.HistoryReadOrWrite }); if (nodesToRead.Count > 0) { DataValueCollection values = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out values, out diagnosticInfos); ClientBase.ValidateResponse(values, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); for (int ii = 0; ii < nodesToRead.Count; ii++) { byte accessLevel = values[ii].GetValue<byte>(0); if ((accessLevel & AccessLevels.HistoryRead) != 0) { properties.Add(new PropertyWithHistory((ReferenceDescription)nodesToRead[ii].Handle, accessLevel)); } } } return properties; }
/// <summary> /// Displays the attributes and properties in the attributes view. /// </summary> /// <param name="sourceId">The NodeId of the Node to browse.</param> private void DisplayAttributes(NodeId sourceId) { try { AttributesLV.Items.Clear(); ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); // attempt to read all possible attributes. for (uint ii = Attributes.NodeClass; ii <= Attributes.UserExecutable; ii++) { ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = sourceId; nodeToRead.AttributeId = ii; nodesToRead.Add(nodeToRead); } int startOfProperties = nodesToRead.Count; // find all of the pror of the node. BrowseDescription nodeToBrowse1 = new BrowseDescription(); nodeToBrowse1.NodeId = sourceId; nodeToBrowse1.BrowseDirection = BrowseDirection.Forward; nodeToBrowse1.ReferenceTypeId = ReferenceTypeIds.HasProperty; nodeToBrowse1.IncludeSubtypes = true; nodeToBrowse1.NodeClassMask = 0; nodeToBrowse1.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse1); // fetch property references from the server. ReferenceDescriptionCollection references = FormUtils.Browse(m_session, nodesToBrowse, false); if (references == null) { return; } for (int ii = 0; ii < references.Count; ii++) { // ignore external references. if (references[ii].NodeId.IsAbsolute) { continue; } ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)references[ii].NodeId; nodeToRead.AttributeId = Attributes.Value; nodesToRead.Add(nodeToRead); } // read all values. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); // process results. for (int ii = 0; ii < results.Count; ii++) { string name = null; string datatype = null; string value = null; // process attribute value. if (ii < startOfProperties) { // ignore attributes which are invalid for the node. if (results[ii].StatusCode == StatusCodes.BadAttributeIdInvalid) { continue; } // get the name of the attribute. name = Attributes.GetBrowseName(nodesToRead[ii].AttributeId); // display any unexpected error. if (StatusCode.IsBad(results[ii].StatusCode)) { datatype = Utils.Format("{0}", Attributes.GetDataTypeId(nodesToRead[ii].AttributeId)); value = Utils.Format("{0}", results[ii].StatusCode); } // display the value. else { TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value); datatype = typeInfo.BuiltInType.ToString(); if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions) { datatype += "[]"; } value = Utils.Format("{0}", results[ii].Value); } } // process property value. else { // ignore properties which are invalid for the node. if (results[ii].StatusCode == StatusCodes.BadNodeIdUnknown) { continue; } // get the name of the property. name = Utils.Format("{0}", references[ii-startOfProperties]); // display any unexpected error. if (StatusCode.IsBad(results[ii].StatusCode)) { datatype = String.Empty; value = Utils.Format("{0}", results[ii].StatusCode); } // display the value. else { TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value); datatype = typeInfo.BuiltInType.ToString(); if (typeInfo.ValueRank >= ValueRanks.OneOrMoreDimensions) { datatype += "[]"; } value = Utils.Format("{0}", results[ii].Value); } } // add the attribute name/value to the list view. ListViewItem item = new ListViewItem(name); item.SubItems.Add(datatype); item.SubItems.Add(value); AttributesLV.Items.Add(item); } // adjust width of all columns. for (int ii = 0; ii < AttributesLV.Columns.Count; ii++) { AttributesLV.Columns[ii].Width = -2; } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// Reads the properties for the node. /// </summary> private void ReadProperties(NodeId nodeId) { // build list of references to browse. BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = nodeId; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HasProperty; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = (uint)NodeClass.Variable; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; nodesToBrowse.Add(nodeToBrowse); // find properties. ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, View, nodesToBrowse, false); // build list of properties to read. ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); for (int ii = 0; references != null && ii < references.Count; ii++) { ReferenceDescription reference = references[ii]; // ignore out of server references. if (reference.NodeId.IsAbsolute) { continue; } ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)reference.NodeId; nodeToRead.AttributeId = Attributes.Value; nodeToRead.Handle = reference; nodesToRead.Add(nodeToRead); } if (nodesToRead.Count == 0) { return; } // read the properties. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); // add the results to the display. for (int ii = 0; ii < results.Count; ii++) { ReferenceDescription reference = (ReferenceDescription)nodesToRead[ii].Handle; TypeInfo typeInfo = TypeInfo.Construct(results[ii].Value); // add the metadata for the attribute. ListViewItem item = new ListViewItem(reference.ToString()); item.SubItems.Add(typeInfo.BuiltInType.ToString()); if (typeInfo.ValueRank >= 0) { item.SubItems[1].Text += "[]"; } // add the value. if (StatusCode.IsBad(results[ii].StatusCode)) { item.SubItems.Add(results[ii].StatusCode.ToString()); } else { item.SubItems.Add(results[ii].WrappedValue.ToString()); } item.Tag = new AttributeInfo() { NodeToRead = nodesToRead[ii], Value = results[ii] }; item.ImageIndex = ClientUtils.GetImageIndex(m_session, NodeClass.Variable, Opc.Ua.VariableTypeIds.PropertyType, false); // display in list. AttributesLV.Items.Add(item); } }
/// <summary> /// Reads the arguments for the method. /// </summary> private void ReadArguments(NodeId nodeId) { m_inputArguments = null; m_outputArguments = null; // build list of references to browse. BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = nodeId; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HasProperty; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = (uint)NodeClass.Variable; nodeToBrowse.ResultMask = (uint)BrowseResultMask.BrowseName; nodesToBrowse.Add(nodeToBrowse); // find properties. ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, null, nodesToBrowse, false); // build list of properties to read. ReadValueIdCollection nodesToRead = new ReadValueIdCollection(); for (int ii = 0; references != null && ii < references.Count; ii++) { ReferenceDescription reference = references[ii]; // ignore out of server references. if (reference.NodeId.IsAbsolute) { continue; } // ignore other properties. if (reference.BrowseName != Opc.Ua.BrowseNames.InputArguments && reference.BrowseName != Opc.Ua.BrowseNames.OutputArguments) { continue; } ReadValueId nodeToRead = new ReadValueId(); nodeToRead.NodeId = (NodeId)reference.NodeId; nodeToRead.AttributeId = Attributes.Value; nodeToRead.Handle = reference; nodesToRead.Add(nodeToRead); } // method has no arguments. if (nodesToRead.Count == 0) { return; } // read the arguments. DataValueCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; m_session.Read( null, 0, TimestampsToReturn.Neither, nodesToRead, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToRead); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToRead); // save the results. for (int ii = 0; ii < results.Count; ii++) { ReferenceDescription reference = (ReferenceDescription)nodesToRead[ii].Handle; if (StatusCode.IsGood(results[ii].StatusCode)) { if (reference.BrowseName == Opc.Ua.BrowseNames.InputArguments) { m_inputArguments = (Argument[])ExtensionObject.ToArray(results[ii].GetValue<ExtensionObject[]>(null), typeof(Argument)); } if (reference.BrowseName == Opc.Ua.BrowseNames.OutputArguments) { m_outputArguments = (Argument[])ExtensionObject.ToArray(results[ii].GetValue<ExtensionObject[]>(null), typeof(Argument)); } } } // set default values for input arguments. if (m_inputArguments != null) { foreach (Argument argument in m_inputArguments) { argument.Value = TypeInfo.GetDefaultValue(argument.DataType, argument.ValueRank, m_session.TypeTree); } } }
/// <summary> /// Handles the BeforeExpand event of the BrowseTV control. /// </summary> private void BrowseTV_BeforeExpand(object sender, TreeViewCancelEventArgs e) { try { ReferenceDescription reference = (ReferenceDescription)e.Node.Tag; e.Node.Nodes.Clear(); // build list of references to browse. BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); for (int ii = 0; ii < m_referenceTypeIds.Length; ii++) { BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = m_rootId; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = m_referenceTypeIds[ii]; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; if (reference != null) { nodeToBrowse.NodeId = (NodeId)reference.NodeId; } nodesToBrowse.Add(nodeToBrowse); } // add the childen to the control. SortedDictionary<ExpandedNodeId, TreeNode> dictionary = new SortedDictionary<ExpandedNodeId, TreeNode>(); ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, View, nodesToBrowse, false); for (int ii = 0; references != null && ii < references.Count; ii++) { reference = references[ii]; // ignore out of server references. if (reference.NodeId.IsAbsolute) { continue; } if (dictionary.ContainsKey(reference.NodeId)) { continue; } TreeNode child = new TreeNode(reference.ToString()); child.Nodes.Add(new TreeNode()); child.Tag = reference; if (!reference.TypeDefinition.IsAbsolute) { try { if (!m_typeImageMapping.ContainsKey((NodeId)reference.TypeDefinition)) { List<NodeId> nodeIds = ClientUtils.TranslateBrowsePaths(m_session, (NodeId)reference.TypeDefinition, m_session.NamespaceUris, Opc.Ua.BrowseNames.Icon); if (nodeIds.Count > 0 && nodeIds[0] != null) { DataValue value = m_session.ReadValue(nodeIds[0]); byte[] bytes = value.Value as byte[]; if (bytes != null) { System.IO.MemoryStream istrm = new System.IO.MemoryStream(bytes); Image icon = Image.FromStream(istrm); BrowseTV.ImageList.Images.Add(icon); m_typeImageMapping[(NodeId)reference.TypeDefinition] = BrowseTV.ImageList.Images.Count - 1; } } } } catch (Exception exception) { Utils.Trace(exception, "Error loading image."); } } int index = 0; if (!m_typeImageMapping.TryGetValue((NodeId)reference.TypeDefinition, out index)) { child.ImageIndex = ClientUtils.GetImageIndex(m_session, reference.NodeClass, reference.TypeDefinition, false); child.SelectedImageIndex = ClientUtils.GetImageIndex(m_session, reference.NodeClass, reference.TypeDefinition, true); } else { child.ImageIndex = index; child.SelectedImageIndex = index; } dictionary[reference.NodeId] = child; } // add nodes to tree. foreach (TreeNode node in dictionary.Values.OrderBy(i => i.Text)) { e.Node.Nodes.Add(node); } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// Browses the address space and returns the references found. /// </summary> public static ReferenceDescriptionCollection Browse(Session session, ViewDescription view, BrowseDescription nodeToBrowse, bool throwOnError) { try { ReferenceDescriptionCollection references = new ReferenceDescriptionCollection(); // construct browse request. BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse); // start the browse operation. BrowseResultCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; session.Browse( null, view, 0, nodesToBrowse, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, nodesToBrowse); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse); do { // check for error. if (StatusCode.IsBad(results[0].StatusCode)) { throw new ServiceResultException(results[0].StatusCode); } // process results. for (int ii = 0; ii < results[0].References.Count; ii++) { references.Add(results[0].References[ii]); } // check if all references have been fetched. if (results[0].References.Count == 0 || results[0].ContinuationPoint == null) { break; } // continue browse operation. ByteStringCollection continuationPoints = new ByteStringCollection(); continuationPoints.Add(results[0].ContinuationPoint); session.BrowseNext( null, false, continuationPoints, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, continuationPoints); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, continuationPoints); } while (true); //return complete list. return references; } catch (Exception exception) { if (throwOnError) { throw new ServiceResultException(exception, StatusCodes.BadUnexpectedError); } return null; } }
/// <summary> /// Updates the application after connecting to or disconnecting from the server. /// </summary> private void Server_ConnectComplete(object sender, EventArgs e) { try { m_session = ConnectServerCTRL.Session; // set a suitable initial state. if (m_session != null && !m_connectedOnce) { m_connectedOnce = true; BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = Opc.Ua.ObjectIds.ViewsFolder; nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HierarchicalReferences; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; ReferenceDescriptionCollection references = ClientUtils.Browse(m_session, nodeToBrowse, false); ViewCB.Items.Clear(); ViewCB.Items.Add(new ReferenceDescription() { NodeId = ExpandedNodeId.Null, DisplayName = "None" }); if (references != null) { for (int ii = 0; ii < references.Count; ii++) { ViewCB.Items.Add(references[ii]); } } ViewCB.SelectedIndex = 0; } // browse the instances in the server. BrowseCTRL.Initialize(m_session, ObjectIds.ObjectsFolder, ReferenceTypeIds.Organizes, ReferenceTypeIds.Aggregates); } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// Collects the fields for the instance node. /// </summary> /// <param name="session">The session.</param> /// <param name="nodeId">The node id.</param> /// <param name="parentPath">The parent path.</param> /// <param name="fields">The event fields.</param> /// <param name="fieldNodeIds">The node id for the declaration of the field.</param> /// <param name="foundNodes">The table of found nodes.</param> private static void CollectFields( Session session, NodeId nodeId, QualifiedNameCollection parentPath, SimpleAttributeOperandCollection fields, List<NodeId> fieldNodeIds, Dictionary<NodeId, QualifiedNameCollection> foundNodes) { // find all of the children of the field. BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = nodeId; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = ReferenceTypeIds.Aggregates; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = (uint)(NodeClass.Object | NodeClass.Variable); nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; ReferenceDescriptionCollection children = ClientUtils.Browse(session, nodeToBrowse, false); if (children == null) { return; } // process the children. for (int ii = 0; ii < children.Count; ii++) { ReferenceDescription child = children[ii]; if (child.NodeId.IsAbsolute) { continue; } // construct browse path. QualifiedNameCollection browsePath = new QualifiedNameCollection(parentPath); browsePath.Add(child.BrowseName); // check if the browse path is already in the list. int index = ContainsPath(fields, browsePath); if (index < 0) { SimpleAttributeOperand field = new SimpleAttributeOperand(); field.TypeDefinitionId = ObjectTypeIds.BaseEventType; field.BrowsePath = browsePath; field.AttributeId = (child.NodeClass == NodeClass.Variable) ? Attributes.Value : Attributes.NodeId; fields.Add(field); fieldNodeIds.Add((NodeId)child.NodeId); } // recusively find all of the children. NodeId targetId = (NodeId)child.NodeId; // need to guard against loops. if (!foundNodes.ContainsKey(targetId)) { foundNodes.Add(targetId, browsePath); CollectFields(session, (NodeId)child.NodeId, browsePath, fields, fieldNodeIds, foundNodes); } } }
/// <summary> /// Fetches the references for the node. /// </summary> private List<ReferenceDescription> Browse(Session session, NodeId nodeId) { List<ReferenceDescription> references = new List<ReferenceDescription>(); // specify the references to follow and the fields to return. BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = nodeId; nodeToBrowse.ReferenceTypeId = ReferenceTypeIds.References; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.BrowseDirection = BrowseDirection.Both; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse); // start the browse operation. BrowseResultCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; ResponseHeader responseHeader = session.Browse( null, null, 2, nodesToBrowse, out results, out diagnosticInfos); // these do sanity checks on the result - make sure response matched the request. ClientBase.ValidateResponse(results, nodesToBrowse); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse); // check status. if (StatusCode.IsBad(results[0].StatusCode)) { // embed the diagnostic information in a exception. throw ServiceResultException.Create(results[0].StatusCode, 0, diagnosticInfos, responseHeader.StringTable); } // add first batch. references.AddRange(results[0].References); // check if server limited the results. while (results[0].ContinuationPoint != null && results[0].ContinuationPoint.Length > 0) { ByteStringCollection continuationPoints = new ByteStringCollection(); continuationPoints.Add(results[0].ContinuationPoint); // continue browse operation. responseHeader = session.BrowseNext( null, false, continuationPoints, out results, out diagnosticInfos); ClientBase.ValidateResponse(results, continuationPoints); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, continuationPoints); // check status. if (StatusCode.IsBad(results[0].StatusCode)) { // embed the diagnostic information in a exception. throw ServiceResultException.Create(results[0].StatusCode, 0, diagnosticInfos, responseHeader.StringTable); } // add next batch. references.AddRange(results[0].References); } return references; }
static void Browse(Session session) { DiagnosticInfoCollection diagnosticInfos; BrowseDescriptionCollection bc = new BrowseDescriptionCollection(); BrowseDescription bd = new BrowseDescription(); bd.BrowseDirection = BrowseDirection.Forward; NodeId nodeId = Opc.Ua.Objects.ObjectsFolder; do { Console.WriteLine("\n Enter nodeId to Browse (or q to exit)"); string s = Console.ReadLine(); if (s == "q") break; if (s.Length == 0) { nodeId = Opc.Ua.Objects.ObjectsFolder; } else nodeId = new NodeId(s); bc.Clear(); bd.NodeId = nodeId; bc.Add(bd); BrowseResultCollection results; ResponseHeader rh = session.Browse(null, null, 100, bc, out results, out diagnosticInfos); foreach ( BrowseResult res in results) { foreach (ReferenceDescription rdc in res.References) { Console.WriteLine(String.Format(" Node = {0} (namespace {1}) {2}", rdc.NodeId.ToString(),rdc.NodeId.NamespaceIndex, Environment.NewLine)); } } } while (true); }
/// <summary> /// Returns the next reference. /// </summary> /// <returns>The next reference that meets the browse criteria.</returns> public override IReference Next() { lock (DataLock) { IReference reference = null; // enumerate pre-defined references. // always call first to ensure any pushed-back references are returned first. reference = base.Next(); if (reference != null) { return reference; } // don't start browsing huge number of references when only internal references are requested. if (InternalOnly) { return null; } if (m_stage == Stage.Begin) { // construct request. BrowseDescription nodeToBrowse = new BrowseDescription(); NodeId startId = ObjectIds.ObjectsFolder; if (m_source != null) { startId = m_mapper.ToRemoteId(m_source.NodeId); } nodeToBrowse.NodeId = startId; nodeToBrowse.BrowseDirection = this.BrowseDirection; nodeToBrowse.ReferenceTypeId = this.ReferenceType; nodeToBrowse.IncludeSubtypes = this.IncludeSubtypes; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse); // start the browse operation. BrowseResultCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; ResponseHeader responseHeader = m_client.Browse( null, null, 0, nodesToBrowse, out results, out diagnosticInfos); // these do sanity checks on the result - make sure response matched the request. ClientBase.ValidateResponse(results, nodesToBrowse); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse); m_position = 0; m_references = null; m_continuationPoint = null; m_stage = Stage.References; // check status. if (StatusCode.IsGood(results[0].StatusCode)) { m_references = results[0].References; m_continuationPoint = results[0].ContinuationPoint; reference = NextChild(); if (reference != null) { return reference; } } } if (m_stage == Stage.References) { reference = NextChild(); if (reference != null) { return reference; } if (m_source == null && IsRequired(ReferenceTypes.HasNotifier, false)) { // construct request. BrowseDescription nodeToBrowse = new BrowseDescription(); nodeToBrowse.NodeId = ObjectIds.Server; nodeToBrowse.BrowseDirection = BrowseDirection.Forward; nodeToBrowse.ReferenceTypeId = ReferenceTypes.HasNotifier; nodeToBrowse.IncludeSubtypes = true; nodeToBrowse.NodeClassMask = 0; nodeToBrowse.ResultMask = (uint)BrowseResultMask.All; BrowseDescriptionCollection nodesToBrowse = new BrowseDescriptionCollection(); nodesToBrowse.Add(nodeToBrowse); // start the browse operation. BrowseResultCollection results = null; DiagnosticInfoCollection diagnosticInfos = null; ResponseHeader responseHeader = m_client.Browse( null, null, 0, nodesToBrowse, out results, out diagnosticInfos); // these do sanity checks on the result - make sure response matched the request. ClientBase.ValidateResponse(results, nodesToBrowse); ClientBase.ValidateDiagnosticInfos(diagnosticInfos, nodesToBrowse); m_position = 0; m_references = null; m_continuationPoint = null; m_stage = Stage.Notifiers; // check status. if (StatusCode.IsGood(results[0].StatusCode)) { m_references = results[0].References; m_continuationPoint = results[0].ContinuationPoint; } } m_stage = Stage.Notifiers; } if (m_stage == Stage.Notifiers) { reference = NextChild(); if (reference != null) { return reference; } m_stage = Stage.Done; } // all done. return null; } }