Example #1
0
        public static async Task <List <DiscoveryEndpoint> > GetEndpointsAsync()
        {
            using (endpointsLock.Lock())
            {
                if (endpoints.Count != 0)
                {
                    endpoints.Clear();
                }
                // discovery json is now generated in Premise vb script to vastly improve discovery
                // event response time
                var     returnClause = new[] { "discoveryJson", "IsDiscoverable" };
                dynamic whereClause  = new ExpandoObject();
                whereClause.TypeOf = AlexaApplianceClassPath;
                int count = 0;
                // ReSharper disable once AsyncConverter.ConfigureAwaitHighlighting
                var devices = await HomeObject.SelectAsync(returnClause, whereClause).ConfigureAwait(false);

                foreach (var device in devices)
                {
                    if (device.IsDiscoverable == false)
                    {
                        continue;
                    }

                    DiscoveryEndpoint endpoint;
                    try
                    {
                        string json = device.discoveryJson.ToString();
                        if (string.IsNullOrEmpty(json))
                        {
                            continue;
                        }
                        endpoint = JsonConvert.DeserializeObject <DiscoveryEndpoint>(json, new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Ignore
                        });
                    }
                    catch
                    {
                        continue;
                    }

                    if (endpoint != null)
                    {
                        endpoints.Add(endpoint);
                        if (++count >= AlexaDeviceLimit)
                        {
                            break;
                        }
                    }
                }

                // Seems like a good idea here.
                HostingEnvironment.QueueBackgroundWorkItem(ct => BackgroundGarbageCollectAsync());

                return(endpoints);
            }
        }