コード例 #1
0
ファイル: LightTests.cs プロジェクト: dutts/SharpHue
        public void SetLightFromColor()
        {
            Configuration.Initialize("36e02089265925772f085fcd3884ec9b");
            LightCollection lights = new LightCollection();

            LightStateBuilder b = new LightStateBuilder()
                .TurnOn()
                .Brightness(255)
                .ColorTemperature(150);

            lights[5].SetState(b);
        }
コード例 #2
0
ファイル: LightTests.cs プロジェクト: retorik/SharpHue
        public void SetLightFromColor()
        {
            Configuration.Initialize("36e02089265925772f085fcd3884ec9b");
            LightCollection lights = new LightCollection();

            LightStateBuilder b = new LightStateBuilder()
                                  .TurnOn()
                                  .Brightness(255)
                                  .ColorTemperature(150);

            lights[5].SetState(b);
        }
コード例 #3
0
        //public delegate void EventFired(HueEventStatus status);
        //public event EventFired OnEventFired;

        public void FireEvent(HueEvent @event)
        {
            try
            {
                Lights.Refresh();

                var oldLightStates = Lights.Select((t) => new
                {
                    on   = t.State.IsOn,
                    col  = t.State.HSBColor,
                    xy   = t.State.ColorSpaceCoordinates,
                    mode = t.State.CurrentColorMode,
                    key  = t.ID
                }).ToList();

                foreach (var stat in @event.States)
                {
                    if (stat.TimerType == TimerType.None)
                    {
                        var newstate = new LightStateBuilder()
                                       .Brightness(stat.Brightness)
                                       .Color(stat.Color)
                                       .TransitionTime(stat.Transition)
                                       .Turn(stat.TurnOn)
                                       .For(Lights[stat.LightKey]);

                        newstate.Apply();

                        //OnEventFired?.Invoke(stat);

                        if (stat.Duration > 0)
                        {
                            System.Threading.Thread.Sleep((int)stat.Duration);
                        }
                    }
                    else
                    {
                        var  watch    = new System.Diagnostics.Stopwatch();
                        bool switchOn = !Lights[stat.LightKey].State.IsOn;

                        if (stat.TimerType == TimerType.Color_Switch)
                        {
                            new LightStateBuilder()
                            .Turn(true)
                            .For(Lights[stat.LightKey])
                            .Apply();
                        }

                        new LightStateBuilder()
                        .TransitionTime(stat.Transition)
                        .For(Lights[stat.LightKey])
                        .Apply();

                        for (int i = 0; i < UInt16.MaxValue; i++)
                        {
                            if (watch.ElapsedMilliseconds > stat.Duration)
                            {
                                break;
                            }

                            if (stat.TimerType == TimerType.On_Off)
                            {
                                new LightStateBuilder()
                                .Turn(switchOn)
                                .For(Lights[stat.LightKey])
                                .Apply();
                            }

                            if (switchOn)
                            {
                                new LightStateBuilder()
                                .Brightness(stat.Brightness)
                                .Color(stat.Color)
                                .For(Lights[stat.LightKey])
                                .Apply();
                            }
                            else if (stat.TimerType == TimerType.Color_Switch)
                            {
                                new LightStateBuilder()
                                .Brightness(stat.Brightness)
                                .Color(stat.Color2)
                                .For(Lights[stat.LightKey])
                                .Apply();
                            }

                            switchOn = !switchOn;

                            System.Threading.Thread.Sleep((int)stat.TimerInterval);

                            if (!watch.IsRunning)
                            {
                                watch.Start();
                            }
                        }
                    }
                }

                foreach (var light in oldLightStates)
                {
                    new LightStateBuilder()
                    .Turn(light.on)
                    .For(Lights[light.key])
                    .Apply();

                    if (light.mode.Equals("xy", StringComparison.OrdinalIgnoreCase))
                    {
                        new LightStateBuilder()
                        .XYCoordinates(light.xy)
                        .For(Lights[light.key])
                        .Apply();
                    }
                    else
                    {
                        new LightStateBuilder()
                        .HSBColor(light.col)
                        .For(Lights[light.key])
                        .Apply();
                    }
                }
            }
            catch (Exception ae)
            {
                ae.Handle("", true);
            }
        }