示例#1
0
        public void Register(
            UOSEventListener listener,
            UpDevice device,
            string driver,
            string instanceId = null,
            string eventKey   = null,
            IDictionary <string, object> parameters = null)
        {
            // If the listener is already registered it cannot be registered again
            string eventIdentifier = GetEventIdentifier(device, driver, instanceId, eventKey);

            logger.Log("Registering listener for event :" + eventIdentifier);

            List <ListenerInfo> list;

            if (!listenerMap.TryGetValue(eventIdentifier, out list))
            {
                list = null;
            }

            if (FindListener(listener, list) == null)
            {
                ListenerInfo info = new ListenerInfo();

                info.driver     = driver;
                info.instanceId = instanceId;
                info.eventKey   = eventKey;
                info.listener   = listener;
                info.device     = device;

                RegisterNewListener(device, parameters, eventIdentifier, info);
            }
        }
示例#2
0
        /// <summary>
        /// Removes a listener for receiving Notify events and notifies the event driver of its removal.
        /// </summary>
        /// <param name="listener"></param>
        /// <param name="device"></param>
        /// <param name="driver"></param>
        /// <param name="instanceId"></param>
        /// <param name="eventKey"></param>
        public void Unregister(
            UOSEventListener listener,
            UpDevice device   = null,
            string driver     = null,
            string instanceId = null,
            string eventKey   = null)
        {
            List <ListenerInfo> listeners = FindListeners(device, driver, instanceId, eventKey);

            if (listeners == null)
            {
                return;
            }

            System.Exception e = null;
            foreach (var li in listeners)
            {
                // only if its the same listener, it should be removed
                if (li.listener.Equals(listener))
                {
                    bool remove = true;

                    // If the driver name is informed, and it's not the same, it must not be removed
                    if ((driver != null) && (li.driver != null))
                    {
                        remove = remove && li.driver.Equals(driver, System.StringComparison.InvariantCultureIgnoreCase);
                    }

                    // If the instanceId is informed, and it's not the same, it must not be removed
                    if ((instanceId != null) && (li.instanceId != null))
                    {
                        remove = remove && li.instanceId.Equals(instanceId, System.StringComparison.InvariantCultureIgnoreCase);
                    }

                    if (remove)
                    {
                        try
                        {
                            //Notify device of the listener removal
                            UnregisterForEvent(li);
                        }
                        catch (System.Exception ex)
                        {
                            string id = GetEventIdentifier(device, driver, instanceId, eventKey);
                            logger.LogError("Failed to unregister for event " + id + ": " + e.Message);
                            e = ex;
                        }
                    }
                }
            }

            if (e != null)
            {
                throw e;
            }
        }
示例#3
0
        private ListenerInfo?FindListener(UOSEventListener listener, List <ListenerInfo> list)
        {
            if ((list != null) && (listener != null))
            {
                int pos = list.FindIndex(i => i.listener.Equals(listener));
                if (pos >= 0)
                {
                    return(list[pos]);
                }
            }

            return(null);
        }
示例#4
0
        private ListenerInfo? FindListener(UOSEventListener listener, List<ListenerInfo> list)
        {
            if ((list != null) && (listener != null))
            {
                int pos = list.FindIndex(i => i.listener.Equals(listener));
                if (pos >= 0)
                    return list[pos];
            }

            return null;
        }
示例#5
0
        /// <summary>
        /// Removes a listener for receiving Notify events and notifies the event driver of its removal.
        /// </summary>
        /// <param name="listener"></param>
        /// <param name="device"></param>
        /// <param name="driver"></param>
        /// <param name="instanceId"></param>
        /// <param name="eventKey"></param>
        public void Unregister(
            UOSEventListener listener,
            UpDevice device = null,
            string driver = null,
            string instanceId = null,
            string eventKey = null)
        {
            List<ListenerInfo> listeners = FindListeners(device, driver, instanceId, eventKey);
            if (listeners == null)
                return;

            System.Exception e = null;
            foreach (var li in listeners)
            {
                // only if its the same listener, it should be removed
                if (li.listener.Equals(listener))
                {
                    bool remove = true;

                    // If the driver name is informed, and it's not the same, it must not be removed
                    if ((driver != null) && (li.driver != null))
                        remove = remove && li.driver.Equals(driver, System.StringComparison.InvariantCultureIgnoreCase);

                    // If the instanceId is informed, and it's not the same, it must not be removed
                    if ((instanceId != null) && (li.instanceId != null))
                        remove = remove && li.instanceId.Equals(instanceId, System.StringComparison.InvariantCultureIgnoreCase);

                    if (remove)
                    {
                        try
                        {
                            //Notify device of the listener removal
                            UnregisterForEvent(li);

                        }
                        catch (System.Exception ex)
                        {
                            string id = GetEventIdentifier(device, driver, instanceId, eventKey);
                            logger.LogError("Failed to unregister for event " + id + ": " + e.Message);
                            e = ex;
                        }
                    }
                }
            }

            if (e != null)
                throw e;
        }
示例#6
0
        public void Register(
            UOSEventListener listener,
            UpDevice device,
            string driver,
            string instanceId = null,
            string eventKey = null,
            IDictionary<string, object> parameters = null)
        {
            // If the listener is already registered it cannot be registered again
            string eventIdentifier = GetEventIdentifier(device, driver, instanceId, eventKey);
            logger.Log("Registering listener for event :" + eventIdentifier);

            List<ListenerInfo> list;
            if (!listenerMap.TryGetValue(eventIdentifier, out list))
                list = null;

            if (FindListener(listener, list) == null)
            {
                ListenerInfo info = new ListenerInfo();

                info.driver = driver;
                info.instanceId = instanceId;
                info.eventKey = eventKey;
                info.listener = listener;
                info.device = device;

                RegisterNewListener(device, parameters, eventIdentifier, info);
            }
        }