Ejemplo n.º 1
0
        public void BeginTemporaryChange()
        {
            using (new LockScope(_classLock))
            {
                if (_beforeTemporaryChangeProperties != null)
                {
                    return;
                }

                _beforeTemporaryChangeProperties            = new LightBulbProperties();
                _beforeTemporaryChangeProperties.Brightness = Brightness;
                switch (ColorMode)
                {
                case LightBulbColorMode.HueSaturation:
                    _beforeTemporaryChangeProperties.Hue           = Hue;
                    _beforeTemporaryChangeProperties.Saturation    = Saturation;
                    _beforeTemporaryChangeProperties.SetHue        = PlatformConverter.ToPlatformBool(true);
                    _beforeTemporaryChangeProperties.SetSaturation = PlatformConverter.ToPlatformBool(true);
                    break;

                case LightBulbColorMode.ColorTemperature:
                    _beforeTemporaryChangeProperties.ColorTemperature    = ColorTemperature;
                    _beforeTemporaryChangeProperties.SetColorTemperature = PlatformConverter.ToPlatformBool(true);
                    break;

                case LightBulbColorMode.XY:
                    _beforeTemporaryChangeProperties.XY    = XY;
                    _beforeTemporaryChangeProperties.SetXY = PlatformConverter.ToPlatformBool(true);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
 public void Lower(ushort brightnessChange)
 {
     SetProperties(new LightBulbProperties()
     {
         SetBrightnessOffset = PlatformConverter.ToPlatformBool(true),
         BrightnessOffset    = (short)(-1 * (short)Math.Max(
                                           (ushort)(BridgeClient.MaxLightBulbBrightness / 100.0),
                                           Math.Min(
                                               brightnessChange,
                                               (ushort)short.MaxValue)))
     });
 }
        internal LightBulbProperties(LightBulb lightBulbFromWhichToSetAll)
        {
            Bool trueBool = PlatformConverter.ToPlatformBool(true);

            SetBrightness           = trueBool;
            SetBrightnessPercentage = trueBool;
            SetHue                  = trueBool;
            SetSaturation           = trueBool;
            SetSaturationPercentage = trueBool;
            SetXY = trueBool;
            SetColorTemperature = trueBool;

            Brightness           = lightBulbFromWhichToSetAll.Brightness;
            BrightnessPercentage = lightBulbFromWhichToSetAll.BrightnessPercentage;
            Hue                  = lightBulbFromWhichToSetAll.Hue;
            Saturation           = lightBulbFromWhichToSetAll.Saturation;
            SaturationPercentage = lightBulbFromWhichToSetAll.SaturationPercentage;
            XY = lightBulbFromWhichToSetAll.XY.ToArray();
            ColorTemperature = lightBulbFromWhichToSetAll.ColorTemperature;
        }
Ejemplo n.º 4
0
        private void StopCycleProperty(
            ref CTimer cycleTimer,
            ref bool?cycleDirection,
            Action <Bool> cyclePropertyInProgressSetter)
        {
            using (new LockScope(_classLock))
            {
                if (cycleTimer != null)
                {
                    cycleTimer.Dispose();
                    cycleTimer = null;
                }

                if (cycleDirection != null)
                {
                    cycleDirection = !cycleDirection.Value;
                }

                cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(false));
            }
        }
Ejemplo n.º 5
0
        private void StartCycleProperty(
            Func <CTimer> cycleTimerGetter,
            Action <CTimer> cycleTimerSetter,
            ushort cyclePropertyTime,
            Func <bool?> cycleDirectionGetter,
            Action <bool?> cycleDirectionSetter,
            Action <Bool> cyclePropertyInProgressSetter,
            Func <ushort> propertyGetter,
            Action <ushort> propertySetter,
            ushort minPropertyValue,
            ushort maxPropertyValue)
        {
            using (new LockScope(_classLock))
            {
                if (cycleTimerGetter() != null)
                {
                    cycleTimerGetter().Dispose();
                    cycleTimerSetter(null);
                }

                if (propertyGetter().CompareTo(minPropertyValue) < 0 || !PlatformConverter.ToBool(On))
                {
                    cycleDirectionSetter(true);
                }
                else if (propertyGetter().CompareTo(maxPropertyValue) >= 0)
                {
                    cycleDirectionSetter(false);
                }
                else if (cycleDirectionGetter() == null)
                {
                    cycleDirectionSetter(true);
                }

                int repeatTime = (int)Math.Max(100.0f, (float)cyclePropertyTime / maxPropertyValue);
                // NOTE: Max 100 because Philips recommends no more than 10 commands per second be sent to a bulb

                ushort offset = (ushort)Math.Max(1.0f, maxPropertyValue / (cyclePropertyTime / repeatTime));

                cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(true));

                CCriticalSection inProgressLock = new CCriticalSection();

                cycleTimerSetter(
                    new CTimer(data =>
                {
                    using (new LockScope(inProgressLock))
                    {
                        if (cycleTimerGetter() == null)
                        {
                            return;
                        }

                        bool direction = cycleDirectionGetter() == true;
                        if (direction)
                        {
                            if (propertyGetter() >= maxPropertyValue)
                            {
                                using (new LockScope(_classLock))
                                {
                                    if (cycleTimerGetter() != null)
                                    {
                                        cycleDirectionSetter(false);
                                        cycleTimerGetter().Dispose();
                                        cycleTimerSetter(null);
                                    }

                                    cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(false));
                                }
                            }
                            else
                            {
                                ushort adjustedOffset = (ushort)Math.Min(offset, maxPropertyValue - propertyGetter());
                                propertySetter((ushort)(propertyGetter() + adjustedOffset));
                            }
                        }
                        else
                        {
                            if (propertyGetter() <= minPropertyValue)
                            {
                                using (new LockScope(_classLock))
                                {
                                    if (cycleTimerGetter() != null)
                                    {
                                        cycleDirectionSetter(true);
                                        cycleTimerGetter().Dispose();
                                        cycleTimerSetter(null);
                                    }

                                    cyclePropertyInProgressSetter(PlatformConverter.ToPlatformBool(false));
                                }
                            }
                            else
                            {
                                ushort adjustedOffset = (ushort)Math.Min(offset, propertyGetter() - minPropertyValue);
                                propertySetter((ushort)(propertyGetter() - adjustedOffset));
                            }
                        }
                    }
                },
                               5000 /*dummy value until Reset is called below since CTimer doesn't seem to respect the ctor parameter*/));

                cycleTimerGetter().Reset(0, (int)repeatTime);
            }
        }
Ejemplo n.º 6
0
 internal ErrorOccurredEventArgs(string errorMessage, bool isUserError)
 {
     ErrorMessage = errorMessage;
     IsUserError  = PlatformConverter.ToPlatformBool(isUserError);
 }