Exemplo n.º 1
0
        /// <summary>
        ///     Changes the requested data type for one or more items in a group.
        /// </summary>
        /// <param name="items">The group items.</param>
        /// <param name="requestedTypes">Array of new Requested Datatypes to be stored.</param>
        /// <returns>
        ///     Array of HRESULTs. Indicates which items were successfully affected.
        /// </returns>
        /// <exception cref="System.ArgumentException">Wrong size of requested types array.;requestedTypes</exception>
        public HRESULT[] SetDataTypes(IList <OpcDaItem> items, IList <Type> requestedTypes)
        {
            CheckItems(items);
            if (items.Count != requestedTypes.Count)
            {
                throw new ArgumentException("Wrong size of requested types array.", "requestedTypes");
            }

            int[]           serverHandles      = ArrayHelpers.GetServerHandles(items);
            IList <VarEnum> requestedDatatypes = requestedTypes.Select(TypeConverter.ToVarEnum).ToArray();

            HRESULT[] ppErrors = As <OpcItemMgt>().SetDatatypes(serverHandles, requestedDatatypes);

            var changedItems = new List <OpcDaItem>(items.Count);

            for (int index = 0; index < items.Count; index++)
            {
                if (ppErrors[index].Succeeded)
                {
                    OpcDaItem item = items[index];
                    item.RequestedDataType = requestedTypes[index];
                    changedItems.Add(item);
                }
            }

            OnItemsChanged(new OpcDaItemsChangedEventArgs(null, null, changedItems.ToArray()));
            return(ppErrors);
        }
Exemplo n.º 2
0
        private OpcDaItemResult[] CreateItemResults(IList <OpcDaItemDefinition> itemDefinitions, OPCITEMDEF[] pItemArray,
                                                    OPCITEMRESULT[] opcDaItemResults, HRESULT[] ppErrors, bool setGroup)
        {
            var results = new OpcDaItemResult[pItemArray.Length];

            for (int index = 0; index < opcDaItemResults.Length; index++)
            {
                OPCITEMRESULT       opcItemResult     = opcDaItemResults[index];
                OpcDaItemDefinition opcItemDefinition = itemDefinitions[index];
                HRESULT             error             = ppErrors[index];

                if (error.Succeeded)
                {
                    var item = new OpcDaItem(opcItemDefinition, opcItemResult, setGroup ? this : null)
                    {
                        UserData = opcItemDefinition.UserData
                    };
                    results[index] = new OpcDaItemResult(item, error);
                }
                else
                {
                    results[index] = new OpcDaItemResult(null, error);
                }
            }
            return(results);
        }
Exemplo n.º 3
0
        private void UpdateClientHandles()
        {
            if (_items.Count == 0) // nothing to update
            {
                return;
            }

            var serverHandles = new int[_items.Count];
            var clientHandles = new int[_items.Count];

            for (int i = 0; i < _items.Count; i++)
            {
                OpcDaItem item = _items[i];
                serverHandles[i] = item.ServerHandle;
                clientHandles[i] = i;
            }

            HRESULT[] errors = SetClientHandles(serverHandles, clientHandles);

            for (int i = 0; i < errors.Length; i++)
            {
                if (errors[i].Succeeded) // Update only for succeded
                {
                    _items[i].ClientHandle = i;
                }
            }
        }
Exemplo n.º 4
0
        internal OpcDaItem GetItem(int clientHandle)
        {
            if (clientHandle < _items.Count)
            {
                OpcDaItem opcDaItem = _items[clientHandle];
                if (opcDaItem.ClientHandle == clientHandle)
                {
                    return(opcDaItem);
                }
            }

            return(_items.SingleOrDefault(i => i.ClientHandle == clientHandle));
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Synchronizes the group items. It means existing items will be replaced with new items. It fires ItemsChanged event
        ///     after synchronization.
        /// </summary>
        public void SyncItems()
        {
            IEnumOPCItemAttributes   enumerator     = As <OpcItemMgt>().CreateEnumerator();
            List <OPCITEMATTRIBUTES> itemAttributes = enumerator.EnumareateAllAndRelease(OpcConfiguration.BatchSize);

            OpcDaItem[] oldItems = _items.ToArray();
            _items.Clear();
            foreach (OPCITEMATTRIBUTES opcitemattributes in itemAttributes)
            {
                var item = new OpcDaItem(opcitemattributes, this);
                _items.Add(item);
            }
            OnItemsChanged(new OpcDaItemsChangedEventArgs(_items.ToArray(), oldItems, null));
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Sets one or more items in a group to active or inactive. This controls whether or not valid data can be obtained
        ///     from Read CACHE for those items and whether or not they are included in the OnDataChange subscription to the group.
        /// </summary>
        /// <param name="items">The group items.</param>
        /// <param name="isActive">If set to <c>true</c> items are to be activated, otherwise items are to be deactivated.</param>
        /// <returns>
        ///     Array of HRESULTs. Indicates which items were successfully affected.
        /// </returns>
        public HRESULT[] SetActiveItems(IList <OpcDaItem> items, bool isActive = true)
        {
            CheckItems(items);
            int[]     serverHandles = ArrayHelpers.GetServerHandles(items);
            HRESULT[] ppErrors      = As <OpcItemMgt>().SetActiveState(serverHandles, isActive);
            var       changedItems  = new List <OpcDaItem>(items.Count);

            for (int index = 0; index < items.Count; index++)
            {
                if (ppErrors[index].Succeeded)
                {
                    OpcDaItem item = items[index];
                    item.IsActive = isActive;
                    changedItems.Add(item);
                }
            }
            OnItemsChanged(new OpcDaItemsChangedEventArgs(null, null, changedItems.ToArray()));
            return(ppErrors);
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Add one or more items to the group.
        /// </summary>
        /// <param name="itemDefinitions">The list of item definitions.</param>
        /// <returns>
        ///     Array of item results.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">itemDefinitions</exception>
        public OpcDaItemResult[] AddItems(IList <OpcDaItemDefinition> itemDefinitions)
        {
            if (itemDefinitions == null)
            {
                throw new ArgumentNullException("itemDefinitions");
            }
            if (itemDefinitions.Count == 0)
            {
                return(new OpcDaItemResult[0]);
            }

            OPCITEMDEF[] pItemArray = ArrayHelpers.CreateOPITEMDEFs(itemDefinitions);

            HRESULT[]       ppErrors;
            OPCITEMRESULT[] opcDaItemResults = As <OpcItemMgt>().AddItems(pItemArray, out ppErrors);

            OpcDaItemResult[] results = CreateItemResults(itemDefinitions, pItemArray, opcDaItemResults, ppErrors, true);

            var addedItems = new List <OpcDaItem>(itemDefinitions.Count);

            foreach (OpcDaItemResult result in results)
            {
                OpcDaItem item = result.Item;
                if (result.Error.Succeeded && item != null)
                {
                    _items.Add(item);
                    addedItems.Add(item);
                }
            }

            // Set client handles to index of items
            UpdateClientHandles();

            OnItemsChanged(new OpcDaItemsChangedEventArgs(addedItems.ToArray(), null, null));
            return(results);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="OpcDaItemResult"/> class.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="error">The HRESULT of the operation.</param>
 public OpcDaItemResult(OpcDaItem item, HRESULT error)
 {
     Item = item;
     Error = error;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OpcDaItemResult"/> class.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="error">The HRESULT of the operation.</param>
 public OpcDaItemResult(OpcDaItem item, HRESULT error)
 {
     Item  = item;
     Error = error;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="OpcDaItemsChangedEventArgs"/> class.
 /// </summary>
 /// <param name="added">The added items.</param>
 /// <param name="removed">The removed items.</param>
 /// <param name="changed">The changed items.</param>
 public OpcDaItemsChangedEventArgs(OpcDaItem[] added, OpcDaItem[] removed, OpcDaItem[] changed)
 {
     Added = added;
     Removed = removed;
     Changed = changed;
 }