Пример #1
0
        /// <summary>
        /// Creates an enumerator for the current browse position.
        /// </summary>
        /// <param name="sources">if set to <c>true</c> then sources are enumerated.</param>
        /// <returns>The wrapped enumerator.</returns>
        private EnumString CreateEnumerator(bool sources)
        {
            IEnumString unknown = null;

            string methodName = "IOPCEventAreaBrowser.BrowseOPCAreas";

            try
            {
                IOPCEventAreaBrowser server = BeginComCall <IOPCEventAreaBrowser>(methodName, true);

                OPCAEBROWSETYPE browseType = OPCAEBROWSETYPE.OPC_AREA;

                if (sources)
                {
                    browseType = OPCAEBROWSETYPE.OPC_SOURCE;
                }

                server.BrowseOPCAreas(browseType, String.Empty, out unknown);
            }
            catch (Exception e)
            {
                ComCallError(methodName, e);
                return(null);
            }
            finally
            {
                EndComCall(methodName);
            }

            // wrapper the enumrator. hardcoding a buffer size of 256.
            return(new EnumString(unknown, 256));
        }
Пример #2
0
        private bool IsValidNode(NodeId nodeId, string nodeName, OPCAEBROWSETYPE dwBrowseFilterType)
        {
            Browser browse = new Browser(m_session);
            ReferenceDescriptionCollection references = null;

            try
            {
                // For a node to be an area, it has to have the 'HasNotifier' reference and must
                // allow event notification (the EventNotifier attribute is set to SubscribeToEvents)
                if (dwBrowseFilterType == OPCAEBROWSETYPE.OPC_AREA)
                {
                    // if node is 'Server' then the HasNotifier is implicit, so return true
                    if (nodeName == "Server")
                    {
                        return(true);
                    }

                    references = browse.Browse(nodeId);

                    foreach (ReferenceDescription reference in references)
                    {
                        if ((reference.ReferenceTypeId == ReferenceTypes.HasNotifier) || (reference.ReferenceTypeId == ReferenceTypes.HasEventSource))
                        {
                            return(IsEventNotificationListAllowed(nodeId));
                        }
                    }
                }

                // For a node to be a source, it should be the target of the HasEventSource reference
                else   // Check if this is a source.
                {
                    IList <INode> parent = m_session.NodeCache.Find(nodeId, ReferenceTypes.HasEventSource, true, false);
                    if (parent.Count != 0)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error in IsValidNode");
            }

            return(false);
        }
Пример #3
0
 private object CreateEnumerator(BrowseType browseType, string browseFilter)
 {
     OPCAEBROWSETYPE dwBrowseFilterType = OpcCom.Ae.Interop.GetBrowseType(browseType);
     IEnumString ppIEnumString = null;
     try
     {
         ((IOPCEventAreaBrowser) this.m_browser).BrowseOPCAreas(dwBrowseFilterType, (browseFilter != null) ? browseFilter : "", out ppIEnumString);
     }
     catch (Exception exception)
     {
         throw OpcCom.Interop.CreateException("IOPCEventAreaBrowser.BrowseOPCAreas", exception);
     }
     if (ppIEnumString == null)
     {
         throw new InvalidResponseException("enumerator == null");
     }
     return ppIEnumString;
 }
Пример #4
0
        private bool IsValidNode(NodeId nodeId, string nodeName, OPCAEBROWSETYPE dwBrowseFilterType)
        {
            Browser browse = new Browser(m_session);
            ReferenceDescriptionCollection references = null;

            try
            {
                // For a node to be an area, it has to have the 'HasNotifier' reference and must
                // allow event notification (the EventNotifier attribute is set to SubscribeToEvents)
                if (dwBrowseFilterType == OPCAEBROWSETYPE.OPC_AREA)
                {
                    // if node is 'Server' then the HasNotifier is implicit, so return true
                    if (nodeName == "Server")
                    {
                        return true;
                    }

                    references = browse.Browse(nodeId);
                    
                    foreach (ReferenceDescription reference in references)
                    {
                        if ((reference.ReferenceTypeId == ReferenceTypes.HasNotifier) || (reference.ReferenceTypeId == ReferenceTypes.HasEventSource))
                        {
                            return IsEventNotificationListAllowed(nodeId);
                        }
                    }
                }

                // For a node to be a source, it should be the target of the HasEventSource reference
                else   // Check if this is a source. 
                {
                    IList<INode> parent = m_session.NodeCache.Find(nodeId, ReferenceTypes.HasEventSource, true, false);
                    if (parent.Count != 0)
                        return true;
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error in IsValidNode");
            }
             
            return false;
        }
Пример #5
0
        /// <summary>
        /// Return an IEnumString for a list of Areas as determined by the passed parameters.
        /// </summary>
        /// <param name="dwBrowseFilterType">
        /// OPC_AREA - returns only areas.
        /// OPC_SOURCE - returns only sources.</param>
        /// <param name="szFilterCriteria">A server specific filter string. A pointer to a NULL string indicates no filtering.</param>
        /// <param name="ppIEnumString">Where to save the returned interface pointer. NULL if the HRESULT is other than S_OK or S_FALSE.</param>
        public void BrowseOPCAreas(
            OPCAEBROWSETYPE             dwBrowseFilterType, 
            string                      szFilterCriteria, 
            out OpcRcw.Comn.IEnumString ppIEnumString)
        {
            ppIEnumString = null;
            if (dwBrowseFilterType != OPCAEBROWSETYPE.OPC_AREA && dwBrowseFilterType != OPCAEBROWSETYPE.OPC_SOURCE)
            {
                throw ComUtils.CreateComException("BrowseOPCAreas", ResultIds.E_INVALIDARG);
            }
            try
            {
                lock (m_lock)
                {
                    // find the current node.
                    INode parent = null;

                    // ensure browse stack has been initialized.
                    if (m_browseStack.Count == 0)
                    {
                        parent = m_session.NodeCache.Find(Objects.Server);
                        m_browseStack.Push(parent);
                    }

                    parent = m_browseStack.Peek();

                    if (parent == null)
                    {
                        throw ComUtils.CreateComException("BrowseOPCAreas",ResultIds.E_FAIL);
                    }

                    List<string> names = new List<string>();
                    IList<INode> children;

                    ////// find children.
                       children = m_session.NodeCache.Find(parent.NodeId, ReferenceTypes.HasEventSource, false, true); // This would also include 
                                                                                                                        // the HasNotifier reference which is 
                                                                                                                        // a subtype of HasEventSource 

                    foreach (INode child in children)
                    {
                        // ignore external nodes.
                        if (child.NodeId.IsAbsolute)
                        {
                            continue;
                        }

                        // ignore non-objects/variables.
                        if ((child.NodeClass & (NodeClass.Object | NodeClass.Variable)) == 0)
                        {
                            continue;
                        }

                        // ignore duplicate browse names.
                        if (names.Contains(child.BrowseName.ToString()))
                        {
                            continue;
                        }

                        // For a node to be an area, it has to have the 'HasNotifier' reference and must
                        // allow event notification (the EventNotifier attribute is set to SubscribeToEvents) 
                        // For a node to be a source, it should be the target of the HasEventSource reference
                        if (IsValidNode((NodeId)child.NodeId, child.BrowseName.ToString(), dwBrowseFilterType))
                        {
                            if (String.IsNullOrEmpty(szFilterCriteria))
                            {
                                names.Add(child.BrowseName.ToString());
                            }
                            else
                            {
                                // apply filters
                                if (ComUtils.Match(child.BrowseName.ToString(), szFilterCriteria, true))
                                {
                                   names.Add(child.BrowseName.ToString());
                                }
                            }
                        }
                     }

                    // create enumerator.
                    ppIEnumString = (OpcRcw.Comn.IEnumString)new EnumString(names);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error in BrowseOPCAreas");
                throw ComUtils.CreateComException(e);
            }
        }
Пример #6
0
        /// <summary>
        /// Return an IEnumString for a list of Areas as determined by the passed parameters.
        /// </summary>
        /// <param name="dwBrowseFilterType">
        /// OPC_AREA - returns only areas.
        /// OPC_SOURCE - returns only sources.</param>
        /// <param name="szFilterCriteria">A server specific filter string. A pointer to a NULL string indicates no filtering.</param>
        /// <param name="ppIEnumString">Where to save the returned interface pointer. NULL if the HRESULT is other than S_OK or S_FALSE.</param>
        public void BrowseOPCAreas(
            OPCAEBROWSETYPE dwBrowseFilterType,
            string szFilterCriteria,
            out OpcRcw.Comn.IEnumString ppIEnumString)
        {
            ppIEnumString = null;
            if (dwBrowseFilterType != OPCAEBROWSETYPE.OPC_AREA && dwBrowseFilterType != OPCAEBROWSETYPE.OPC_SOURCE)
            {
                throw ComUtils.CreateComException("BrowseOPCAreas", ResultIds.E_INVALIDARG);
            }
            try
            {
                lock (m_lock)
                {
                    // find the current node.
                    INode parent = null;

                    // ensure browse stack has been initialized.
                    if (m_browseStack.Count == 0)
                    {
                        parent = m_session.NodeCache.Find(Objects.Server);
                        m_browseStack.Push(parent);
                    }

                    parent = m_browseStack.Peek();

                    if (parent == null)
                    {
                        throw ComUtils.CreateComException("BrowseOPCAreas", ResultIds.E_FAIL);
                    }

                    List <string> names = new List <string>();
                    IList <INode> children;

                    ////// find children.
                    children = m_session.NodeCache.Find(parent.NodeId, ReferenceTypes.HasEventSource, false, true);    // This would also include
                                                                                                                       // the HasNotifier reference which is
                                                                                                                       // a subtype of HasEventSource

                    foreach (INode child in children)
                    {
                        // ignore external nodes.
                        if (child.NodeId.IsAbsolute)
                        {
                            continue;
                        }

                        // ignore non-objects/variables.
                        if ((child.NodeClass & (NodeClass.Object | NodeClass.Variable)) == 0)
                        {
                            continue;
                        }

                        // ignore duplicate browse names.
                        if (names.Contains(child.BrowseName.ToString()))
                        {
                            continue;
                        }

                        // For a node to be an area, it has to have the 'HasNotifier' reference and must
                        // allow event notification (the EventNotifier attribute is set to SubscribeToEvents)
                        // For a node to be a source, it should be the target of the HasEventSource reference
                        if (IsValidNode((NodeId)child.NodeId, child.BrowseName.ToString(), dwBrowseFilterType))
                        {
                            if (String.IsNullOrEmpty(szFilterCriteria))
                            {
                                names.Add(child.BrowseName.ToString());
                            }
                            else
                            {
                                // apply filters
                                if (ComUtils.Match(child.BrowseName.ToString(), szFilterCriteria, true))
                                {
                                    names.Add(child.BrowseName.ToString());
                                }
                            }
                        }
                    }

                    // create enumerator.
                    ppIEnumString = (OpcRcw.Comn.IEnumString) new EnumString(names);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error in BrowseOPCAreas");
                throw ComUtils.CreateComException(e);
            }
        }