Stores a request to subscribe to the attributes of an HDA item.
Esempio n. 1
0
        /// <summary>
        /// Assigns the handles to the requests.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="requests">The requests.</param>
        private void AssignHandles(ComHdaClient client, Dictionary <string, HdaSubscribeAttributeRequest> requests)
        {
            List <string> itemIds = null;

            lock (m_lock)
            {
                foreach (KeyValuePair <string, HdaSubscribeAttributeRequest> entry in requests)
                {
                    if (entry.Value.ServerHandle == null)
                    {
                        if (itemIds == null)
                        {
                            itemIds = new List <string>();
                        }

                        itemIds.Add(entry.Key);
                    }
                }
            }

            if (itemIds == null)
            {
                return;
            }

            HdaItem[] items = client.GetItems(itemIds.ToArray());

            lock (m_lock)
            {
                for (int ii = 0; ii < items.Length; ii++)
                {
                    HdaSubscribeAttributeRequest request = null;

                    if (!requests.TryGetValue(itemIds[ii], out request))
                    {
                        continue;
                    }

                    if (items[ii].Error < 0)
                    {
                        request.ServerHandle = null;
                        continue;
                    }

                    request.ServerHandle = items[ii].ServerHandle;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Finds the item attributes request for the specifed item.
        /// </summary>
        /// <param name="itemId">The item id.</param>
        /// <param name="create">if set to <c>true</c> a new request is created if it does not exist.</param>
        /// <returns>The subscribe attribute request.</returns>
        private HdaSubscribeAttributeRequest FindAttributeRequest(string itemId, bool create)
        {
            lock (m_lock)
            {
                HdaSubscribeAttributeRequest requests = null;

                if (!m_requests.TryGetValue(itemId, out requests))
                {
                    if (!create)
                    {
                        return(null);
                    }

                    requests = new HdaSubscribeAttributeRequest(itemId);
                    m_requests.Add(itemId, requests);
                }

                return(requests);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Deletes subscription requests for monitored items.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="monitoredItems">The monitored items.</param>
        public void DeleteItems(ServerSystemContext context, IList <IMonitoredItem> monitoredItems)
        {
            lock (m_lock)
            {
                if (monitoredItems != null)
                {
                    Dictionary <string, HdaSubscribeAttributeRequest> requests = new Dictionary <string, HdaSubscribeAttributeRequest>();

                    for (int ii = 0; ii < monitoredItems.Count; ii++)
                    {
                        MonitoredItem monitoredItem = monitoredItems[ii] as MonitoredItem;

                        if (monitoredItem == null)
                        {
                            continue;
                        }

                        NodeHandle handle = monitoredItem.ManagerHandle as NodeHandle;

                        if (handle == null)
                        {
                            continue;
                        }

                        if (!HasExternalSource(handle.Node, monitoredItem.AttributeId))
                        {
                            continue;
                        }

                        HdaSubscribeAttributeRequest request = Remove(handle.Node, monitoredItem);

                        if (request != null)
                        {
                            requests[request.ItemId] = request;
                        }
                    }

                    ApplyChanges(requests, false);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Modifies the monitored item to the collection.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="monitoredItem">The monitored item.</param>
        private HdaSubscribeAttributeRequest Modify(NodeState source, MonitoredItem monitoredItem)
        {
            lock (m_lock)
            {
                // get the HDA item id.
                string itemId = GetItemId(source);

                // update the subscribe request for the HDA item.
                if (itemId != null)
                {
                    HdaSubscribeAttributeRequest request = FindAttributeRequest(itemId, false);

                    if (request != null)
                    {
                        request.Modify(m_context, monitoredItem);
                        return(request);
                    }
                }

                return(null);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Adds the monitored item to the collection.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="monitoredItem">The monitored item.</param>
        private HdaSubscribeAttributeRequest Add(NodeState source, MonitoredItem monitoredItem)
        {
            lock (m_lock)
            {
                m_monitoredItems.Add(monitoredItem.Id, monitoredItem);

                // get the HDA item id.
                string itemId = GetItemId(source);

                // create/update the subscribe request for the HDA item.
                if (itemId != null)
                {
                    HdaSubscribeAttributeRequest request = FindAttributeRequest(itemId, true);

                    if (request != null)
                    {
                        request.Add(m_context, monitoredItem);
                        return(request);
                    }
                }

                return(null);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Applies any changes to the subscriptions.
        /// </summary>
        private void ApplyChanges(Dictionary <string, HdaSubscribeAttributeRequest> requests, bool initialUpdateRequired)
        {
            List <HdaSubscribeAttributeRequest> requestsToRemove = null;

            lock (m_lock)
            {
                // update existing requests.
                foreach (KeyValuePair <string, HdaSubscribeAttributeRequest> entry in requests)
                {
                    HdaSubscribeAttributeRequest request = entry.Value;

                    // remove unused requests.
                    if (request.MonitoredItems == null || request.MonitoredItems.Count == 0)
                    {
                        if (requestsToRemove == null)
                        {
                            requestsToRemove = new List <HdaSubscribeAttributeRequest>();
                        }

                        requestsToRemove.Add(entry.Value);
                        continue;
                    }

                    request.ChangesComplete(m_configuration.AttributeSamplingInterval);
                }

                // remove deleted requests from dictionary.
                if (requestsToRemove != null)
                {
                    for (int ii = 0; ii < requestsToRemove.Count; ii++)
                    {
                        m_requests.Remove(requestsToRemove[ii].ItemId);
                    }
                }

                // check if the attribute scanner needs to be stopped/started.
                if (m_attributeScanTimer == null)
                {
                    if (m_requests.Count > 0)
                    {
                        m_attributeScanTimer = new Timer(OnScanAttributes, null, m_configuration.AttributeSamplingInterval, m_configuration.AttributeSamplingInterval);
                    }
                }
                else
                {
                    if (m_requests.Count == 0)
                    {
                        m_attributeScanTimer.Dispose();
                        m_attributeScanTimer = null;
                    }
                }
            }

            // release any unused handles.
            if (requestsToRemove != null)
            {
                ComHdaClientManager system = (ComHdaClientManager)m_context.SystemHandle;
                ComHdaClient        client = (ComHdaClient)system.SelectClient(m_context, false);
                ReleaseHandles(client, requestsToRemove);
            }

            // send initial update.
            if (initialUpdateRequired)
            {
                DoScan(requests, true);
            }
        }
        /// <summary>
        /// Finds the item attributes request for the specifed item. 
        /// </summary>
        /// <param name="itemId">The item id.</param>
        /// <param name="create">if set to <c>true</c> a new request is created if it does not exist.</param>
        /// <returns>The subscribe attribute request.</returns>
        private HdaSubscribeAttributeRequest FindAttributeRequest(string itemId, bool create)
        {
            lock (m_lock)
            {
                HdaSubscribeAttributeRequest requests = null;

                if (!m_requests.TryGetValue(itemId, out requests))
                {
                    if (!create)
                    {
                        return null;
                    }

                    requests = new HdaSubscribeAttributeRequest(itemId);
                    m_requests.Add(itemId, requests);
                }

                return requests;
            }
        }