Exemple #1
0
        public static int GetOpcServerAllItem(OpcServer opcSer, out List <DriveNodeEntity> driveNodes)
        {
            int rtc = -1;

            driveNodes = null;
            BrowseTree browseTress = new BrowseTree(opcSer);

            rtc = browseTress.CreateTree();
            if (HRESULTS.Succeeded(rtc))
            {
                TreeNode[] root = browseTress.Root();
                if (root != null)
                {
                    driveNodes = new List <DriveNodeEntity>();
                    // driveNotes 固定两层 ,没有使用递归 转换
                    foreach (TreeNode item in root)
                    {
                        DriveNodeEntity driveNodeTemp = CloneDriveNode(item, DriveNodeModel.OPCGroup);
                        foreach (TreeNode itemChild in item.Nodes)
                        {
                            driveNodeTemp.childNode.Add(CloneDriveNode(itemChild, DriveNodeModel.OPCItem));
                        }
                        driveNodes.Add(driveNodeTemp);
                    }
                    return(0);
                }
            }
            return(rtc);
        }
        /// <summary>
        /// Call the Browse service of an UA server.
        /// </summary>
        /// <param name="parentNode">The node of the treeview to browse.</param>
        public int Browse(TreeNode parentNode)
        {
            NodeId             nodeToBrowse     = null;
            TreeNodeCollection parentCollection = null;
            int ret = 0;

            // Check if we browse from root
            if (parentNode == null)
            {
                nodeToBrowse = new NodeId(Objects.RootFolder, 0);
                // Get all the tree nodes that are assigned to the control
                parentCollection = tvBrowseTree.Nodes;
            }
            else
            {
                // Get the data about the parent tree node
                ReferenceDescription parentRefDescription = (ReferenceDescription)parentNode.Tag;

                if (parentRefDescription == null)
                {
                    return(-1);
                }

                // XXX ToDo
                // set nodeid
                nodeToBrowse     = ExpandedNodeId.ToNodeId(parentRefDescription.NodeId, Session.NamespaceUris);
                parentCollection = parentNode.Nodes;
            }

            bool bBrowse;

            // Set wait cursor.
            Cursor.Current = Cursors.WaitCursor;

            // Check if we want to browse on the server.
            if (m_RebrowseOnExpandNode)
            {
                bBrowse = true;
            }
            else if (parentNode == null)
            {
                bBrowse = true;
            }
            else if (parentNode.Checked)
            {
                bBrowse = false;
            }
            else
            {
                bBrowse = true;
            }

            // Delete children if required.
            if (bBrowse)
            {
                if (parentNode == null)
                {
                    BrowseTree.BeginUpdate();
                    BrowseTree.Nodes.Clear();
                    BrowseTree.EndUpdate();
                }
                else
                {
                    BrowseTree.BeginUpdate();
                    parentNode.Nodes.Clear();
                    BrowseTree.EndUpdate();
                }

                byte[] continuationPoint;
                List <ReferenceDescription> results;

                BrowseContext browseContext = new BrowseContext()
                {
                    BrowseDirection       = BrowseDirection.Forward,
                    ReferenceTypeId       = UnifiedAutomation.UaBase.ReferenceTypeIds.HierarchicalReferences,
                    IncludeSubtypes       = true,
                    NodeClassMask         = 0,
                    ResultMask            = (uint)BrowseResultMask.All,
                    MaxReferencesToReturn = 100
                };

                try
                {
                    // Call browse service.
                    results = m_Session.Browse(
                        nodeToBrowse,
                        browseContext,
                        out continuationPoint);

                    // Add children.
                    tvBrowseTree.BeginUpdate();

                    // Mark parent node as browsed.
                    if (parentNode != null)
                    {
                        parentNode.Checked = true;
                    }

                    AddResultsToTree(parentCollection, results);

                    while (continuationPoint != null)
                    {
                        results = m_Session.BrowseNext(ref continuationPoint);
                        AddResultsToTree(parentCollection, results);
                    }

                    // Sort the tree nodes of the particular node collection
                    this.SortTreeNode((parentNode == null) ? tvBrowseTree.Nodes : parentNode.Nodes);
                    tvBrowseTree.EndUpdate();

                    // Update status label.
                    OnUpdateStatusLabel("Browse succeeded.", true);
                }
                catch (Exception e)
                {
                    ret = -1;
                    // Update status label.
                    OnUpdateStatusLabel("An exception occured while browsing: " + e.Message, false);
                }
            }
            // Restore default cursor.
            Cursor.Current = Cursors.Default;
            return(ret);
        }