예제 #1
0
        public void StartCycleDim()
        {
            if (PlatformConverter.ToBool(CycleDimInProgress))
            {
                return;
            }

            if (PlatformConverter.ToBool(RaiseInProgress))
            {
                StopRaise();
            }
            if (PlatformConverter.ToBool(LowerInProgress))
            {
                StopLower();
            }

            if (!State.On && _cycleDimDirection != true)
            {
                State.Brightness = BridgeClient.MinLightBulbBrightness;
            }

            StartCycleProperty(
                () => _cycleDimTimer,
                timer => _cycleDimTimer = timer,
                _cycleDimTime,
                () => _cycleDimDirection,
                direction => _cycleDimDirection  = direction,
                inProgress => CycleDimInProgress = inProgress,
                () => Brightness,
                brightness => State.Brightness = brightness,
                BridgeClient.MinLightBulbBrightness,
                BridgeClient.MaxLightBulbBrightness);
        }
예제 #2
0
        public void StartLower()
        {
            if (PlatformConverter.ToBool(LowerInProgress))
            {
                return;
            }

            if (PlatformConverter.ToBool(RaiseInProgress))
            {
                StopRaise();
            }
            if (PlatformConverter.ToBool(CycleDimInProgress))
            {
                StopCycleDim();
            }

            StartCycleProperty(
                () => _lowerTimer,
                timer => _lowerTimer = timer,
                _lowerTime,
                () => false,
                direction =>
            {
                if (State.Brightness <= BridgeClient.MinLightBulbBrightness)
                {
                    State.On = false;
                }
            },
                inProgress => LowerInProgress = inProgress,
                () => Brightness,
                brightness => State.Brightness = brightness,
                BridgeClient.MinLightBulbBrightness,
                BridgeClient.MaxLightBulbBrightness);
        }
예제 #3
0
        public void SetProperties(LightBulbProperties lightBulbPropertiesToSet)
        {
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetBrightness) && PlatformConverter.ToBool(RaiseInProgress))
            {
                StopRaise();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetBrightness) && PlatformConverter.ToBool(LowerInProgress))
            {
                StopLower();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetBrightness) && PlatformConverter.ToBool(CycleDimInProgress))
            {
                StopCycleDim();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetHue) && PlatformConverter.ToBool(CycleHueInProgress))
            {
                StopCycleHue();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetSaturation) && PlatformConverter.ToBool(CycleSaturationInProgress))
            {
                StopCycleSaturation();
            }
            if (PlatformConverter.ToBool(lightBulbPropertiesToSet.SetColorTemperature) && PlatformConverter.ToBool(CycleColorTemperatureInProgress))
            {
                StopCycleColorTemperature();
            }

            SetHuePropertiesRequestAction(lightBulbPropertiesToSet);
        }
예제 #4
0
 private void StopCycles()
 {
     if (PlatformConverter.ToBool(RaiseInProgress))
     {
         StopRaise();
     }
     if (PlatformConverter.ToBool(LowerInProgress))
     {
         StopLower();
     }
     if (PlatformConverter.ToBool(CycleDimInProgress))
     {
         StopCycleDim();
     }
     if (PlatformConverter.ToBool(CycleHueInProgress))
     {
         StopCycleHue();
     }
     if (PlatformConverter.ToBool(CycleSaturationInProgress))
     {
         StopCycleSaturation();
     }
     if (PlatformConverter.ToBool(CycleColorTemperatureInProgress))
     {
         StopCycleColorTemperature();
     }
 }
예제 #5
0
        public void StopCycleHue()
        {
            if (!PlatformConverter.ToBool(CycleHueInProgress))
            {
                return;
            }

            StopCycleProperty(
                ref _cycleHueTimer,
                ref _cycleHueDirection,
                inProgress => CycleHueInProgress = inProgress);
        }
예제 #6
0
        public void StopLower()
        {
            if (!PlatformConverter.ToBool(LowerInProgress))
            {
                return;
            }

            bool?lowerDirection = true;

            StopCycleProperty(
                ref _lowerTimer,
                ref lowerDirection,
                inProgress => LowerInProgress = inProgress);
        }
예제 #7
0
        public void StopRaise()
        {
            if (!PlatformConverter.ToBool(RaiseInProgress))
            {
                return;
            }

            bool?raiseDirection = true;

            StopCycleProperty(
                ref _raiseTimer,
                ref raiseDirection,
                inProgress => RaiseInProgress = inProgress);
        }
예제 #8
0
        public void StartCycleHue()
        {
            if (PlatformConverter.ToBool(CycleHueInProgress))
            {
                return;
            }

            StartCycleProperty(
                () => _cycleHueTimer,
                timer => _cycleHueTimer = timer,
                _cycleHueTime,
                () => _cycleHueDirection,
                direction => _cycleHueDirection  = direction,
                inProgress => CycleHueInProgress = inProgress,
                () => Hue,
                hue => State.Hue = hue,
                BridgeClient.MinLightBulbHue,
                BridgeClient.MaxLightBulbHue);
        }
예제 #9
0
        public void StartCycleColorTemperature()
        {
            if (PlatformConverter.ToBool(CycleColorTemperatureInProgress))
            {
                return;
            }

            StartCycleProperty(
                () => _cycleColorTemperatureTimer,
                timer => _cycleColorTemperatureTimer = timer,
                _cycleColorTemperatureTime,
                () => _cycleColorTemperatureDirection,
                direction => _cycleColorTemperatureDirection  = direction,
                inProgress => CycleColorTemperatureInProgress = inProgress,
                () => ColorTemperature,
                colorTemperature => State.ColorTemperature = colorTemperature,
                BridgeClient.MinLightBulbColorTemperature,
                BridgeClient.MaxLightBulbColorTemperature);
        }
예제 #10
0
        public void StartCycleSaturation()
        {
            if (PlatformConverter.ToBool(CycleSaturationInProgress))
            {
                return;
            }

            StartCycleProperty(
                () => _cycleSaturationTimer,
                timer => _cycleSaturationTimer = timer,
                _cycleSaturationTime,
                () => _cycleSaturationDirection,
                direction => _cycleSaturationDirection  = direction,
                inProgress => CycleSaturationInProgress = inProgress,
                () => Saturation,
                saturation => State.Saturation = saturation,
                BridgeClient.MinLightBulbSaturation,
                BridgeClient.MaxLightBulbSaturation);
        }
예제 #11
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);
            }
        }