Exemplo n.º 1
0
        //此方法通过OPCBrowser将数据结节加载到TreeView中.
        private bool LoadDataToTree(TreeNode node, string nodeName)
        {
            TreeNode childNode = null;
            OPCGroup group     = null;

            try
            {
                this.opcBrowser.ShowBranches();
                int count = this.opcBrowser.Count;
                if (count == 0)
                {
                    this.opcBrowser.ShowLeafs(true);
                    group              = this.kepServer.OPCGroups.Add(nodeName);
                    group.UpdateRate   = 1000;
                    group.IsSubscribed = true;
                    group.IsActive     = true;
                    node.Tag           = group;
                    this.groupMap.Add(nodeName, group);
                }
                HashSet <string> itemList = new HashSet <string>();
                foreach (object turn in this.opcBrowser)
                {
                    string path = nodeName;
                    string name = turn.ToString();

                    if (count != 0)
                    {
                        childNode = node.Nodes.Add(name);
                        if (path != "")
                        {
                            path += ".";
                        }
                        path += name;
                        this.opcBrowser.MoveDown(name);
                        LoadDataToTree(childNode, path);
                        this.opcBrowser.MoveUp();
                    }
                    else
                    {
                        OPCItem opcItem = group.OPCItems.AddItem(name, 0);
                        itemList.Add(name);
                        if (!this.itemMap.ContainsKey(opcItem.GetHashCode()))
                        {
                            this.itemMap.Add(opcItem.GetHashCode(), opcItem);
                        }
                    }
                }

                this.opcBrowser.ShowLeafs(false);
                List <string> leafItems = new List <string>();
                foreach (object t in this.opcBrowser)
                {
                    string itemId = this.opcBrowser.GetItemID(t.ToString());
                    if (itemList.Contains(itemId))
                    {
                        continue;
                    }
                    leafItems.Add(itemId);
                }

                if (leafItems.Count != 0)
                {
                    group              = this.kepServer.OPCGroups.Add(nodeName);
                    group.UpdateRate   = 1000;
                    group.IsSubscribed = true;
                    group.IsActive     = true;
                    this.groupMap.Add(nodeName, group);
                    node.Tag = group;
                    foreach (string t in leafItems)
                    {
                        OPCItem opcItem = group.OPCItems.AddItem(t, 0);
                        //childNode = node.Nodes.Add(t.ToString());
                        if (!this.itemMap.ContainsKey(opcItem.GetHashCode()))
                        {
                            this.itemMap.Add(opcItem.GetHashCode(), opcItem);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
            return(true);
        }