Example #1
0
        public int[] Write(int[] serverHandles, object[] values)
        {
            var ppErrors = IntPtr.Zero;

            SyncIO.Write(serverHandles.Length, serverHandles, values, out ppErrors);
            return(ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true));
        }
Example #2
0
        /// <summary>
        /// Fetches the available locales.
        /// </summary>
        public int[] QueryAvailableLocales()
        {
            string methodName = "IOPCCommon.QueryAvailableLocales";

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

                // query for available locales.
                int    count      = 0;
                IntPtr pLocaleIDs = IntPtr.Zero;

                server.QueryAvailableLocaleIDs(out count, out pLocaleIDs);

                // unmarshal results.
                return(ComUtils.GetInt32s(ref pLocaleIDs, count, true));
            }
            catch (Exception e)
            {
                ComCallError(methodName, e);
                return(null);
            }
            finally
            {
                EndComCall(methodName);
            }
        }
Example #3
0
        public int[] SetDataTypes(int[] serverHandles, short[] requestedDataTypes)
        {
            var ppErrors = IntPtr.Zero;

            ItemMgt.SetDatatypes(serverHandles.Length, serverHandles, requestedDataTypes, out ppErrors);
            return(ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true));
        }
Example #4
0
        public int[] SetActiveState(int[] serverHandles, bool isActive)
        {
            var ppErrors = IntPtr.Zero;

            ItemMgt.SetActiveState(serverHandles.Length, serverHandles, isActive ? 1 : 0, out ppErrors);
            return(ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true));
        }
Example #5
0
        public int[] SetClientHandles(int[] serverHandles, int[] clientHandles)
        {
            var ppErrors = IntPtr.Zero;

            ItemMgt.SetClientHandles(serverHandles.Length, serverHandles, clientHandles, out ppErrors);
            return(ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true));
        }
Example #6
0
        /// <summary>
        /// Writes the values for a set of items.
        /// </summary>
        public int[] SyncWrite(int[] serverHandles, object[] values, int count)
        {
            // initialize output parameters.
            IntPtr pErrors = IntPtr.Zero;

            string methodName = "IOPCSyncIO.Write";

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

                server.Write(
                    count,
                    serverHandles,
                    values,
                    out pErrors);
            }
            catch (Exception e)
            {
                ComUtils.TraceComError(e, methodName);
                return(null);
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal output parameters.
            return(ComUtils.GetInt32s(ref pErrors, count, true));
        }
Example #7
0
        public int[] RemoveItems(int[] serverHandles)
        {
            var ppErrors = IntPtr.Zero;

            ItemMgt.RemoveItems(serverHandles.Length, serverHandles, out ppErrors);
            return(ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true));
        }
Example #8
0
        public int[] AsyncWrite(int connection, int[] serverHandles, object[] values, out int transactionId)
        {
            var ppErrors = IntPtr.Zero;

            AsyncIO.Write(connection, serverHandles.Length, serverHandles, values, out transactionId, out ppErrors);
            return(ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true));
        }
Example #9
0
        public int[] AsyncRead(int connection, OpcDataSource source, int[] serverHandles, out int transactionId)
        {
            var ppErrors = IntPtr.Zero;

            AsyncIO.Read(connection, (OPCDATASOURCE)source, serverHandles.Length, serverHandles, out transactionId, out ppErrors);
            return(ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true));
        }
Example #10
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;
                }
            }
        }
Example #11
0
        /// <summary>
        /// Gets the event categories.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="categories">The categories.</param>
        /// <param name="descriptions">The descriptions.</param>
        public void GetEventCategories(int eventType, out int[] categories, out string[] descriptions)
        {
            string methodName = "IOPCEventServer.QueryEventCategories";

            int    count         = 0;
            IntPtr pCategories   = IntPtr.Zero;
            IntPtr pDescriptions = IntPtr.Zero;

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

                server.QueryEventCategories(
                    eventType,
                    out count,
                    out pCategories,
                    out pDescriptions);
            }
            catch (Exception e)
            {
                ComCallError(methodName, e);
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal results.
            categories   = ComUtils.GetInt32s(ref pCategories, count, true);
            descriptions = ComUtils.GetUnicodeStrings(ref pDescriptions, count, true);
        }
Example #12
0
        public int[] QueryAvailableLocaleIDs()
        {
            var pdwCount = 0;
            var pdwLcid  = IntPtr.Zero;

            _server.QueryAvailableLocaleIDs(out pdwCount, out pdwLcid);
            return(ComUtils.GetInt32s(ref pdwLcid, pdwCount, true));
        }
Example #13
0
        public void QueryAvailableProperties(string itemId)
        {
            var pdwCount       = 0;
            var ppPropertyIds  = IntPtr.Zero;
            var ppDescriptions = IntPtr.Zero;
            var ppvtDataTypes  = IntPtr.Zero;

            _server.QueryAvailableProperties(itemId, out pdwCount, out ppPropertyIds, out ppDescriptions, out ppvtDataTypes);
            if (pdwCount > 0)
            {
                var ids   = ComUtils.GetInt32s(ref ppPropertyIds, pdwCount, true);
                var descs = ComUtils.GetUnicodeStrings(ref ppDescriptions, pdwCount, true);
                var types = ComUtils.GetInt16s(ref ppvtDataTypes, pdwCount, true);
            }
        }
Example #14
0
        public OpcItemResult[] AddItems(OpcItemDefine[] items)
        {
            var rawItems = items.Select(item => new OPCITEMDEF
            {
                bActive             = item.IsActive ? 1 : 0,
                hClient             = item.ClientHandle,
                szAccessPath        = item.AccessPath,
                szItemID            = item.ItemId,
                vtRequestedDataType = item.RequestedDataType
            })
                           .ToArray();

            var ppAddResults = IntPtr.Zero;
            var ppErrors     = IntPtr.Zero;

            ItemMgt.AddItems(items.Length, rawItems, out ppAddResults, out ppErrors);

            var errors  = ComUtils.GetInt32s(ref ppErrors, items.Length, true);
            var results = GetStructures <OPCITEMRESULT>(ppAddResults, items.Length).Zip(errors, (r, e) =>
            {
                try
                {
                    if (r.pBlob != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(r.pBlob);
                        r.pBlob = IntPtr.Zero;
                    }
                    return(new OpcItemResult
                    {
                        AccessRights = r.dwAccessRights,
                        CanonicalDataType = r.vtCanonicalDataType,
                        ErrorCode = e,
                        ServerHandle = r.hServer
                    });
                }
                catch (Exception ex)
                {
                    return(new OpcItemResult {
                        ErrorCode = ex.HResult
                    });
                }
            })
                          .ToArray();

            return(results);
        }
Example #15
0
        /// <summary>
        /// Reads the values for a set of items.
        /// </summary>
        public DaValue[] SyncRead(int[] serverHandles, int count)
        {
            // initialize output parameters.
            IntPtr pValues = IntPtr.Zero;
            IntPtr pErrors = IntPtr.Zero;

            if (count > 0)
            {
                string methodName = "IOPCSyncIO.Read";

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

                    server.Read(
                        OPCDATASOURCE.OPC_DS_DEVICE,
                        count,
                        serverHandles,
                        out pValues,
                        out pErrors);
                }
                catch (Exception e)
                {
                    ComUtils.TraceComError(e, methodName);
                    return(null);
                }
                finally
                {
                    EndComCall(methodName);
                }
            }

            // unmarshal output parameters.
            DaValue[] values = GetItemValues(ref pValues, count, true);

            int[] errors = ComUtils.GetInt32s(ref pErrors, count, true);

            // save error codes.
            for (int ii = 0; ii < count; ii++)
            {
                values[ii].Error = errors[ii];
            }

            return(values);
        }
        /// <summary>
        /// Returns the filter currently in use for event subscriptions.
        /// </summary>
        /// <param name="pdwEventType">Bit map specifying which event types are of allowed through the filter</param>
        /// <param name="pdwNumCategories">Length of the event category array returned.</param>
        /// <param name="ppdwEventCategories">Array of event categories for the filter.</param>
        /// <param name="pdwLowSeverity">Lowest severity allowed through filter.</param>
        /// <param name="pdwHighSeverity">Highest severity allowed through filter.</param>
        /// <param name="pdwNumAreas">Length of the area list array returned.</param>
        /// <param name="ppszAreaList">List of process areas for the filter.</param>
        /// <param name="pdwNumSources">Length of the event source list returned.</param>
        /// <param name="ppszSourceList">List of sources for the filter.</param>
        public void GetFilter(out int pdwEventType, out int pdwNumCategories, out IntPtr ppdwEventCategories, out int pdwLowSeverity, out int pdwHighSeverity, out int pdwNumAreas, out IntPtr ppszAreaList, out int pdwNumSources, out IntPtr ppszSourceList)
        {
            pdwEventType        = 0;
            pdwNumCategories    = 0;
            ppdwEventCategories = IntPtr.Zero;
            pdwLowSeverity      = 0;
            pdwHighSeverity     = 0;
            pdwNumAreas         = 0;
            ppszAreaList        = IntPtr.Zero;
            pdwNumSources       = 0;
            ppszSourceList      = IntPtr.Zero;

            try
            {
                pdwEventType    = m_dwEventType;
                pdwLowSeverity  = m_dwLowSeverity;
                pdwHighSeverity = m_dwHighSeverity;
                if (m_EventCategoryVector.Count != 0)
                {
                    int[] EventCategoryIDs = m_EventCategoryVector.ToArray();
                    pdwNumCategories    = m_EventCategoryVector.Count;
                    ppdwEventCategories = ComUtils.GetInt32s(EventCategoryIDs);
                }
                if (m_AreaVector.Count != 0)
                {
                    string[] Areas = m_AreaVector.ToArray();
                    pdwNumAreas  = m_AreaVector.Count;
                    ppszAreaList = ComUtils.GetUnicodeStrings(Areas);
                }
                if (m_SourceVector.Count != 0)
                {
                    string[] Sources = m_SourceVector.ToArray();
                    pdwNumSources  = m_SourceVector.Count;
                    ppszSourceList = ComUtils.GetUnicodeStrings(Sources);
                }
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error in GetFilter");
                throw ComUtils.CreateComException(e);
            }
        }
Example #17
0
        public OpcItemState[] Read(OpcDataSource source, int[] serverHandles)
        {
            var ppItemValues = IntPtr.Zero;
            var ppErrors     = IntPtr.Zero;

            SyncIO.Read((OPCDATASOURCE)source, serverHandles.Length, serverHandles, out ppItemValues, out ppErrors);

            var errors = ComUtils.GetInt32s(ref ppErrors, serverHandles.Length, true);
            var values = GetStructures <OPCITEMSTATE>(ppItemValues, serverHandles.Length)
                         .Zip(errors, (v, e) => new OpcItemState
            {
                ClientHandle = v.hClient,
                DataValue    = v.vDataValue,
                ErrorCode    = e,
                Quality      = v.wQuality,
                Timestamp    = ComUtils.GetDateTime(v.ftTimeStamp)
            })
                         .ToArray();

            return(values);
        }
        /// <summary>
        /// Retrieves the attributes which are currently specified to be returned with event notifications in the IOPCEventSink::OnEvent callback.
        /// </summary>
        /// <param name="dwEventCategory">The specific event category for which to retrieve the list of attributes.</param>
        /// <param name="pdwCount">The size of the attribute IDs array which is being returned. Is set to zero if no attributes are currently specified.</param>
        /// <param name="ppdwAttributeIDs">The list IDs of the attributes which are currently specified to be returned with event notifications for the event type and event category specified.</param>
        public void GetReturnedAttributes(int dwEventCategory, out int pdwCount, out IntPtr ppdwAttributeIDs)
        {
            pdwCount         = 0;
            ppdwAttributeIDs = IntPtr.Zero;

            try
            {
                // Make sure we are passed a valid dwEventCategory
                NodeId catNodeId = m_server.FindEventCatNodeId(dwEventCategory);
                if (catNodeId == null)
                {
                    throw ComUtils.CreateComException("SetFilter", ResultIds.E_INVALIDARG);
                }

                lock (m_csData)
                {
                    ReturnedAttributeList ras;
                    if (m_ReturnedAttributes.TryGetValue(dwEventCategory, out ras))
                    {
                        pdwCount = ras.Count;
                        int[] attrIDs = ras.ToArray();

                        ppdwAttributeIDs = ComUtils.GetInt32s(attrIDs);
                    }
                }
            }
            catch (COMException e)
            {
                throw ComUtils.CreateComException(e);
            }
            catch (Exception e)
            {
                Utils.Trace(e, "Unexpected error in GetReturnedAttributes");
                throw ComUtils.CreateComException(e);
            }
        }
Example #19
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);
        }
Example #20
0
        /// <summary>
        /// Removes the items from the group that have been marked as deleted.
        /// </summary>
        public void RemoveItems()
        {
            // count the number of items to remove.
            List <GroupItem> itemsToRemove = new List <GroupItem>();

            lock (Lock)
            {
                List <GroupItem> itemsToKeep = new List <GroupItem>();

                for (int ii = 0; ii < m_items.Count; ii++)
                {
                    if (m_items[ii].Deleted && m_items[ii].Created)
                    {
                        itemsToRemove.Add(m_items[ii]);
                        continue;
                    }

                    itemsToKeep.Add(m_items[ii]);
                }

                m_items = itemsToKeep;
            }

            // check if nothing to do.
            if (itemsToRemove.Count == 0)
            {
                return;
            }

            // build list of items to remove.
            int count = itemsToRemove.Count;

            int[] serverHandles = new int[count];

            for (int ii = 0; ii < itemsToRemove.Count; ii++)
            {
                serverHandles[ii] = itemsToRemove[ii].ServerHandle;

                // remove the associated monitored items.
                if (m_monitoredItems != null)
                {
                    lock (m_monitoredItems)
                    {
                        m_monitoredItems.Remove(itemsToRemove[ii].ClientHandle);
                    }
                }
            }

            IntPtr pErrors = IntPtr.Zero;

            string methodName = "IOPCItemMgt.RemoveItems";

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

                // remove items.
                server.RemoveItems(
                    count,
                    serverHandles,
                    out pErrors);
            }
            catch (Exception e)
            {
                ComUtils.TraceComError(e, methodName);

                for (int ii = 0; ii < itemsToRemove.Count; ii++)
                {
                    itemsToRemove[ii].Created = false;
                    itemsToRemove[ii].ErrorId = Marshal.GetHRForException(e);
                }

                return;
            }
            finally
            {
                EndComCall(methodName);
            }

            // free returned error array.
            int[] errors = ComUtils.GetInt32s(ref pErrors, count, true);

            // save error codes.
            for (int ii = 0; ii < count; ii++)
            {
                itemsToRemove[ii].Created = false;
                itemsToRemove[ii].ErrorId = errors[ii];
            }

            /*
             * Utils.Trace(
             *  "Group {0} RemoveItems({4}/{5}) {1}/{2}ms {3}%",
             *  m_clientHandle,
             *  m_samplingInterval,
             *  m_actualSamplingInterval,
             *  m_deadband,
             *  itemsToRemove.Count,
             *  m_items.Count);
             */
        }
Example #21
0
        /// <summary>
        /// Sets the active state for a set of items in a group.
        /// </summary>
        public void ActivateItems(bool active)
        {
            // count the number of items to activate.
            List <GroupItem> itemsToActivate = new List <GroupItem>();

            lock (Lock)
            {
                for (int ii = 0; ii < m_items.Count; ii++)
                {
                    if (m_items[ii].ActiveChanged && m_items[ii].Active == active && m_items[ii].Created)
                    {
                        itemsToActivate.Add(m_items[ii]);
                    }
                }
            }

            // check if nothing to do.
            if (itemsToActivate.Count == 0)
            {
                return;
            }

            // build list of items to remove.
            int count = itemsToActivate.Count;

            int[] serverHandles = new int[count];

            for (int ii = 0; ii < itemsToActivate.Count; ii++)
            {
                serverHandles[ii] = itemsToActivate[ii].ServerHandle;
            }

            // initialize output parameters.
            IntPtr pErrors = IntPtr.Zero;

            string methodName = "IOPCItemMgt.SetActiveState";

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

                server.SetActiveState(
                    count,
                    serverHandles,
                    (active) ? 1 : 0,
                    out pErrors);
            }
            catch (Exception e)
            {
                ComUtils.TraceComError(e, methodName);

                for (int ii = 0; ii < itemsToActivate.Count; ii++)
                {
                    itemsToActivate[ii].ActiveChanged = false;
                    itemsToActivate[ii].ErrorId       = Marshal.GetHRForException(e);
                }
            }
            finally
            {
                EndComCall(methodName);
            }

            // free returned error array.
            int[] errors = ComUtils.GetInt32s(ref pErrors, count, true);

            // save error codes.
            for (int ii = 0; ii < count; ii++)
            {
                itemsToActivate[ii].ActiveChanged = false;
                itemsToActivate[ii].ErrorId       = errors[ii];
            }

            /*
             * Utils.Trace(
             *  "Group {0} ActivateItems({4}/{5}) {1}/{2}ms {3}%",
             *  m_clientHandle,
             *  m_samplingInterval,
             *  m_actualSamplingInterval,
             *  m_deadband,
             *  active,
             *  itemsToActivate.Count);
             */
        }
Example #22
0
        /// <summary>
        /// Adds all items to the group that have not already been added.
        /// </summary>
        public void AddItems()
        {
            // count the number of items to add.
            List <GroupItem> itemsToAdd = new List <GroupItem>();

            lock (Lock)
            {
                for (int ii = 0; ii < m_items.Count; ii++)
                {
                    if (!m_items[ii].Created)
                    {
                        itemsToAdd.Add(m_items[ii]);
                    }
                }
            }

            // check if nothing to do.
            if (itemsToAdd.Count == 0)
            {
                return;
            }

            // create item definitions.
            int count = itemsToAdd.Count;

            OpcRcw.Da.OPCITEMDEF[] definitions = new OpcRcw.Da.OPCITEMDEF[count];

            for (int ii = 0; ii < count; ii++)
            {
                definitions[ii] = new OpcRcw.Da.OPCITEMDEF();

                definitions[ii].szItemID            = itemsToAdd[ii].ItemId;
                definitions[ii].bActive             = (itemsToAdd[ii].Active) ? 1 : 0;
                definitions[ii].szAccessPath        = String.Empty;
                definitions[ii].vtRequestedDataType = (short)VarEnum.VT_EMPTY;
                definitions[ii].hClient             = itemsToAdd[ii].ClientHandle;
            }

            // initialize output parameters.
            IntPtr pResults = IntPtr.Zero;
            IntPtr pErrors  = IntPtr.Zero;

            // add items to group.
            string methodName = "IOPCItemMgt.AddItems";

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

                server.AddItems(
                    count,
                    definitions,
                    out pResults,
                    out pErrors);
            }
            catch (Exception e)
            {
                ComUtils.TraceComError(e, methodName);

                for (int ii = 0; ii < itemsToAdd.Count; ii++)
                {
                    itemsToAdd[ii].ErrorId = Marshal.GetHRForException(e);
                }

                return;
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal output parameters.
            int[] serverHandles = GetItemResults(ref pResults, count, true);
            int[] errors        = ComUtils.GetInt32s(ref pErrors, count, true);

            // save handles and error codes.
            for (int ii = 0; ii < count; ii++)
            {
                GroupItem item = itemsToAdd[ii];

                item.ServerHandle = serverHandles[ii];
                item.ErrorId      = errors[ii];

                if (item.ErrorId >= 0)
                {
                    itemsToAdd[ii].Created = true;
                }
            }

            /*
             * Utils.Trace(
             *  "Group {0} AddItems({4}/{5}) {1}/{2}ms {3}%",
             *  m_clientHandle,
             *  m_samplingInterval,
             *  m_actualSamplingInterval,
             *  m_deadband,
             *  itemsToAdd.Count,
             *  m_items.Count);
             */
        }
Example #23
0
        /// <summary>
        /// Gets the event attributes.
        /// </summary>
        /// <param name="categoryId">The category id.</param>
        /// <param name="attributeIds">The attribute ids.</param>
        /// <param name="descriptions">The descriptions.</param>
        /// <param name="datatypes">The datatypes.</param>
        public bool GetEventAttributes(
            int categoryId,
            out int[] attributeIds,
            out string[] descriptions,
            out short[] datatypes)
        {
            string methodName = "IOPCEventServer.QueryEventAttributes";

            int    count         = 0;
            IntPtr pAttributeIds = IntPtr.Zero;
            IntPtr pDescriptions = IntPtr.Zero;
            IntPtr pDataTypes    = IntPtr.Zero;

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

                server.QueryEventAttributes(
                    categoryId,
                    out count,
                    out pAttributeIds,
                    out pDescriptions,
                    out pDataTypes);
            }
            catch (Exception e)
            {
                ComCallError(methodName, e);
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal results.
            attributeIds = ComUtils.GetInt32s(ref pAttributeIds, count, true);
            descriptions = ComUtils.GetUnicodeStrings(ref pDescriptions, count, true);
            datatypes    = ComUtils.GetInt16s(ref pDataTypes, count, true);

            // remove the AREAS attribute which is never exposed.
            for (int ii = 0; ii < count; ii++)
            {
                if (String.Compare(descriptions[ii], "AREAS", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    int[]    attributeIds2 = new int[count - 1];
                    string[] descriptions2 = new string[count - 1];
                    short[]  datatypes2    = new short[count - 1];

                    if (ii > 0)
                    {
                        Array.Copy(attributeIds, attributeIds2, ii);
                        Array.Copy(descriptions, descriptions2, ii);
                        Array.Copy(datatypes, datatypes2, ii);
                    }

                    if (ii < count - 1)
                    {
                        Array.Copy(attributeIds, ii + 1, attributeIds2, ii, count - ii - 1);
                        Array.Copy(descriptions, ii + 1, descriptions2, ii, count - ii - 1);
                        Array.Copy(datatypes, ii + 1, datatypes2, ii, count - ii - 1);
                    }

                    attributeIds = attributeIds2;
                    descriptions = descriptions2;
                    datatypes    = datatypes2;
                    break;
                }
            }

            return(count > 0);
        }
Example #24
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);
        }
Example #25
0
        /// <summary>
        /// Acknowledges the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="eventId">The event id.</param>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public uint Acknowledge(
            ServerSystemContext context,
            byte[] eventId,
            LocalizedText comment)
        {
            // get the user name from the context.
            string userName = String.Empty;

            if (context.UserIdentity != null)
            {
                userName = context.UserIdentity.DisplayName;
            }

            // get the comment.
            string commentText = String.Empty;

            if (comment != null)
            {
                commentText = comment.Text;
            }

            System.Runtime.InteropServices.ComTypes.FILETIME ftActiveTime;

            // unpack the event id.
            ServiceMessageContext messageContext = new ServiceMessageContext();

            messageContext.NamespaceUris = context.NamespaceUris;
            messageContext.ServerUris    = context.ServerUris;
            messageContext.Factory       = context.EncodeableFactory;

            BinaryDecoder decoder = new BinaryDecoder(eventId, messageContext);

            string source        = decoder.ReadString(null);
            string conditionName = decoder.ReadString(null);

            ftActiveTime.dwHighDateTime = decoder.ReadInt32(null);
            ftActiveTime.dwLowDateTime  = decoder.ReadInt32(null);
            int cookie = decoder.ReadInt32(null);

            decoder.Close();

            string methodName = "IOPCEventServer.AckCondition";

            IntPtr pErrors = IntPtr.Zero;

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

                server.AckCondition(
                    1,
                    userName,
                    commentText,
                    new string[] { source },
                    new string[] { conditionName },
                    new System.Runtime.InteropServices.ComTypes.FILETIME[] { ftActiveTime },
                    new int[] { cookie },
                    out pErrors);
            }
            catch (Exception e)
            {
                ComCallError(methodName, e);
                return(StatusCodes.BadUnexpectedError);
            }
            finally
            {
                EndComCall(methodName);
            }

            // unmarshal results.
            int[] errors = ComUtils.GetInt32s(ref pErrors, 1, true);

            if (errors[0] == ResultIds.S_ALREADYACKED)
            {
                return(StatusCodes.BadConditionBranchAlreadyAcked);
            }
            else if (errors[0] < 0)
            {
                return(StatusCodes.BadEventIdUnknown);
            }

            return(StatusCodes.Good);
        }