private void Client_DeviceDiscovered(object sender, LifxNet.LifxClient.DeviceDiscoveryEventArgs e) { var _ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { var bulb = e.Device as LifxNet.LightBulb; if (!bulbs.Contains(bulb)) bulbs.Add(bulb); }); }
private async void SetColor(LifxNet.LightBulb bulb, ushort? hue, ushort? saturation, ushort? brightness) { if (client == null || bulb == null) return; //Is a task already running? This avoids updating too often. //Come back and execute last call when currently running operation is complete if (pendingUpdateColor != null) { pendingUpdateColorAction = () => SetColor(bulb, hue, saturation, brightness); return; } this.hue = hue.HasValue ? hue.Value : this.hue; this.saturation = saturation.HasValue ? saturation.Value : this.saturation; var b = brightness.HasValue ? brightness.Value : (UInt16)brightnessSlider.Value; var setColorTask = client.SetColorAsync(bulb, this.hue, this.saturation, b, 2700, TimeSpan.Zero); var throttleTask = Task.Delay(50); //Ensure task takes minimum 50 ms (no more than 20 messages per second) pendingUpdateColor = Task.WhenAll(new Task[] { setColorTask, throttleTask }); try { Task timeoutTask = Task.Delay(2000); await Task.WhenAny(new Task[] { timeoutTask, pendingUpdateColor }); if(!pendingUpdateColor.IsCompleted) { //timeout } } catch { } //ignore errors (usually timeout) pendingUpdateColor = null; if (pendingUpdateColorAction != null) //if a pending action is waiting, run it now; { var a = pendingUpdateColorAction; pendingUpdateColorAction = null; a(); } }