public uint TransitionLampState(ulong Timestamp, BridgeRT.State NewState, uint TransitionPeriod, out uint LampResponseCode) { var command = new LightCommand(); command.TransitionTime = TimeSpan.FromMilliseconds(TransitionPeriod); command.On = NewState.IsOn; if (NewState.Brightness.HasValue && LampDetails_Dimmable) { command.Brightness = (byte)(NewState.Brightness.Value * 254d / UInt32Max); } if (NewState.Hue.HasValue && LampDetails_Color) { command.Hue = (int)(NewState.Hue.Value * 65535d / UInt32Max); } if (NewState.Saturation.HasValue && LampDetails_Color) { command.Saturation = (int)(NewState.Saturation.Value * 254d / UInt32Max); } if (NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp) { double kelvin = NewState.ColorTemp.Value * 19000d / UInt32Max + 1000; int kelvinLimited = (int)Math.Max(LampDetails_MinTemperature, Math.Min(LampDetails_MaxTemperature, kelvin)); int mired = (int)(1000000d / kelvinLimited); //Currently hue doesn't like setting color temp if the other parameters are also set. //Skipping for now. //command.ColorTemperature = mired; } LampResponseCode = 0; System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(Timestamp)).ContinueWith(_ => { _client.SendCommandAsync(command, new[] { _light.Id }); //Update state if (command.Hue.HasValue) { _light.State.Hue = command.Hue.Value; } if (command.Brightness.HasValue) { _light.State.Brightness = command.Brightness.Value; } if (command.Saturation.HasValue) { _light.State.Saturation = command.Saturation.Value; } if (command.ColorTemperature.HasValue) { _light.State.ColorTemperature = command.ColorTemperature.Value; } if (command.On.HasValue) { _light.State.On = command.On.Value; } }); return(0); //TODO }
protected async void Bla(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines) { var bridges = await new Q42.HueApi.HttpBridgeLocator().LocateBridgesAsync(TimeSpan.FromMinutes(1)); var bridge = bridges.Single(); var client = new HueClient(bridge, "frUwREPr3m"); var lights = await client.GetLightsAsync(); var sorted = lights.OrderBy(l => l.Id); var random = new Random(); var duration = TimeSpan.FromSeconds(random.Next(10, 25)); var start = DateTime.Now; var end = start + duration; var offCommand = new LightCommand(); offCommand.On = false; offCommand.TransitionTime = TimeSpan.Zero; await client.SendCommandAsync(offCommand); Q42.HueApi.Light previousLight = null; while (DateTime.Now < end) { foreach (var light in sorted) { if (DateTime.Now >= end) { break; } if (previousLight != null) { var offCommand1 = new LightCommand(); offCommand1.On = false; offCommand1.TransitionTime = TimeSpan.FromTicks(1); client.SendCommandAsync(offCommand1, new[] { previousLight.Id }); } var command = new LightCommand(); command.Hue = 65280; command.Saturation = 255; command.Brightness = 255; command.On = true; command.TransitionTime = TimeSpan.FromTicks(1); await client.SendCommandAsync(command, new[] { light.Id }); previousLight = light; } } var c = new LightCommand(); c.Hue = 25500; c.Saturation = 255; c.Brightness = 255; c.On = true; c.TransitionTime = TimeSpan.FromTicks(1); await client.SendCommandAsync(c, new[] { previousLight.Id }); base.ApplicationStartup(container, pipelines); }