public uint TransitionLampState(ulong Timestamp, BridgeRT.State NewState, uint TransitionPeriod, out uint LampResponseCode)
        {
            var command = new LightCommand();

            command.TransitionTime = TimeSpan.FromMilliseconds(TransitionPeriod);
            command.On             = NewState.IsOn;
            if (NewState.Brightness.HasValue && LampDetails_Dimmable)
            {
                command.Brightness = (byte)(NewState.Brightness.Value * 254d / UInt32Max);
            }
            if (NewState.Hue.HasValue && LampDetails_Color)
            {
                command.Hue = (int)(NewState.Hue.Value * 65535d / UInt32Max);
            }
            if (NewState.Saturation.HasValue && LampDetails_Color)
            {
                command.Saturation = (int)(NewState.Saturation.Value * 254d / UInt32Max);
            }
            if (NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp)
            {
                double kelvin        = NewState.ColorTemp.Value * 19000d / UInt32Max + 1000;
                int    kelvinLimited = (int)Math.Max(LampDetails_MinTemperature, Math.Min(LampDetails_MaxTemperature, kelvin));
                int    mired         = (int)(1000000d / kelvinLimited);
                //Currently hue doesn't like setting color temp if the other parameters are also set.
                //Skipping for now.
                //command.ColorTemperature = mired;
            }
            LampResponseCode = 0;
            System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(Timestamp)).ContinueWith(_ =>
            {
                _client.SendCommandAsync(command, new[] { _light.Id });
                //Update state
                if (command.Hue.HasValue)
                {
                    _light.State.Hue = command.Hue.Value;
                }
                if (command.Brightness.HasValue)
                {
                    _light.State.Brightness = command.Brightness.Value;
                }
                if (command.Saturation.HasValue)
                {
                    _light.State.Saturation = command.Saturation.Value;
                }
                if (command.ColorTemperature.HasValue)
                {
                    _light.State.ColorTemperature = command.ColorTemperature.Value;
                }
                if (command.On.HasValue)
                {
                    _light.State.On = command.On.Value;
                }
            });
            return(0); //TODO
        }
예제 #2
0
        private async void DoPulse(Light light, BridgeRT.State FromState, BridgeRT.State ToState, uint Period, uint Duration, uint NumPulses)
        {
            for (int i = 0; i < NumPulses; i++)
            {
                SetLamp(light, FromState, Period);
                await Task.Delay((int)(Period + Duration));

                SetLamp(light, ToState, Period);
                await Task.Delay((int)(Period + Duration));
            }
        }
예제 #3
0
        public uint TransitionLampState(ulong Timestamp, BridgeRT.State ToState, uint Period, out uint LampResponseCode)
        {
            LampResponseCode = 0;

            SetLamp(light, ToState, Period);

            this.Native_Saturation = ToState.Saturation;
            this.Native_Brightness = ToState.Brightness;
            this.Native_Hue        = ToState.Hue;
            this.LampState_OnOff   = ToState.IsOn;
            return(0);
        }
        /// <summary>
        /// Change the state of the lamp at the specified time, between the specified OnOff, Brightness, Hue,
        /// Saturation, and ColorTemp values. Pulse for the specified number of times, at the specified duration
        /// </summary>
        /// <param name="FromState">Current state of the lamp to transition from</param>
        /// <param name="ToState">New state of the lamp to transition to</param>
        /// <param name="Period">Time period(in ms) to transition over to new state</param>
        /// <param name="Duration">Time period(in ms) to remain in new state</param>
        /// <param name="NumPulses">Number of pulses</param>
        /// <param name="Timestamp">Timestamp (in ms) of when to start the pulses</param>
        private async void ApplyPulseEffectAsync(BridgeRT.State FromState, BridgeRT.State ToState, uint Period, uint Duration, uint NumPulses, ulong Timestamp)
        {
            uint response;
            await System.Threading.Tasks.Task.Delay((int)Timestamp).ConfigureAwait(false);

            TransitionLampState(0, FromState, 0, out response);
            for (int i = 0; i < NumPulses; i++)
            {
                TransitionLampState(0, ToState, Period, out response);
                await System.Threading.Tasks.Task.Delay((int)(Period + Duration)).ConfigureAwait(false);

                TransitionLampState(0, FromState, Period, out response);
                await System.Threading.Tasks.Task.Delay((int)(Period + Duration)).ConfigureAwait(false);
            }
        }
예제 #5
0
        private void SetLamp(Light light, BridgeRT.State thestate, uint Period)
        {
            var command = new LightCommand();

            command.TransitionTime = TimeSpan.FromMilliseconds(Period);
            command.On             = thestate.IsOn;

            uint saturation = thestate.Saturation >> 24;
            uint brightness = thestate.Brightness >> 24;
            int  hue        = (int)thestate.Hue >> 24;

            hue &= 0X000000FF;

            byte r = 0; byte g = 0; byte b = 0;

            ColorFromHSV((double)hue, (double)saturation, (double)brightness, out r, out g, out b);
            PhilipsHueController.SetColorAndOnOff(light, (int)r, (int)g, (int)b, thestate.IsOn);
        }
예제 #6
0
 public uint LampState_ApplyPulseEffect(BridgeRT.State FromState, BridgeRT.State ToState, uint Period, uint Duration, uint NumPulses, ulong Timestamp, out uint LampResponseCode)
 {
     LampResponseCode = 0;
     DoPulse(light, FromState, ToState, Period, Duration, NumPulses);
     return(0);
 }
        private async void TransitionLampStateAsync(ulong Timestamp, BridgeRT.State NewState, uint TransitionPeriod)
        {
            if (Timestamp > 0)
            {
                await Task.Delay((int)Timestamp);
            }
            System.Threading.CancellationToken token = System.Threading.CancellationToken.None;
            lock (transitionLock)
            {
                if (transitionCancellation != null)
                {
                    //Cancel any ongoing transitions
                    transitionCancellation.Cancel();
                }
                transitionCancellation = new System.Threading.CancellationTokenSource();
                token = transitionCancellation.Token;
            }
            var steps           = Math.Floor(30d / (1000d / TransitionPeriod));
            var stepdelay       = TransitionPeriod / steps;
            var startHue        = LampState_Hue;
            var startBrightness = LampState_Brightness;
            var startSaturation = LampState_Saturation;
            var startTemp       = LampState_ColorTemp;

            if (NewState.IsOn.HasValue && NewState.IsOn.Value)
            {
                LampState_OnOff = true;
            }
            if (NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp)
            {
                //Clip to supported temperatures
                if (NewState.ColorTemp.Value > kelvinToUInt(LampDetails_MaxTemperature))
                {
                    NewState.ColorTemp = kelvinToUInt(LampDetails_MaxTemperature);
                }
                else if (NewState.ColorTemp.Value < kelvinToUInt(LampDetails_MinTemperature))
                {
                    NewState.ColorTemp = kelvinToUInt(LampDetails_MinTemperature);
                }
            }
            for (int i = 1; i < steps; i++)
            {
                if (NewState.Hue.HasValue && LampDetails_Color)
                {
                    var inc = (NewState.Hue.Value - (double)startHue) / steps;
                    this.hue = (uint)(startHue + inc * i);
                    OnPropertyChanged(nameof(LampState_Hue));
                }
                if (NewState.Brightness.HasValue && LampDetails_Dimmable)
                {
                    var inc = (NewState.Brightness.Value - (double)startBrightness) / steps;
                    this.brightness = (uint)(startBrightness + inc * i);
                    OnPropertyChanged(nameof(LampState_Brightness));
                }
                if (NewState.Saturation.HasValue && LampDetails_Color)
                {
                    var inc = (NewState.Saturation.Value - (double)startSaturation) / steps;
                    this.saturation = (uint)(startSaturation + inc * i);
                    OnPropertyChanged(nameof(LampState_Saturation));
                }
                if (NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp)
                {
                    var inc = (NewState.ColorTemp.Value - (double)startTemp) / steps;
                    this.colorTemp = (uint)(startTemp + inc * i);
                    OnPropertyChanged(nameof(LampState_ColorTemp));
                }
                OnPropertyChanged(nameof(Color));
                OnPropertyChanged(nameof(ColorFullBrightness));
                await Task.Delay((int)stepdelay);

                if (token.IsCancellationRequested)
                {
                    return;
                }
            }
            if (NewState.Hue.HasValue && LampDetails_Color)
            {
                this.hue = NewState.Hue.Value;
                OnPropertyChanged(nameof(LampState_Hue));
            }
            if (NewState.Brightness.HasValue && LampDetails_Dimmable)
            {
                this.brightness = NewState.Brightness.Value;
                OnPropertyChanged(nameof(LampState_Brightness));
            }
            if (NewState.Saturation.HasValue && LampDetails_Color)
            {
                this.saturation = NewState.Saturation.Value;
                OnPropertyChanged(nameof(LampState_Saturation));
            }
            if (NewState.ColorTemp.HasValue && LampDetails_VariableColorTemp)
            {
                this.colorTemp = NewState.ColorTemp.Value;
                OnPropertyChanged(nameof(LampState_ColorTemp));
            }
            OnPropertyChanged(nameof(Color));
            OnPropertyChanged(nameof(ColorFullBrightness));
            if (NewState.IsOn.HasValue && !NewState.IsOn.Value)
            {
                LampState_OnOff = false;
            }
        }
 public uint TransitionLampState(ulong Timestamp, BridgeRT.State NewState, uint TransitionPeriod, out uint LampResponseCode)
 {
     TransitionLampStateAsync(Timestamp, NewState, TransitionPeriod);
     LampResponseCode = 0;
     return(0); //TODO
 }
 public uint LampState_ApplyPulseEffect(BridgeRT.State FromState, BridgeRT.State ToState, uint Period, uint Duration, uint NumPulses, ulong Timestamp, out uint LampResponseCode)
 {
     LampResponseCode = 0;
     return(1); //TODO
 }
 public uint LampState_ApplyPulseEffect(BridgeRT.State FromState, BridgeRT.State ToState, uint Period, uint Duration, uint NumPulses, ulong Timestamp, out uint LampResponseCode)
 {
     throw new NotImplementedException();
 }
 public uint TransitionLampState(ulong Timestamp, BridgeRT.State NewState, uint TransitionPeriod, out uint LampResponseCode)
 {
     throw new NotImplementedException();
 }
예제 #12
0
 public uint TransitionLampState(ulong Timestamp, BridgeRT.State NewState, uint TransitionPeriod, out uint LampResponseCode)
 {
     LampResponseCode = 0;
     return(0);
 }