public async void Init(Label labelHint) { client = new StreamingHueClient(ip, key, entertainmentKey); //Get the entertainment group IReadOnlyList <Group> all = null; try { all = await client.LocalHueClient.GetEntertainmentGroups(); } catch (Exception e) { labelHint.Text = "Hue init exception: " + e.Message; return; } var group = all.FirstOrDefault(); if (group == null) { //throw new Exception("No Entertainment Group found. Create one using the Q42.HueApi.UniversalWindows.Sample"); labelHint.Text = "No Entertainment Group found"; return; } else { labelHint.Text = $"Using Entertainment Group {group.Id}"; Console.WriteLine($"Using Entertainment Group {group.Id}"); } //Create a streaming group entGroup = new StreamingGroup(group.Locations); //Connect to the streaming group await client.Connect(group.Id); //Start auto updating this entertainment group client.AutoUpdate(entGroup, 50); //Optional: calculated effects that are placed in the room client.AutoCalculateEffectUpdate(entGroup); //Optional: Check if streaming is currently active var bridgeInfo = await client.LocalHueClient.GetBridgeAsync(); Console.WriteLine(bridgeInfo.IsStreamingActive ? "Streaming is active" : "Streaming is not active"); labelHint.Text = bridgeInfo.IsStreamingActive ? "Streaming is active" : "Streaming is not active"; //Order lights based on position in the room var orderedLeft = entGroup.GetLeft().OrderByDescending(x => x.LightLocation.Y).ThenBy(x => x.LightLocation.X); var orderedRight = entGroup.GetRight().OrderByDescending(x => x.LightLocation.Y).ThenByDescending(x => x.LightLocation.X); var allLightsOrdered = orderedLeft.Concat(orderedRight.Reverse()).ToArray(); var allLightsReverse = allLightsOrdered.ToList(); allLightsReverse.Reverse(); Random rnd = new Random(); hue = rnd.Next(0, 65535); entGroup.SetState(HueToRGB(hue, 1, 1), 1, TimeSpan.FromMilliseconds(0)); isInit = true; }
public async Task Start() { //string ip = "192.168.0.4"; //string key = "8JwWAj5J1tSsKLxyUOdAkWmcCQFcNc51AKRhxdH9"; //string entertainmentKey = "AFFD322C34C993C19503D369481869FD"; //var useSimulator = false; //string ip = "10.42.39.194"; //string key = "tocjq6GmPJ8KX5DyLDKXQreZE6txQVQ5oBqbYDFn"; //string entertainmentKey = "DB088F63639524B5A8CDC8AEEAC9C322"; //var useSimulator = false; string ip = "127.0.0.1"; string key = "aSimulatedUser"; string entertainmentKey = "01234567890123456789012345678901"; var useSimulator = true; //Initialize streaming client StreamingHueClient client = new StreamingHueClient(ip, key, entertainmentKey); //Get the entertainment group var all = await client.LocalHueClient.GetEntertainmentGroups(); var group = all.FirstOrDefault(); if (group == null) { throw new Exception("No Entertainment Group found. Create one using the Q42.HueApi.UniversalWindows.Sample"); } else { Console.WriteLine($"Using Entertainment Group {group.Id}"); } //Create a streaming group var entGroup = new StreamingGroup(group.Locations); entGroup.IsForSimulator = useSimulator; //Connect to the streaming group await client.Connect(group.Id, simulator : useSimulator); //Start auto updating this entertainment group client.AutoUpdate(entGroup, 50); //Optional: calculated effects that are placed in the room client.AutoCalculateEffectUpdate(entGroup); //Optional: Check if streaming is currently active var bridgeInfo = await client.LocalHueClient.GetBridgeAsync(); Console.WriteLine(bridgeInfo.IsStreamingActive ? "Streaming is active" : "Streaming is not active"); //Order lights based on position in the room var orderedLeft = entGroup.GetLeft().OrderByDescending(x => x.LightLocation.Y).ThenBy(x => x.LightLocation.X); var orderedRight = entGroup.GetRight().OrderByDescending(x => x.LightLocation.Y).ThenByDescending(x => x.LightLocation.X); var allLightsOrdered = orderedLeft.Concat(orderedRight.Reverse()).ToArray(); var allLightsReverse = allLightsOrdered.ToList(); allLightsReverse.Reverse(); CancellationTokenSource cst = new CancellationTokenSource(); Console.WriteLine("Random color on all lights"); entGroup.SetRandomColor(IteratorEffectMode.All, TimeSpan.FromMilliseconds(250), cancellationToken: cst.Token); cst = WaitCancelAndNext(cst); Console.WriteLine("Different random colors on all lights"); entGroup.SetRandomColor(IteratorEffectMode.AllIndividual, TimeSpan.FromMilliseconds(250), cancellationToken: cst.Token); cst = WaitCancelAndNext(cst); Console.WriteLine("Knight rider (works best with 6+ lights)"); allLightsOrdered.KnightRider(cancellationToken: cst.Token); cst = WaitCancelAndNext(cst); Ref <TimeSpan?> waitTime = TimeSpan.FromMilliseconds(750); Console.WriteLine("Flash lights (750ms), press enter to decrease by 200 ms"); allLightsOrdered.FlashQuick(new Q42.HueApi.ColorConverters.RGBColor("FFFFFF"), IteratorEffectMode.Cycle, waitTime: waitTime, cancellationToken: cst.Token); Console.ReadLine(); waitTime.Value -= TimeSpan.FromMilliseconds(200); Console.WriteLine($"Flash ({waitTime.Value.Value.TotalMilliseconds})"); Console.ReadLine(); waitTime.Value -= TimeSpan.FromMilliseconds(200); Console.WriteLine($"Flash ({waitTime.Value.Value.TotalMilliseconds})"); Console.ReadLine(); waitTime.Value -= TimeSpan.FromMilliseconds(200); Console.WriteLine($"Flash ({waitTime.Value.Value.TotalMilliseconds})"); Console.ReadLine(); waitTime.Value -= TimeSpan.FromMilliseconds(100); Console.WriteLine($"Flash ({waitTime.Value.Value.TotalMilliseconds})"); cst = WaitCancelAndNext(cst); Console.WriteLine("Flash on random lights"); allLightsOrdered.FlashQuick(new Q42.HueApi.ColorConverters.RGBColor("FFFFFF"), IteratorEffectMode.Random, waitTime: waitTime, cancellationToken: cst.Token); cst = WaitCancelAndNext(cst); Console.WriteLine("Flash on ALL lights"); waitTime.Value = TimeSpan.FromMilliseconds(150); allLightsOrdered.Flash(new Q42.HueApi.ColorConverters.RGBColor("FFFFFF"), IteratorEffectMode.All, waitTime: waitTime, cancellationToken: cst.Token); cst = WaitCancelAndNext(cst); Console.WriteLine("Flash effect with transition times"); entGroup.GetLeft().Flash(new Q42.HueApi.ColorConverters.RGBColor("FF0000"), IteratorEffectMode.All, waitTime: TimeSpan.FromSeconds(1), transitionTimeOn: TimeSpan.FromMilliseconds(1000), transitionTimeOff: TimeSpan.FromMilliseconds(1000), cancellationToken: cst.Token); await Task.Delay(2000); entGroup.GetRight().Flash(new Q42.HueApi.ColorConverters.RGBColor("FF0000"), IteratorEffectMode.All, waitTime: TimeSpan.FromSeconds(1), transitionTimeOn: TimeSpan.FromMilliseconds(1000), transitionTimeOff: TimeSpan.FromMilliseconds(1000), cancellationToken: cst.Token); cst = WaitCancelAndNext(cst); //Console.WriteLine("Or build your own effects"); //Task.Run(async () => //{ // while (true && !cst.Token.IsCancellationRequested) // { // entGroup.SetState(new RGBColor("0000FF"), 1, TimeSpan.FromSeconds(4), cancellationToken: cst.Token); // await Task.Delay(TimeSpan.FromSeconds(5)); // entGroup.SetState(new RGBColor("FF0000"), 0.6, TimeSpan.FromSeconds(4), cancellationToken: cst.Token); // await Task.Delay(TimeSpan.FromSeconds(5)); // } //}, cst.Token); //cst = WaitCancelAndNext(cst); Console.WriteLine("A red light that is moving in horizontal direction and is placed on an XY grid, matching your entertainment setup"); var redLightEffect = new RedLightEffect(); redLightEffect.Radius = 0.3; redLightEffect.Y = -0.8; redLightEffect.X = -0.8; entGroup.PlaceEffect(redLightEffect); redLightEffect.Start(); Task.Run(async() => { double step = 0.2; while (true) { redLightEffect.Y += step; await Task.Delay(100); if (redLightEffect.Y >= 2) { step = -0.1; } if (redLightEffect.Y <= -2) { step = +0.1; } } }, cst.Token); cst = WaitCancelAndNext(cst); redLightEffect.Stop(); Console.WriteLine("Thank you for using Q42.Hue.Streaming. This library was developed during Christmas 2017."); await allLightsOrdered.Christmas(cancellationToken : cst.Token); cst = WaitCancelAndNext(cst); Console.WriteLine("Press Enter to Exit"); Console.ReadLine(); }
public async Task Start() { /*IBridgeLocator locator = new HttpBridgeLocator(); * var locateBridges = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5)); * IEnumerable<LocatedBridge> bridgeIPs = locateBridges; * * if (bridgeIPs.Count() == 0) * { * throw new Exception("No bridges found."); * }*/ string ip = "192.168.50.3"; string appName = "LightingLink"; string deviceName = "WarMachine"; string key = "8pJuacIOcvBN9XA34hZWLSWTI0CQ9JYoif7SyEn9"; string entertainmentKey = "87E3D4B1CC91E68B44F4393951F838DA"; //--------Get a new key--------- //ILocalHueClient baseClient = new LocalHueClient(ip); //var appKey = await baseClient.RegisterAsync(appName, deviceName); //--------Get a new key and entertainment key--------- //var entKey = await LocalHueClient.RegisterAsync(ip, appName, deviceName, true); //Initialize Streaming Client StreamingHueClient client = new StreamingHueClient(ip, key, entertainmentKey); //var localClient = client.LocalHueClient; //await localClient.SendCommandAsync(new LightCommand().SetColor(new RGBColor(255, 255, 255)), new List<string> { "1" }); //Get the Entertainment Groups var all = await client.LocalHueClient.GetBridgeAsync(); var group = all.Groups.Where(x => x.Type == Q42.HueApi.Models.Groups.GroupType.Entertainment).FirstOrDefault(); if (group == null) { throw new Exception("No Entertainment Group found."); } else { Console.WriteLine($"Using Entertainment Group {group.Id}"); } //Create a streaming group entGroup = new StreamingGroup(group.Locations); //Connect to the streaming group await client.Connect(group.Id); //Start auto updating this entertainment group client.AutoUpdate(entGroup, 50); //Optional: calculated effects that are placed in the room client.AutoCalculateEffectUpdate(entGroup); //Optional: Check if streaming is currently active var bridgeInfo = await client.LocalHueClient.GetBridgeAsync(); Console.WriteLine(bridgeInfo.IsStreamingActive ? "Streaming is active" : "Streaming is not active"); //Order lights based on position in the room var orderedLeft = entGroup.GetLeft().OrderByDescending(x => x.LightLocation.Y).ThenBy(x => x.LightLocation.X); var orderedRight = entGroup.GetRight().OrderByDescending(x => x.LightLocation.Y).ThenByDescending(x => x.LightLocation.X); var allLightsOrdered = orderedLeft.Concat(orderedRight.Reverse()).ToArray(); var allLightsReverse = allLightsOrdered.ToList(); allLightsReverse.Reverse(); }