Ejemplo n.º 1
0
        private bool BuildBrowseStack(ItemIdentifier itemID, ItemIdentifier targetID)
        {
            BrowseFilters filters = new BrowseFilters {
                MaxElementsReturned  = 0,
                BrowseFilter         = browseFilter.all,
                ElementNameFilter    = null,
                VendorFilter         = null,
                ReturnAllProperties  = false,
                PropertyIDs          = null,
                ReturnPropertyValues = false
            };

            BrowseElement[]       elementArray = null;
            Opc.Da.BrowsePosition position     = null;
            try
            {
                elementArray = this.m_server.Browse(itemID, filters, out position);
            }
            catch (Exception)
            {
                this.m_browseStack.Clear();
                return(false);
            }
            if (position != null)
            {
                position.Dispose();
                position = null;
            }
            if ((elementArray == null) || (elementArray.Length == 0))
            {
                this.m_browseStack.Clear();
                return(false);
            }
            foreach (BrowseElement element in elementArray)
            {
                if (element.ItemName == targetID.ItemName)
                {
                    return(true);
                }
                if (targetID.ItemName.StartsWith(element.ItemName))
                {
                    ItemIdentifier identifier = new ItemIdentifier(targetID.ItemName);
                    this.m_browseStack.Push(identifier);
                    return(this.BuildBrowseStack(identifier, targetID));
                }
            }
            return(false);
        }
Ejemplo n.º 2
0
        private BrowseElement FindChild(string name)
        {
            ItemIdentifier itemID = null;

            if (this.m_browseStack.Count > 0)
            {
                itemID = (ItemIdentifier)this.m_browseStack.Peek();
            }
            BrowseElement[] elementArray = null;
            try
            {
                BrowseFilters filters = new BrowseFilters {
                    MaxElementsReturned  = 0,
                    BrowseFilter         = browseFilter.all,
                    ElementNameFilter    = name,
                    VendorFilter         = null,
                    ReturnAllProperties  = false,
                    PropertyIDs          = null,
                    ReturnPropertyValues = false
                };
                Opc.Da.BrowsePosition position = null;
                elementArray = this.m_server.Browse(itemID, filters, out position);
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }
            }
            catch (Exception)
            {
                return(null);
            }
            if ((elementArray != null) && (elementArray.Length > 0))
            {
                return(elementArray[0]);
            }
            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns the set of elements in the address space that meet the specified criteria.
        /// </summary>
        public Opc.Da.BrowseElement[] Browse(
            ItemIdentifier itemID,
            BrowseFilters filters,
            ref Opc.Da.BrowsePosition position)
        {
            lock (this)
            {
                if (m_disposed)
                {
                    throw new ObjectDisposedException("Opc.Da.Cache");
                }

                BrowseElement element = m_addressSpace;

                // find desired element.
                string browsePath = (itemID != null) ? itemID.ItemName : null;

                if (browsePath != null && browsePath.Length > 0)
                {
                    element = m_addressSpace.Find(browsePath);

                    if (element == null)
                    {
                        // check if browsing a property item.
                        PropertyID property = Property.VALUE;

                        string rootID = LookupProperty(browsePath, out property);

                        if (rootID != null)
                        {
                            element = m_addressSpace.Find(rootID);
                        }

                        if (element == null)
                        {
                            throw new ResultIDException(ResultID.Da.E_UNKNOWN_ITEM_NAME);
                        }

                        // property items never contain children.
                        return(new Opc.Da.BrowseElement[0]);
                    }
                }

                // check if no elements exist.
                if (element.Count == 0)
                {
                    return(new Opc.Da.BrowseElement[0]);
                }

                // determine start position.
                int start = 0;

                if (position != null)
                {
                    start = ((BrowsePosition)position).Index;
                    position.Dispose();
                    position = null;
                }

                // process child nodes.
                ArrayList results = new ArrayList();

                for (int ii = start; ii < element.Count; ii++)
                {
                    BrowseElement child = element.Child(ii);

                    // exclude elements without children.
                    if (filters.BrowseFilter == browseFilter.branch && child.Count == 0)
                    {
                        continue;
                    }

                    // check if an element is an item.
                    CacheItem item = (CacheItem)m_items[child.ItemID];

                    // exclude elements which are not items.
                    if (filters.BrowseFilter == browseFilter.item && item == null)
                    {
                        continue;
                    }

                    // apply name filter (using the SQL LIKE operator).
                    if (filters.ElementNameFilter != null && filters.ElementNameFilter.Length > 0)
                    {
                        if (!Opc.Convert.Match(child.Name, filters.ElementNameFilter, true))
                        {
                            continue;
                        }
                    }

                    // add element to list of results.
                    Opc.Da.BrowseElement result = new Opc.Da.BrowseElement();

                    result.Name        = child.Name;
                    result.ItemName    = child.ItemID;
                    result.ItemPath    = null;
                    result.IsItem      = item != null;
                    result.HasChildren = child.Count > 0;
                    result.Properties  = null;

                    // add properties to results.
                    if (filters.ReturnAllProperties || filters.PropertyIDs != null)
                    {
                        result.Properties = GetProperties(item, (filters.ReturnAllProperties) ? null : filters.PropertyIDs, filters.ReturnPropertyValues);
                    }

                    results.Add(result);

                    // check if max elements exceeded.
                    if (filters.MaxElementsReturned > 0 && results.Count >= filters.MaxElementsReturned)
                    {
                        if (ii + 1 < element.Count)
                        {
                            position = new BrowsePosition(itemID, filters);
                            ((BrowsePosition)position).Index = ii + 1;
                        }

                        break;
                    }
                }
                // return results.
                return((Opc.Da.BrowseElement[])results.ToArray(typeof(Opc.Da.BrowseElement)));
            }
        }
Ejemplo n.º 4
0
        private void Browse(ItemIdentifier itemID, OPCBROWSETYPE dwBrowseFilterType, string szFilterCriteria, short vtDataTypeFilter, int dwAccessRightsFilter, ArrayList hits)
        {
            BrowseFilters filters = new BrowseFilters {
                MaxElementsReturned = 0,
                BrowseFilter        = browseFilter.all,
                ElementNameFilter   = (dwBrowseFilterType != OPCBROWSETYPE.OPC_FLAT) ? szFilterCriteria : "",
                VendorFilter        = null,
                ReturnAllProperties = false
            };

            filters.PropertyIDs          = new PropertyID[] { Property.DATATYPE, Property.ACCESSRIGHTS };
            filters.ReturnPropertyValues = true;
            BrowseElement[] elementArray = null;
            try
            {
                Opc.Da.BrowsePosition position = null;
                elementArray = this.m_server.Browse(itemID, filters, out position);
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }
            }
            catch
            {
                throw new ExternalException("BrowseOPCItemIDs", -2147467259);
            }
            foreach (BrowseElement element in elementArray)
            {
                if (dwBrowseFilterType == OPCBROWSETYPE.OPC_FLAT)
                {
                    if (element.HasChildren)
                    {
                        this.Browse(new ItemIdentifier(element.ItemName), dwBrowseFilterType, szFilterCriteria, vtDataTypeFilter, dwAccessRightsFilter, hits);
                    }
                }
                else
                {
                    if (dwBrowseFilterType == OPCBROWSETYPE.OPC_BRANCH)
                    {
                        if (element.HasChildren)
                        {
                            goto Label_00F9;
                        }
                        continue;
                    }
                    if ((dwBrowseFilterType == OPCBROWSETYPE.OPC_LEAF) && element.HasChildren)
                    {
                        continue;
                    }
                }
Label_00F9:
                if (element.IsItem)
                {
                    if (vtDataTypeFilter != 0)
                    {
                        short type = (short)OpcCom.Interop.GetType((System.Type)element.Properties[0].Value);
                        if (type != vtDataTypeFilter)
                        {
                            continue;
                        }
                    }
                    if (dwAccessRightsFilter != 0)
                    {
                        accessRights rights = (accessRights)element.Properties[1].Value;
                        if (((dwAccessRightsFilter == 1) && (rights == accessRights.writable)) || ((dwAccessRightsFilter == 2) && (rights == accessRights.readable)))
                        {
                            continue;
                        }
                    }
                }
                if (dwBrowseFilterType != OPCBROWSETYPE.OPC_FLAT)
                {
                    hits.Add(element.Name);
                }
                else if (element.IsItem && ((szFilterCriteria.Length == 0) || Opc.Convert.Match(element.ItemName, szFilterCriteria, true)))
                {
                    hits.Add(element.ItemName);
                }
            }
        }
Ejemplo n.º 5
0
        public void ChangeBrowsePosition(OPCBROWSEDIRECTION dwBrowseDirection, string szString)
        {
            OpcCom.Da.Wrapper.Server server;
            Monitor.Enter(server = this);
            try
            {
                BrowseElement element;
                BrowseFilters filters = new BrowseFilters {
                    MaxElementsReturned  = 0,
                    BrowseFilter         = browseFilter.all,
                    ElementNameFilter    = null,
                    VendorFilter         = null,
                    ReturnAllProperties  = false,
                    PropertyIDs          = null,
                    ReturnPropertyValues = false
                };
                ItemIdentifier        itemID   = null;
                Opc.Da.BrowsePosition position = null;
                switch (dwBrowseDirection)
                {
                case OPCBROWSEDIRECTION.OPC_BROWSE_UP:
                    if (this.m_browseStack.Count == 0)
                    {
                        throw CreateException(-2147467259);
                    }
                    goto Label_0146;

                case OPCBROWSEDIRECTION.OPC_BROWSE_DOWN:
                    if ((szString == null) || (szString.Length == 0))
                    {
                        throw CreateException(-2147024809);
                    }
                    goto Label_00F4;

                case OPCBROWSEDIRECTION.OPC_BROWSE_TO:
                    if ((szString != null) && (szString.Length != 0))
                    {
                        break;
                    }
                    this.m_browseStack.Clear();
                    goto Label_0179;

                default:
                    goto Label_0179;
                }
                itemID = new ItemIdentifier(szString);
                BrowseElement[] elementArray = null;
                try
                {
                    elementArray = this.m_server.Browse(itemID, filters, out position);
                }
                catch (Exception)
                {
                    throw CreateException(-2147024809);
                }
                if ((elementArray == null) || (elementArray.Length == 0))
                {
                    throw CreateException(-2147024809);
                }
                this.m_browseStack.Clear();
                this.m_browseStack.Push(null);
                this.m_browseStack.Push(itemID);
                goto Label_0179;
Label_00F4:
                element = this.FindChild(szString);
                if ((element == null) || !element.HasChildren)
                {
                    throw CreateException(-2147024809);
                }
                this.m_browseStack.Push(new ItemIdentifier(element.ItemName));
                goto Label_0179;
Label_0146:
                itemID = (ItemIdentifier)this.m_browseStack.Pop();
                if ((this.m_browseStack.Count > 0) && (this.m_browseStack.Peek() == null))
                {
                    this.BuildBrowseStack(itemID);
                }
Label_0179:
                if (position != null)
                {
                    position.Dispose();
                    position = null;
                }
            }
            catch (Exception exception)
            {
                throw CreateException(exception);
            }
            finally
            {
                Monitor.Exit(server);
            }
        }
Ejemplo n.º 6
0
        public void ChangeBrowsePosition(OPCBROWSEDIRECTION dwBrowseDirection, string szString)
        {
            lock (this)
            {
                try
                {
                    BrowseFilters browseFilters = new BrowseFilters();
                    browseFilters.MaxElementsReturned  = 0;
                    browseFilters.BrowseFilter         = browseFilter.all;
                    browseFilters.ElementNameFilter    = null;
                    browseFilters.VendorFilter         = null;
                    browseFilters.ReturnAllProperties  = false;
                    browseFilters.PropertyIDs          = null;
                    browseFilters.ReturnPropertyValues = false;
                    ItemIdentifier        itemIdentifier = null;
                    Opc.Da.BrowsePosition position       = null;
                    switch (dwBrowseDirection)
                    {
                    case OPCBROWSEDIRECTION.OPC_BROWSE_TO:
                    {
                        if (szString == null || szString.Length == 0)
                        {
                            m_browseStack.Clear();
                            break;
                        }

                        itemIdentifier = new ItemIdentifier(szString);
                        BrowseElement[] array = null;
                        try
                        {
                            array = m_server.Browse(itemIdentifier, browseFilters, out position);
                        }
                        catch (Exception)
                        {
                            throw CreateException(-2147024809);
                        }

                        if (array == null || array.Length == 0)
                        {
                            throw CreateException(-2147024809);
                        }

                        m_browseStack.Clear();
                        m_browseStack.Push(null);
                        m_browseStack.Push(itemIdentifier);
                        break;
                    }

                    case OPCBROWSEDIRECTION.OPC_BROWSE_DOWN:
                    {
                        if (szString == null || szString.Length == 0)
                        {
                            throw CreateException(-2147024809);
                        }

                        BrowseElement browseElement = FindChild(szString);
                        if (browseElement == null || !browseElement.HasChildren)
                        {
                            throw CreateException(-2147024809);
                        }

                        m_browseStack.Push(new ItemIdentifier(browseElement.ItemName));
                        break;
                    }

                    case OPCBROWSEDIRECTION.OPC_BROWSE_UP:
                        if (m_browseStack.Count == 0)
                        {
                            throw CreateException(-2147467259);
                        }

                        itemIdentifier = (ItemIdentifier)m_browseStack.Pop();
                        if (m_browseStack.Count > 0 && m_browseStack.Peek() == null)
                        {
                            BuildBrowseStack(itemIdentifier);
                        }

                        break;
                    }

                    if (position != null)
                    {
                        position.Dispose();
                        position = null;
                    }
                }
                catch (Exception e)
                {
                    throw CreateException(e);
                }
            }
        }