Ejemplo n.º 1
0
        /// <summary>
        /// Creates the area browser.
        /// </summary>
        /// <returns>An object which browses areas and sources.</returns>
        public IOPCEventAreaBrowser CreateAreaBrowser()
        {
            object unknown = null;

            string methodName = "IOPCEventServer.CreateAreaBrowser";

            try
            {
                IOPCEventServer server = BeginComCall <IOPCEventServer>(methodName, true);
                Guid            riid   = typeof(IOPCEventAreaBrowser).GUID;
                server.CreateAreaBrowser(ref riid, out unknown);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_NOTIMPL))
                {
                    ComCallError(methodName, e);
                }

                return(null);
            }
            finally
            {
                EndComCall(methodName);
            }

            return((IOPCEventAreaBrowser)unknown);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the property item ids.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        /// <param name="properties">The properties to update.</param>
        private void GetPropertyItemIds(string itemId, DaProperty[] properties)
        {
            string methodName = "IOPCItemProperties.LookupItemIDs";

            int count = properties.Length;

            // get list of ids to request.
            int[] propertyIds = new int[count];

            for (int ii = 0; ii < propertyIds.Length; ii++)
            {
                propertyIds[ii] = properties[ii].PropertyId;
            }

            // request the item ids.
            IntPtr pItemIds = IntPtr.Zero;
            IntPtr pErrors  = IntPtr.Zero;

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

                server.LookupItemIDs(
                    itemId,
                    count,
                    propertyIds,
                    out pItemIds,
                    out pErrors);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_UNKNOWNITEMID, ResultIds.E_INVALIDITEMID))
                {
                    ComUtils.TraceComError(e, methodName);
                }

                return;
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal results.
            string[] itemIds = ComUtils.GetUnicodeStrings(ref pItemIds, count, true);
            int[]    errors  = ComUtils.GetInt32s(ref pErrors, count, true);

            // update the objects.
            for (int ii = 0; ii < count; ii++)
            {
                if (errors[ii] >= 0)
                {
                    properties[ii].ItemId = itemIds[ii];
                }
                else
                {
                    properties[ii].ItemId = null;
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Looks up the item id for the node relative to the current browse position.
        /// </summary>
        /// <param name="browseName">Name of the element to lookup.</param>
        /// <returns>The item id. Null if an error occurs.</returns>
        private string GetItemId(string browseName)
        {
            string methodName = "IOPCBrowseServerAddressSpace.GetItemID";

            try
            {
                IOPCBrowseServerAddressSpace server = BeginComCall <IOPCBrowseServerAddressSpace>(methodName, true);
                string itemId = null;
                server.GetItemID(browseName, out itemId);
                return(itemId);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL))
                {
                    ComCallError(methodName, e);
                }

                return(null);
            }
            finally
            {
                EndComCall(methodName);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Changes the browse position.
        /// </summary>
        /// <param name="direction">The direction.</param>
        /// <param name="name">The name.</param>
        /// <returns>True if the operation was successful.</returns>
        private bool ChangeBrowsePosition(OPCBROWSEDIRECTION direction, string name)
        {
            string methodName = "IOPCBrowseServerAddressSpace.ChangeBrowsePosition";

            try
            {
                IOPCBrowseServerAddressSpace server = BeginComCall <IOPCBrowseServerAddressSpace>(methodName, true);
                server.ChangeBrowsePosition(direction, name);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_INVALIDARG))
                {
                    ComCallError(methodName, e);
                }

                return(false);
            }
            finally
            {
                EndComCall(methodName);
            }

            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Sets the filter.
        /// </summary>
        /// <param name="qualifiedName">Name of the qualified.</param>
        /// <param name="isSource">if set to <c>true</c> if filtering by source.</param>
        /// <returns></returns>
        private bool SetFilter(string qualifiedName, bool isSource)
        {
            string methodName = "IOPCEventSubscriptionMgt.SetFilter";

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

                string[] filter = null;

                if (!String.IsNullOrEmpty(qualifiedName))
                {
                    filter = new string[] { qualifiedName };

                    // need to use a wild card to include sub-areas.
                    if (!String.IsNullOrEmpty(m_separators))
                    {
                        filter    = new string[m_separators.Length + 1];
                        filter[0] = qualifiedName;

                        for (int ii = 0; ii < m_separators.Length; ii++)
                        {
                            filter[ii + 1] = Utils.Format("{0}{1}*", qualifiedName, m_separators[ii]);
                        }
                    }
                }
                else
                {
                    filter = new string[0];
                }

                server.SetFilter(
                    Constants.ALL_EVENTS,
                    0,
                    new int[0],
                    1,
                    1000,
                    (isSource)?0:filter.Length,
                    filter,
                    (!isSource)?0:filter.Length,
                    filter);

                return(true);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL))
                {
                    ComCallError(methodName, e);
                }

                return(false);
            }
            finally
            {
                EndComCall(methodName);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Selects the returned attributes.
        /// </summary>
        /// <param name="categoryId">The category id.</param>
        /// <param name="attributeIds">The attribute ids.</param>
        /// <returns></returns>
        private bool SelectReturnedAttributes(int categoryId, int[] attributeIds)
        {
            string methodName = "IOPCEventSubscriptionMgt.SelectReturnedAttributes";

            try
            {
                IOPCEventSubscriptionMgt server = BeginComCall <IOPCEventSubscriptionMgt>(methodName, true);
                server.SelectReturnedAttributes(categoryId, attributeIds.Length, attributeIds);
                return(true);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL))
                {
                    ComCallError(methodName, e);
                }

                return(false);
            }
            finally
            {
                EndComCall(methodName);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Cancels the refresh for all conditions.
        /// </summary>
        /// <returns></returns>
        private bool CancelRefresh()
        {
            string methodName = "IOPCEventSubscriptionMgt.CancelRefresh";

            try
            {
                IOPCEventSubscriptionMgt server = BeginComCall <IOPCEventSubscriptionMgt>(methodName, true);
                server.CancelRefresh(m_callback.Cookie);
                return(true);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_INVALIDARG, ResultIds.E_NOTIMPL))
                {
                    ComCallError(methodName, e);
                }

                return(false);
            }
            finally
            {
                EndComCall(methodName);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Changes the browse position.
        /// </summary>
        /// <param name="direction">The direction.</param>
        /// <param name="qualifiedName">The qualified area name.</param>
        private bool ChangeBrowsePosition(OPCAEBROWSEDIRECTION direction, string qualifiedName)
        {
            string methodName = "IOPCEventAreaBrowser.CreateAreaBrowser";

            try
            {
                IOPCEventAreaBrowser server = BeginComCall <IOPCEventAreaBrowser>(methodName, true);
                server.ChangeBrowsePosition(direction, qualifiedName);
                return(true);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL))
                {
                    ComCallError(methodName, e);
                }

                return(false);
            }
            finally
            {
                EndComCall(methodName);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Fetches the next string (returns null if no more data).
        /// </summary>
        public string Next()
        {
            // return null if at end of list.
            if (m_fetched == -1)
            {
                return(null);
            }

            // see if next item is available.
            if (m_position < m_fetched)
            {
                return(m_buffer[m_position++]);
            }

            m_position = 0;

            try
            {
                // fetch next batch.
                m_fetched = 0;

                IntPtr pBuffer = Marshal.AllocCoTaskMem(IntPtr.Size * m_buffer.Length);

                try
                {
                    int error = m_enumerator.RemoteNext(m_buffer.Length, pBuffer, out m_fetched);

                    if (error < 0 || m_fetched == 0)
                    {
                        return(null);
                    }

                    IntPtr[] pStrings = new IntPtr[m_fetched];
                    Marshal.Copy(pBuffer, pStrings, 0, m_fetched);

                    for (int ii = 0; ii < m_fetched; ii++)
                    {
                        m_buffer[ii] = Marshal.PtrToStringUni(pStrings[ii]);
                        Marshal.FreeCoTaskMem(pStrings[ii]);
                    }
                }
                finally
                {
                    Marshal.FreeCoTaskMem(pBuffer);
                }

                // check if end of list.
                if (m_fetched == 0)
                {
                    m_fetched = -1;
                    return(null);
                }

                // return first item.
                return(m_buffer[m_position++]);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL))
                {
                    throw ComUtils.CreateException(e, "IEnumString.RemoteNext");
                }

                // some (incorrect) implementations return E_FAIL at the end of the list.
                m_fetched = -1;
                return(null);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Reads the values of the properties from the server.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        /// <param name="propertyIds">The list of property ids to read. All properties are read if null.</param>
        /// <returns>True if the element has properies.</returns>
        private DaValue[] ReadPropertyValues(string itemId, params int[] propertyIds)
        {
            string methodName = "IOPCItemProperties.GetItemProperties";

            // check for trivial case.
            if (propertyIds == null)
            {
                return(null);
            }

            int count = propertyIds.Length;

            DaValue[] results = new DaValue[count];

            // check for empty list.
            if (count == 0)
            {
                return(results);
            }

            // get the values from the server.
            IntPtr pValues = IntPtr.Zero;
            IntPtr pErrors = IntPtr.Zero;

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

                server.GetItemProperties(
                    itemId,
                    count,
                    propertyIds,
                    out pValues,
                    out pErrors);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_UNKNOWNITEMID, ResultIds.E_INVALIDITEMID))
                {
                    ComUtils.TraceComError(e, methodName);
                    return(null);
                }

                for (int ii = 0; ii < count; ii++)
                {
                    DaValue result = results[ii] = new DaValue();
                    result.Value     = null;
                    result.Quality   = OpcRcw.Da.Qualities.OPC_QUALITY_GOOD;
                    result.Timestamp = DateTime.UtcNow;
                    result.Error     = Marshal.GetHRForException(e);
                }

                return(results);
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal results.
            object[] values = ComUtils.GetVARIANTs(ref pValues, count, true);
            int[]    errors = ComUtils.GetInt32s(ref pErrors, count, true);

            for (int ii = 0; ii < count; ii++)
            {
                DaValue result = results[ii] = new DaValue();
                result.Value     = ComUtils.ProcessComValue(values[ii]);
                result.Quality   = OpcRcw.Da.Qualities.OPC_QUALITY_GOOD;
                result.Timestamp = DateTime.UtcNow;
                result.Error     = errors[ii];
            }

            return(results);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Read the available non-built in properties from the server.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        /// <param name="updateCache">if set to <c>true</c> the cache is updated.</param>
        /// <returns>The array of properties.</returns>
        public DaProperty[] ReadAvailableProperties(string itemId, bool updateCache)
        {
            string methodName = "IOPCItemProperties.QueryAvailableProperties";

            // query for available properties.
            int count = 0;

            IntPtr pPropertyIds  = IntPtr.Zero;
            IntPtr pDescriptions = IntPtr.Zero;
            IntPtr pDataTypes    = IntPtr.Zero;

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

                server.QueryAvailableProperties(
                    itemId,
                    out count,
                    out pPropertyIds,
                    out pDescriptions,
                    out pDataTypes);
            }
            catch (Exception e)
            {
                if (ComUtils.IsUnknownError(e, ResultIds.E_FAIL, ResultIds.E_UNKNOWNITEMID, ResultIds.E_INVALIDITEMID))
                {
                    ComUtils.TraceComError(e, methodName);
                }

                return(null);
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal results.
            int[]    propertyIds  = ComUtils.GetInt32s(ref pPropertyIds, count, true);
            string[] descriptions = ComUtils.GetUnicodeStrings(ref pDescriptions, count, true);
            short[]  datatype     = ComUtils.GetInt16s(ref pDataTypes, count, true);

            List <DaProperty> properties = new List <DaProperty>();

            for (int ii = 0; ii < count; ii++)
            {
                // do not return any of the built in properties.
                if (propertyIds[ii] <= PropertyIds.TimeZone)
                {
                    continue;
                }

                DaProperty property = new DaProperty();

                property.PropertyId = propertyIds[ii];
                property.Name       = descriptions[ii];
                property.DataType   = datatype[ii];

                properties.Add(property);
            }

            // fetch the item ids.
            if (properties.Count > 0)
            {
                DaProperty[] array = properties.ToArray();

                GetPropertyItemIds(itemId, array);

                // update the cache.
                if (updateCache)
                {
                    lock (m_cache)
                    {
                        DaElement element = null;

                        if (m_cache.TryGetValue(itemId, out element))
                        {
                            element.Properties = array;
                        }
                    }
                }

                return(array);
            }

            return(null);
        }