Пример #1
0
        /// <summary>
        /// Creates a item result list from a set of items and sets the handles for use by the server.
        /// </summary>
        public ItemResult[] CreateItems(OpcItem[] items)
        {
            if (items == null)
            {
                return(null);
            }

            ItemResult[] results = new ItemResult[items.Length];

            for (int ii = 0; ii < items.Length; ii++)
            {
                // initialize result with the item
                results[ii] = new ItemResult((OpcItem)items[ii]);

                // lookup the cached identifier.
                ItemIdentifier itemID = this[items[ii].ServerHandle];

                if (itemID != null)
                {
                    results[ii].ItemName     = itemID.ItemName;
                    results[ii].ItemPath     = itemID.ItemName;
                    results[ii].ServerHandle = itemID.ServerHandle;

                    // update the client handle.
                    itemID.ClientHandle = items[ii].ClientHandle;
                }

                // check if handle not found.
                if (results[ii].ServerHandle == null)
                {
                    results[ii].ResultID       = ResultID.Da.E_INVALIDHANDLE;
                    results[ii].DiagnosticInfo = null;
                    continue;
                }

                // replace client handle with internal handle.
                results[ii].ClientHandle = items[ii].ServerHandle;
            }

            return(results);
        }
Пример #2
0
        public ItemResult[] AddItems(string[] itemIds)
        {
            int count = itemIds.Count();

            if (count <= 0)
            {
                return(new ItemResult[0]);
            }

            // construct OpcItems
            //OpcRcw.Da.OPCITEMDEF[] output = null;
            OpcItem[] items = new OpcItem[count];

            for (int ii = 0; ii < count; ii++)
            {
                items[ii]              = new OpcItem();
                items[ii].ItemName     = itemIds[ii];
                items[ii].ClientHandle = Guid.NewGuid();
            }

            // get opc item def
            OpcRcw.Da.OPCITEMDEF[] definitions = GetOPCITEMDEFs(items);

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

            try
            {
                ((IOPCItemMgt)m_group).AddItems(
                    count,
                    definitions,
                    out pResults,
                    out pErrors);
            }
            catch (Exception e)
            {
                throw OpcDa.Client.Com.Interop.CreateException("IOPCItemMgt.AddItems", e);
            }

            // unmarshal output parameters.
            int[] serverHandles = this.GetItemResults(ref pResults, count, true);
            int[] errors        = OpcDa.Client.Com.Interop.GetInt32s(ref pErrors, count, true);

            // construct result list.
            ItemResult[] results = new ItemResult[count];

            for (int ii = 0; ii < count; ii++)
            {
                // create a new ResultIDs.
                results[ii] = new ItemResult(items[ii]);

                // save server handles.
                results[ii].ServerHandle = serverHandles[ii];
                //results[ii].ClientHandle = definitions[ii].hClient;

                // items created active by default.
                if (!results[ii].ActiveSpecified)
                {
                    results[ii].Active          = true;
                    results[ii].ActiveSpecified = true;
                }

                // update result id.
                results[ii].ResultID       = OpcDa.Client.Com.Interop.GetResultID(errors[ii]);
                results[ii].DiagnosticInfo = null;

                // save client handle.
                results[ii].ClientHandle = items[ii].ClientHandle;

                // add new item table.
                if (results[ii].ResultID.Succeeded())
                {
                    lock (m_items)
                    {
                        m_items[items[ii].ClientHandle.GetHashCode()] = new ItemIdentifier(results[ii]);
                    }

                    // restore internal handle.
                    // results[ii].ClientHandle = definitions[ii].hClient;
                }
            }

            // return results.
            lock (m_items)
            {
                return((ItemResult[])m_items.ApplyFilters(m_filters, results));
            }
        }