Example #1
0
        /// <summary>
        /// Creates a new long running task to listen for events
        /// </summary>
        /// <param name="path">Relative path to Particle Cloud event endpoint</param>
        /// <param name="eventHandler">ParticleEventHandler to call when new event arrives</param>
        /// <returns>Returns GUID reference to long running event task</returns>
        private async Task <Guid> SubscribeToEventAsync(string path, ParticleEventHandler eventHandler)
        {
            var  guid     = Guid.NewGuid();
            bool newEvent = false;

            if (particleEventGroups == null)
            {
                particleEventGroups       = new Dictionary <string, ParticleEventGroup>();
                particleEventHandlerDatas = new Dictionary <Guid, ParticleEventHandlerData>();
            }

            if (!particleEventGroups.ContainsKey(path))
            {
                particleEventGroups.Add(path, new ParticleEventGroup());
                newEvent = true;
            }

            particleEventHandlerDatas.Add(guid, new ParticleEventHandlerData(path, eventHandler));

            var particleEventGroup = particleEventGroups[path];

            particleEventGroup.AddHandler(eventHandler);

            if (newEvent)
            {
                await Task.Factory.StartNew(() => ListenForEventAsync(path, particleEventGroup).ConfigureAwait(false), TaskCreationOptions.LongRunning);
            }

            return(guid);
        }
Example #2
0
 /** Copy attributes to another event handler.
  */
 public void CopyAttributesTo(ParticleEventHandler eventHandler)
 {
     if (eventHandler == null)
     {
         throw new ArgumentNullException("eventHandler cannot be null!");
     }
     DoAffectorEventHandler_CopyAttributesTo(nativePtr, eventHandler.nativePtr);
 }
Example #3
0
 /// <summary>
 /// Creates a new long running task to listen for events on users devices
 /// </summary>
 /// <param name="eventHandler">ParticleEventHandler to call when new event arrives</param>
 /// <param name="eventNamePrefix">Prefix to monitor on event stream</param>
 /// <returns>Returns GUID reference to long running event task</returns>
 public async Task <Guid> SubscribeToDevicesEventsWithPrefixAsync(ParticleEventHandler eventHandler, string eventNamePrefix = "")
 {
     if (string.IsNullOrWhiteSpace(eventNamePrefix))
     {
         return(await SubscribeToEventAsync($"{ParticleApiVersion}/{ParticleApiPathDevicesEvents}", eventHandler));
     }
     else
     {
         return(await SubscribeToEventAsync($"{ParticleApiVersion}/{ParticleApiPathDevicesEvents}/{eventNamePrefix}", eventHandler));
     }
 }
Example #4
0
        async Task <Guid> subscribeToEventWithUrlAsync(string url, ParticleEventHandler handler, string eventNamePrefix)
        {
            var guid   = Guid.NewGuid();
            var source = new EventSource(url, ParticleCloud.AccessToken.Token, eventNamePrefix);

            source.AddEventListener(guid.ToString(), handler);

            await Task.Factory.StartNew(() => source.StartHandlingEvents().ConfigureAwait(false), TaskCreationOptions.LongRunning);

            ParticleCloud.SharedInstance.SubscibedEvents.Add(guid, source);
            return(guid);
        }
Example #5
0
 public void AddEventListener(string eventName, ParticleEventHandler handler)
 {
     if (!Listeners.ContainsKey(eventName))
     {
         Listeners.Add(eventName, handler);
         OnMessage += handler;
     }
     else
     {
         throw new Exception("Event Name is present in dictionary. Consider using a GUID to ensure a unique number is generated");
     }
 }
Example #6
0
 /// <summary>
 /// Creates a new long running task to listen for events on a specific users device
 /// </summary>
 /// <param name="eventHandler">ParticleEventHandler to call when new event arrives</param>
 /// <param name="device">ParticleDevice to limit event stream to</param>
 /// <param name="eventNamePrefix">Prefix to monitor on event stream</param>
 /// <returns>Returns GUID reference to long running event task</returns>
 public async Task <Guid> SubscribeToDeviceEventsWithPrefixAsync(ParticleEventHandler eventHandler, ParticleDevice device, string eventNamePrefix = "")
 {
     return(await SubscribeToDeviceEventsWithPrefixAsync(eventHandler, device.Id, eventNamePrefix));
 }
Example #7
0
        /// <summary>
        /// Subscribes to events with a given prefix.
        /// </summary>
        /// <returns>A Guid that uniquely identifies the subscription</returns>
        /// <param name="eventNamePrefix">Event name prefix.</param>
        /// <param name="handler">ParticleEventHandler to invoke as events are received</param>
        public async Task <Guid> SubscribeToEventsWithPrefixAsync(string eventNamePrefix, ParticleEventHandler handler)
        {
            string endpoint;

            if (IsNullOrEmpty(eventNamePrefix))
            {
                endpoint = $"https://api.particle.io/v1/devices/{Id}/events/";
            }
            else
            {
                endpoint = $"https://api.particle.io/v1/devices/{Id}/events/{eventNamePrefix}";
            }

            var eventListenerId = await subscribeToEventWithUrlAsync(endpoint, handler, eventNamePrefix);

            System.Diagnostics.Debug.WriteLine(endpoint);
            return(eventListenerId);
        }
 /// <summary>
 /// Creates a new long running task to listen for events on this specific device
 /// </summary>
 /// <param name="eventHandler">ParticleEventHandler to call when new event arrives</param>
 /// <param name="eventNamePrefix">Prefix to monitor on event stream</param>
 /// <returns>Returns GUID reference to long running event task</returns>
 public async Task <Guid> SubscribeToDeviceEventsWithPrefixAsync(ParticleEventHandler eventHandler, string eventNamePrefix = "")
 {
     return(await particleCloud.SubscribeToDeviceEventsWithPrefixAsync(eventHandler, this, eventNamePrefix));
 }
Example #9
0
        /// <summary>
        /// Subscribes to events with a given prefix.
        /// </summary>
        /// <returns>A Guid that uniquely identifies the subscription</returns>
        /// <param name="eventNamePrefix">Event name prefix.</param>
        /// <param name="deviceID">Device identifier.</param>
        /// <param name="handler">ParticleEventHandler to invoke as events are received</param>
        public async Task <Guid> SubscribeToMyDevicesEventsWithPrefixAsync(string eventNamePrefix, string deviceID, ParticleEventHandler handler)       //add event handler
        {
            string endpoint;

            if (IsNullOrEmpty(eventNamePrefix))
            {
                endpoint = $"https://api.particle.io/v1/devices/{deviceID}/events/";
            }
            else
            {
                endpoint = $"https://api.particle.io/v1/devices/events/{eventNamePrefix}";
            }

            var eventListenerId = await subscribeToEventWithUrlAsync(endpoint, handler, eventNamePrefix);

            return(eventListenerId);
        }
Example #10
0
 /// <summary>
 /// Remove ParticleEventHandler
 /// </summary>
 /// <param name="eventHandler">ParticleEventHandler to remove</param>
 public void RemoveHandler(ParticleEventHandler eventHandler)
 {
     OnMessage -= eventHandler;
 }
Example #11
0
 /// <summary>
 /// Add new ParticleEventHandler
 /// </summary>
 /// <param name="eventHandler">ParticleEventHandler to add</param>
 public void AddHandler(ParticleEventHandler eventHandler)
 {
     OnMessage += eventHandler;
 }
Example #12
0
 public ParticleEventHandlerData(string path, ParticleEventHandler eventHandler)
 {
     Path         = path;
     EventHandler = eventHandler;
 }