private AeBrowseElement Find(Session session, string itemId, AeBrowseElement root, Stack<string> names, bool isArea)
{
string browseText = null;
BrowsePath browsePath = new BrowsePath();
browsePath.StartingNode = root.NodeId;
while (names.Count > 0)
{
RelativePathElement path = new RelativePathElement();
path.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HasNotifier;
path.IsInverse = false;
path.IncludeSubtypes = true;
// final hop can be HasEventSource for sources.
if (!isArea && names.Count == 1)
{
path.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HasEventSource;
}
browseText = names.Pop();
path.TargetName = m_mapper.GetRemoteBrowseName(browseText);
browsePath.RelativePath.Elements.Add(path);
}
BrowsePathCollection browsePaths = new BrowsePathCollection();
browsePaths.Add(browsePath);
// make the call to the server.
BrowsePathResultCollection results;
DiagnosticInfoCollection diagnosticInfos;
ResponseHeader responseHeader = session.TranslateBrowsePathsToNodeIds(
null,
browsePaths,
out results,
out diagnosticInfos);
// ensure that the server returned valid results.
Session.ValidateResponse(results, browsePaths);
Session.ValidateDiagnosticInfos(diagnosticInfos, browsePaths);
// check if the start node actually exists.
if (StatusCode.IsBad(results[0].StatusCode))
{
return null;
}
// must be exact one target.
if (results[0].Targets.Count != 1)
{
return null;
}
// can't be an external reference.
BrowsePathTarget target = results[0].Targets[0];
if (target.RemainingPathIndex != UInt32.MaxValue)
{
return null;
}
// need to check if at the end of the tree.
BrowseDescription nodeToBrowse = new BrowseDescription();
nodeToBrowse.NodeId = (NodeId)target.TargetId;
nodeToBrowse.ReferenceTypeId = Opc.Ua.ReferenceTypeIds.HasEventSource;
nodeToBrowse.BrowseDirection = BrowseDirection.Forward;
nodeToBrowse.IncludeSubtypes = true;
ReferenceDescriptionCollection children = ComAeUtils.Browse(session, nodeToBrowse, false);
if (!isArea)
{
if (children != null && children.Count > 0)
{
return null;
}
}
else
{
if (children == null || children.Count == 0)
{
return null;
}
}
// construct the element.
AeBrowseElement element = new AeBrowseElement();
element.NodeId = (NodeId)target.TargetId;
element.ItemId = itemId;
element.BrowseText = browseText;
element.IsArea = isArea;
return element;
}
#endregion