Example #1
0
    private async static void ToggleBulb(LightBulb target)
    {
        var state = await Client.GetLightStateAsync(target);

        var isOn = state.Brightness != 0;

        if (isOn)
        {
            await Client.SetColorAsync(target, state.Hue, state.Saturation, 0, 9000,
                                       TimeSpan.FromMilliseconds(0));
        }
        else
        {
            ushort brightness = 3000;
            if (LastOnLightState == null)
            {
                LastOnLightState = state;
            }
            if (LastOnLightState.Brightness != 0)
            {
                brightness = LastOnLightState.Brightness;
            }
            await Client.SetColorAsync(target, state.Hue, state.Saturation, brightness,
                                       LastOnLightState.Kelvin, TimeSpan.FromMilliseconds(0));
        }
    }
Example #2
0
    public static async void SetHue(LightBulb target, ushort newHue)
    {
        Debug.Log(target.HostName + ".Hue = " + newHue);
        var last = LastOnLightState;
        await Client.SetColorAsync(target, newHue, last.Saturation, last.Brightness, last.Kelvin, TimeSpan.FromTicks(0));

        LastOnLightState = (await Client.GetLightStateAsync(target));
    }
Example #3
0
 internal LightState(LightStateResponse state)
 {
     // TODO: translate into degrees/percentages
     Hue        = state.Hue;
     Saturation = state.Saturation;
     Brightness = state.Brightness;
     Kelvin     = state.Kelvin;
     IsOn       = state.IsOn;
     Label      = state.Label;
 }
 /// <summary>
 ///     Returns hexvalue from Light State
 /// </summary>
 /// <param name="state"></param>
 /// <returns></returns>
 public static string HexFromState(LightStateResponse state)
 {
     if (state == null)
     {
         throw new ArgumentNullException("state");
     }
     if (state.On == false || state.Brightness <= 5)
     {
         return("000000");
     }
     return(HexFromXy(state.ColorCoordinates[0], state.ColorCoordinates[1]));
 }
Example #5
0
        public async Task Update()
        {
            try
            {
                // ***
                // *** Locking the API limiter prevents all calls
                // *** from being made.
                // ***
                await this.Limiter.Lock();

                if (this.Client != null)
                {
                    LightStateResponse state = await this.Client.GetLightStateAsync(this.LightBulb);

                    if (state != null)
                    {
                        // ***
                        // *** Since this is a view model, it is possible that the bindable properties such
                        // *** as LightColor are connected to the UI. For that reason, only update them on
                        // *** the UI thread.
                        // ***
                        await((DispatchedHandler)(() =>
                        {
                            this.Name = state.Label;
                            this.IsOn = state.IsOn;
                            this.Hue = Lifx.Hue.FromLifx(state.Hue);
                            this.Saturation = Lifx.Saturation.FromLifx(state.Saturation);
                            this.Brightness = Lifx.Brightness.FromLifx(state.Brightness);
                            this.Kelvin = state.Kelvin;
                            this.LightColor = Lifx.Rgb.CreateSolidColorBrush(state.Hue, state.Saturation, state.Brightness);
                        })).RunOnUiThread();
                    }
                }
            }
            finally
            {
                // ***
                // *** Unlock the API limiter.
                // ***
                await this.Limiter.Unlock();
            }
        }