michielpost / Q42.HueApi

C# helper library to talk to the Philips Hue bridge
MIT License
411 stars 114 forks source link

Make "sequence" of colors? #91

Closed jaytonic closed 7 years ago

jaytonic commented 7 years ago

Hi,

For some tests I wanted to simulated some cops lights, the same way "http://ambieye.com/" is doing:

Blinking 2 fast time in red, very small pause, two blink in blue. I think there is no way to give a sequence so basically I have to make a command per blink.

Currently I've done this:

private static void SendSomeCommands(ILocalHueClient client)
{
    LightCommand redCommand = new LightCommand();
    LightCommand blueCommand = new LightCommand();
    blueCommand.SetColor(new RGBColor(0.0, 0.0, 1.0));
    blueCommand.Alert = Alert.None;
    blueCommand.TransitionTime = TimeSpan.FromTicks(0);
    blueCommand.Brightness=Byte.MaxValue;

    redCommand.SetColor(new RGBColor(1.0, 0.0, 0.0));
    redCommand.Alert = Alert.None;
    redCommand.TransitionTime = TimeSpan.FromTicks(0);
    redCommand.Brightness = Byte.MaxValue;

    _logger.Info("Starting blinking");
    for (int i = 0; i < 1000; i++)
    {
        Task<Task<HueResults>> task = client.SendCommandAsync(redCommand.TurnOn())
            .ContinueWith(t => Thread.Sleep(50))
            .ContinueWith(t => client.SendCommandAsync(redCommand.TurnOff()))
            .ContinueWith(t => Thread.Sleep(50))
            .ContinueWith(t => client.SendCommandAsync(redCommand.TurnOn()))
            .ContinueWith(t => Thread.Sleep(50))
            .ContinueWith(t => client.SendCommandAsync(redCommand.TurnOff()))
            .ContinueWith(t => Thread.Sleep(100))
            .ContinueWith(t => client.SendCommandAsync(blueCommand.TurnOn()))
            .ContinueWith(t => Thread.Sleep(50))
            .ContinueWith(t => client.SendCommandAsync(blueCommand.TurnOff()))
            .ContinueWith(t => Thread.Sleep(50))
            .ContinueWith(t => client.SendCommandAsync(blueCommand.TurnOn()))
            .ContinueWith(t => Thread.Sleep(50))
            .ContinueWith(t => client.SendCommandAsync(blueCommand.TurnOff()));

        task.Wait();
    }
}

It works but the blinks is very slow, it stay blue/red for severals seconds before going to the next step.

By checking the ambieye website, they are just "spamming" commands, and I'm not sure where this "delay" comes from.

Is there some parameter that I should change

michielpost commented 7 years ago

You can use a tool like Fiddler to see the traffic to the bridge. I tested your code and if floods the bridge with messages. The bridge can't handle them all and runs out of processing power to send anything useful to the lights.

I modified your code a bit to something that works a bit better:

        LightCommand offCommand = new LightCommand().TurnOff();
        offCommand.TransitionTime = TimeSpan.FromTicks(0);

        LightCommand redCommand = new LightCommand().TurnOn();
        LightCommand blueCommand = new LightCommand().TurnOn();
        blueCommand.SetColor(new RGBColor(0.0, 0.0, 1.0));
        blueCommand.TransitionTime = TimeSpan.FromTicks(0);
        blueCommand.Brightness = Byte.MaxValue;

        redCommand.SetColor(new RGBColor(1.0, 0.0, 0.0));
        redCommand.TransitionTime = TimeSpan.FromTicks(0);
        redCommand.Brightness = Byte.MaxValue;

        for (int i = 0; i < 1000; i++)
        {
            await client.SendCommandAsync(redCommand);
            await Task.Delay(300);
            await client.SendCommandAsync(offCommand);
            await Task.Delay(300);
            await client.SendCommandAsync(redCommand);
            await Task.Delay(300);
            await client.SendCommandAsync(offCommand);
            await Task.Delay(300);
            await client.SendCommandAsync(blueCommand);
            await Task.Delay(300);
            await client.SendCommandAsync(offCommand);
            await Task.Delay(300);
            await client.SendCommandAsync(blueCommand);
            await Task.Delay(300);
            await client.SendCommandAsync(offCommand);
            await Task.Delay(300);
        }

After each command, wait some time for the next command. Send lightweight commands. When turning a light off, the color, brightness etc don't matter. Setting the Alert to Off each time is also not needed.

You can play with the delay time and see how your Bridge handles it.