GetLightsAsync() public method

Gets all lights known to the bridge.
public GetLightsAsync ( ) : Task>
return Task>
コード例 #1
0
        /// <summary>
        /// The entry point of a background task.
        /// </summary>
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            backgroundTaskInstance = taskInstance;
            var details = taskInstance.TriggerDetails as BluetoothLEAdvertisementWatcherTriggerDetails;
            if (details != null)
            {
                _deferral = backgroundTaskInstance.GetDeferral();
                taskInstance.Canceled += (s, e) => _deferral.Complete();

                var localStorage = ApplicationData.Current.LocalSettings.Values;
                _bridge = new Bridge(localStorage["bridgeIp"].ToString(), localStorage["userId"].ToString()); 
                try
                {
                    _lights = await _bridge.GetLightsAsync();
                }
                catch (Exception)
                {
                    _deferral.Complete();
                    return; 
                }
                foreach(var item in details.Advertisements)
                {
                    Debug.WriteLine(item.RawSignalStrengthInDBm);
                }

                // -127 is a BTLE magic number that indicates out of range. If we hit this, 
                // turn off the lights. Send the command regardless if they are on/off
                // just to be safe, since it will only be sent once.
                if (details.Advertisements.Any(x => x.RawSignalStrengthInDBm == -127))
                {
                    foreach (Light light in _lights)
                    {
                        light.State.On = false;
                        await Task.Delay(250);
                    }
                }
                // If there is no magic number, we are in range. Toggle any lights reporting
                // as off to on. Do not spam the command to lights arleady on. 
                else
                {
                    foreach (Light light in _lights.Where(x => !x.State.On))
                    {
                        light.State.On = true;
                        await Task.Delay(250);
                    }
                }
                // Wait 1 second before exiting to ensure all HTTP requests have sent.
                await Task.Delay(1000);
                _deferral.Complete();
            }
        }
コード例 #2
0
        /// <summary>
        /// Helper method for initalizing the voice service, bridge, and lights. Returns if successful. 
        /// </summary>
        private async Task<bool> InitializeAsync(AppServiceTriggerDetails triggerDetails)
        {
            _voiceServiceConnection = 
                VoiceCommandServiceConnection.FromAppServiceTriggerDetails(triggerDetails);
            _voiceServiceConnection.VoiceCommandCompleted += (s, e) => _deferral.Complete();

            _voiceCommand = await _voiceServiceConnection.GetVoiceCommandAsync();
            _colors = HsbColor.CreateAll().ToDictionary(x => x.Name);

            var localStorage = ApplicationData.Current.LocalSettings.Values;
            _bridge = new Bridge(
                localStorage["bridgeIp"].ToString(), localStorage["userId"].ToString());
            try
            {
                _lights = await _bridge.GetLightsAsync();
            }
            catch (Exception)
            {
                var response = CreateCortanaResponse("Sorry, I couldn't connect to your bridge.");
                await _voiceServiceConnection.ReportFailureAsync(response);
                return false;
            }
            if (!_lights.Any())
            {
                var response = CreateCortanaResponse("Sorry, I couldn't find any lights.");
                await _voiceServiceConnection.ReportFailureAsync(response);
                return false;
            }
            return true;
        }