Пример #1
0
        private void btn_CreateEvent_Click(object sender, EventArgs e)
        {
            var item     = (SharpHue.Light)listBox_Lights.SelectedItem;
            var hueevent = new HueEvent(item.ID);

            hueEventHandler.Events.Add(hueevent);
            listBox_Events.Items.Add(hueevent);
            listBox_Events.Refresh();
        }
Пример #2
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);
            }
        }