/// <summary> /// Loads the dictionary idetified by the node id. /// </summary> public async Task Load(ReferenceDescription dictionary) { if (dictionary == null) { throw new ArgumentNullException("dictionary"); } NodeId dictionaryId = ExpandedNodeId.ToNodeId(dictionary.NodeId, m_session.NamespaceUris); GetTypeSystem(dictionaryId); byte[] schema = ReadDictionary(dictionaryId); if (schema == null || schema.Length == 0) { throw ServiceResultException.Create(StatusCodes.BadUnexpectedError, "Cannot parse empty data dictionary."); } await Validate(schema); ReadDataTypes(dictionaryId); m_dictionaryId = dictionaryId; m_name = dictionary.ToString(); }
/// <summary> /// Displays the a root in the control. /// </summary> public void Initialize( Session session, NodeId rootId, NodeId viewId, NodeId referenceTypeId, BrowseDirection browseDirection) { m_session = session; m_rootId = rootId; m_viewId = viewId; m_referenceTypeId = referenceTypeId; m_browseDirection = browseDirection; NodesTV.Nodes.Clear(); if (m_session == null) { return; } if (NodeId.IsNull(m_rootId)) { m_rootId = Objects.RootFolder; } if (NodeId.IsNull(m_referenceTypeId)) { m_referenceTypeId = ReferenceTypeIds.HierarchicalReferences; } ReferenceTypeCTRL.Initialize(m_session, ReferenceTypeIds.HierarchicalReferences); ReferenceTypeCTRL.SelectedTypeId = m_referenceTypeId; ILocalNode root = m_session.NodeCache.Find(m_rootId) as ILocalNode; if (root == null) { return; } ReferenceDescription reference = new ReferenceDescription(); reference.ReferenceTypeId = referenceTypeId; reference.IsForward = true; reference.NodeId = root.NodeId; reference.NodeClass = root.NodeClass; reference.BrowseName = root.BrowseName; reference.DisplayName = root.DisplayName; reference.TypeDefinition = root.TypeDefinitionId; TreeNode rootNode = new TreeNode(reference.ToString()); rootNode.ImageKey = rootNode.SelectedImageKey = GuiUtils.GetTargetIcon(session, reference); rootNode.Tag = reference; rootNode.Nodes.Add(new TreeNode()); NodesTV.Nodes.Add(rootNode); }
private void EncodingCB_SelectedIndexChanged(object sender, EventArgs e) { try { DescriptionTB.Text = null; TypeNameTB.Text = null; DictionaryNameTB.Text = null; TypeSystemNameTB.Text = null; if (EncodingCB.SelectedIndex < 0 || EncodingCB.SelectedIndex > m_encodings.Count) { return; } // get the current encoding. ReferenceDescription encoding = m_encodings[EncodingCB.SelectedIndex]; // find the desctiption. ReferenceDescription description = m_session.FindDataDescription((NodeId)encoding.NodeId); if (description == null) { return; } TypeNameTB.Text = description.ToString(); // find the dictionary. DataDictionary dictionary = m_session.FindDataDictionary((NodeId)description.NodeId); if (dictionary == null) { return; } NodeId descriptionId = null; if (!ShowEntireDictionaryCHK.Checked) { descriptionId = (NodeId)description.NodeId; } DictionaryNameTB.Text = dictionary.Name; TypeSystemNameTB.Text = dictionary.TypeSystemName; DescriptionTB.Text = dictionary.GetSchema(descriptionId); Cursor = Cursors.WaitCursor; try { FormatDescription(); } finally { Cursor = Cursors.Default; } } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }
/// <summary> /// Loads the dictionary identified by the node id. /// </summary> public Task Load(ReferenceDescription dictionary) { if (dictionary == null) { throw new ArgumentNullException(nameof(dictionary)); } NodeId dictionaryId = ExpandedNodeId.ToNodeId(dictionary.NodeId, m_session.NamespaceUris); return(Load(dictionaryId, dictionary.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); } }
/// <inheritdoc/> public string GetDisplayText(ReferenceDescription reference) { if (reference == null || NodeId.IsNull(reference.NodeId)) { return(String.Empty); } INode node = Find(reference.NodeId); if (node != null) { return(GetDisplayText(node)); } return(reference.ToString()); }
/// <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) { MessageBox.Show(exception.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Displays the references in the control. /// </summary> private void DisplayReferences(Session session, List <ReferenceDescription> references) { ReferencesLV.Items.Clear(); for (int ii = 0; ii < references.Count; ii++) { ReferenceDescription reference = references[ii]; string referenceType = null; // look up the name for the reference IReferenceType referenceTypeNode = session.NodeCache.Find(reference.ReferenceTypeId) as IReferenceType; if (referenceTypeNode != null) { referenceType = referenceTypeNode.DisplayName.Text; if (!reference.IsForward && !LocalizedText.IsNullOrEmpty(referenceTypeNode.InverseName)) { referenceType = referenceTypeNode.InverseName.Text; } } // the node cache is used to store the type model so it can be accessed locally. string typeDefinition = session.NodeCache.GetDisplayText(reference.TypeDefinition); ListViewItem item = new ListViewItem(referenceType); // the ToString() operator on the ReferenceDescription returns the target name. item.SubItems.Add(reference.ToString()); item.SubItems.Add(reference.NodeClass.ToString()); item.SubItems.Add(typeDefinition); item.Tag = reference; ReferencesLV.Items.Add(item); } // auto size the columns. for (int ii = 0; ii < ReferencesLV.Columns.Count; ii++) { ReferencesLV.Columns[ii].Width = -2; } }
/// <summary> /// Adds the browse results to the node (if not null). /// </summary> private void UpdateNode(TreeNode parent, ReferenceDescriptionCollection references) { try { for (int ii = 0; ii < references.Count; ii++) { ReferenceDescription reference = references[ii]; TreeNode childNode = new TreeNode(reference.ToString()); childNode.ImageKey = childNode.SelectedImageKey = GuiUtils.GetTargetIcon(m_session, reference); childNode.Tag = reference; childNode.Nodes.Add(new TreeNode()); parent.Nodes.Add(childNode); } } catch (Exception exception) { GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception); } }
/// <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> /// Loads the dictionary idetified by the node id. /// </summary> public async Task Load(ReferenceDescription dictionary) { if (dictionary == null) throw new ArgumentNullException("dictionary"); NodeId dictionaryId = ExpandedNodeId.ToNodeId(dictionary.NodeId, m_session.NamespaceUris); GetTypeSystem(dictionaryId); byte[] schema = ReadDictionary(dictionaryId); if (schema == null || schema.Length == 0) { throw ServiceResultException.Create(StatusCodes.BadUnexpectedError, "Cannot parse empty data dictionary."); } await Validate(schema); ReadDataTypes(dictionaryId); m_dictionaryId = dictionaryId; m_name = dictionary.ToString(); }
/// <summary> /// Returns a display name for the target of a reference. /// </summary> public string GetDisplayText(ReferenceDescription reference) { if (reference == null || NodeId.IsNull(reference.NodeId)) { return String.Empty; } INode node = Find(reference.NodeId); if (node != null) { return GetDisplayText(node); } return reference.ToString(); }
private void BrowseCTRL_NodeSelected(object sender, EventArgs e) { try { // disable ok button if selection is not valid. OkBTN.IsEnabled = false; ReferenceDescription reference = sender as ReferenceDescription; if (reference == null) { return; } if (NodeId.IsNull(reference.NodeId)) { return; } // set the display name. DisplayNameTB.Text = reference.ToString(); NodeClassCB.SelectedItem = (NodeClass)reference.NodeClass; // set identifier type. IdentifierTypeCB.SelectedItem = reference.NodeId.IdType; // set namespace uri. if (!String.IsNullOrEmpty(reference.NodeId.NamespaceUri)) { NamespaceUriCB.SelectedIndex = -1; NamespaceUriCB.SelectedValue = reference.NodeId.NamespaceUri; } else { if (reference.NodeId.NamespaceIndex < NamespaceUriCB.Items.Count) { NamespaceUriCB.SelectedIndex = (int)reference.NodeId.NamespaceIndex; } else { NamespaceUriCB.SelectedIndex = -1; NamespaceUriCB.SelectedValue = String.Empty; } } // set identifier. switch (reference.NodeId.IdType) { case IdType.Opaque: { NodeIdentifierTB.Text = Convert.ToBase64String((byte[])reference.NodeId.Identifier); break; } default: { NodeIdentifierTB.Text = Utils.Format("{0}", reference.NodeId.Identifier); break; } } // selection valid - enable ok. OkBTN.IsEnabled = true; m_reference = reference; } catch (Exception exception) { GuiUtils.HandleException(String.Empty, GuiUtils.CallerName(), 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 = results[ii]; // display in list. AttributesLV.Items.Add(item); } // set the column widths. for (int ii = 0; ii < AttributesLV.Columns.Count; ii++) { AttributesLV.Columns[ii].Width = -2; } }