Example #1
0
 /// <summary>
 /// Delete/remove registered webhook.
 /// </summary>
 /// <example>
 /// <code>
 /// try
 /// {
 ///     connectApi.DeleteWebhook();
 /// }
 /// catch (CloudApiException)
 /// {
 ///     throw;
 /// }
 /// </code>
 /// </example>
 /// <exception cref="CloudApiException">CloudApiException</exception>
 public void DeleteWebhook()
 {
     try
     {
         NotificationsApi.DeregisterWebhook();
         ResourceSubscribtions.Clear();
     }
     catch (mds.Client.ApiException ex)
     {
         throw new CloudApiException(ex.ErrorCode, ex.Message, ex.ErrorContent);
     }
 }
 /// <summary>
 /// Unsubscribe from device and/or resource_path updates.
 /// </summary>
 /// <param name="deviceId">device to unsubscribe events from.</param>
 /// <param name="resourcePath">resource_path to unsubscribe events from.</param>
 /// <example>
 /// <code>
 /// try
 /// {
 ///     connectApi.DeleteResourceSubscription("015bb66a92a30000000000010010006d", "3200/0/5500");
 /// }
 /// catch (CloudApiException)
 /// {
 ///     throw;
 /// }
 /// </code>
 /// </example>
 /// <exception cref="CloudApiException">CloudApiException</exception>
 public void DeleteResourceSubscription(string deviceId, string resourcePath)
 {
     try
     {
         var fixedPath = RemoveLeadingSlash(resourcePath);
         SubscriptionsApi.DeleteResourceSubscription(deviceId, fixedPath);
         var subscribePath = deviceId + resourcePath;
         ResourceSubscribtions.TryRemove(subscribePath, out var removedItem);
     }
     catch (mds.Client.ApiException ex)
     {
         throw new CloudApiException(ex.ErrorCode, ex.Message, ex.ErrorContent);
     }
 }
 /// <summary>
 /// Removes a device's subscriptions
 /// </summary>
 /// <param name="deviceId">Device Id</param>
 /// <example>
 /// <code>
 /// try
 /// {
 ///     connectApi.DeleteDeviceSubscriptions("015bb66a92a30000000000010010006d");
 /// }
 /// catch (CloudApiException)
 /// {
 ///     throw;
 /// }
 /// </code>
 /// </example>
 /// <exception cref="CloudApiException">CloudApiException</exception>
 public void DeleteDeviceSubscriptions(string deviceId)
 {
     try
     {
         SubscriptionsApi.DeleteEndpointSubscriptions(deviceId);
         ResourceSubscribtions.Keys
         .Where(k => k.Contains(deviceId))
         .ToList()
         .ForEach(d => ResourceSubscribtions.TryRemove(d, out var removedItem));
     }
     catch (mds.Client.ApiException e) when(e.ErrorCode != 404)
     {
         throw new CloudApiException(e.ErrorCode, e.Message, e.ErrorContent);
     }
 }
        /// <summary>
        /// Subscribe to resource updates.
        /// </summary>
        /// <param name="deviceId">Id of device.</param>
        /// <param name="resourcePath">Resource path.</param>
        /// <returns>Async Consumer with String</returns>
        /// <example>
        /// <code>
        /// var consumer = await api.AddResourceSubscriptionAsync("015bb66a92a30000000000010010006d", "3200/0/5500");
        /// var counter = 0;
        /// while (true)
        /// {
        ///     var t = consumer.GetValue();
        ///     Console.WriteLine(t.Result);
        ///     counter++;
        ///     if (counter >= 2)
        ///     {
        ///     break;
        ///     }
        /// }
        /// </code>
        /// </example>
        /// <exception cref="CloudApiException">CloudApiException</exception>
        public async Task <Resource> AddResourceSubscriptionAsync(string deviceId, string resourcePath)
        {
            Log.Info($"adding subscription for {deviceId} at path {resourcePath}");
            try
            {
                await StartNotificationsAsync();

                var fixedPath = RemoveLeadingSlash(resourcePath);
                await SubscriptionsApi.AddResourceSubscriptionAsync(deviceId, fixedPath);

                var subscribePath = deviceId + resourcePath;
                var resource      = new Resource(deviceId, this);
                ResourceSubscribtions.AddOrUpdate(subscribePath, resource, (key, _) => resource);

                return(resource);
            }
            catch (mds.Client.ApiException ex)
            {
                throw new CloudApiException(ex.ErrorCode, ex.Message, ex.ErrorContent);
            }
        }
        /// <summary>
        /// Remove all subscriptions
        /// </summary>
        /// <remarks>
        /// Warning: This could be slow for large numbers of connected devices. If possible, explicitly delete subscriptions known to have been created.
        /// </remarks>
        /// <example>
        /// <code>
        /// try
        /// {
        ///     connectApi.DeleteSubscriptions();
        /// }
        /// catch (CloudApiException)
        /// {
        ///     throw;
        /// }
        /// </code>
        /// </example>
        /// <exception cref="CloudApiException">CloudApiException</exception>
        public void DeleteSubscriptions()
        {
            try
            {
                foreach (var item in ListConnectedDevices())
                {
                    try
                    {
                        DeleteDeviceSubscriptions(item.Id);
                    }
                    catch (CloudApiException)
                    {
                        Log.Debug("no subscriptions found for this device");
                    }
                }

                ResourceSubscribtions.Clear();
            }
            catch (mds.Client.ApiException e)
            {
                throw new CloudApiException(e.ErrorCode, e.Message, e.ErrorContent);
            }
        }