Exemplo n.º 1
0
        private async Task BrowseEntireTree(BrowseNode parent, List <BrowseNode> result, HashSet <BrowseNode> set)
        {
            var children = await BrowseTree(parent.ID);

            if (children == null || connection == null)
            {
                return;
            }
            foreach (ReferenceDescription item in children)
            {
                if (item.NodeId == null)
                {
                    continue;
                }
                NodeId id = ExpandedNodeId.ToNodeId(item.NodeId, connection.NamespaceUris);
                if (item.NodeClass == NodeClass.Object && id.NamespaceIndex != 0 && item.BrowseName != null)
                {
                    var nodeObject = new BrowseNode(id, item.BrowseName, parent);
                    await BrowseEntireTree(nodeObject, result, set);
                }
                else if (item.NodeClass == NodeClass.Variable && id.NamespaceIndex != 0 && item.BrowseName != null)
                {
                    var nodeVariable = new BrowseNode(id, item.BrowseName, parent);
                    if (!set.Contains(nodeVariable))
                    {
                        result.Add(nodeVariable);
                        set.Add(nodeVariable);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public override async Task <BrowseDataItemsResult> BrowseDataItems()
        {
            string clientCertificate = GetCertificate(certificateLocation);

            if (connection == null)
            {
                string endpoint = config?.Address ?? "";
                string msg      = $"No connection to OPC UA server '{endpoint}': " + lastConnectErrMsg;
                return(new BrowseDataItemsResult(
                           supportsBrowsing: true,
                           browsingError: msg,
                           items: new DataItemBrowseInfo[0],
                           clientCertificate: clientCertificate));
            }

            NodeId     objectsID = ExpandedNodeId.ToNodeId(ExpandedNodeId.Parse(ObjectIds.ObjectsFolder), connection.NamespaceUris);
            BrowseNode objects   = new BrowseNode(objectsID, new QualifiedName("Objects"));

            var result = new List <BrowseNode>();
            var set    = new HashSet <BrowseNode>();

            await BrowseEntireTree(objects, result, set);

            var items = result.Select(MakeDataItemBrowseInfo).ToArray();

            return(new BrowseDataItemsResult(
                       supportsBrowsing: true,
                       browsingError: "",
                       items: items,
                       clientCertificate: clientCertificate));
        }
Exemplo n.º 3
0
        private static DataItemBrowseInfo MakeDataItemBrowseInfo(BrowseNode n)
        {
            var list = new List <BrowseNode>();

            BrowseNode.BuildPath(n, list, includeRoot: false);
            string[] path = list.Select(node => node.BrowseName.Name ?? node.BrowseName.ToString()).ToArray();

            return(new DataItemBrowseInfo(n.ID.ToString(), path));
        }
Exemplo n.º 4
0
        public override async Task <string[]> BrowseDataItemAddress(string?idOrNull)
        {
            if (connection == null)
            {
                return(new string[0]);
            }

            if (cachedBrowseResult != null && cacheTime.HasValue && Timestamp.Now - cacheTime.Value < Duration.FromMinutes(CacheTimeMinutes))
            {
                PrintLine("Returning cached browse result.");
                return(cachedBrowseResult);
            }

            cachedBrowseResult = null;
            cacheTime          = null;

            var result = new List <BrowseNode>();

            NodeId objectsID = ExpandedNodeId.ToNodeId(ExpandedNodeId.Parse(ObjectIds.ObjectsFolder), connection.NamespaceUris);
            var    objects   = new BrowseNode(objectsID, new QualifiedName("Objects"));

            //NodeId viewsID = ExpandedNodeId.ToNodeId(ExpandedNodeId.Parse(ObjectIds.ViewsFolder), connection.NamespaceUris);
            //var views = new BrowseNode(viewsID, new QualifiedName("Views"));
            var set = new HashSet <BrowseNode>();
            var sw  = System.Diagnostics.Stopwatch.StartNew();

            await BrowseEntireTree(objects, result, set);

            sw.Stop();

            //await BrowseEntireTree(views, result);

            string[] ids = new string[result.Count];
            for (int i = 0; i < result.Count; ++i)
            {
                ids[i] = result[i].ToString();
            }

            if (ids.Length > 0)
            {
                Task _ = File.WriteAllLinesAsync("Browse_OPC_UA.txt", ids);
            }

            if (sw.Elapsed > TimeSpan.FromSeconds(5))
            {
                PrintLine($"Caching browse result for {CacheTimeMinutes} minutes.");
                cachedBrowseResult = ids;
                cacheTime          = Timestamp.Now;
            }

            return(ids);
        }
Exemplo n.º 5
0
 private static void PrintPath(BrowseNode node, StringBuilder sb)
 {
     if (node.Parent == null)
     {
         sb.Append(node.BrowseName.Name);
     }
     else
     {
         PrintPath(node.Parent, sb);
         sb.Append('/');
         sb.Append(node.BrowseName.ToString());
     }
 }
Exemplo n.º 6
0
        public override async Task <string[]> BrowseDataItemAddress(string idOrNull)
        {
            if (connection == null)
            {
                return(new string[0]);
            }

            var result = new List <BrowseNode>();

            NodeId objectsID = ExpandedNodeId.ToNodeId(ExpandedNodeId.Parse(ObjectIds.ObjectsFolder), connection.NamespaceUris);
            var    objects   = new BrowseNode(objectsID, new QualifiedName("Objects"));

            NodeId viewsID = ExpandedNodeId.ToNodeId(ExpandedNodeId.Parse(ObjectIds.ViewsFolder), connection.NamespaceUris);
            var    views   = new BrowseNode(viewsID, new QualifiedName("Views"));

            await BrowseEntireTree(objects, result);
            await BrowseEntireTree(views, result);

            return(result.Select(n => n.ToString()).ToArray());
        }
Exemplo n.º 7
0
        private async Task BrowseEntireTree(BrowseNode parent, List <BrowseNode> result)
        {
            var children = await BrowseTree(parent.ID);

            foreach (ReferenceDescription item in children)
            {
                NodeId id = ExpandedNodeId.ToNodeId(item.NodeId, connection.NamespaceUris);
                if (item.NodeClass == NodeClass.Object && id.NamespaceIndex != 0)
                {
                    var nodeObject = new BrowseNode(id, item.BrowseName, parent);
                    await BrowseEntireTree(nodeObject, result);
                }
                else if (item.NodeClass == NodeClass.Variable && id.NamespaceIndex != 0)
                {
                    var nodeVariable = new BrowseNode(id, item.BrowseName, parent);
                    if (result.All(n => n.ID != id))
                    {
                        result.Add(nodeVariable);
                    }
                }
            }
        }
Exemplo n.º 8
0
 public BrowseNode(NodeId id, QualifiedName browseName, BrowseNode parent = null)
 {
     ID         = id;
     BrowseName = browseName;
     Parent     = parent;
 }